text:
namespace content {

using ppapi::FileIOStateManager;
using ppapi::PPTimeToTime;
using ppapi::TimeToPPTime;
using ppapi::host::ReplyMessageContext;
using ppapi::thunk::EnterResourceNoLock;
using ppapi::thunk::PPB_FileRef_API;
using webkit::ppapi::PPB_FileRef_Impl;
using webkit::ppapi::PluginDelegate;

namespace {

// The maximum size we'll support reading in one chunk. The renderer process
// must allocate a buffer sized according to the request of the plugin. To
// keep things from getting out of control, we cap the read size to this value.
// This should generally be OK since the API specifies that it may perform a
// partial read.
static const int32_t kMaxReadSize = 32 * 1024 * 1024;  // 32MB

typedef base::Callback<void (base::PlatformFileError)> PlatformGeneralCallback;

int32_t ErrorOrByteNumber(int32_t pp_error, int32_t byte_number) {
  // On the plugin side, some callbacks expect a parameter that means different
  // things depending on whether is negative or not.  We translate for those
  // callbacks here.
  return pp_error == PP_OK ? byte_number : pp_error;
}

}  // namespace

PepperFileIOHost::PepperFileIOHost(RendererPpapiHost* host,
                                   PP_Instance instance,
                                   PP_Resource resource)
    : ResourceHost(host->GetPpapiHost(), instance, resource),
      file_(base::kInvalidPlatformFileValue),
      file_system_type_(PP_FILESYSTEMTYPE_INVALID),
      quota_policy_(quota::kQuotaLimitTypeUnknown),
      is_running_in_process_(host->IsRunningInProcess()),
      open_flags_(0),
      weak_factory_(this) {
  // TODO(victorhsieh): eliminate plugin_delegate_ as it's no longer needed.
  webkit::ppapi::PluginInstance* plugin_instance =
      webkit::ppapi::HostGlobals::Get()->GetInstance(instance);
  plugin_delegate_ = plugin_instance ? plugin_instance->delegate() : NULL;
}

PepperFileIOHost::~PepperFileIOHost() {
  OnHostMsgClose(NULL);
}

int32_t PepperFileIOHost::OnResourceMessageReceived(
    const IPC::Message& msg,
    ppapi::host::HostMessageContext* context) {
  IPC_BEGIN_MESSAGE_MAP(PepperFileIOHost, msg)
    PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FileIO_Open,
                                      OnHostMsgOpen)
    PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_FileIO_Query,
                                        OnHostMsgQuery)
    PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FileIO_Touch,
                                      OnHostMsgTouch)
    PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FileIO_Read,
                                      OnHostMsgRead)
    PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FileIO_Write,
                                      OnHostMsgWrite)
    PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FileIO_SetLength,
                                      OnHostMsgSetLength)
    PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_FileIO_Flush,
                                        OnHostMsgFlush)
    PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_FileIO_Close,
                                        OnHostMsgClose)
    PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FileIO_WillWrite,
                                      OnHostMsgWillWrite)
    PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FileIO_WillSetLength,
                                      OnHostMsgWillSetLength)
    PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_FileIO_GetOSFileDescriptor,
                                        OnHostMsgGetOSFileDescriptor)
    PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_FileIO_RequestOSFileHandle,
                                        OnHostMsgRequestOSFileHandle)
  IPC_END_MESSAGE_MAP()
  return PP_ERROR_FAILED;
}

int32_t PepperFileIOHost::OnHostMsgOpen(
    ppapi::host::HostMessageContext* context,
    PP_Resource file_ref_resource,
    int32_t open_flags) {
  int32_t rv = state_manager_.CheckOperationState(
      FileIOStateManager::OPERATION_EXCLUSIVE, false);
  if (rv != PP_OK)
    return rv;

  int flags = 0;
  open_flags_ = open_flags;
  if (!::ppapi::PepperFileOpenFlagsToPlatformFileFlags(open_flags, &flags))
    return PP_ERROR_BADARGUMENT;

  EnterResourceNoLock<PPB_FileRef_API> enter(file_ref_resource, true);
  if (enter.failed())
    return PP_ERROR_BADRESOURCE;

  PPB_FileRef_API* file_ref_api = enter.object();
  PP_FileSystemType type = file_ref_api->GetFileSystemType();
  if (type != PP_FILESYSTEMTYPE_LOCALPERSISTENT &&
      type != PP_FILESYSTEMTYPE_LOCALTEMPORARY &&
      type != PP_FILESYSTEMTYPE_EXTERNAL &&
      type != PP_FILESYSTEMTYPE_ISOLATED)
    return PP_ERROR_FAILED;
  file_system_type_ = type;

  if (!plugin_delegate_)
    return PP_ERROR_FAILED;

  PPB_FileRef_Impl* file_ref = static_cast<PPB_FileRef_Impl*>(file_ref_api);
  if (file_ref->HasValidFileSystem()) {
    file_system_url_ = file_ref->GetFileSystemURL();
    if (!plugin_delegate_->AsyncOpenFileSystemURL(
            file_system_url_, flags,
            base::Bind(
                &PepperFileIOHost::ExecutePlatformOpenFileSystemURLCallback,
                weak_factory_.GetWeakPtr(),
                context->MakeReplyMessageContext())))
      return PP_ERROR_FAILED;
  } else {
    if (file_system_type_ != PP_FILESYSTEMTYPE_EXTERNAL)
      return PP_ERROR_FAILED;
    if (!plugin_delegate_->AsyncOpenFile(
            file_ref->GetSystemPath(), flags,
            base::Bind(&PepperFileIOHost::ExecutePlatformOpenFileCallback,
                       weak_factory_.GetWeakPtr(),
                       context->MakeReplyMessageContext())))
      return PP_ERROR_FAILED;
  }

  state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE);
  return PP_OK_COMPLETIONPENDING;
}

int32_t PepperFileIOHost::OnHostMsgQuery(
    ppapi::host::HostMessageContext* context) {
  int32_t rv = state_manager_.CheckOperationState(
      FileIOStateManager::OPERATION_EXCLUSIVE, true);
  if (rv != PP_OK)
    return rv;

  if (!plugin_delegate_)
    return PP_ERROR_FAILED;

  if (!base::FileUtilProxy::GetFileInfoFromPlatformFile(
          plugin_delegate_->GetFileThreadMessageLoopProxy(), file_,
          base::Bind(&PepperFileIOHost::ExecutePlatformQueryCallback,
                     weak_factory_.GetWeakPtr(),
                     context->MakeReplyMessageContext())))
    return PP_ERROR_FAILED;

  state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE);
  return PP_OK_COMPLETIONPENDING;
}

int32_t PepperFileIOHost::OnHostMsgTouch(
    ppapi::host::HostMessageContext* context,
    PP_Time last_access_time,
    PP_Time last_modified_time) {
  int32_t rv = state_manager_.CheckOperationState(
      FileIOStateManager::OPERATION_EXCLUSIVE, true);
  if (rv != PP_OK)
    return rv;

  if (!plugin_delegate_)
    return PP_ERROR_FAILED;

  if (file_system_type_ != PP_FILESYSTEMTYPE_EXTERNAL) {
    if (!plugin_delegate_->Touch(
            file_system_url_,
            PPTimeToTime(last_access_time),
            PPTimeToTime(last_modified_time),
            base::Bind(&PepperFileIOHost::ExecutePlatformGeneralCallback,
                        weak_factory_.GetWeakPtr(),
                        context->MakeReplyMessageContext())))
      return PP_ERROR_FAILED;
    state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE);
    return PP_OK_COMPLETIONPENDING;
  }

  // TODO(nhiroki): fix a failure of FileIO.Touch for an external filesystem on
  // Mac and Linux due to sandbox restrictions (http://crbug.com/101128).
  if (!base::FileUtilProxy::Touch(
          plugin_delegate_->GetFileThreadMessageLoopProxy(),
          file_, PPTimeToTime(last_access_time),
          PPTimeToTime(last_modified_time),
          base::Bind(&PepperFileIOHost::ExecutePlatformGeneralCallback,
                     weak_factory_.GetWeakPtr(),
                     context->MakeReplyMessageContext())))
    return PP_ERROR_FAILED;

  state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE);
  return PP_OK_COMPLETIONPENDING;
}

int32_t PepperFileIOHost::OnHostMsgRead(
    ppapi::host::HostMessageContext* context,
    int64_t offset,
    int32_t max_read_length) {
  int32_t rv = state_manager_.CheckOperationState(
      FileIOStateManager::OPERATION_READ, true);
  if (rv != PP_OK)
    return rv;

  // Validate max_read_length before allocating below. This value is coming from
  // the untrusted plugin.
  if (max_read_length < 0) {
    ReplyMessageContext reply_context = context->MakeReplyMessageContext();
    reply_context.params.set_result(PP_ERROR_FAILED);
    host()->SendReply(reply_context,
                      PpapiPluginMsg_FileIO_ReadReply(std::string()));
    return PP_OK_COMPLETIONPENDING;
  }

  if (!plugin_delegate_)
    return PP_ERROR_FAILED;

  if (!base::FileUtilProxy::Read(
          plugin_delegate_->GetFileThreadMessageLoopProxy(), file_, offset,
          max_read_length,
          base::Bind(&PepperFileIOHost::ExecutePlatformReadCallback,
                     weak_factory_.GetWeakPtr(),
                     context->MakeReplyMessageContext())))
    return PP_ERROR_FAILED;

  state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_READ);
  return PP_OK_COMPLETIONPENDING;
}

int32_t PepperFileIOHost::OnHostMsgWrite(
    ppapi::host::HostMessageContext* context,
    int64_t offset,
    const std::string& buffer) {
  int32_t rv = state_manager_.CheckOperationState(
      FileIOStateManager::OPERATION_WRITE, true);
  if (rv != PP_OK)
    return rv;

  if (quota_file_io_) {
    if (!quota_file_io_->Write(
            offset, buffer.c_str(), buffer.size(),
            base::Bind(&PepperFileIOHost::ExecutePlatformWriteCallback,
                       weak_factory_.GetWeakPtr(),
                       context->MakeReplyMessageContext())))
      return PP_ERROR_FAILED;
  } else {
    if (!plugin_delegate_)
      return PP_ERROR_FAILED;

    if (!base::FileUtilProxy::Write(
            plugin_delegate_->GetFileThreadMessageLoopProxy(), file_, offset,
            buffer.c_str(), buffer.size(),
            base::Bind(&PepperFileIOHost::ExecutePlatformWriteCallback,
                       weak_factory_.GetWeakPtr(),
                       context->MakeReplyMessageContext())))
      return PP_ERROR_FAILED;
  }

  state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_WRITE);
  return PP_OK_COMPLETIONPENDING;
}

int32_t PepperFileIOHost::OnHostMsgSetLength(
    ppapi::host::HostMessageContext* context,
    int64_t length) {
  int32_t rv = state_manager_.CheckOperationState(
      FileIOStateManager::OPERATION_EXCLUSIVE, true);
  if (rv != PP_OK)
    return rv;

  if (!plugin_delegate_)
    return PP_ERROR_FAILED;

  if (file_system_type_ != PP_FILESYSTEMTYPE_EXTERNAL) {
    if (!plugin_delegate_->SetLength(
            file_system_url_, length,
            base::Bind(&PepperFileIOHost::ExecutePlatformGeneralCallback,
                       weak_factory_.GetWeakPtr(),
                       context->MakeReplyMessageContext())))
      return PP_ERROR_FAILED;
  } else {
    // TODO(nhiroki): fix a failure of FileIO.SetLength for an external
    // filesystem on Mac due to sandbox restrictions (http://crbug.com/156077).
    if (!base::FileUtilProxy::Truncate(
            plugin_delegate_->GetFileThreadMessageLoopProxy(), file_, length,
            base::Bind(&PepperFileIOHost::ExecutePlatformGeneralCallback,
                       weak_factory_.GetWeakPtr(),
                       context->MakeReplyMessageContext())))
      return PP_ERROR_FAILED;
  }

  state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE);
  return PP_OK_COMPLETIONPENDING;
}

int32_t PepperFileIOHost::OnHostMsgFlush(
    ppapi::host::HostMessageContext* context) {
  int32_t rv = state_manager_.CheckOperationState(
      FileIOStateManager::OPERATION_EXCLUSIVE, true);
  if (rv != PP_OK)
    return rv;

  if (!plugin_delegate_)
    return PP_ERROR_FAILED;

  if (!base::FileUtilProxy::Flush(
          plugin_delegate_->GetFileThreadMessageLoopProxy(), file_,
          base::Bind(&PepperFileIOHost::ExecutePlatformGeneralCallback,
                     weak_factory_.GetWeakPtr(),
                     context->MakeReplyMessageContext())))
    return PP_ERROR_FAILED;

  state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE);
  return PP_OK_COMPLETIONPENDING;
}

int32_t PepperFileIOHost::OnHostMsgClose(
    ppapi::host::HostMessageContext* context) {
  if (file_ != base::kInvalidPlatformFileValue && plugin_delegate_) {
    base::FileUtilProxy::Close(
        plugin_delegate_->GetFileThreadMessageLoopProxy(),
        file_,
        base::ResetAndReturn(&notify_close_file_callback_));
    file_ = base::kInvalidPlatformFileValue;
    quota_file_io_.reset();
  }
  return PP_OK;
}

int32_t PepperFileIOHost::OnHostMsgWillWrite(
    ppapi::host::HostMessageContext* context,
    int64_t offset,
    int32_t bytes_to_write) {
  int32_t rv = state_manager_.CheckOperationState(
      FileIOStateManager::OPERATION_EXCLUSIVE, true);
  if (rv != PP_OK)
    return rv;

  if (!quota_file_io_)
    return PP_OK;

  if (!quota_file_io_->WillWrite(
          offset, bytes_to_write,
          base::Bind(&PepperFileIOHost::ExecutePlatformWriteCallback,
                     weak_factory_.GetWeakPtr(),
                     context->MakeReplyMessageContext())))
    return PP_ERROR_FAILED;

  state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE);
  return PP_OK_COMPLETIONPENDING;
}

int32_t PepperFileIOHost::OnHostMsgWillSetLength(
    ppapi::host::HostMessageContext* context,
    int64_t length) {
  int32_t rv = state_manager_.CheckOperationState(
      FileIOStateManager::OPERATION_EXCLUSIVE, true);
  if (rv != PP_OK)
    return rv;

  if (!quota_file_io_)
    return PP_OK;

  if (!quota_file_io_->WillSetLength(
          length,
          base::Bind(&PepperFileIOHost::ExecutePlatformGeneralCallback,
                     weak_factory_.GetWeakPtr(),
                     context->MakeReplyMessageContext())))
    return PP_ERROR_FAILED;

  state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE);
  return PP_OK_COMPLETIONPENDING;
}

int32_t PepperFileIOHost::OnHostMsgRequestOSFileHandle(
    ppapi::host::HostMessageContext* context) {
  if (!is_running_in_process_ &&
      quota_policy_ != quota::kQuotaLimitTypeUnlimited)
    return PP_ERROR_FAILED;

  RendererPpapiHost* renderer_ppapi_host =
      RendererPpapiHost::GetForPPInstance(pp_instance());

  IPC::PlatformFileForTransit file =
      renderer_ppapi_host->ShareHandleWithRemote(file_, false);
  if (file == IPC::InvalidPlatformFileForTransit())
    return PP_ERROR_FAILED;
  ppapi::host::ReplyMessageContext reply_context =
      context->MakeReplyMessageContext();
  ppapi::proxy::SerializedHandle file_handle;
  file_handle.set_file_handle(file, open_flags_);
  reply_context.params.AppendHandle(file_handle);
  host()->SendReply(reply_context,
                    PpapiPluginMsg_FileIO_RequestOSFileHandleReply());
  return PP_OK_COMPLETIONPENDING;
}

int32_t PepperFileIOHost::OnHostMsgGetOSFileDescriptor(
    ppapi::host::HostMessageContext* context) {
  if (!is_running_in_process_)
    return PP_ERROR_FAILED;

  int32_t fd =
#if defined(OS_POSIX)
      file_;
#elif defined(OS_WIN)
      reinterpret_cast<uintptr_t>(file_);
#else
      -1;
#endif

  host()->SendReply(context->MakeReplyMessageContext(),
                    PpapiPluginMsg_FileIO_GetOSFileDescriptorReply(fd));
  return PP_OK_COMPLETIONPENDING;
}

void PepperFileIOHost::ExecutePlatformGeneralCallback(
    ppapi::host::ReplyMessageContext reply_context,
    base::PlatformFileError error_code) {
  reply_context.params.set_result(
      ::ppapi::PlatformFileErrorToPepperError(error_code));
  host()->SendReply(reply_context, PpapiPluginMsg_FileIO_GeneralReply());
  state_manager_.SetOperationFinished();
}

void PepperFileIOHost::ExecutePlatformOpenFileCallback(
    ppapi::host::ReplyMessageContext reply_context,
    base::PlatformFileError error_code,
    base::PassPlatformFile file) {
  int32_t pp_error = ::ppapi::PlatformFileErrorToPepperError(error_code);
  if (pp_error == PP_OK)
    state_manager_.SetOpenSucceed();

  DCHECK(file_ == base::kInvalidPlatformFileValue);
  file_ = file.ReleaseValue();

  DCHECK(!quota_file_io_.get());
  if (file_ != base::kInvalidPlatformFileValue &&
      (file_system_type_ == PP_FILESYSTEMTYPE_LOCALTEMPORARY ||
       file_system_type_ == PP_FILESYSTEMTYPE_LOCALPERSISTENT)) {
    quota_file_io_.reset(new webkit::ppapi::QuotaFileIO(
        pp_instance(), file_, file_system_url_, file_system_type_));
  }

  reply_context.params.set_result(pp_error);
  host()->SendReply(reply_context, PpapiPluginMsg_FileIO_OpenReply());
  state_manager_.SetOperationFinished();
}

void PepperFileIOHost::ExecutePlatformOpenFileSystemURLCallback(
    ppapi::host::ReplyMessageContext reply_context,
    base::PlatformFileError error_code,
    base::PassPlatformFile file,
    quota::QuotaLimitType quota_policy,
    const PluginDelegate::NotifyCloseFileCallback& callback) {
  if (error_code == base::PLATFORM_FILE_OK)
    notify_close_file_callback_ = callback;
  quota_policy_ = quota_policy;
  ExecutePlatformOpenFileCallback(reply_context, error_code, file);
}

void PepperFileIOHost::ExecutePlatformQueryCallback(
    ppapi::host::ReplyMessageContext reply_context,
    base::PlatformFileError error_code,
    const base::PlatformFileInfo& file_info) {
  PP_FileInfo pp_info;
  pp_info.size = file_info.size;
  pp_info.creation_time = TimeToPPTime(file_info.creation_time);
  pp_info.last_access_time = TimeToPPTime(file_info.last_accessed);
  pp_info.last_modified_time = TimeToPPTime(file_info.last_modified);
  pp_info.system_type = file_system_type_;
  if (file_info.is_directory)
    pp_info.type = PP_FILETYPE_DIRECTORY;
  else
    pp_info.type = PP_FILETYPE_REGULAR;

  int32_t pp_error = ::ppapi::PlatformFileErrorToPepperError(error_code);
  reply_context.params.set_result(pp_error);
  host()->SendReply(reply_context,
                    PpapiPluginMsg_FileIO_QueryReply(pp_info));
  state_manager_.SetOperationFinished();
}

void PepperFileIOHost::ExecutePlatformReadCallback(
    ppapi::host::ReplyMessageContext reply_context,
    base::PlatformFileError error_code,
    const char* data, int bytes_read) {
  int32_t pp_error = ::ppapi::PlatformFileErrorToPepperError(error_code);

  // Only send the amount of data in the string that was actually read.
  std::string buffer;
  if (pp_error == PP_OK)
    buffer.append(data, bytes_read);
  reply_context.params.set_result(ErrorOrByteNumber(pp_error, bytes_read));
  host()->SendReply(reply_context, PpapiPluginMsg_FileIO_ReadReply(buffer));
  state_manager_.SetOperationFinished();
}

void PepperFileIOHost::ExecutePlatformWriteCallback(
    ppapi::host::ReplyMessageContext reply_context,
    base::PlatformFileError error_code,
    int bytes_written) {
  // On the plugin side, the callback expects a parameter with different meaning
  // depends on whether is negative or not. It is the result here. We translate
  // for the callback.
  int32_t pp_error = ::ppapi::PlatformFileErrorToPepperError(error_code);
  reply_context.params.set_result(ErrorOrByteNumber(pp_error, bytes_written));
  host()->SendReply(reply_context, PpapiPluginMsg_FileIO_GeneralReply());
  state_manager_.SetOperationFinished();
}

}  // namespace content


text:
package org.apache.fluo.recipes.spark;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.accumulo.core.client.Accumulo;
import org.apache.accumulo.core.client.AccumuloClient;
import org.apache.accumulo.core.client.AccumuloException;
import org.apache.accumulo.core.client.AccumuloSecurityException;
import org.apache.accumulo.core.client.Connector;
import org.apache.accumulo.core.client.TableNotFoundException;
import org.apache.accumulo.core.client.mapreduce.AccumuloFileOutputFormat;
import org.apache.accumulo.core.data.Key;
import org.apache.accumulo.core.data.Value;
import org.apache.fluo.api.config.FluoConfiguration;
import org.apache.fluo.api.data.Bytes;
import org.apache.fluo.api.data.RowColumn;
import org.apache.fluo.api.data.RowColumnValue;
import org.apache.fluo.mapreduce.FluoEntryInputFormat;
import org.apache.fluo.mapreduce.FluoKeyValue;
import org.apache.fluo.mapreduce.FluoKeyValueGenerator;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.spark.Partitioner;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import scala.Tuple2;

/**
 * Helper methods for using Fluo with Spark
 *
 * @since 1.0.0
 */
public class FluoSparkHelper {

  private static final Logger log = LoggerFactory.getLogger(FluoSparkHelper.class);
  private static AtomicInteger tempDirCounter = new AtomicInteger(0);
  private FluoConfiguration fluoConfig;
  private Configuration hadoopConfig;
  private Path tempBaseDir;
  private FileSystem hdfs;

  // @formatter:off
  public FluoSparkHelper(FluoConfiguration fluoConfig, Configuration hadoopConfig,
                         Path tempBaseDir) {
    // @formatter:on
    this.fluoConfig = fluoConfig;
    this.hadoopConfig = hadoopConfig;
    this.tempBaseDir = tempBaseDir;
    this.fluoConfig = fluoConfig;
    try {
      hdfs = FileSystem.get(hadoopConfig);
    } catch (IOException e) {
      throw new IllegalStateException("Unable to get HDFS client from hadoop config", e);
    }
  }

  /**
   * Converts RowColumnValue RDD to RowColumn/Bytes PairRDD
   *
   * @param rcvRDD RowColumnValue RDD to convert
   * @return RowColumn/Bytes PairRDD
   */
  public static JavaPairRDD<RowColumn, Bytes> toPairRDD(JavaRDD<RowColumnValue> rcvRDD) {
    return rcvRDD.mapToPair(rcv -> new Tuple2<>(rcv.getRowColumn(), rcv.getValue()));
  }

  /**
   * Converts RowColumn/Bytes PairRDD to RowColumnValue RDD
   *
   * @param pairRDD RowColumn/Bytes PairRDD
   * @return RowColumnValue RDD
   */
  public static JavaRDD<RowColumnValue> toRcvRDD(JavaPairRDD<RowColumn, Bytes> pairRDD) {
    return pairRDD.map(t -> new RowColumnValue(t._1().getRow(), t._1().getColumn(), t._2()));
  }

  private static AccumuloClient getAccumuloClient(FluoConfiguration config) {
    return Accumulo.newClient().to(config.getAccumuloInstance(), config.getAccumuloZookeepers())
        .as(config.getAccumuloUser(), config.getAccumuloPassword()).build();
  }

  /**
   * Reads all data from a snapshot in Fluo and returns it as a RowColumn/Value RDD.
   *
   * @param ctx Java Spark context
   * @return RowColumn/Value RDD containing all data in Fluo
   */
  public JavaPairRDD<RowColumn, Bytes> readFromFluo(JavaSparkContext ctx) {
    Job job;
    try {
      job = Job.getInstance(hadoopConfig);
    } catch (IOException e) {
      throw new IllegalStateException(e);
    }
    FluoEntryInputFormat.configure(job, fluoConfig);

    return ctx.newAPIHadoopRDD(job.getConfiguration(), FluoEntryInputFormat.class, RowColumn.class,
        Bytes.class);
  }

  /**
   * Bulk import RowColumn/Value data into Fluo table (obtained from Fluo configuration). This
   * method will repartition RDD using the current split points of the Fluo table, creating one
   * partition per tablet in the table. This is done so that one RFile is created per tablet for
   * bulk import.
   *
   * @param data RowColumn/Value data to import
   * @param opts Bulk import options
   */
  public void bulkImportRcvToFluo(JavaPairRDD<RowColumn, Bytes> data, BulkImportOptions opts) {

    data = partitionForAccumulo(data, fluoConfig.getAccumuloTable(), opts);

    JavaPairRDD<Key, Value> kvData = data.flatMapToPair(tuple -> {
      List<Tuple2<Key, Value>> output = new LinkedList<>();
      RowColumn rc = tuple._1();
      FluoKeyValueGenerator fkvg = new FluoKeyValueGenerator();
      fkvg.setRow(rc.getRow()).setColumn(rc.getColumn()).setValue(tuple._2().toArray());
      for (FluoKeyValue kv : fkvg.getKeyValues()) {
        output.add(new Tuple2<>(kv.getKey(), kv.getValue()));
      }
      return output.iterator();
    });

    bulkImportKvToAccumulo(kvData, fluoConfig.getAccumuloTable(), opts);
  }

  /**
   * Bulk import Key/Value data into into Fluo table (obtained from Fluo configuration). This method
   * does not repartition data. One RFile will be created for each partition in the passed in RDD.
   * Ensure the RDD is reasonably partitioned before calling this method.
   *
   * @param data Key/Value data to import
   * @param opts Bulk import options
   */
  public void bulkImportKvToFluo(JavaPairRDD<Key, Value> data, BulkImportOptions opts) {
    bulkImportKvToAccumulo(data, fluoConfig.getAccumuloTable(), opts);
  }

  /**
   * Bulk import RowColumn/Value data into specified Accumulo table. This method will repartition
   * RDD using the current split points of the specified table, creating one partition per tablet in
   * the table. This is done so that one RFile is created per tablet for bulk import.
   *
   * @param data RowColumn/Value data to import
   * @param accumuloTable Accumulo table used for import
   * @param opts Bulk import options
   */
  public void bulkImportRcvToAccumulo(JavaPairRDD<RowColumn, Bytes> data, String accumuloTable,
      BulkImportOptions opts) {

    data = partitionForAccumulo(data, accumuloTable, opts);

    JavaPairRDD<Key, Value> kvData = data.mapToPair(tuple -> {
      RowColumn rc = tuple._1();
      byte[] row = rc.getRow().toArray();
      byte[] cf = rc.getColumn().getFamily().toArray();
      byte[] cq = rc.getColumn().getQualifier().toArray();
      byte[] val = tuple._2().toArray();
      return new Tuple2<>(new Key(new Text(row), new Text(cf), new Text(cq), 0), new Value(val));
    });
    bulkImportKvToAccumulo(kvData, accumuloTable, opts);
  }

  /**
   * Bulk import Key/Value data into specified Accumulo table. This method does not repartition
   * data. One RFile will be created for each partition in the passed in RDD. Ensure the RDD is
   * reasonably partitioned before calling this method.
   *
   * @param data Key/value data to import
   * @param accumuloTable Accumulo table used for import
   * @param opts Bulk import options
   */
  public void bulkImportKvToAccumulo(JavaPairRDD<Key, Value> data, String accumuloTable,
      BulkImportOptions opts) {

    Path tempDir = getTempDir(opts);

    try (AccumuloClient client = getAccumuloClient(fluoConfig)) {
      if (hdfs.exists(tempDir)) {
        throw new IllegalArgumentException("HDFS temp dir already exists: " + tempDir.toString());
      }
      hdfs.mkdirs(tempDir);
      Path dataDir = new Path(tempDir.toString() + "/data");
      Path failDir = new Path(tempDir.toString() + "/fail");
      hdfs.mkdirs(failDir);

      // save data to HDFS
      Job job = Job.getInstance(hadoopConfig);
      AccumuloFileOutputFormat.setOutputPath(job, dataDir);
      // must use new API here as saveAsHadoopFile throws exception
      data.saveAsNewAPIHadoopFile(dataDir.toString(), Key.class, Value.class,
          AccumuloFileOutputFormat.class, job.getConfiguration());

      // bulk import data to Accumulo
      log.info("Wrote data for bulk import to HDFS temp directory: {}", dataDir);
      Connector conn = chooseConnector(client, opts);
      conn.tableOperations().importDirectory(accumuloTable, dataDir.toString(), failDir.toString(),
          false);

      // throw exception if failures directory contains files
      if (hdfs.listFiles(failDir, true).hasNext()) {
        throw new IllegalStateException("Bulk import failed!  Found files that failed to import "
            + "in failures directory: " + failDir);
      }
      log.info("Successfully bulk imported data in {} to '{}' Accumulo table", dataDir,
          accumuloTable);

      // delete data directory
      hdfs.delete(tempDir, true);
      log.info("Deleted HDFS temp directory created for bulk import: {}", tempDir);
      // @formatter:off
    } catch (IOException | TableNotFoundException | AccumuloException
        | AccumuloSecurityException e) {
      // @formatter:on
      throw new IllegalStateException(e);
    }
  }

  /**
   * Optional settings for Bulk Imports
   *
   * @since 1.0.0
   */
  public static class BulkImportOptions {

    public static BulkImportOptions DEFAULT = new BulkImportOptions();

    Connector conn = null;
    Path tempDir = null;

    public BulkImportOptions() {}

    /**
     * If this methods is not called, then a Connector will be created using properties in the
     * FluoConfiguration supplied to
     * {@link FluoSparkHelper#FluoSparkHelper(FluoConfiguration, Configuration, Path)}
     *
     * @param conn Use this connector to bulk import files into Accumulo.
     * @return this
     * @deprecated use {@link #setAccumuloClient(AccumuloClient)}
     */
    @Deprecated(since = "1.3.0", forRemoval = true)
    public BulkImportOptions setAccumuloConnector(Connector conn) {
      Objects.requireNonNull(conn);
      this.conn = conn;
      return this;
    }

    /**
     * If this methods is not called, then a Client will be created using properties in the
     * FluoConfiguration supplied to
     * {@link FluoSparkHelper#FluoSparkHelper(FluoConfiguration, Configuration, Path)}
     *
     * @param client Use this client to bulk import files into Accumulo.
     * @return this
     *
     * @since 1.3.0
     */
    public BulkImportOptions setAccumuloClient(AccumuloClient client) {
      Objects.requireNonNull(client);
      try {
        this.conn = Connector.from(client);
      } catch (AccumuloSecurityException | AccumuloException e) {
        throw new RuntimeException(e);
      }
      return this;
    }

    /**
     * If this method is not called, then a temp dir will be created based on the path passed
     * supplied to {@link FluoSparkHelper#FluoSparkHelper(FluoConfiguration, Configuration, Path)}
     *
     * @param tempDir Use this directory to store RFiles generated for bulk import.
     * @return this
     */
    public BulkImportOptions setTempDir(Path tempDir) {
      Objects.requireNonNull(tempDir);
      this.tempDir = tempDir;
      return this;
    }
  }

  private Path getPossibleTempDir() {
    return new Path(tempBaseDir.toString() + "/" + tempDirCounter.getAndIncrement());
  }

  private Path getTempDir(BulkImportOptions opts) {
    Path tempDir;
    if (opts.tempDir == null) {
      try {
        tempDir = getPossibleTempDir();
        while (hdfs.exists(tempDir)) {
          tempDir = getPossibleTempDir();
        }
      } catch (IOException e) {
        throw new IllegalStateException(e);
      }
    } else {
      tempDir = opts.tempDir;
    }
    return tempDir;
  }

  private JavaPairRDD<RowColumn, Bytes> partitionForAccumulo(JavaPairRDD<RowColumn, Bytes> data,
      String accumuloTable, BulkImportOptions opts) {
    // partition and sort data so that one file is created per an accumulo tablet
    Partitioner accumuloPartitioner;
    try (AccumuloClient client = getAccumuloClient(fluoConfig)) {
      accumuloPartitioner = new AccumuloRangePartitioner(
          chooseConnector(client, opts).tableOperations().listSplits(accumuloTable));
    } catch (TableNotFoundException | AccumuloSecurityException | AccumuloException e) {
      throw new IllegalStateException(e);
    }
    return data.repartitionAndSortWithinPartitions(accumuloPartitioner);
  }

  private Connector chooseConnector(AccumuloClient client, BulkImportOptions opts) {
    try {
      return opts.conn == null ? Connector.from(client) : opts.conn;
    } catch (AccumuloSecurityException | AccumuloException e) {
      throw new RuntimeException(e);
    }
  }
}

redpajama_set_name: RedPajamaGithub


text:
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;

namespace MyHome.UI
{
    class SortableBindingList<T> : BindingList<T>
    {
        CustomComparer comparer;

        protected override bool SupportsSortingCore
        {
            get { return true; }
        }

        bool isSorted = false;
        protected override bool IsSortedCore
        {
            get { return isSorted; }
        }

        PropertyDescriptor propertyDescriptor;
        protected override PropertyDescriptor SortPropertyCore
        {
            get { return propertyDescriptor; }
        }

        private ListSortDirection listSortDirection;
        protected override ListSortDirection SortDirectionCore
        {
            get { return listSortDirection; }
        }

        public SortableBindingList() : this(new Tuple<string, ListSortDirection>[0], new List<T>()) { }
        public SortableBindingList(IEnumerable<Tuple<string, ListSortDirection>> props) : this(props, new List<T>()) { }
        public SortableBindingList(IEnumerable<Tuple<string, ListSortDirection>> props, List<T> list)
            : base(list)
        {
            // Build Comparer chain
            var t = TypeDescriptor.GetProperties(typeof(T));

            foreach (var prop in props)
            {
                var p = t.Find(prop.Item1, false);
                if (p == null)
                    throw new ArgumentException("The property \"" + prop.Item1 + "\" was not found on " + typeof(T).FullName + ".", "prop");

                isSorted = true;
                this.propertyDescriptor = p;
                this.listSortDirection = prop.Item2;
                this.comparer = new CustomComparer(this.comparer, p, prop.Item2);
            }

            // Wrap in final Comparer for user sorting
            this.comparer = new CustomComparer(this.comparer);

            var items = this.Items as List<T>;
            items.Sort(this.comparer);
        }

        public void Load(IEnumerable<T> collection)
        {
            var data = this.Items as List<T>;
            data.Clear();
            data.AddRange(collection);
            data.Sort(this.comparer);
            this.OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
        }

        protected override void ApplySortCore(PropertyDescriptor prop, ListSortDirection direction)
        {
            var items = this.Items as List<T>;

            this.comparer.SetSort(prop, direction);
            items.Sort(this.comparer);

            this.propertyDescriptor = prop;
            this.listSortDirection = direction;
            this.isSorted = true;
            this.OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
        }

        protected override int FindCore(PropertyDescriptor prop, object key)
        {
            for (int i = 0, count = this.Count; i < count; i++)
            {
                if (prop.GetValue(this[i]).Equals(key))
                {
                    return i;
                }
            }
            return -1;
        }

        private class CustomComparer : IComparer<T>
        {
            static readonly Dictionary<PropertyDescriptor, IComparer> comparerCache = new Dictionary<PropertyDescriptor, IComparer>();

            readonly CustomComparer baseComparer;
            IComparer comparer;
            public ListSortDirection Direction { get; set; }
            public PropertyDescriptor Property { get; private set; }

            public CustomComparer(PropertyDescriptor prop, ListSortDirection direction) : this(null, prop, direction) { }
            public CustomComparer(CustomComparer baseComparer) : this(baseComparer, null, ListSortDirection.Ascending) { }
            public CustomComparer(CustomComparer baseComparer, PropertyDescriptor prop, ListSortDirection direction)
            {
                this.baseComparer = baseComparer;
                this.Direction = direction;
                this.Property = prop;
                this.comparer = GetComparer(prop);
            }

            public void SetSort(PropertyDescriptor prop, ListSortDirection direction)
            {
                this.Direction = direction;

                if (baseComparer != null && prop == baseComparer.Property)
                {
                    baseComparer.Direction = direction;
                    prop = null;
                }

                Property = prop;
                comparer = GetComparer(prop);
            }

            public int Compare(T x, T y)
            {
                if (comparer != null)
                {
                    var result = comparer.Compare(Property.GetValue(x), Property.GetValue(y));
                    if (Direction == ListSortDirection.Descending)
                        result = -result;

                    if (result != 0)
                        return result;
                }

                if (baseComparer != null)
                    return baseComparer.Compare(x, y);

                return 0;
            }

            IComparer GetComparer(PropertyDescriptor prop)
            {
                if (prop == null) return null;
                if (comparerCache.ContainsKey(prop)) return comparerCache[prop];

                var propComparer = typeof(Comparer<>).MakeGenericType(prop.PropertyType);
                var result = propComparer.InvokeMember("Default", BindingFlags.Static | BindingFlags.GetProperty | BindingFlags.Public, null, null, null) as IComparer;
                comparerCache.Add(prop, result);
                return result;
            }
        }
    }
}


text:
//#####################################################################//
// ENUMERABLE
//#####################################################################//

/**
 * An Enumerable object is an indexable iterable collection. It
 * always uses an array as a backing data store and exposes
 * that array to the user for read/write access.
 * @class
 * @extends Iterable
 * @param {array} data - (Optional) Backing data array
 */
var Enumerable = function(data) {
    this._data = data || [];
};

Enumerable.prototype = new Iterable();

/**
 * @override
 */
Enumerable.prototype.iterator = function() {
    var data = this.data();
    return {
        _data: data,
        _i: 0,
        _hasElements: data.length > 0,
        next: function() {
            var result = this._data[this._i++];
            if (this._i >= this._data.length) {
                this._hasElements = false;
            }
            return result;
        },
        hasElements: function() {
            return this._hasElements;
        }
    };
};

/**
 * Exposes the backing array so its default methods can be used
 * on this collection
 * @returns {array}
 */
Enumerable.prototype.data = function() {
    return this._data;
};

/**
 * Set the backing data array to the new given one
 * @param {array} newData - New backing array
 */
Enumerable.prototype.setData = function(newData) {
    this._data = newData;
};

/**
 * @override
 */
Enumerable.prototype.count = function() {
    return this.data().length;
};

/**
 * Returns the index of the first element of the iterable for which
 * the predicate returns true or -1 if no such element is found
 * @param {function} predicate - A function mapping an item to a boolean
 * @param {object} thisArg - Object to use as *this* when executing predicate
 * @returns {number}
 */
Enumerable.prototype.firstIndex = function(predicate, thisArg) {
    for (var i = 0; i < this.data().length; ++i) {
        if (predicate.call(thisArg, this.data()[i])) {
            return i;
        }
    }
    return -1;
};

/**
 * @override
 */
Enumerable.prototype.last = function(predicate, thisArg) {
    if (predicate === undefined) {
        return this.data()[this.data().length - 1];
    } else {
        for (var i = this.data().length - 1; i >= 0; --i) {
            if (predicate.call(thisArg, this.data()[i])) {
                return this.data()[i];
            }
        }
    }
    return undefined;
};

/**
 * Returns the index of the last element of the iterable for which
 * the predicate returns true or -1 if no such element is found
 * @param {function} predicate - A function mapping an item to a boolean
 * @param {object} thisArg - Object to use as *this* when executing predicate
 * @returns {number}
 */
Enumerable.prototype.lastIndex = function(predicate, thisArg) {
    for (var i = this.count() - 1; i >= 0; --i) {
        if (predicate.call(thisArg, this.data()[i])) {
            return i;
        }
    }
    return -1;
};

/**
 * Removes all items in the enumerable which are equal to the given
 * item according to the equalFunc supplied. If no equal function
 * is supplied strict equality is used instead.
 * @param {any} item - Item to be removed
 * @param {function} equalFunc - Function taking two arguments and
 * @param {object} thisArg - Object to use as *this* when executing equalFunc
 * returning true if they are equal
 */
Enumerable.prototype.remove = function(item, equalFunc, thisArg) {
    equalFunc = equalFunc || function(a, b) {
        return a === b;
    };
    var data = this.data();
    for (var i = 0; i < this.count(); ++i) {
        if (equalFunc.call(thisArg, data[i], item)) {
            data.splice(i, 1);
            i--;
        }
    }
    this.setData(data);
};

/**
 * @override
 */
Enumerable.prototype.toArray = function() {
    return this._data.slice();
};

/**
 * Returns an enumerable collection of numbers.
 * @param {number} start - Starting number (Optional, 0 default)
 * @param {number} step - Increase from one number to
 * the next (Optional, 1 default)
 * @param {number} stop - Last number (Required)
 * @returns {Enumerable}
 */
Enumerable.range = function(start, step, stop) {
    if (start === undefined) {
        return undefined;
    } else if (step === undefined) {
        stop = start;
        step = 1;
        start = 0;
    } else if (stop === undefined) {
        stop = step;
        step = 1;
    }
    var result = [];
    for (var i = start; step > 0 ? i < stop : i > stop; i += step) {
        result.push(i);
    }
    return new Enumerable(result);
};


text:
#include <eigen3/Eigen/Dense>
#include <assertext.h>
#include <MatrixIO.h>
#include <ConMatrixTools.h>
#include "RS2Euler.h"
#include "DefGradOperator.h"
#include "ComputeBj.h"
using namespace Eigen;
using namespace LSW_WARPING;

/**
 * 1. compute the deformation gradient operator G
 * 2. compute the volumes V and Sqrt_V of all the tetrahedron.
 */
void RS2Euler::setTetMesh(pTetMesh_const tetmesh){

  assert (tetmesh != NULL);
  this->tetmesh = tetmesh;
  const int elem_num = tetmesh->tets().size();
  const int node_num = tetmesh->nodes().size();
  SparseMatrix<double> V;

  if (DefGradOperator::compute(tetmesh,G)){

	assert_eq(G.cols(), node_num*3);
	assert_eq(G.rows(), elem_num*9);
	constructVolumeMatrix(tetmesh,V,Sqrt_V);
	assert_eq(V.cols(), G.rows());

	VG_t = (V*G).transpose();
	VG_t.makeCompressed();
	L = VG_t*VG_t.transpose();
  }else{
	ERROR_LOG("failed to compute gradient operator G");
  }
}

void RS2Euler::fixBaryCenter(){

  assert (tetmesh != NULL);
  const int node_num = tetmesh->nodes().size();
  set<int> all_nodes;
  for (int i = 0; i < node_num; ++i){
	all_nodes.insert(i);
  }
  UTILITY::computeBaryCenterConM(all_nodes,node_num,F);
}

// compute the constraint matrix for the fixed nodes
void RS2Euler::setFixedNodes(const vector<int> &fixed_nodes){

  assert (tetmesh != NULL);
  const int node_num = tetmesh->nodes().size();
  UTILITY::computeConM(fixed_nodes,F, node_num);
  assert_eq(F.cols(), node_num*3);
}

// compute the constraint matrix for the fixed nodes
void RS2Euler::setFixedNodes(const set<int> &fixed_nodes){

  assert (tetmesh != NULL);
  const int node_num = tetmesh->nodes().size();
  UTILITY::computeConM(fixed_nodes,F, node_num);
  assert_eq(F.cols(), node_num*3);
}

// compute the constraint matrix for the constrained nodes
void RS2Euler::setConNodes(const vector<set<int> > &c_nodes,const VectorXd &bcen_uc){

  assert (tetmesh != NULL);
  assert_eq ((int)c_nodes.size()*3, bcen_uc.size());
  this->barycenter_uc = bcen_uc;
  const int node_num = tetmesh->nodes().size();
  if (c_nodes.size() > 0){
	UTILITY::computeBaryCenterConM(c_nodes,node_num,C);
  }else{
	C.resize(0,0);
  }
}

// compute the constraint matrix for the constrained nodes
void RS2Euler::setConNodes(const vector<int> &c_nodes,const VectorXd &uc){

  assert (tetmesh != NULL);
  assert_eq ((int)c_nodes.size()*3, uc.size());
  this->barycenter_uc = uc;
  const int node_num = tetmesh->nodes().size();
  if (c_nodes.size() > 0){
	UTILITY::computeConM(c_nodes,C,node_num);
  }else{
	C.resize(0,0);
  }
}

/**
 * assemble A and make factorization.
 */
bool RS2Euler::precompute(){

  bool succ = true;
  assembleA();
  A.makeCompressed();
  A_solver.compute(A);
  if(A_solver.info()!=Eigen::Success) {
	ERROR_LOG("failed to decompse A for solving A X = B");
	succ = false;
  }
  return succ;
}

/**
 * reconstruct the displacements u in Euler space with RS coordinates y
 * provided.
 *
 * @param y the RS coordinates.
 * @param u the constructed Euler coordinates represents the displacements.
 *
 * @return true if construction is success.
 */
bool RS2Euler::reconstruct(const VectorXd &y, VectorXd &u){

  assert (tetmesh != NULL);
  const int node_numx3 = tetmesh->nodes().size()*3;
  bool succ = true;

  // assemble the right_side
  VectorXd b;
  assemble_b(y,b);
  assert_eq(VG_t.cols(),b.size());
  assert_eq(VG_t.rows(),node_numx3);

  VectorXd right_side(node_numx3 + numFixedDofs());
  right_side.segment(0, node_numx3) = VG_t*b;
  right_side.segment(node_numx3,numFixedDofs()).setZero();
  right_side.segment(node_numx3,barycenter_uc.size()) = barycenter_uc;

  // solve A*u = right_side
  assert_eq (right_side.size(), A_solver.rows());
  u = A_solver.solve(right_side);

  // get the result
  if(A_solver.info()!=Eigen::Success) {

  	succ = false;
  	u.resize(node_numx3);
  	u.setZero();
  	ERROR_LOG("failed to solve for A X = P.");
  }else{
	assert_gt(u.size(), node_numx3);
	const VectorXd x = u.head(node_numx3);
	u = x;
  }
  return succ;
}

void RS2Euler::constructVolumeMatrix(pTetMesh_const tetmesh, SparseMatrix<double> &V,vector<double> &Sqrt_V)const{

  const int elem_num = tetmesh->tets().size();

  Sqrt_V.resize(elem_num);
  V.resize(elem_num*9,elem_num*9);
  V.reserve(elem_num*9);

  for (int e = 0; e < elem_num; e++){

	const double sqrt_v = sqrt(tetmesh->volume(e));
	Sqrt_V[e] = sqrt_v;
	for (int i = 0; i < 9; ++i){
	  V.insert(e*9+i,e*9+i) = sqrt_v;
	}
  }
}

/*
 * A = | L C^t F^t|
 *     | C  O  O  |
 *     | F  O  O  |
 *
 */
void RS2Euler::assembleA(){

  const int n = L.rows() + C.rows() + F.rows();
  const int nz = L.nonZeros() +C.nonZeros()*2 + F.nonZeros()*2;

  typedef Eigen::Triplet<double> T;
  vector<T> A_triplet;
  A_triplet.reserve(nz);

  // assign L to A_triplet
  for (int k=0; k<L.outerSize(); ++k){
	for (SparseMatrix<double>::InnerIterator it(L,k); it; ++it){
	  A_triplet.push_back(  T(it.row(),it.col(),it.value())  );
	}
  }

  // assign C and C^t to A_triplet
  const int Ln = L.rows();
  for (int k=0; k<C.outerSize(); ++k){
	for (SparseMatrix<double>::InnerIterator it(C,k); it; ++it){
	  A_triplet.push_back( T(it.row() + Ln,it.col(), it.value()) );
	  A_triplet.push_back( T(it.col(),it.row() + Ln, it.value()) );
	}
  }

  // assign F and F^t to A_triplet
  const int LnCn = Ln+C.rows();
  for (int k=0; k<F.outerSize(); ++k){
	for (SparseMatrix<double>::InnerIterator it(F,k); it; ++it){
	  A_triplet.push_back( T(it.row() + LnCn,it.col(), it.value()) );
	  A_triplet.push_back( T(it.col(),it.row() + LnCn, it.value()) );
	}
  }

  // set A from the tripletlist
  A.resize (n,n);
  A.reserve (nz);
  A.setFromTriplets(A_triplet.begin(), A_triplet.end());
}

void RS2Euler::assemble_b(const VectorXd &y,VectorXd & b)const{

  const int elem_num = tetmesh->tets().size();
  assert_eq(y.size(), elem_num*9);
  b.resize(elem_num*9);

  /// compute b from y
  for (int j = 0; j < elem_num; ++j){
    const double *yj = &(y[9*j]);
	double *bj = &(b[9*j]);
	ComputeBj::compute(yj,Sqrt_V[j],bj);
  }
}


text:
Executing tests
===============

Browser
-------

The testsuite uses Selenium. By default it uses the HtmlUnit WebDriver, but can also be executed with Chrome or Firefox.

To run the tests with Firefox add `-Dbrowser=firefox` or for Chrome add `-Dbrowser=chrome`

Database
--------

By default the testsuite uses an embedded H2 database to test with other databases see (Database Testing)[tests-db.md].

Test utils
==========

All the utils can be executed from the directory testsuite/utils:

    cd testsuite/utils

Keycloak server
---------------

To start a basic Keycloak server for testing run:

    mvn exec:java -Pkeycloak-server

or run org.keycloak.testsuite.KeycloakServer from your favourite IDE!

When starting the server it can also import a realm from a json file:

    mvn exec:java -Pkeycloak-server -Dimport=testrealm.json

When starting the server, https transport can be set up by setting keystore containing the server certificate
and https port, optionally setting the truststore.

    mvn exec:java -Pkeycloak-server \
        -Djavax.net.ssl.trustStore=/path/to/truststore.jks \
        -Djavax.net.ssl.keyStore=/path/to/keystore.jks \
        -Djavax.net.ssl.keyStorePassword=CHANGEME \
        -Dkeycloak.port.https=8443

### Default admin account

The default admin within the master realm are created with credentials:
* Username: `admin`
* Password: `admin`

The Keycloak test suite server will automatically create the new master realm user when the following conditions are met
* Property `keycloak.createAdminUser` is set to `true` (defaults to `true` if not present)
* There is no existing user within the master realm

### Live edit of html and styles

The Keycloak test server can load resources directly from the filesystem instead of the classpath. This allows editing html, styles and updating images without restarting the server. To make the server use resources from the filesystem start with:

    mvn exec:java -Pkeycloak-server -Dresources

You can also specify the theme directory used by the server with:

    mvn exec:java -Pkeycloak-server -Dkeycloak.theme.dir=<PATH TO THEMES DIR>

For example to use the example themes run the server with:

    mvn exec:java -Pkeycloak-server -Dkeycloak.theme.dir=examples/themes

**NOTE:** If `keycloak.theme.dir` is specified the default themes (base, rcue and keycloak) are loaded from the classpath

TOTP codes
----------

To generate totp codes without Google authenticator run:

    mvn exec:java -Ptotp

or run org.keycloak.testsuite.TotpGenerator from your favourite IDE!

Once started copy/paste the totp secret and press enter. To use a new secret just copy/paste and press enter again.

Mail server
-----------

To start a test mail server for testing email sending run:

    mvn exec:java -Pmail-server

or run org.keycloak.testsuite.MailServer from your favourite IDE!

To configure Keycloak to use the above server add the following system properties:

    keycloak.mail.smtp.from=auto@keycloak.org
    keycloak.mail.smtp.host=localhost
    keycloak.mail.smtp.port=3025

For example if using the test utils Keycloak server start it with:

    mvn exec:java -Pkeycloak-server -Dkeycloak.mail.smtp.from=auto@keycloak.org -Dkeycloak.mail.smtp.host=localhost -Dkeycloak.mail.smtp.port=3025

LDAP server
-----------

To start a ApacheDS based LDAP server for testing LDAP sending run:

    mvn exec:java -Pldap

There are additional system properties you can use to configure (See LDAPEmbeddedServer class for details). Once done, you can create LDAP Federation provider
in Keycloak admin console with the settings like:
* Vendor: Other
* Connection URL: ldap://localhost:10389
* User DN Suffix: ou=People,dc=keycloak,dc=org
* Bind DN: uid=admin,ou=system
* Bind credential: secret

Kerberos server
---------------

To start a ApacheDS based Kerberos server for testing Kerberos + LDAP sending run:

    mvn exec:java -Pkerberos

There are additional system properties you can use to configure (See LDAPEmbeddedServer and KerberosEmbeddedServer class for details) but for testing purposes default values should be good.
By default ApacheDS LDAP server will be running on localhost:10389 and Kerberos KDC on localhost:6088 .

The alternative is to start Kerberos with the alternative realm KC2.COM instead of default KEYCLOAK.ORG.
The ApacheDS server will be then started with all ports shifted by 1000 (EG. LDAP on 11389, Kerberos KDC on 7088).
This allows to start 2 kerberos servers in parallel to test things like Kerberos cross-realm trust:

    mvn exec:java -Pkerberos -Dkeycloak.kerberos.realm=KC2.COM

Once kerberos is running, you can create LDAP Federation provider in Keycloak admin console with same settings like mentioned in previous LDAP section.
But additionally you can enable Kerberos authentication in LDAP provider with the settings like:

* Kerberos realm: KEYCLOAK.ORG
* Server Principal: HTTP/localhost@KEYCLOAK.ORG
* KeyTab: $KEYCLOAK_SOURCES/testsuite/integration-arquillian/tests/base/src/test/resources/kerberos/http.keytab (Replace $KEYCLOAK_SOURCES with correct absolute path of your sources)

Once you do this, you should also ensure that your Kerberos client configuration file is properly configured with KEYCLOAK.ORG domain.
See [../testsuite/integration-arquillian/tests/base/src/test/resources/kerberos/test-krb5.conf](../testsuite/integration-arquillian/tests/base/src/test/resources/kerberos/test-krb5.conf) for inspiration. The location of Kerberos configuration file
is platform dependent (In linux it's file `/etc/krb5.conf` )

Then you need to configure your browser to allow SPNEGO/Kerberos login from `localhost` .

Exact steps are again browser dependent. For Firefox see for example [http://www.microhowto.info/howto/configure_firefox_to_authenticate_using_spnego_and_kerberos.html](http://www.microhowto.info/howto/configure_firefox_to_authenticate_using_spnego_and_kerberos.html) .
URI `localhost` must be allowed in `network.negotiate-auth.trusted-uris` config option.

For Chrome, you just need to run the browser with command similar to this (more details in Chrome documentation):

```
/usr/bin/google-chrome-stable --auth-server-whitelist="localhost"
```

Finally test the integration by retrieve kerberos ticket. In many OS you can achieve this by running command from CMD like:

```
kinit hnelson@KEYCLOAK.ORG
```

and provide password `secret`

Now when you access `http://localhost:8081/auth/realms/master/account` you should be logged in automatically as user `hnelson` .

Simple loadbalancer
-------------------

You can run class `SimpleUndertowLoadBalancer` from IDE. By default, it executes the embedded undertow loadbalancer running on `http://localhost:8180`, which communicates with 2 backend Keycloak nodes
running on `http://localhost:8181` and `http://localhost:8182` . See javadoc for more details.

Create many users or offline sessions
-------------------------------------
Run testsuite with the command like this:

```
mvn exec:java -Pkeycloak-server -DstartTestsuiteCLI
```

Alternatively if you want to use your MySQL database use the command like this (replace properties values according your DB connection):

```
mvn exec:java -Pkeycloak-server -Dkeycloak.connectionsJpa.url=jdbc:mysql://localhost/keycloak -Dkeycloak.connectionsJpa.driver=com.mysql.jdbc.Driver -Dkeycloak.connectionsJpa.user=keycloak -Dkeycloak.connectionsJpa.password=keycloak -DstartTestsuiteCLI
```

Then once CLI is started, you can use command `help` to see all the available commands.

### Creating many users

For create many users you can use command `createUsers`
For example this will create 500 users `test0, test1, test2, ... , test499` in realm `demo` and each 100 users in separate transaction. All users will be granted realm roles `user` and `admin` :

```
createUsers test test demo 0 500 100 user,admin
```

Check count of users:

```
getUsersCount demo
```

Check if concrete user was really created:

```
getUser demo test499
```

### Creating many offline sessions

For create many offline sessions you can use command `persistSessions` . For example create 50000 sessions (each 500 in separate transaction) with command:

```
persistSessions 50000 500
```

Once users or sessions are created, you can restart to ensure the startup import of offline sessions will be triggered and you can see impact on startup time. After restart you can use command:

```
size
```

to doublecheck total count of sessions in infinispan (it will be 2 times as there is also 1 client session per each user session created)


text:
select sum(lo_extendedprice*lo_discount) as
revenue
from lineorder join  dim_date on lo_orderdatekey = d_datekey
where
d_year = 1993
and lo_discount between 1 and 3
and lo_quantity < 25
WITH HINT(USE_OLAP_PLAN);


text:
package org.jf.dexlib2;

import java.util.HashMap;

public enum AccessFlags {
    PUBLIC(0x1, "public", true, true, true),
    PRIVATE(0x2, "private", true, true, true),
    PROTECTED(0x4, "protected", true, true, true),
    STATIC(0x8, "static", true, true, true),
    FINAL(0x10, "final", true, true, true),
    SYNCHRONIZED(0x20, "synchronized", false, true, false),
    VOLATILE(0x40, "volatile", false, false, true),
    BRIDGE(0x40, "bridge", false, true, false),
    TRANSIENT(0x80, "transient", false, false, true),
    VARARGS(0x80, "varargs", false, true, false),
    NATIVE(0x100, "native", false, true, false),
    INTERFACE(0x200, "interface", true, false, false),
    ABSTRACT(0x400, "abstract", true, true, false),
    STRICTFP(0x800, "strictfp", false, true, false),
    SYNTHETIC(0x1000, "synthetic", true, true, true),
    ANNOTATION(0x2000, "annotation", true, false, false),
    ENUM(0x4000, "enum", true, false, true),
    CONSTRUCTOR(0x10000, "constructor", false, true, false),
    DECLARED_SYNCHRONIZED(0x20000, "declared-synchronized", false, true, false);

    //cache the array of all AccessFlags, because .values() allocates a new array for every call
    private final static AccessFlags[] allFlags;
    private static HashMap<String, AccessFlags> accessFlagsByName;

    static {
        allFlags = AccessFlags.values();

        accessFlagsByName = new HashMap<String, AccessFlags>();
        for (AccessFlags accessFlag : allFlags) {
            accessFlagsByName.put(accessFlag.accessFlagName, accessFlag);
        }
    }

    private int value;
    private String accessFlagName;
    private boolean validForClass;
    private boolean validForMethod;
    private boolean validForField;

    AccessFlags(int value, String accessFlagName, boolean validForClass, boolean validForMethod,
                boolean validForField) {
        this.value = value;
        this.accessFlagName = accessFlagName;
        this.validForClass = validForClass;
        this.validForMethod = validForMethod;
        this.validForField = validForField;
    }

    public static AccessFlags[] getAccessFlagsForClass(int accessFlagValue) {
        int size = 0;
        for (AccessFlags accessFlag : allFlags) {
            if (accessFlag.validForClass && (accessFlagValue & accessFlag.value) != 0) {
                size++;
            }
        }

        AccessFlags[] accessFlags = new AccessFlags[size];
        int accessFlagsPosition = 0;
        for (AccessFlags accessFlag : allFlags) {
            if (accessFlag.validForClass && (accessFlagValue & accessFlag.value) != 0) {
                accessFlags[accessFlagsPosition++] = accessFlag;
            }
        }
        return accessFlags;
    }

    private static String formatAccessFlags(AccessFlags[] accessFlags) {
        int size = 0;
        for (AccessFlags accessFlag : accessFlags) {
            size += accessFlag.toString().length() + 1;
        }

        StringBuilder sb = new StringBuilder(size);
        for (AccessFlags accessFlag : accessFlags) {
            sb.append(accessFlag.toString());
            sb.append(" ");
        }
        if (accessFlags.length > 0) {
            sb.delete(sb.length() - 1, sb.length());
        }
        return sb.toString();
    }

    public static String formatAccessFlagsForClass(int accessFlagValue) {
        return formatAccessFlags(getAccessFlagsForClass(accessFlagValue));
    }

    public static AccessFlags[] getAccessFlagsForMethod(int accessFlagValue) {
        int size = 0;
        for (AccessFlags accessFlag : allFlags) {
            if (accessFlag.validForMethod && (accessFlagValue & accessFlag.value) != 0) {
                size++;
            }
        }

        AccessFlags[] accessFlags = new AccessFlags[size];
        int accessFlagsPosition = 0;
        for (AccessFlags accessFlag : allFlags) {
            if (accessFlag.validForMethod && (accessFlagValue & accessFlag.value) != 0) {
                accessFlags[accessFlagsPosition++] = accessFlag;
            }
        }
        return accessFlags;
    }

    public static String formatAccessFlagsForMethod(int accessFlagValue) {
        return formatAccessFlags(getAccessFlagsForMethod(accessFlagValue));
    }

    public static AccessFlags[] getAccessFlagsForField(int accessFlagValue) {
        int size = 0;
        for (AccessFlags accessFlag : allFlags) {
            if (accessFlag.validForField && (accessFlagValue & accessFlag.value) != 0) {
                size++;
            }
        }

        AccessFlags[] accessFlags = new AccessFlags[size];
        int accessFlagsPosition = 0;
        for (AccessFlags accessFlag : allFlags) {
            if (accessFlag.validForField && (accessFlagValue & accessFlag.value) != 0) {
                accessFlags[accessFlagsPosition++] = accessFlag;
            }
        }
        return accessFlags;
    }

    public static String formatAccessFlagsForField(int accessFlagValue) {
        return formatAccessFlags(getAccessFlagsForField(accessFlagValue));
    }

    public static AccessFlags getAccessFlag(String accessFlag) {
        return accessFlagsByName.get(accessFlag);
    }

    public boolean isSet(int accessFlags) {
        return (this.value & accessFlags) != 0;
    }

    public int getValue() {
        return value;
    }

    public String toString() {
        return accessFlagName;
    }
}


text:
{extend name="base@:style" /}
{block name="body"}

                <form class="layui-form" action="/php/data.php">

                            <select name="city" lay-verify="required" lay-filter="ajax" data-params='{"url": "/php/test.php","data":"id=1&name=ni&va=23","confirm":"true"}'>
                                <option value="0">请选择分类</option>
                                <option value="010">北京</option>
                                <option value="021">上海</option>
                                <option value="0571">杭州</option>
                            </select>

                                <input class="layui-input start-date" name="start_date" placeholder="开始日">

                                <input class="layui-input end-date" name="end_date" placeholder="截止日">

                                <input class="layui-input" name="keyword" placeholder="关键字">

                            <button lay-submit class="layui-btn" lay-filter="search">查找</button>

                </form>

                    <button class="layui-btn layui-btn-small layui-btn-danger ajax-all" data-name="checkbox" data-params='{"url": "/php/test.php","data":"id=1&name=ni&va=23","confirm":"true"}'>
                         删除
                    </button>
                    <button class="layui-btn layui-btn-small layui-btn-normal ajax-all" data-name="checkbox" data-params='{"url": "/php/test.php","data":"id=1&name=hao","confirm":"true"}'>
                         推荐
                    </button>
                    <button class="layui-btn layui-btn-small layui-btn-normal ajax-all" data-name="checkbox" data-params='{"url": "/php/test.php","data":"id=1&name=hao&va=23"}'>
                         置顶
                    </button>
                    <button class="layui-btn layui-btn-small layui-btn-normal ajax-all" data-name="checkbox" data-params='{"url": "allChangeState","data":"model=cms_article"}'>
                        စ 审核
                    </button>
                    <button class="layui-btn layui-btn-small modal-iframe" data-params='{"content": "save.html", "title": "添加文章","full":"true"}'>
                         添加
                    </button>

    <form id="form1" class="layui-form layui-form-pane" action="save">

            模板代码

                <input type="text" name="name" required jq-verify="required" jq-error="错误提示信息" placeholder="提示信息"
                       autocomplete="off" class="layui-input ">

            排序

                <input type="text" name="orders" required jq-verify="number" value="99" jq-error="只有数字才能排序"
                       placeholder="数字越小越排在前面" autocomplete="off" class="layui-input ">

            状态

                <input type="radio" name="state" title="显示" value="1" checked/>
                <input type="radio" name="state" title="隐藏" value="0"/>

                <button class="layui-btn" jq-submit jq-filter="submit">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>

        <input type="hidden" name="id" value="0"/>
    </form>

{/block}
{block name="each"}

{/block}

{block name="js"}

{/block}


text:
/**
 * @author Denis M. Kishenko
 * @version $Revision$
 */
package java.awt.geom;

import java.awt.Rectangle;

import junit.framework.TestCase;

public class Rectangle2DDoubleTest extends TestCase {

    Rectangle2D.Double r;

    public Rectangle2DDoubleTest(String name) {
        super(name);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        r = new Rectangle2D.Double(1, 2, 3, 4);
    }

    @Override
    protected void tearDown() throws Exception {
        r = null;
        super.tearDown();
    }

    public void testCreate() {
        assertEquals(new Rectangle2D.Double(0, 0, 0, 0), new Rectangle2D.Double());
    }

    public void testGetX() {
        assertEquals(1.0f, r.getX(), 0.0f);
    }

    public void testGetY() {
        assertEquals(2.0f, r.getY(), 0.0f);
    }

    public void testGetWidth() {
        assertEquals(3.0f, r.getWidth(), 0.0f);
    }

    public void testGetHeight() {
        assertEquals(4.0f, r.getHeight(), 0.0f);
    }

    public void testIsEmpty() {
        assertFalse(r.isEmpty());
        assertTrue(new Rectangle2D.Double(1, 2, -1, 1).isEmpty());
        assertTrue(new Rectangle2D.Double(3, 4, 1, -1).isEmpty());
        assertTrue(new Rectangle2D.Double(3, 4, 1, 0).isEmpty());
        assertTrue(new Rectangle2D.Double(5, 6, -1, -1).isEmpty());
    }

    public void testSetRect1() {
        r.setRect(5.0, 6.0, 7.0, 8.0);
        assertEquals(new Rectangle2D.Double(5, 6, 7, 8), r);
    }

    public void testSetRect2() {
        r.setRect(5.0f, 6.0f, 7.0f, 8.0f);
        assertEquals(new Rectangle2D.Double(5, 6, 7, 8), r);
    }

    public void testSetRect3() {
        r.setRect(new Rectangle(5, 6, 7, 8));
        assertEquals(new Rectangle2D.Double(5, 6, 7, 8), r);
    }

    public void testOutcode() {
        assertEquals(Rectangle2D.OUT_LEFT | Rectangle2D.OUT_TOP,     r.outcode(0, 0));
        assertEquals(Rectangle2D.OUT_TOP,                            r.outcode(2, 0));
        assertEquals(Rectangle2D.OUT_RIGHT | Rectangle2D.OUT_TOP,    r.outcode(5, 0));
        assertEquals(Rectangle2D.OUT_RIGHT,                          r.outcode(5, 3));
        assertEquals(Rectangle2D.OUT_RIGHT | Rectangle2D.OUT_BOTTOM, r.outcode(5, 7));
        assertEquals(Rectangle2D.OUT_BOTTOM,                         r.outcode(2, 7));
        assertEquals(Rectangle2D.OUT_LEFT | Rectangle2D.OUT_BOTTOM,  r.outcode(0, 7));
        assertEquals(Rectangle2D.OUT_LEFT,                           r.outcode(0, 3));
        assertEquals(0, r.outcode(2, 3));
    }

    public void testGetBounds2D() {
        assertEquals(new Rectangle2D.Double(1, 2, 3, 4), r.getBounds2D());
    }

    public void testToString() {
        assertEquals("java.awt.geom.Rectangle2D$Double[x=1.0,y=2.0,width=3.0,height=4.0]", r.toString());
    }

    public void testCreateIntersection() {
        assertEquals(new Rectangle2D.Float(1, 2, -1, -2),  r.createIntersection(new Rectangle2D.Float()));            // Empty
        assertEquals(new Rectangle2D.Double(1, 2, 3, 4),   r.createIntersection(new Rectangle2D.Double(1, 2, 3, 4))); // The same
        assertEquals(new Rectangle2D.Float(2, 3, 1, 2),    r.createIntersection(new Rectangle2D.Float(2, 3, 1, 2)));  // Inside
        assertEquals(new Rectangle2D.Double(5, 7, -1, -1), r.createIntersection(new Rectangle2D.Double(5, 7, 1, 2))); // Outside
        assertEquals(new Rectangle2D.Float(2, 3, 2, 3),    r.createIntersection(new Rectangle2D.Float(2, 3, 5, 6)));  // Intersect
        assertEquals(new Rectangle2D.Double(1, 2, 3, 4),   r.createIntersection(new Rectangle2D.Double(0, 0, 5, 6))); // Cover
    }

    public void testCreateUnion() {
//      assertEquals(r.createUnion(new Rectangle2D.Double()), new Rectangle2D.Double(1, 2, 3, 4));           // Empty
        assertEquals(new Rectangle2D.Float(1, 2, 3, 4), r.createUnion(new Rectangle2D.Float(1, 2, 3, 4)));   // The same
        assertEquals(new Rectangle2D.Double(1, 2, 3, 4), r.createUnion(new Rectangle2D.Double(2, 3, 1, 2))); // Inside
        assertEquals(new Rectangle2D.Float(1, 2, 5, 7), r.createUnion(new Rectangle2D.Float(5, 7, 1, 2)));   // Outside
        assertEquals(new Rectangle2D.Double(1, 2, 6, 7), r.createUnion(new Rectangle2D.Double(2, 3, 5, 6))); // Intersect
        assertEquals(new Rectangle2D.Float(0, 0, 5, 6), r.createUnion(new Rectangle2D.Float(0, 0, 5, 6)));   // Cover
    }

    public static void main(String[] args) {
        junit.textui.TestRunner.run(Rectangle2DDoubleTest.class);
    }

}


text:
package main

import (
	"fmt"

	"github.com/opsgenie/opsgenie-go-sdk/_samples/constants"
	ogcli "github.com/opsgenie/opsgenie-go-sdk/client"
	"github.com/opsgenie/opsgenie-go-sdk/scheduleoverride"
)

func main() {
	cli := new(ogcli.OpsGenieClient)
	cli.SetAPIKey(constants.APIKey)

	overrideCli, cliErr := cli.ScheduleOverride()
	if cliErr != nil {
		panic(cliErr)
	}
	req := override.AddScheduleOverrideRequest{
		Alias: "override_add",
		Schedule: "test",
		User: "fazilet@test.com",
		StartDate: "2013-01-27 22:00",
		EndDate: "2018-01-28 08:00"}
	overrideResponse, overrideErr := overrideCli.Add(req)

	if overrideErr != nil {
		panic(overrideErr)
	}

	fmt.Printf("alias: %s\n", overrideResponse.Alias)
	fmt.Printf("status: %s\n", overrideResponse.Status)
	fmt.Printf("code: %d\n", overrideResponse.Code)

}


text:
package main

import (
	"flag"
	"fmt"
	"net"
	"net/http"
	"os"
	"os/exec"
	"strings"
	"time"
)

var version = "0.2.3"

var (
	port        = flag.Int("p", 5000, "port to serve on")
	prefix      = flag.String("x", "/", "prefix to serve under")
	showVersion = flag.Bool("v", false, "show version info")
	openBrowser = flag.Bool("o", false, "open the url")
)

func main() {
	flag.Parse()

	if *showVersion {
		fmt.Println("serve version", version)
		os.Exit(0)
	}

	var dir string
	// Get the dir to serve
	if flag.NArg() < 1 {
		cwd, err := os.Getwd()
		if err != nil {
			fmt.Println("Please provide the dir to serve as the last argument. A simple . will also do")
			os.Exit(1)
		}
		dir = cwd
	}
	dir = flag.Arg(0)
	portStr := fmt.Sprintf(":%v", *port)
	if !strings.HasPrefix(*prefix, "/") {
		*prefix = "/" + *prefix
	}
	if !strings.HasSuffix(*prefix, "/") {
		*prefix = *prefix + "/"
	}

	uri := fmt.Sprintf("http://localhost:%v%v", *port, *prefix)

	fmt.Printf("Service traffic from %v under port %v with prefix %v\n", dir, *port, *prefix)
	fmt.Printf("Or simply put, just open %v to get rocking!\n", uri)

	go func() {
		if *openBrowser {
			success := waitForWebserver()
			if !success {
				// We have waited too long for the webserver to start; bail.
				fmt.Fprintf(os.Stderr, "The webserver did not start within the required time. Cannot open the browser for you\n")
				return
			}
			fmt.Printf("Opening your browser to %v\n", uri)
			cmd := exec.Command("open", uri)
			if err := cmd.Run(); err != nil {
				fmt.Fprintf(os.Stderr, "Could not open the url in your browser\n%v\n", err)
			}
		}
	}()

	http.Handle(*prefix, http.StripPrefix(*prefix, http.FileServer(http.Dir(dir))))
	if err := http.ListenAndServe(portStr, nil); err != nil {
		fmt.Fprintf(os.Stderr, "Error while starting the web server\n%v\n", err)
		os.Exit(1)
	}
}

func waitForWebserver() bool {
	timeout := time.After(1 * time.Second)
	connStr := fmt.Sprintf("127.0.0.1:%v", *port)
	for {
		select {
		case <-timeout:
			return false
		default:
			conn, err := net.DialTimeout("tcp", connStr, 50*time.Millisecond)
			if err != nil {
				continue
			}
			conn.Close()
			return true
		}
	}
}


text:
require 'spec_helper'

describe SamlIdp::Controller do
  include SamlIdp::Controller

  def render(*)
  end

  def params
    @params ||= {}
  end

  def head(status, options = {})
  end

  it "should find the SAML ACS URL" do
    requested_saml_acs_url = "https://example.com/saml/consume"
    params[:SAMLRequest] = make_saml_request(requested_saml_acs_url)
    validate_saml_request
    expect(saml_acs_url).to eq(requested_saml_acs_url)
  end

  context "SP-initiated logout w/o embed" do
    before do
      SamlIdp.configure do |config|
        config.service_provider.finder = lambda do |_|
          {
            certs: [
              OpenSSL::X509::Certificate.new(Base64.decode64(SamlIdp::Default::X509_CERTIFICATE))
            ],
            private_key: SamlIdp::Default::SECRET_KEY,
            assertion_consumer_logout_service_url: 'http://foo.example.com/sp-initiated/slo'
          }
        end
      end
    end

    it "should respect Logout Request" do
      request_url = URI.parse(make_sp_logout_request)
      params.merge!(Rack::Utils.parse_nested_query(request_url.query)).symbolize_keys!
      decode_request(params[:SAMLRequest])
      expect(saml_request.logout_request?).to eq true
      expect(valid_saml_request?).to eq true
    end

    it "requires Signature be present in params" do
      request_url = URI.parse(make_sp_logout_request)
      params.merge!(Rack::Utils.parse_nested_query(request_url.query)).symbolize_keys!
      params.delete(:Signature)
      decode_request(params[:SAMLRequest])

      expect(saml_request.logout_request?).to eq true
      expect(valid_saml_request?).to eq false
    end
  end

  context "SAML Responses" do
    before(:each) do
      params[:SAMLRequest] = make_saml_request
      validate_saml_request
    end

    let(:principal) { double email_address: "foo@example.com" }
    let (:encryption_opts) do
      {
        cert: SamlIdp::Default::X509_CERTIFICATE,
        block_encryption: 'aes256-cbc',
        key_transport: 'rsa-oaep-mgf1p',
      }
    end

    it "should create a SAML Response" do
      saml_response = encode_response(principal)
      response = OneLogin::RubySaml::Response.new(saml_response)
      name_id_format_email = Saml::XML::Namespaces::Formats::NameId::EMAIL_ADDRESS
      expect(response.name_id_format).to eq(name_id_format_email)
      expect(response.name_id).to eq("foo@example.com")
      expect(response.issuers.first).to eq("http://example.com")
      response.settings = saml_settings
      expect(response.is_valid?).to be_truthy
    end

    it "should create a SAML Response with specified name id format" do
      name_id_format_persistent = Saml::XML::Namespaces::Formats::NameId::PERSISTENT
      opts = {name_id_format: name_id_format_persistent}
      expect(principal).to receive(:id).twice
      saml_response = encode_response(principal, opts)
      response = OneLogin::RubySaml::Response.new(saml_response)
      expect(response.name_id_format).to eq(name_id_format_persistent)
      expect(response.issuers.first).to eq("http://example.com")
    end

    it "should sign a SAML Response if requested" do
      saml_response_encoded = encode_response(principal, signed_response_message: true)
      saml_response_text = Base64.decode64(saml_response_encoded)
      saml_response = REXML::Document.new(saml_response_text)
      response_id = REXML::XPath.match(saml_response, '//samlp:Response').first.attributes['ID']
      signature_ref = REXML::XPath.match(saml_response, '//ds:Reference').first.attributes['URI'][1..-1]

      expect(signature_ref).to eq response_id
    end

    it "should create a SAML Logout Response" do
      params[:SAMLRequest] = make_saml_logout_request
      validate_saml_request
      expect(saml_request.logout_request?).to eq true
      saml_response = encode_response(principal)
      response = OneLogin::RubySaml::Logoutresponse.new(saml_response, saml_settings)
      expect(response.validate).to eq(true)
      expect(response.issuer).to eq("http://example.com")
    end

    [:sha1, :sha256, :sha384, :sha512].each do |algorithm_name|
      it "should create a SAML Response using the #{algorithm_name} algorithm" do
        self.algorithm = algorithm_name
        saml_response = encode_response(principal)
        response = OneLogin::RubySaml::Response.new(saml_response)
        expect(response.name_id).to eq("foo@example.com")
        expect(response.issuers.first).to eq("http://example.com")
        response.settings = saml_settings
        expect(response.is_valid?).to be_truthy
      end

      it "should encrypt SAML Response assertion" do
        self.algorithm = algorithm_name
        saml_response = encode_response(principal, encryption: encryption_opts)
        resp_settings = saml_settings
        resp_settings.private_key = SamlIdp::Default::SECRET_KEY
        response = OneLogin::RubySaml::Response.new(saml_response, settings: resp_settings)
        expect(response.document.to_s).not_to match("foo@example.com")
        expect(response.decrypted_document.to_s).to match("foo@example.com")
        expect(response.name_id).to eq("foo@example.com")
        expect(response.issuers.first).to eq("http://example.com")
        expect(response.is_valid?).to be_truthy
      end
    end
  end

  context 'invalid SAML Request' do
    it 'returns headers only with a forbidden status' do
      params[:SAMLRequest] = make_invalid_saml_request

      expect(self).to receive(:head).with(:forbidden)

      validate_saml_request
    end
  end

  context 'invalid XML in SAML Request' do
    # This was encountered IRL on 2021-02-09
    before do
      allow_any_instance_of(subject).to receive(:valid_saml_request?).and_raise(Nokogiri::XML::SyntaxError)
    end

    it 'returns headers only with a bad_request status' do
      params[:SAMLRequest] = make_saml_request

      expect(self).to receive(:head).with(:bad_request)

      validate_saml_request
    end
  end
end