text:
package org.apache.hadoop.yarn.server.resourcemanager;

import static org.mockito.Matchers.any;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.metrics2.MetricsSystem;
import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem;
import org.apache.hadoop.net.NetUtils;
import org.apache.hadoop.yarn.api.records.ApplicationAttemptId;
import org.apache.hadoop.yarn.api.records.ApplicationId;
import org.apache.hadoop.yarn.api.records.ContainerId;
import org.apache.hadoop.yarn.api.records.ContainerState;
import org.apache.hadoop.yarn.api.records.ContainerStatus;
import org.apache.hadoop.yarn.api.records.NodeId;
import org.apache.hadoop.yarn.api.records.NodeState;
import org.apache.hadoop.yarn.api.records.Priority;
import org.apache.hadoop.yarn.api.records.Resource;
import org.apache.hadoop.yarn.conf.YarnConfiguration;
import org.apache.hadoop.yarn.event.Dispatcher;
import org.apache.hadoop.yarn.event.DrainDispatcher;
import org.apache.hadoop.yarn.event.Event;
import org.apache.hadoop.yarn.event.EventHandler;
import org.apache.hadoop.yarn.server.api.protocolrecords.NMContainerStatus;
import org.apache.hadoop.yarn.server.api.protocolrecords.NodeHeartbeatResponse;
import org.apache.hadoop.yarn.server.api.protocolrecords.RegisterNodeManagerRequest;
import org.apache.hadoop.yarn.server.api.protocolrecords.RegisterNodeManagerResponse;
import org.apache.hadoop.yarn.server.api.records.NodeAction;
import org.apache.hadoop.yarn.server.resourcemanager.rmapp.RMApp;
import org.apache.hadoop.yarn.server.resourcemanager.rmapp.attempt.RMAppAttemptImpl;
import org.apache.hadoop.yarn.server.resourcemanager.scheduler.QueueMetrics;
import org.apache.hadoop.yarn.server.resourcemanager.scheduler.event.SchedulerEvent;
import org.apache.hadoop.yarn.server.utils.BuilderUtils;
import org.apache.hadoop.yarn.util.Records;
import org.apache.hadoop.yarn.util.YarnVersionInfo;
import org.junit.After;
import org.junit.Assert;
import org.junit.Test;

public class TestResourceTrackerService {

  private final static File TEMP_DIR = new File(System.getProperty(
      "test.build.data", "/tmp"), "decommision");
  private final File hostFile = new File(TEMP_DIR + File.separator + "hostFile.txt");
  private MockRM rm;

  /**
   * Test RM read NM next heartBeat Interval correctly from Configuration file,
   * and NM get next heartBeat Interval from RM correctly
   */
  @Test (timeout = 50000)
  public void testGetNextHeartBeatInterval() throws Exception {
    Configuration conf = new Configuration();
    conf.set(YarnConfiguration.RM_NM_HEARTBEAT_INTERVAL_MS, "4000");

    rm = new MockRM(conf);
    rm.start();

    MockNM nm1 = rm.registerNode("host1:1234", 5120);
    MockNM nm2 = rm.registerNode("host2:5678", 10240);

    NodeHeartbeatResponse nodeHeartbeat = nm1.nodeHeartbeat(true);
    Assert.assertEquals(4000, nodeHeartbeat.getNextHeartBeatInterval());

    NodeHeartbeatResponse nodeHeartbeat2 = nm2.nodeHeartbeat(true);
    Assert.assertEquals(4000, nodeHeartbeat2.getNextHeartBeatInterval());

  }

  /**
   * Decommissioning using a pre-configured include hosts file
   */
  @Test
  public void testDecommissionWithIncludeHosts() throws Exception {

    writeToHostsFile("localhost", "host1", "host2");
    Configuration conf = new Configuration();
    conf.set(YarnConfiguration.RM_NODES_INCLUDE_FILE_PATH, hostFile
        .getAbsolutePath());

    rm = new MockRM(conf);
    rm.start();

    MockNM nm1 = rm.registerNode("host1:1234", 5120);
    MockNM nm2 = rm.registerNode("host2:5678", 10240);
    MockNM nm3 = rm.registerNode("localhost:4433", 1024);

    ClusterMetrics metrics = ClusterMetrics.getMetrics();
    assert(metrics != null);
    int metricCount = metrics.getNumDecommisionedNMs();

    NodeHeartbeatResponse nodeHeartbeat = nm1.nodeHeartbeat(true);
    Assert.assertTrue(NodeAction.NORMAL.equals(nodeHeartbeat.getNodeAction()));
    nodeHeartbeat = nm2.nodeHeartbeat(true);
    Assert.assertTrue(NodeAction.NORMAL.equals(nodeHeartbeat.getNodeAction()));
    nodeHeartbeat = nm3.nodeHeartbeat(true);
    Assert.assertTrue(NodeAction.NORMAL.equals(nodeHeartbeat.getNodeAction()));

    // To test that IPs also work
    String ip = NetUtils.normalizeHostName("localhost");
    writeToHostsFile("host1", ip);

    rm.getNodesListManager().refreshNodes(conf);

    checkDecommissionedNMCount(rm, ++metricCount);

    nodeHeartbeat = nm1.nodeHeartbeat(true);
    Assert.assertTrue(NodeAction.NORMAL.equals(nodeHeartbeat.getNodeAction()));
    Assert
        .assertEquals(1, ClusterMetrics.getMetrics().getNumDecommisionedNMs());

    nodeHeartbeat = nm2.nodeHeartbeat(true);
    Assert.assertTrue("Node is not decommisioned.", NodeAction.SHUTDOWN
        .equals(nodeHeartbeat.getNodeAction()));

    nodeHeartbeat = nm3.nodeHeartbeat(true);
    Assert.assertTrue(NodeAction.NORMAL.equals(nodeHeartbeat.getNodeAction()));
    Assert.assertEquals(metricCount, ClusterMetrics.getMetrics()
      .getNumDecommisionedNMs());
  }

  /**
   * Decommissioning using a pre-configured exclude hosts file
   */
  @Test
  public void testDecommissionWithExcludeHosts() throws Exception {
    Configuration conf = new Configuration();
    conf.set(YarnConfiguration.RM_NODES_EXCLUDE_FILE_PATH, hostFile
        .getAbsolutePath());

    writeToHostsFile("");
    final DrainDispatcher dispatcher = new DrainDispatcher();
    rm = new MockRM(conf) {
      @Override
      protected Dispatcher createDispatcher() {
        return dispatcher;
      }
    };
    rm.start();

    MockNM nm1 = rm.registerNode("host1:1234", 5120);
    MockNM nm2 = rm.registerNode("host2:5678", 10240);
    MockNM nm3 = rm.registerNode("localhost:4433", 1024);

    dispatcher.await();

    int metricCount = ClusterMetrics.getMetrics().getNumDecommisionedNMs();
    NodeHeartbeatResponse nodeHeartbeat = nm1.nodeHeartbeat(true);
    Assert.assertTrue(NodeAction.NORMAL.equals(nodeHeartbeat.getNodeAction()));
    nodeHeartbeat = nm2.nodeHeartbeat(true);
    Assert.assertTrue(NodeAction.NORMAL.equals(nodeHeartbeat.getNodeAction()));
    dispatcher.await();

    // To test that IPs also work
    String ip = NetUtils.normalizeHostName("localhost");
    writeToHostsFile("host2", ip);

    rm.getNodesListManager().refreshNodes(conf);

    checkDecommissionedNMCount(rm, metricCount + 2);

    nodeHeartbeat = nm1.nodeHeartbeat(true);
    Assert.assertTrue(NodeAction.NORMAL.equals(nodeHeartbeat.getNodeAction()));
    nodeHeartbeat = nm2.nodeHeartbeat(true);
    Assert.assertTrue("The decommisioned metrics are not updated",
        NodeAction.SHUTDOWN.equals(nodeHeartbeat.getNodeAction()));

    nodeHeartbeat = nm3.nodeHeartbeat(true);
    Assert.assertTrue("The decommisioned metrics are not updated",
        NodeAction.SHUTDOWN.equals(nodeHeartbeat.getNodeAction()));
    dispatcher.await();

    writeToHostsFile("");
    rm.getNodesListManager().refreshNodes(conf);

    nm3 = rm.registerNode("localhost:4433", 1024);
    dispatcher.await();
    nodeHeartbeat = nm3.nodeHeartbeat(true);
    dispatcher.await();
    Assert.assertTrue(NodeAction.NORMAL.equals(nodeHeartbeat.getNodeAction()));
    // decommissined node is 1 since 1 node is rejoined after updating exclude
    // file
    checkDecommissionedNMCount(rm, metricCount + 1);
  }

  /**
  * Decommissioning using a post-configured include hosts file
  */
  @Test
  public void testAddNewIncludePathToConfiguration() throws Exception {
    Configuration conf = new Configuration();
    rm = new MockRM(conf);
    rm.start();
    MockNM nm1 = rm.registerNode("host1:1234", 5120);
    MockNM nm2 = rm.registerNode("host2:5678", 10240);
    ClusterMetrics metrics = ClusterMetrics.getMetrics();
    assert(metrics != null);
    int initialMetricCount = metrics.getNumDecommisionedNMs();
    NodeHeartbeatResponse nodeHeartbeat = nm1.nodeHeartbeat(true);
    Assert.assertEquals(
        NodeAction.NORMAL,
        nodeHeartbeat.getNodeAction());
    nodeHeartbeat = nm2.nodeHeartbeat(true);
    Assert.assertEquals(
        NodeAction.NORMAL,
        nodeHeartbeat.getNodeAction());
    writeToHostsFile("host1");
    conf.set(YarnConfiguration.RM_NODES_INCLUDE_FILE_PATH, hostFile
        .getAbsolutePath());
    rm.getNodesListManager().refreshNodes(conf);
    checkDecommissionedNMCount(rm, ++initialMetricCount);
    nodeHeartbeat = nm1.nodeHeartbeat(true);
    Assert.assertEquals(
        "Node should not have been decomissioned.",
        NodeAction.NORMAL,
        nodeHeartbeat.getNodeAction());
    nodeHeartbeat = nm2.nodeHeartbeat(true);
    Assert.assertEquals("Node should have been decomissioned but is in state" +
        nodeHeartbeat.getNodeAction(),
        NodeAction.SHUTDOWN, nodeHeartbeat.getNodeAction());
  }

  /**
   * Decommissioning using a post-configured exclude hosts file
   */
  @Test
  public void testAddNewExcludePathToConfiguration() throws Exception {
    Configuration conf = new Configuration();
    rm = new MockRM(conf);
    rm.start();
    MockNM nm1 = rm.registerNode("host1:1234", 5120);
    MockNM nm2 = rm.registerNode("host2:5678", 10240);
    ClusterMetrics metrics = ClusterMetrics.getMetrics();
    assert(metrics != null);
    int initialMetricCount = metrics.getNumDecommisionedNMs();
    NodeHeartbeatResponse nodeHeartbeat = nm1.nodeHeartbeat(true);
    Assert.assertEquals(
        NodeAction.NORMAL,
        nodeHeartbeat.getNodeAction());
    nodeHeartbeat = nm2.nodeHeartbeat(true);
    Assert.assertEquals(
        NodeAction.NORMAL,
        nodeHeartbeat.getNodeAction());
    writeToHostsFile("host2");
    conf.set(YarnConfiguration.RM_NODES_EXCLUDE_FILE_PATH, hostFile
        .getAbsolutePath());
    rm.getNodesListManager().refreshNodes(conf);
    checkDecommissionedNMCount(rm, ++initialMetricCount);
    nodeHeartbeat = nm1.nodeHeartbeat(true);
    Assert.assertEquals(
        "Node should not have been decomissioned.",
        NodeAction.NORMAL,
        nodeHeartbeat.getNodeAction());
    nodeHeartbeat = nm2.nodeHeartbeat(true);
    Assert.assertEquals("Node should have been decomissioned but is in state" +
        nodeHeartbeat.getNodeAction(),
        NodeAction.SHUTDOWN, nodeHeartbeat.getNodeAction());
  }

  @Test
  public void testNodeRegistrationSuccess() throws Exception {
    writeToHostsFile("host2");
    Configuration conf = new Configuration();
    conf.set(YarnConfiguration.RM_NODES_INCLUDE_FILE_PATH, hostFile
        .getAbsolutePath());
    rm = new MockRM(conf);
    rm.start();

    ResourceTrackerService resourceTrackerService = rm.getResourceTrackerService();
    RegisterNodeManagerRequest req = Records.newRecord(
        RegisterNodeManagerRequest.class);
    NodeId nodeId = NodeId.newInstance("host2", 1234);
    Resource capability = BuilderUtils.newResource(1024, 1);
    req.setResource(capability);
    req.setNodeId(nodeId);
    req.setHttpPort(1234);
    req.setNMVersion(YarnVersionInfo.getVersion());
    // trying to register a invalid node.
    RegisterNodeManagerResponse response = resourceTrackerService.registerNodeManager(req);
    Assert.assertEquals(NodeAction.NORMAL,response.getNodeAction());
  }

  @Test
  public void testNodeRegistrationVersionLessThanRM() throws Exception {
    writeToHostsFile("host2");
    Configuration conf = new Configuration();
    conf.set(YarnConfiguration.RM_NODES_INCLUDE_FILE_PATH, hostFile
        .getAbsolutePath());
    conf.set(YarnConfiguration.RM_NODEMANAGER_MINIMUM_VERSION,"EqualToRM" );
    rm = new MockRM(conf);
    rm.start();
    String nmVersion = "1.9.9";

    ResourceTrackerService resourceTrackerService = rm.getResourceTrackerService();
    RegisterNodeManagerRequest req = Records.newRecord(
        RegisterNodeManagerRequest.class);
    NodeId nodeId = NodeId.newInstance("host2", 1234);
    Resource capability = BuilderUtils.newResource(1024, 1);
    req.setResource(capability);
    req.setNodeId(nodeId);
    req.setHttpPort(1234);
    req.setNMVersion(nmVersion);
    // trying to register a invalid node.
    RegisterNodeManagerResponse response = resourceTrackerService.registerNodeManager(req);
    Assert.assertEquals(NodeAction.SHUTDOWN,response.getNodeAction());
    Assert.assertTrue("Diagnostic message did not contain: 'Disallowed NodeManager " +
        "Version "+ nmVersion + ", is less than the minimum version'",
        response.getDiagnosticsMessage().contains("Disallowed NodeManager Version " +
            nmVersion + ", is less than the minimum version "));

  }

  @Test
  public void testNodeRegistrationFailure() throws Exception {
    writeToHostsFile("host1");
    Configuration conf = new Configuration();
    conf.set(YarnConfiguration.RM_NODES_INCLUDE_FILE_PATH, hostFile
        .getAbsolutePath());
    rm = new MockRM(conf);
    rm.start();

    ResourceTrackerService resourceTrackerService = rm.getResourceTrackerService();
    RegisterNodeManagerRequest req = Records.newRecord(
        RegisterNodeManagerRequest.class);
    NodeId nodeId = NodeId.newInstance("host2", 1234);
    req.setNodeId(nodeId);
    req.setHttpPort(1234);
    // trying to register a invalid node.
    RegisterNodeManagerResponse response = resourceTrackerService.registerNodeManager(req);
    Assert.assertEquals(NodeAction.SHUTDOWN,response.getNodeAction());
    Assert
      .assertEquals(
        "Disallowed NodeManager from  host2, Sending SHUTDOWN signal to the NodeManager.",
        response.getDiagnosticsMessage());
  }

  @Test
  public void testSetRMIdentifierInRegistration() throws Exception {

    Configuration conf = new Configuration();
    rm = new MockRM(conf);
    rm.start();

    MockNM nm = new MockNM("host1:1234", 5120, rm.getResourceTrackerService());
    RegisterNodeManagerResponse response = nm.registerNode();

    // Verify the RMIdentifier is correctly set in RegisterNodeManagerResponse
    Assert.assertEquals(ResourceManager.getClusterTimeStamp(),
      response.getRMIdentifier());
  }

  @Test
  public void testNodeRegistrationWithMinimumAllocations() throws Exception {
    Configuration conf = new Configuration();
    conf.set(YarnConfiguration.RM_SCHEDULER_MINIMUM_ALLOCATION_MB, "2048");
    conf.set(YarnConfiguration.RM_SCHEDULER_MINIMUM_ALLOCATION_VCORES, "4");
    rm = new MockRM(conf);
    rm.start();

    ResourceTrackerService resourceTrackerService
      = rm.getResourceTrackerService();
    RegisterNodeManagerRequest req = Records.newRecord(
        RegisterNodeManagerRequest.class);
    NodeId nodeId = BuilderUtils.newNodeId("host", 1234);
    req.setNodeId(nodeId);

    Resource capability = BuilderUtils.newResource(1024, 1);
    req.setResource(capability);
    RegisterNodeManagerResponse response1 =
        resourceTrackerService.registerNodeManager(req);
    Assert.assertEquals(NodeAction.SHUTDOWN,response1.getNodeAction());

    capability.setMemory(2048);
    capability.setVirtualCores(1);
    req.setResource(capability);
    RegisterNodeManagerResponse response2 =
        resourceTrackerService.registerNodeManager(req);
    Assert.assertEquals(NodeAction.SHUTDOWN,response2.getNodeAction());

    capability.setMemory(1024);
    capability.setVirtualCores(4);
    req.setResource(capability);
    RegisterNodeManagerResponse response3 =
        resourceTrackerService.registerNodeManager(req);
    Assert.assertEquals(NodeAction.SHUTDOWN,response3.getNodeAction());

    capability.setMemory(2048);
    capability.setVirtualCores(4);
    req.setResource(capability);
    RegisterNodeManagerResponse response4 =
        resourceTrackerService.registerNodeManager(req);
    Assert.assertEquals(NodeAction.NORMAL,response4.getNodeAction());
  }

  @Test
  public void testReboot() throws Exception {
    Configuration conf = new Configuration();
    rm = new MockRM(conf);
    rm.start();

    MockNM nm1 = rm.registerNode("host1:1234", 5120);
    MockNM nm2 = rm.registerNode("host2:1234", 2048);

    int initialMetricCount = ClusterMetrics.getMetrics().getNumRebootedNMs();
    NodeHeartbeatResponse nodeHeartbeat = nm1.nodeHeartbeat(true);
    Assert.assertTrue(NodeAction.NORMAL.equals(nodeHeartbeat.getNodeAction()));

    nodeHeartbeat = nm2.nodeHeartbeat(
      new HashMap<ApplicationId, List<ContainerStatus>>(), true, -100);
    Assert.assertTrue(NodeAction.RESYNC.equals(nodeHeartbeat.getNodeAction()));
    Assert.assertEquals("Too far behind rm response id:0 nm response id:-100",
      nodeHeartbeat.getDiagnosticsMessage());
    checkRebootedNMCount(rm, ++initialMetricCount);
  }

  private void checkRebootedNMCount(MockRM rm2, int count)
      throws InterruptedException {

    int waitCount = 0;
    while (ClusterMetrics.getMetrics().getNumRebootedNMs() != count
        && waitCount++ < 20) {
      synchronized (this) {
        wait(100);
      }
    }
    Assert.assertEquals("The rebooted metrics are not updated", count,
        ClusterMetrics.getMetrics().getNumRebootedNMs());
  }

  @Test
  public void testUnhealthyNodeStatus() throws Exception {
    Configuration conf = new Configuration();
    conf.set(YarnConfiguration.RM_NODES_EXCLUDE_FILE_PATH, hostFile
        .getAbsolutePath());

    rm = new MockRM(conf);
    rm.start();

    MockNM nm1 = rm.registerNode("host1:1234", 5120);
    Assert.assertEquals(0, ClusterMetrics.getMetrics().getUnhealthyNMs());
    // node healthy
    nm1.nodeHeartbeat(true);

    // node unhealthy
    nm1.nodeHeartbeat(false);
    checkUnealthyNMCount(rm, nm1, true, 1);

    // node healthy again
    nm1.nodeHeartbeat(true);
    checkUnealthyNMCount(rm, nm1, false, 0);
  }

  private void checkUnealthyNMCount(MockRM rm, MockNM nm1, boolean health,
      int count) throws Exception {

    int waitCount = 0;
    while((rm.getRMContext().getRMNodes().get(nm1.getNodeId())
        .getState() != NodeState.UNHEALTHY) == health
        && waitCount++ < 20) {
      synchronized (this) {
        wait(100);
      }
    }
    Assert.assertFalse((rm.getRMContext().getRMNodes().get(nm1.getNodeId())
        .getState() != NodeState.UNHEALTHY) == health);
    Assert.assertEquals("Unhealthy metrics not incremented", count,
        ClusterMetrics.getMetrics().getUnhealthyNMs());
  }

  @SuppressWarnings("unchecked")
  @Test
  public void testHandleContainerStatusInvalidCompletions() throws Exception {
    rm = new MockRM(new YarnConfiguration());
    rm.start();

    EventHandler handler =
        spy(rm.getRMContext().getDispatcher().getEventHandler());

    // Case 1: Unmanaged AM
    RMApp app = rm.submitApp(1024, true);

    // Case 1.1: AppAttemptId is null
    NMContainerStatus report =
        NMContainerStatus.newInstance(
          ContainerId.newContainerId(
            ApplicationAttemptId.newInstance(app.getApplicationId(), 2), 1),
          ContainerState.COMPLETE, Resource.newInstance(1024, 1),
          "Dummy Completed", 0, Priority.newInstance(10), 1234);
    rm.getResourceTrackerService().handleNMContainerStatus(report, null);
    verify(handler, never()).handle((Event) any());

    // Case 1.2: Master container is null
    RMAppAttemptImpl currentAttempt =
        (RMAppAttemptImpl) app.getCurrentAppAttempt();
    currentAttempt.setMasterContainer(null);
    report = NMContainerStatus.newInstance(
          ContainerId.newContainerId(currentAttempt.getAppAttemptId(), 0),
          ContainerState.COMPLETE, Resource.newInstance(1024, 1),
          "Dummy Completed", 0, Priority.newInstance(10), 1234);
    rm.getResourceTrackerService().handleNMContainerStatus(report, null);
    verify(handler, never()).handle((Event)any());

    // Case 2: Managed AM
    app = rm.submitApp(1024);

    // Case 2.1: AppAttemptId is null
    report = NMContainerStatus.newInstance(
          ContainerId.newContainerId(
            ApplicationAttemptId.newInstance(app.getApplicationId(), 2), 1),
          ContainerState.COMPLETE, Resource.newInstance(1024, 1),
          "Dummy Completed", 0, Priority.newInstance(10), 1234);
    try {
      rm.getResourceTrackerService().handleNMContainerStatus(report, null);
    } catch (Exception e) {
      // expected - ignore
    }
    verify(handler, never()).handle((Event)any());

    // Case 2.2: Master container is null
    currentAttempt =
        (RMAppAttemptImpl) app.getCurrentAppAttempt();
    currentAttempt.setMasterContainer(null);
    report = NMContainerStatus.newInstance(
      ContainerId.newContainerId(currentAttempt.getAppAttemptId(), 0),
      ContainerState.COMPLETE, Resource.newInstance(1024, 1),
      "Dummy Completed", 0, Priority.newInstance(10), 1234);
    try {
      rm.getResourceTrackerService().handleNMContainerStatus(report, null);
    } catch (Exception e) {
      // expected - ignore
    }
    verify(handler, never()).handle((Event)any());
  }

  @Test
  public void testReconnectNode() throws Exception {
    final DrainDispatcher dispatcher = new DrainDispatcher();
    rm = new MockRM() {
      @Override
      protected EventHandler<SchedulerEvent> createSchedulerEventDispatcher() {
        return new SchedulerEventDispatcher(this.scheduler) {
          @Override
          public void handle(SchedulerEvent event) {
            scheduler.handle(event);
          }
        };
      }

      @Override
      protected Dispatcher createDispatcher() {
        return dispatcher;
      }
    };
    rm.start();

    MockNM nm1 = rm.registerNode("host1:1234", 5120);
    MockNM nm2 = rm.registerNode("host2:5678", 5120);
    nm1.nodeHeartbeat(true);
    nm2.nodeHeartbeat(false);
    dispatcher.await();
    checkUnealthyNMCount(rm, nm2, true, 1);
    final int expectedNMs = ClusterMetrics.getMetrics().getNumActiveNMs();
    QueueMetrics metrics = rm.getResourceScheduler().getRootQueueMetrics();
    // TODO Metrics incorrect in case of the FifoScheduler
    Assert.assertEquals(5120, metrics.getAvailableMB());

    // reconnect of healthy node
    nm1 = rm.registerNode("host1:1234", 5120);
    NodeHeartbeatResponse response = nm1.nodeHeartbeat(true);
    Assert.assertTrue(NodeAction.NORMAL.equals(response.getNodeAction()));
    dispatcher.await();
    Assert.assertEquals(expectedNMs, ClusterMetrics.getMetrics().getNumActiveNMs());
    checkUnealthyNMCount(rm, nm2, true, 1);

    // reconnect of unhealthy node
    nm2 = rm.registerNode("host2:5678", 5120);
    response = nm2.nodeHeartbeat(false);
    Assert.assertTrue(NodeAction.NORMAL.equals(response.getNodeAction()));
    dispatcher.await();
    Assert.assertEquals(expectedNMs, ClusterMetrics.getMetrics().getNumActiveNMs());
    checkUnealthyNMCount(rm, nm2, true, 1);

    // unhealthy node changed back to healthy
    nm2 = rm.registerNode("host2:5678", 5120);
    dispatcher.await();
    response = nm2.nodeHeartbeat(true);
    response = nm2.nodeHeartbeat(true);
    dispatcher.await();
    Assert.assertEquals(5120 + 5120, metrics.getAvailableMB());

    // reconnect of node with changed capability
    nm1 = rm.registerNode("host2:5678", 10240);
    dispatcher.await();
    response = nm1.nodeHeartbeat(true);
    dispatcher.await();
    Assert.assertTrue(NodeAction.NORMAL.equals(response.getNodeAction()));
    Assert.assertEquals(5120 + 10240, metrics.getAvailableMB());

    // reconnect of node with changed capability and running applications
    List<ApplicationId> runningApps = new ArrayList<ApplicationId>();
    runningApps.add(ApplicationId.newInstance(1, 0));
    nm1 = rm.registerNode("host2:5678", 15360, 2, runningApps);
    dispatcher.await();
    response = nm1.nodeHeartbeat(true);
    dispatcher.await();
    Assert.assertTrue(NodeAction.NORMAL.equals(response.getNodeAction()));
    Assert.assertEquals(5120 + 15360, metrics.getAvailableMB());
  }

  private void writeToHostsFile(String... hosts) throws IOException {
    if (!hostFile.exists()) {
      TEMP_DIR.mkdirs();
      hostFile.createNewFile();
    }
    FileOutputStream fStream = null;
    try {
      fStream = new FileOutputStream(hostFile);
      for (int i = 0; i < hosts.length; i++) {
        fStream.write(hosts[i].getBytes());
        fStream.write("\n".getBytes());
      }
    } finally {
      if (fStream != null) {
        IOUtils.closeStream(fStream);
        fStream = null;
      }
    }
  }

  private void checkDecommissionedNMCount(MockRM rm, int count)
      throws InterruptedException {
    int waitCount = 0;
    while (ClusterMetrics.getMetrics().getNumDecommisionedNMs() != count
        && waitCount++ < 20) {
      synchronized (this) {
        wait(100);
      }
    }
    Assert.assertEquals(count, ClusterMetrics.getMetrics()
        .getNumDecommisionedNMs());
    Assert.assertEquals("The decommisioned metrics are not updated", count,
        ClusterMetrics.getMetrics().getNumDecommisionedNMs());
  }

  @After
  public void tearDown() {
    if (hostFile != null && hostFile.exists()) {
      hostFile.delete();
    }

    ClusterMetrics.destroy();
    if (rm != null) {
      rm.stop();
    }

    MetricsSystem ms = DefaultMetricsSystem.instance();
    if (ms.getSource("ClusterMetrics") != null) {
      DefaultMetricsSystem.shutdown();
    }
  }
}


text:
/*
 * Exercise 16.12: Write your own version of the Blob and BlobPtr templates. including the various const members that were not shown in the text.
 */

#include "Blob.h"
#include "BlobPtr.h"

int main()
{
    return 0;
}

redpajama_set_name: RedPajamaGithub


text:
. /etc/init.d/functions

PID=
DISPLAY_NAME=geowave
GEOWAVE_HOME=/usr/local/geowave

handle_return_value_and_exit() {
  case $1 in
    0) success ;;
    *) failure ;;
  esac
  echo
  exit $1
}

get_pid() {
  echo $(ps -u geowave | grep -v TTY | awk '{print $1}')
}

is_running() {
  PID=$(get_pid)
  if [ "x$PID" = "x" ]; then return 1; else return 0; fi
}

wait_for_start() {
  MAX_SECONDS_BEFORE_FAIL=15
  timeElapsed=0
  until is_running; do
	  sleep 1
	  let timeElapsed+=1
	  if [ $timeElapsed -gt $MAX_SECONDS_BEFORE_FAIL ]; then
		  return 1
	  fi
  done
  return 0
}

start() {
  echo -n "Starting $DISPLAY_NAME: "
  if ! is_running; then
    if [ -f /etc/geowave/geowave.config ]; then
      SOURCE_CMD="source /etc/geowave/geowave.config;"
    fi
  	runuser -l geowave -c "$SOURCE_CMD $GEOWAVE_HOME/geoserver/bin/startup.sh &" > /dev/null 2>&1
    wait_for_start
    handle_return_value_and_exit $?
  else
    echo -n "Already running, PID $PID"
  fi
  handle_return_value_and_exit 1
}

stop () {
  echo -n "Stopping $DISPLAY_NAME: "

  if is_running; then
    kill_process
    RETVAL=$?
    if [ "$1" != "restart" ]; then
      handle_return_value_and_exit $RETVAL
    else
      if [ $RETVAL -eq 0 ]; then success; else failure; fi
    fi
  else
    if [ "$1" != "restart" ]; then
      echo -n "$DISPLAY_NAME is not running"
      handle_return_value_and_exit 1
    else
      success
    fi
  fi
}

kill_process() {
  MAX_WAIT=10
  PID=$(get_pid)
  runuser -l geowave -c "$GEOWAVE_HOME/geoserver/bin/shutdown.sh"
  RETVAL=$?

  sleep 1
  while is_running; do
    sleep 1
    if [ $MAX_WAIT -le 2 ]; then # Try killing if we get to the end
        if [ "x$PID" != "x" ]; then
            kill $PID
        fi
    fi
    MAX_WAIT=$(expr $MAX_WAIT - 1)
    if [ $MAX_WAIT -le 0 ]; then handle_return_value_and_exit 1; fi
  done
  return $RETVAL
}

status () {
  if is_running; then
    echo "$DISPLAY_NAME is running, PID $PID"
  else
    echo "$DISPLAY_NAME is not running"
    exit 1
  fi
}

case "$1" in
  start) start ;;
  stop)  stop ;;
  restart)
	sleep 1 # in certain cases there is a race condition when puppet restarts the app repeatedly, this is to circumvent that
    stop "restart"
    echo
    start
    ;;
  status) status ;;
  *) echo "Usage: $0 {start|stop|restart|status}" ;;
esac


text:
/**
 * Opens up a popup window for oauth authentication (i.e. facebook/twitter). Expects the callback window to use window.opener
 * to call $window.oauthCallback with the oauth result. The result will then be piped into the callback function given.
 *
 * @author Alex Dong (https://github.com/Kiiri)
 */

var oauth = angular.module("kiiri.angular.oauth", []);

oauth.directive("oauth", ["$window", "$q",
    function ($window, $q) {
        "use strict";
        return {
            restrict: "A",
            scope: {
                url: "@",
                promise: "=?",
                successCallback: "=?",
                errorCallback: "=?"
            },
            link: function ($scope, element) {
                element.bind("click", function() {
                    var width = 600, height = 400;
                    var left = (screen.width / 2) - (width / 2);
                    var top = (screen.height / 2) - (2 * height / 3);
                    var features = 'menubar=no,toolbar=no,status=no,width=' + width + ',height=' + height + ',toolbar=no,left=' + left + ',top=' + top;
                    var loginWindow = $window.open($scope.url, '_blank', features);
                    loginWindow.focus();

                    // These has to be called on every click, so that $window.oauthCallback and $window.oauthErrorCallback
                    // are bound to the expected callbacks
                    $window.oauthCallback = function(response) {
                        if ($scope.promise) { $scope.promise.resolve({ url: $scope.url, data: response }); }
                        ($scope.successCallback || angular.noop)({ url: $scope.url, data: response });
                    };

                    $window.oauthErrorCallback = function(response) {
                        if ($scope.promise) { $scope.promise.reject({ url: $scope.url, data: response }); }
                        ($scope.errorCallback || angular.noop)({ url: $scope.url, data: response });
                    };
                });
            }
        };
    }
]);


text:
The Secret class exists to mask sensitive strings.
This helps avoid accidentally revealing data in backtraces, which are often written to logs (including remote logging services) or - if an environment is configured for development - revealed to the end-user.

The masked value is *not* encrypted; it's just `XOR`ed with a randomly-generated key that is discarded at the end of the request.
That means that Secrets may not be persisted across requests in any way; only their underlying values (which must be manually revealed).

The API is very simple:

```php
$masked_string = new Firehed\Security\Secret('string to mask');

$unmasked_string = $masked_string->reveal();
```

Any other method of getting at the underlying value will be either `"<secret>"` (where export detection is possible) or the masked value (likely binary garbage).

```php
require 'vendor/autoload.php';
$x = new Firehed\Security\Secret('asdf');
echo $x;
// <secret>

print_r($x);
// Firehed\Security\Secret Object
// (
//     [secret] => <secret>
// )

var_dump($x);
// class Firehed\Security\Secret#2 (1) {
//   public $secret =>
//   string(8) "<secret>"
// }

var_export($x);
// Firehed\Security\Secret::__set_state(array(
//    'value' => '�AZ�',
// ))

var_dump($x->reveal());
// string(4) "asdf"

try {
    doSomethingThatThrows($x);
} catch (Throwable $ex) {
    echo $ex;
}
// Stack trace:
// #0 /some/file.php(15): doSomethingThatThrows(Object(Firehed\Security\Secret))
// #1 {main}Exception: Some message in /some/file.php:9
```

### FAQ

#### Can this leak the hidden data?
Not directly, but at some point, you have to reveal the data to use it.
Example: `new PDO(...)` or `new mysqli(...)`.
If the function consuming the revealed secret throws an exception, the secret can still be revealed.
This cannot be solved in user space :(

#### Is this a replacement for encryption?
**NO**, it is not.
This obfuscates data, and does not encrypt it.

#### How and when should I use this?

This is a great wrapper for holding temporary sensitive data; e.g. a user's password from a POST request can be wrapped right up until the actual comparision.
More concretely:

```php
class User
{
    function isPasswordCorrect(string $password): bool
    {
        return password_verify($password, $this->pw_hash);
    }
}
// ...

if ($user->isPasswordCorrect($_POST['password'])) { ... }
```

becomes

```php
use Firehed\Security\Secret;

class User
{
    function isPasswordCorrect(Secret $password): bool
    {
        return password_verify($password->reveal(), $this->pw_hash);
    }
}
// ...
if ($user->isPasswordCorrect(new Secret($_POST['password']))) { ... }
```

It's also useful for holding API keys, connection passwords, etc:

```php
$container = new MyDIContainer();
$container['db_username'] = getenv('DB_USER');
$container['db_password'] = new Secret(getenv('DB_PASS'));
$container['database'] = function ($c) {
    return new PDO(
        'mysql:host=127.0.0.1;db=test',
        $c['db_username'],
        $c['db_password']->reveal(),
    );
};
```

(although if you're already using DI properly, the value is diminished)

#### What happens if I put a Secret in $_SESSION?
**Don't do this.**
It will work as expected for the rest of the request, but subsequent requests reading it will get garbage.

#### Why use this over passing around encrypted strings?
* No keys to manage
* No external dependencies (OpenSSL, libsodium, etc)
* Straightforward API
* Dead-simple, easy-to-follow implementation
* Encrypted strings still require storing key material somewhere

#### If the masked string is leaked, could it be reversed?
Yes and no.

It's vulnerable to known-plaintext attacks, so if an attacker gets the masked string for both a string they know/control and a string they are trying to capture *from the same request*, they could determine the mask (up to the first N bytes, where N is the length of the known plaintext) and then apply it to the targeted string.
Meaning if `strlen($known) >= strlen($target)`, then the target is revealed; if not, only the first N bytes are revealed.

Note that the mask is 128 characters long, and will be repeated if the string to be masked is longer.
So if an attacker figures out the first byte of the mask, they will know bytes 1, 129, 257, ... of the masked string.

The implementation makes every effort possible to make the masked value impossible to leak, but it can't catch every scenario due to PHP's user-land limitations (e.g. it's impossible to intercept `var_export`).

## OTP: One-Time Passwords

OTPs allow for a shared secret between a client and a server to perform authentication by hashing a known "counter" or "moving factor".
For HOTP, the counter is typically a monotonically-increasing value; TOTP is based on the current time.

### HOTP: HMAC-based One Time Password (RFC 4226)

You probably will not need to use this directly, since most user-facing OTP applications are based on the TOTP protocol (see below).
However, for reference, the API is as follows:

```php
// Preferred: Object-oriented

$otp = new \Firehed\Security\OTP(Secret $secret);
$code = $otp->getHOTP(int $counter, int $digits = 6, string $algorithm = OTP::ALGORITHM_SHA1);

// Legacy: function-based

$code = \Firehed\Security\HOTP(Secret $key, int $counter, int $digits = 6, string $algorithm = 'sha1');
```

Detailed parameter documentation is on the OTP class.

### TOTP: Time-based One Time Password (RFC 6238)

This builds off HOTPs by using time-based counters to make one-time passwords.
This was made popular by Google Authenticator, although a handful of TOTP clients now exist.

The API is extremely straightforward with the default values:

```php
// Preferred: Object-oriented

$otp = new \Firehed\Security\OTP(Secret $secret);
$code = $otp->getTOTP(int $step = 30, int $t0 = 0, int $digits = 6, string $algorithm = OTP::ALGORITHM_SHA1);

// Legacy: function-based
$code = \Firehed\Security\TOTP(Secret $key, array $options = []): string
```

Detailed parameter documentation is on the OTP class.
The default values for parameters align with a typical Google Authenticator-style TOTP setup.

Generating the one-time code is therefore very simple:

```php
// The string parameter to $secret should be user-specific, and kept protected at rest.
$secret = new \Firehed\Security\Secret('some shared secret');
$otp = new \Firehed\Security\OTP($secret);
$code = $otp->getTOTP();

// Or: $code = \Firehed\Security\TOTP($secret);
```

You should verify the expected value against the user's provided value with the
`hash_equals` function, in order to mitigate timing attacks:

```php
return hash_equals($user_input, $code);
```

Options allow for changing the number of output digits (default 6), hashing algorithm (sha1), or step (30 seconds).
Because most TOTP client apps don't fully support all of the options, it is recommended to only use the default values at this time.
See the docblocks in `src/OTP.php` and `src/TOTP.php` for additional information.

Note:

The secret provided to the `TOTP()` function must be the *raw value* - the one that a user adds to their app is normally sent to the user Base32-encoded.
If you provide the Base32-encoded secret to the function, you will get the wrong result.

### Shared Secrets

Both HOTP and TOTP are based on a secret shared between the client and server.
This secret must be generated by the server in a cryptographically-secure manner and stored encrypted;
providing the secret to the client must also be done in a secure way (likely using TLS) and should only be done once to avoid key cloning.

It is *highly recommended* to use the `random_bytes()` function in PHP to generate the shared secret.


text:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Uses of Interface org.apache.orc.impl.HadoopShims (ORC Core 1.4.3 API)</title>
<meta name="date" content="2018-02-09">
<link rel="stylesheet" type="text/css" href="../../../../../stylesheet.css" title="Style">

JavaScript is disabled on your browser.

Skip navigation links

Overview

Package

Class

Use

Tree

Deprecated

Index

Help

Prev

Next

Frames

No Frames

All Classes

Uses of Interface
org.apache.orc.impl.HadoopShims

Packages that use HadoopShims

Package
Description

org.apache.orc.impl

Uses of HadoopShims in org.apache.orc.impl

Classes in org.apache.orc.impl that implement HadoopShims

Modifier and Type
Class and Description

class
HadoopShims_2_2

Shims for versions of Hadoop up to and including 2.2.x

class
HadoopShimsCurrent

Shims for recent versions of Hadoop

Methods in org.apache.orc.impl that return HadoopShims

Modifier and Type
Method and Description

static HadoopShims
HadoopShims.Factory.get()

Skip navigation links

Overview

Package

Class

Use

Tree

Deprecated

Index

Help

Prev

Next

Frames

No Frames

All Classes

Copyright © 2013–2018 The Apache Software Foundation. All rights reserved.


text:
package org.wildfly.swarm.microprofile.utils.jwt;

import io.smallrye.jwt.KeyUtils;
import org.jboss.shrinkwrap.impl.base.io.IOUtil;
import org.jose4j.jws.AlgorithmIdentifiers;
import org.jose4j.jws.JsonWebSignature;
import org.jose4j.jwt.JwtClaims;

import java.io.InputStream;
import java.security.PrivateKey;

/**
 * @author Michal Szynkiewicz, michal.l.szynkiewicz@gmail.com
 * <br>
 * Date: 7/9/18
 */
public class TokenUtils {

    private TokenUtils() {
    }

    public static final String SUBJECT = "24400320";

    public static String createToken(String groupName) throws Exception {
        JwtClaims claims = new JwtClaims();
        claims.setIssuer("http://testsuite-jwt-issuer.io");
        claims.setSubject(SUBJECT);
        claims.setStringListClaim("groups", groupName);
        claims.setClaim("upn", "jdoe@example.com");
        claims.setExpirationTimeMinutesInTheFuture(1);

        JsonWebSignature jws = new JsonWebSignature();
        jws.setPayload(claims.toJson());
        jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.RSA_USING_SHA256);
        jws.setKey(getPrivateKey());
        return jws.getCompactSerialization();
    }

    private static PrivateKey getPrivateKey() throws Exception {
        InputStream is = TokenUtils.class.getResourceAsStream("/keys/private-key.pem");
        return KeyUtils.decodePrivateKey(new String(IOUtil.asByteArray(is)));
    }
}


text:
---
title:		'01. Example post (not "content")'
description:	"A boring description. Doesn't matter if it's to show when to justify and not."
layout: post
author:		"P.J."
tags:
- 01. Example
- Post
---

<style>

 /* This is just for the 'img' examples towards the end */

 img {
  border: solid grey 1px;
}

  h5 {
  text-align: center;
  color: #000;
  text-decoration: underline;
  margin-top: -7px;
}
</style>

**If I were asked to answer the following question: WHAT IS SLAVERY? and I should answer in one word, IT IS MURDER, my meaning would be understood at once.**{:id="theft"}

No extended argument would be required to show that the power to take from a man his thought, his will, his personality, is a power of life and death; and that to enslave a man is to kill him. Why, then, to this other question: WHAT IS PROPERTY! may I not likewise answer, IT IS THEFT, without the certainty of being misunderstood; the second proposition being no other than a transformation of the first?.

## PROPERTY IS THEFT! . . .
What a revolution in human ideas! PROPRIETOR and THIEF have been at all times expressions as contradictory as the beings whom they designate are hostile; all languages have perpetuated this opposition.

### My name, like yours, is...
Of what consequence to you, reader, is my obscure individuality? I live, like you, in a century in which reason submits only to fact and to evidence. My name, like yours, is TRUTH- SEEKER.[1] My mission is written in these words of the law: SPEAK WITHOUT HATRED AND WITHOUT FEAR; TELL THAT WHICH THOU KNOWEST!
µµThe work of our race is to build the temple of science, and this science includes man and Nature. Now, truth reveals itself to all; to-day to Newton and Pascal, tomorrow to the herdsman in the valley and the journeyman in the shop. Each one contributes his stone to the edifice; and, his task accomplished, disappears. Eternity precedes us, eternity follows us: between two infinites, of what account is one poor mortal that the century should inquire about him?

> [1] In Greek, {GREEK e ncg } examiner; a philosopher whose business is to seek the truth.

## Universal Error
Disregard then, reader, my title and my character, and attend only to my arguments. It is in accordance with universal consent that I undertake to correct universal error; from the OPINION of the human race I appeal to its FAITH.

```
user@terminal: ~$ echo "error"
error
```

### Golem
Others offer you the spectacle of genius wresting Nature's secrets from her, and unfolding before you her sublime messages; you will find here only a series of experiments upon JUSTICE and RIGHT... The operations shall be conducted under your very eyes; and you shall weigh the result.

Nevertheless, I build no system. I ask:

* An End To Privilege,
* The Abolition Of Slavery,
* Equality Of Rights,
* The Reign Of Law.

1. Justice,
2. Nothing Else;
3. That Is The Alpha And Omega Of My Argument

A defender of equality, I shall speak without bitterness and without anger; with the independence becoming a philosopher, with the courage and firmness of a free man.

kramdown
: A Markdown-superset converter

Maruku
: Another Markdown-superset converter

[What is Property?](https://en.wikisource.org/wiki/What_is_Property%3F "By P.J. Proudhon.")
: Not a Markdown-superset converter

May I, in this momentous struggle, carry into all hearts the light with which I am filled; and show, by the success of my argument, that equality failed to conquer by the sword only that it might conquer by the pen!

##### 540px #
![Large example image](https://upload.wikimedia.org/wikipedia/commons/thumb/3/37/Placeholder_4-3.svg/540px-Placeholder_4-3.svg.png "Large example image")

##### 360px #
![Medium example image](https://upload.wikimedia.org/wikipedia/commons/thumb/3/37/Placeholder_4-3.svg/360px-Placeholder_4-3.svg.png "Medium example image")

##### 180px #
![Small example image](https://upload.wikimedia.org/wikipedia/commons/thumb/3/37/Placeholder_4-3.svg/180px-Placeholder_4-3.svg.png "Small example image")

But I must say that I recognized at once that we had never understood the meaning of these words, so common and yet so sacred: JUSTICE, EQUITY, LIBERTY.

|-----------------+------------+-----------------+----------------|
| Default aligned |Left aligned| Center aligned  | Right aligned  |
|-----------------|:-----------|:---------------:|---------------:|
| First body part |Second cell | Third cell      | fourth cell    |
| Second line     |foo         | **strong**      | baz            |
| Third line      |quux        | baz             | bar            |
|-----------------+------------+-----------------+----------------|
| Second body     |            |                 |                |
| 2 line          |            |                 |                |
|=================+============+=================+================|
| Footer row      |            |                 |                |
|-----------------+------------+-----------------+----------------|

That concerning each of these principles our ideas have been utterly obscure; and, in fact, that this ignorance was the sole cause, both of the poverty that devours us, and of all the calamities that have ever afflicted the human race.


text:
{% extends 'base.html' %}

{% block content %}

Institutional

Welcome to Institutional, a restfull interface for
    RateMyProfessors.com.
    This is the main page. It's bassically a liveblog of the site as I slowly add
    to it over its developement. Eventually it'll be fully functional and allow
    for services to poll it for inforamtion concerning ratemyprofs, specifically
    for ualberta. This could of course be expanded, but for now it will be limited
    so as to keep the scope manageble.

Latest Changes

        {% for msg in git %}

{{ msg.message }}

        {% endfor %}

API Documentation

        Please see the documentation for information as to how it works.
        This documentation is auto genereated by flask-autodoc,
        and therefore is as up to date as possible, since im not acutally
        writting it.

{% endblock %}


text:
package org.apache.camel.component.mail;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.camel.Attachment;
import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.RuntimeCamelException;
import org.apache.camel.impl.DefaultMessage;
import org.apache.camel.support.ExpressionAdapter;
import org.apache.camel.util.IOHelper;

/**
 * A {@link org.apache.camel.Expression} which can be used to split a {@link MailMessage}
 * per attachment. For example if a mail message has 5 attachments, then this
 * expression will return a <tt>List<Message></tt> that contains 5 {@link Message}.
 * The message can be split 2 ways:
 *

 *

 *     As an attachment
 *
 *       The message is split into cloned messages, each has only one attachment.  The mail attachment in each message
 *       remains unprocessed.
 *
 *

 *

 *     As a byte[] or String
 *
 *       The attachments are split into new messages as the body. This allows the split messages to be easily used by
 *       other processors / routes, as many other camel components can work on the byte[] or String, e.g. it can be written to disk
 *       using camel-file.
 *
 *

 *

 *
 * In both cases the attachment name is written to a the camel header "CamelSplitAttachmentId"
 */
public class SplitAttachmentsExpression extends ExpressionAdapter {

    public static final String HEADER_NAME = "CamelSplitAttachmentId";

    private boolean extractAttachments;

    public SplitAttachmentsExpression() {
    }

    public SplitAttachmentsExpression(boolean extractAttachments) {
        this.extractAttachments = extractAttachments;
    }

    @Override
    public Object evaluate(Exchange exchange) {
        // must use getAttachments to ensure attachments is initial populated
        if (exchange.getIn().getAttachments().isEmpty()) {
            return null;
        }

        try {
            List<Message> answer = new ArrayList<>();
            Message inMessage = exchange.getIn();
            for (Map.Entry<String, Attachment> entry : inMessage.getAttachmentObjects().entrySet()) {
                Message attachmentMessage;
                if (extractAttachments) {
                    attachmentMessage = extractAttachment(inMessage, entry.getKey());
                } else {
                    attachmentMessage = splitAttachment(inMessage, entry.getKey(), entry.getValue());
                }

                if (attachmentMessage != null) {
                    answer.add(attachmentMessage);
                }
            }

            return answer;
        } catch (Exception e) {
            throw new RuntimeCamelException("Unable to split attachments from MimeMultipart message", e);
        }
    }

    private Message splitAttachment(Message inMessage, String attachmentName, Attachment attachmentHandler) {
        final Message copy = inMessage.copy();
        Map<String, Attachment> attachments = copy.getAttachmentObjects();
        attachments.clear();
        attachments.put(attachmentName, attachmentHandler);
        copy.setHeader(HEADER_NAME, attachmentName);
        return copy;
    }

    private Message extractAttachment(Message inMessage, String attachmentName) throws Exception {
        final Message outMessage = new DefaultMessage(inMessage.getExchange().getContext());
        outMessage.setHeader(HEADER_NAME, attachmentName);
        Object attachment = inMessage.getAttachment(attachmentName).getContent();
        if (attachment instanceof InputStream) {
            outMessage.setBody(readMimePart((InputStream) attachment));
            return outMessage;
        } else if (attachment instanceof String || attachment instanceof byte[]) {
            outMessage.setBody(attachment);
            return outMessage;
        } else {
            return null;
        }
    }

    private byte[] readMimePart(InputStream mimePartStream) throws Exception {
        // mimePartStream could be base64 encoded, or not, but we don't need to worry about it as
        // Camel is smart enough to wrap it in a decoder stream (eg Base64DecoderStream) when required
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        IOHelper.copyAndCloseInput(mimePartStream, bos);
        return bos.toByteArray();
    }

    public boolean isExtractAttachments() {
        return extractAttachments;
    }

    public void setExtractAttachments(boolean extractAttachments) {
        this.extractAttachments = extractAttachments;
    }
}


text:
using CollectionManager.Enums;
using StreamCompanionTypes.DataTypes;
using StreamCompanionTypes.Interfaces;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using PpCalculatorTypes;
using StreamCompanion.Common;
using StreamCompanionTypes.Enums;
using StreamCompanionTypes.Interfaces.Services;
using StreamCompanionTypes.Interfaces.Sources;

namespace BeatmapPpReplacements
{
    public class PpReplacements : IPlugin, ITokensSource
    {
        private const string PpFormat = "{0:0.00}";
        private Tokens.TokenSetter _tokenSetter;
        private ISettings _settings;
        private IPpCalculator _ppCalculator = null;
        private readonly IToken _strainsToken;
        public static ConfigEntry StrainsAmount = new ConfigEntry("StrainsAmount", (int?)100);
        private readonly Dictionary<TokenMode, Dictionary<string, PpValue>> ppTokenDefinitions;

        public string Description { get; } = "";
        public string Name { get; } = nameof(PpReplacements);
        public string Author { get; } = "Piotrekol";
        public string Url { get; } = "";
        public string UpdateUrl { get; } = "";

        private delegate double PpValue(CancellationToken cancellationToken, string mods = "");
        enum TokenMode
        {
            Osu,
            Mania
        }

        public PpReplacements(ISettings settings)
        {
            _settings = settings;
            _tokenSetter = Tokens.CreateTokenSetter(Name);
            _strainsToken = _tokenSetter("mapStrains", new Lazy<object>(() => new Dictionary<int, double>()), TokenType.Normal, ",", new Lazy<object>(() => new Dictionary<int, double>()));
            ppTokenDefinitions = new Dictionary<TokenMode, Dictionary<string, PpValue>>
            {
                {TokenMode.Osu,new Dictionary<string, PpValue>
                {
                    {"osu_SSPP",(c,mods)=>GetPp(c,_ppCalculator, 100d)},
                    {"osu_99_9PP",(c,mods)=>GetPp(c,_ppCalculator, 99.9d)},
                    {"osu_99PP",(c,mods)=>GetPp(c,_ppCalculator, 99d)},
                    {"osu_98PP",(c,mods)=>GetPp(c,_ppCalculator, 98d)},
                    {"osu_97PP",(c,mods)=>GetPp(c,_ppCalculator, 97d)},
                    {"osu_96PP",(c,mods)=>GetPp(c,_ppCalculator, 96d)},
                    {"osu_95PP",(c,mods)=>GetPp(c,_ppCalculator, 95d)},
                    {"osu_90PP",(c,mods)=>GetPp(c,_ppCalculator, 90d)},
                    {"osu_mSSPP",(c,mods)=>GetPp(c,_ppCalculator, 100d, mods)},
                    {"osu_m99_9PP",(c,mods)=>GetPp(c,_ppCalculator, 99.9d, mods)},
                    {"osu_m99PP",(c,mods)=>GetPp(c,_ppCalculator, 99d, mods)},
                    {"osu_m98PP",(c,mods)=>GetPp(c,_ppCalculator, 98d, mods)},
                    {"osu_m97PP",(c,mods)=>GetPp(c,_ppCalculator, 97d, mods)},
                    {"osu_m96PP",(c,mods)=>GetPp(c,_ppCalculator, 96d, mods)},
                    {"osu_m95PP",(c,mods)=>GetPp(c,_ppCalculator, 95d, mods)},
                    {"osu_m90PP",(c,mods)=>GetPp(c,_ppCalculator, 90d, mods)},
                }
                },
                {TokenMode.Mania,new Dictionary<string,PpValue>
                {
                    {"mania_1_000_000PP",(c,mods)=>GetPp(c,_ppCalculator, 0, "", 1_000_000)},
                    {"mania_990_000PP",(c,mods)=>GetPp(c,_ppCalculator, 0, "", 990_000)},
                    {"mania_950_000PP",(c,mods)=>GetPp(c,_ppCalculator, 0, "", 950_000)},
                    {"mania_900_000PP",(c,mods)=>GetPp(c,_ppCalculator, 0, "", 900_000)},
                    {"mania_800_000PP",(c,mods)=>GetPp(c,_ppCalculator, 0, "", 800_000)},
                    {"mania_700_000PP",(c,mods)=>GetPp(c,_ppCalculator, 0, "", 700_000)},
                    {"mania_600_000PP",(c,mods)=>GetPp(c,_ppCalculator, 0, "", 600_000)},
                    {"mania_m1_000_000PP",(c,mods)=>GetPp(c,_ppCalculator, 0, mods, 1_000_000)},
                    {"mania_m990_000PP",(c,mods)=>GetPp(c,_ppCalculator, 0, mods, 990_000)},
                    {"mania_m950_000PP",(c,mods)=>GetPp(c,_ppCalculator, 0, mods, 950_000)},
                    {"mania_m900_000PP",(c,mods)=>GetPp(c,_ppCalculator, 0, mods, 900_000)},
                    {"mania_m800_000PP",(c,mods)=>GetPp(c,_ppCalculator, 0, mods, 800_000)},
                    {"mania_m700_000PP",(c,mods)=>GetPp(c,_ppCalculator, 0, mods, 700_000)},
                    {"mania_m600_000PP",(c,mods)=>GetPp(c,_ppCalculator, 0, mods, 600_000)},
                }
                }
            };

            ResetTokens(TokenMode.Osu);
            ResetTokens(TokenMode.Mania);
        }

        private void ResetTokens(TokenMode tokenMode)
        {
            foreach (var tokenDefinition in ppTokenDefinitions[tokenMode])
            {
                _tokenSetter(tokenDefinition.Key, null, format: PpFormat, defaultValue: 0);
            }
        }

        public async Task CreateTokensAsync(IMapSearchResult map, CancellationToken cancellationToken)
        {
            if (map.SearchArgs.EventType != OsuEventType.MapChange)
                return;

            _ppCalculator = await map.GetPpCalculator(cancellationToken);

            if (_ppCalculator is null)
            {
                ResetTokens(TokenMode.Osu);
                ResetTokens(TokenMode.Mania);
                _tokenSetter("gameMode", null);
                _tokenSetter("maxCombo", -1);
                return;
            }

            _strainsToken.Value = new Lazy<object>(() =>
            {
                var ppcalculator = (IPpCalculator)_ppCalculator?.Clone();
                try
                {
                    return ppcalculator?.CalculateStrains(cancellationToken, _settings.Get<int?>(StrainsAmount));
                }
                catch (OperationCanceledException)
                {
                    return null;
                }
            });
            var playMode = (PlayMode)_ppCalculator.RulesetId;
            _tokenSetter("gameMode", playMode.ToString());

            _ppCalculator.Score = playMode == PlayMode.OsuMania
                ? 1_000_000
                : 0;
            _ppCalculator.Mods = null;
            var mods = map.Mods?.WorkingMods ?? "";
            var tokenMode = playMode == PlayMode.OsuMania
                ? TokenMode.Mania
                : TokenMode.Osu;

            foreach (var tokenDefinition in ppTokenDefinitions[tokenMode])
            {
                cancellationToken.ThrowIfCancellationRequested();
                _tokenSetter(tokenDefinition.Key, tokenDefinition.Value(cancellationToken, mods), format: PpFormat);
            }

            _tokenSetter("maxCombo", _ppCalculator.GetMaxCombo());
            ResetTokens(tokenMode == TokenMode.Osu ? TokenMode.Mania : TokenMode.Osu);
        }

        private double GetPp(CancellationToken cancellationToken, IPpCalculator ppCalculator, double acc, string mods = "", int score = 0)
        {
            _ppCalculator.Mods = mods.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            ppCalculator.Accuracy = acc;
            ppCalculator.Score = score;
            return Math.Round(ppCalculator.Calculate(cancellationToken).Total, 3);
        }
    }
}


text:
Linking ADIOS 2
###############

**********
From CMake
**********

ADIOS exports a CMake package configuration file that allows its targets to be directly imported into another CMake project via the ``find_package`` command:

.. code-block:: cmake

    cmake_minimum_required(VERSION 3.12)
    project(MySimulation C CXX)

    find_package(MPI REQUIRED)
    find_package(ADIOS2 REQUIRED)
    #...
    add_library(my_library src1.cxx src2.cxx)
    target_link_libraries(my_library PRIVATE adios2::cxx11_mpi MPI::MPI_C)

When configuring your project you can then set the ``ADIOS2_ROOT`` or ``ADIOS2_DIR`` environment variables to the install prefix of ADIOS2.

****************************
From non-CMake build systems
****************************

If you're not using CMake then you can manually get the necessary compile and link flags for your project using ``adios2-config``:

.. code-block:: bash

    $ /path/to/install-prefix/bin/adios2-config --cxxflags
    ADIOS2_DIR: /path/to/install-prefix
    -isystem /path/to/install-prefix/include -isystem /opt/ohpc/pub/mpi/openmpi3-gnu7/3.1.0/include -pthread -std=gnu++11
    $ /path/to/install-prefix/bin/adios2-config --cxxlibs
    ADIOS2_DIR: /path/to/install-prefix
    -Wl,-rpath,/path/to/install-prefix/lib:/opt/ohpc/pub/mpi/openmpi3-gnu7/3.1.0/lib /path/to/install-prefix/lib/libadios2.so.2.4.0 -pthread -Wl,-rpath -Wl,/opt/ohpc/pub/mpi/openmpi3-gnu7/3.1.0/lib -Wl,--enable-new-dtags -pthread /opt/ohpc/pub/mpi/openmpi3-gnu7/3.1.0/lib/libmpi.so -Wl,-rpath-link,/path/to/install-prefix/lib


text:
.. image:: https://travis-ci.org/Wilduck/django-localized-recurrence.svg?branch=develop
    :target: https://travis-ci.org/Wilduck/django-localized-recurrence

Django Localized Recurrence
===========================
Django Localized Recurrence allowes you to store events that recur in
users' local times, and not have to worry about things like

This django app provides a model, ``LocalizedRecurrence``, with
methods to help store recurring events, check if the events are due,
and update their next scheduled time.

Installation
------------
This packages is currently available through Pypi and github. To
install from Pypi using ``pip`` (recommended)::

    pip install django-localized-recurrence

To install the development version from github::

    pip install git+git://github.com/ambitioninc/django-localized-recurrence.git@develop

Documentation
-------------
Full documentation is available at http://django-localized-recurrence.readthedocs.org

License
-------
MIT License (see LICENSE)


text:
<?php

namespace Bazo\Rest\Middleware;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
 * @author Martin Bažík <martin@bazik.sk>
 */
interface MiddlewareInterface
{

	public function handle(Request $req, Response $res);
}


text:
  `id` int(10) unsigned NOT NULL,
  `page` int(10) unsigned NOT NULL,
  `content` text NOT NULL,

  PRIMARY KEY  (`id`,`page`)


text:
// Copyright (c) 2013 TopCoder. All rights reserved.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//
//  TakeLabelViewController.m
//  FoodIntakeTracker
//
//  Created by lofzcx 06/25/2013
//

#import "TakeLabelViewController.h"
#import "TakePhotoViewController.h"
#import "Helper.h"
#import "AppDelegate.h"
#import "FoodProductServiceImpl.h"

@implementation TakeLabelViewController

/**
 * This method will initialize the view.
 */
- (void)viewDidLoad{
    [super viewDidLoad];
    [[self.searchBar.subviews objectAtIndex:0] setHidden:YES];
    [[self.searchBar.subviews objectAtIndex:0] removeFromSuperview];
    [self.scrollView setContentSize:CGSizeMake(560, 54)];
    for (UIView *subview in self.searchBar.subviews) {
        if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
            [subview removeFromSuperview];
            break;
        }
    }
}

/**
 * release resource by setting nil value.
 */
- (void)viewDidUnload {
    [self setLblNoteBottom:nil];
    [self setImgPhoto:nil];
    [self setSearchBar:nil];
    [self setImgBG:nil];
    [super viewDidUnload];
}

/**
 * show clear label after a delay of 1.
 * @param animated If YES, the view is being added to the window using an animation.
 */
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self take:self.takeButton];
}

/**
 * show clear label and update scan line, bracket, note label at top.
 */
- (void)showClearLabel{
}

/**
 * action for cancel button in progress view.
 * @param sender the button.
 */
- (IBAction)cancelProcessing:(id)sender {
    [updateProcessTimer invalidate];
    updateProcessTimer = nil;
    self.processView.hidden = YES;
    self.lblNoteBottom.hidden = NO;

    [self.btnTake setEnabled:YES];
    [self.lblTakeButtonTitle setTextColor:[UIColor colorWithRed:0.2 green:0.43 blue:0.62 alpha:1]];

    self.lblTakeButtonTitle.text = @"Scan Label";

    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(showClearLabel) object:nil];

    [self performSelector:@selector(showClearLabel) withObject:nil afterDelay:1];

}

/**
 * This method will take picture.
 * @param sender the button.
 */
- (IBAction)take:(id)sender{
    UIButton *button = (UIButton *)sender;
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        picker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
        picker.cameraViewTransform = CGAffineTransformScale(picker.cameraViewTransform, -1, 1);
    }
    else {
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }

    self.popover = [[UIPopoverController alloc] initWithContentViewController:picker];
    self.popover.delegate = self;
    [self.popover presentPopoverFromRect:button.frame
                                  inView:button.superview
                permittedArrowDirections:UIPopoverArrowDirectionAny
                                animated:YES];
}

/**
 * This method will take another picture.
 * @param sender the button.
 */
- (IBAction)takeAnotherPhoto:(id)sender {
    self.resultView.hidden = YES;
    [self.foodAddedPopup setHidden:YES];
}

/**
 * This method will add food to consumption.
 * @param sender the button.
 */
- (IBAction)addToConsumption:(id)sender {
    [self addSelectedFoodsToConsumption];
    self.resultView.hidden = YES;
    [self.foodAddedPopup setHidden:NO];
}

#pragma mark - UISearchBarDelegate methods
/**
 * This method will filter by food product name.
 * @param searchBar the searchBar.
 */
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    AppDelegate *appDelegate = (AppDelegate*) [[UIApplication sharedApplication] delegate];
    FoodProductServiceImpl *foodProductService = appDelegate.foodProductService;
    NSError *error;
    FoodProduct* foodProduct = [foodProductService getFoodProductByName:appDelegate.loggedInUser
                                                                   name:searchBar.text
                                                                  error:&error];
    if ([Helper displayError:error]) return;
    [resultFoods addObject:foodProduct];

    self.resultView.hidden = NO;
    self.imgFood.image = [UIImage imageNamed:foodProduct.foodImage.filename];
    self.lblFoodName.text = foodProduct.name;
    // self.lblFoodCategory.text = [[foodProduct.categories allObjects] objectAtIndex:0];
    self.lblCalories.text = [NSString stringWithFormat:@"%@",foodProduct.energy];
    self.lblSodium.text = [NSString stringWithFormat:@"%@",foodProduct.sodium];
    self.lblFluid.text = [NSString stringWithFormat:@"%@",foodProduct.fluid];
    self.lblProtein.text = [NSString stringWithFormat:@"%@",foodProduct.protein];
    self.lblCarb.text = [NSString stringWithFormat:@"%@",foodProduct.carb];
    self.lblFat.text = [NSString stringWithFormat:@"%@",foodProduct.fat];
    [self.scrollView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];
    [self buildResults];

    [self.view bringSubviewToFront:self.resultView];
    [self.btnTake setEnabled:YES];
    self.lblTakeButtonTitle.text = @"Scan Another Label";
    [self.lblTakeButtonTitle setTextColor:[UIColor colorWithRed:0.2 green:0.43 blue:0.62 alpha:1]];
    [self.btnResults setEnabled:YES];
    [searchBar resignFirstResponder];
    //    [self dismissModalViewControllerAnimated:NO];
    [self dismissViewControllerAnimated:NO completion:nil];
}

/**
 * This method will be called when the picture taking is cancelled.
 * @param picker the UIImagePickerController
 */
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [self.popover dismissPopoverAnimated:YES];
    self.processView.hidden = NO;
}
@end


text: export PYTHONPATH=$PYTHONPATH:$(pwd)/src


text:
@interface ADCollectionViewCell ()

@property (nonatomic, strong) UIImageView *ADImageView;

@property (nonatomic, strong) UILabel *describeLabel;

@property (nonatomic, strong) UIView *bottomView;

@end

@implementation ADCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {

        [self ADImageView];
        [self bottomView];
        [self describeLabel];
    }
    return self;
}

- (void)setADImage:(NSString *)image ADtitle:(NSString *)title {
//    [self.ADImageView setImage:[UIImage imageNamed:image]];
    self.describeLabel.text = title;
}

- (UIImageView *)ADImageView {
    if (!_ADImageView) {
        _ADImageView = [[UIImageView alloc]init];
        [self.contentView addSubview:_ADImageView];
        [_ADImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(0);
        }];
    }
    return _ADImageView;
}

- (UIView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[UIView alloc]init];
        _bottomView.backgroundColor = kUIColor_RGB(100, 100, 100, 0.5);
        [self.contentView addSubview:_bottomView];
        [_bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.bottom.right.mas_equalTo(0);
            make.height.mas_equalTo(35);
        }];
    }
    return _bottomView;
}

- (UILabel *)describeLabel {
    if (!_describeLabel) {
        _describeLabel = [[UILabel alloc]init];
        _describeLabel.font = kFontSize(14);
        _describeLabel.textColor = [UIColor whiteColor];
        _describeLabel.text = @"是你的接口";
        [self.bottomView addSubview:_describeLabel];
        [_describeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(5);
            make.top.bottom.mas_equalTo(0);
            make.right.mas_equalTo(70);
        }];
    }
    return _describeLabel;
}

@end


text:
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    ArrowView *av = [[ArrowView alloc] initWithFrame:CGRectMake(self.view.frame.size.width * 0.5 - 50, 200, 100, 120)];
    [self.view addSubview:av];
}

@end


text:
import { IConfig } from '../types';
import { Db } from 'mongodb';
import Controller from '../controllers/controller';
import { VolumesController } from '../controllers/volumes';
import { FilesController } from '../controllers/files';
import { PostsController } from '../controllers/posts';
import { SessionsController } from '../controllers/sessions';
import { UsersController } from '../controllers/users';
import { CommentsController } from '../controllers/comments';
import { CategoriesController } from '../controllers/categories';
import { TemplatesController } from '../controllers/templates';
import { DocumentsController } from '../controllers/documents';

/**
 * Factory classs for creating & getting controllers
 */
export class ControllerFactory {
  private _config: IConfig;
  private _db: Db;
  private _controllers: { [name: string]: Controller };

  initialize(config: IConfig, database: Db) {
    this._config = config;
    this._db = database;
    this._controllers = {};
  }

  /**
   * Adds the default models to the system
   */
  async addDefaults() {
    const controllers: Controller[] = [];
    controllers.push(await this.create('sessions'));
    controllers.push(await this.create('categories'));
    controllers.push(await this.create('volumes'));
    controllers.push(await this.create('files'));
    controllers.push(await this.create('posts'));
    controllers.push(await this.create('comments'));
    controllers.push(await this.create('users'));
    controllers.push(await this.create('templates'));
    controllers.push(await this.create('documents'));

    for (const controller of controllers) await controller.initialize(this._db);
  }

  get(type: 'volumes'): VolumesController;
  get(type: 'posts'): PostsController;
  get(type: 'comments'): CommentsController;
  get(type: 'sessions'): SessionsController;
  get(type: 'users'): UsersController;
  get(type: 'files'): FilesController;
  get(type: 'categories'): CategoriesController;
  get(type: 'templates'): TemplatesController;
  get(type: 'documents'): DocumentsController;
  get(type: string): Controller;
  get(type: string): Controller {
    const toRet = this._controllers[type];
    if (!toRet) throw new Error(`Cannot find controller '${type}'`);

    return toRet;
  }

  /**
   * A factory method for creating models
   * @param type The type of model to create
   */
  private async create(type: string): Promise<Controller> {
    let newModel: Controller;

    if (this._controllers[type]) return this._controllers[type];

    switch (type) {
      case 'volumes':
        newModel = new VolumesController(this._config);
        break;
      case 'files':
        newModel = new FilesController(this._config);
        break;
      case 'categories':
        newModel = new CategoriesController(this._config);
        break;
      case 'posts':
        newModel = new PostsController(this._config);
        break;
      case 'comments':
        newModel = new CommentsController(this._config);
        break;
      case 'sessions':
        newModel = new SessionsController(this._config);
        break;
      case 'users':
        newModel = new UsersController(this._config);
        break;
      case 'templates':
        newModel = new TemplatesController(this._config);
        break;
      case 'documents':
        newModel = new DocumentsController(this._config);
        break;
      default:
        throw new Error(`Controller '${type}' cannot be created`);
    }

    this._controllers[type] = newModel;
    return newModel;
  }
}

export default new ControllerFactory();


text:
function selectNews(id, news_date, news_name) {
    if(!document.getElementById(id).getAttribute('name')) {
        var divs = document.getElementsByClassName('news-entry');

        for(var i = 0; i < divs.length; i++) {
            if(divs[i].getAttribute('name')) {
                divs[i].removeAttribute('name');
                divs[i].style.width = '99%';
                divs[i].style.backgroundColor = '#d0d0b5';

                var attr = divs[i].getAttribute('id');
                var news_id = attr.substr(4);
                var img = 'img' + news_id;
                var text = 'text' + news_id;

                var img1 = new Image();
                var img2 = new Image();

                img1.src = 'img/pencil_black.png';
                img2.src = '../img/pencil_black.png';

                img1.onload = function(){document.getElementById(img).src = img1.src;}
                img2.onload = function(){document.getElementById(img).src = img2.src;}

                document.getElementById(text).style.color = '#404040';
            }
        }

        document.getElementById(id).setAttribute('name', 'active');
        document.getElementById(id).style.width = '95%';
        document.getElementById(id).style.backgroundColor = '#0c9a8c';
        document.getElementById(text).style.color = '#fff';

        img1 = new Image();
        img2 = new Image();

        img1.src = 'img/pencil_white.png';
        img2.src = '../img/pencil_white.png';

        img1.onload = function(){document.getElementById(img).src = img1.src;}
        img2.onload = function(){document.getElementById(img).src = img2.src;}
    }
}


text:
(function () {
  // "Constants" for keys used in `localStorage`.
  var HEARTBEAT_KEY      = '_browbeat_heartbeat';
  var ELECTION_KEY       = '_browbeat_election';
  var ELECTION_START_KEY = '_browbeat_election_start';
  var CURRENT_KEY        = '_browbeat_currentMaster';
  var MSG_PREFIX         = '_browbeat_msg';
  var KEY_PREFIX         = '_browbeat_';

  //
  // ## Constructor
  //
  // Creates a new browbeat instance. Creating more then one Browbeat instance
  // per window is not recommended since storage events are not triggered on
  // the tab that initiated the change in `localStorage`. Hence the message
  // bus is broken.
  //
  // * **options**, an object of options. Available options are the properties
  //   assigned to `this` below.
  //
  var Browbeat = function Browbeat(options) {
    if (!this instanceof Browbeat) { return new Browbeat(options); }

    options = options || {};

    // How long to wait for a heartbeat before initiating a new election.
    // The actual heartbeat will be half of this value.
    this.heartbeatTTL = 2000;
    // For how long will the election be running?
    this.electionTime = 2000;
    // Set to `true` to recieve debug output.
    this.debug        = false;
    // Maximum number of messages to garbage collect on each run.
    this.gcLimit      = 100;

    for (var i in options) {
      if (typeof this[i] !== 'undefined') this[i] = options[i];
    }

    this.id              = Math.random() * 1000;
    this.store           = window.localStorage || false;
    this.isMaster        = false;
    this.sanityTimer     = null;
    this.heartbeatTimer  = null;
    this.gcTimer         = null;
    this.listeners       = {};
    this.heartbeatOffset = Math.random() * 10 + 500;

    this.init();
  };

  //
  // ## Log
  //
  // Debug function for console.logging.
  //
  Browbeat.prototype.log = function () {
    var args = Array.prototype.slice.call(arguments, 0);
    args.unshift('[Browbeat]');
    if (this.debug) console.log.apply(console, args);
  };

  //
  // ## Init
  //
  // Initializes the current Browbeat instance. Setups up event listeners and
  // checks the current state and acts accordingly.
  //
  Browbeat.prototype.init = function browbeatInit() {
    this.log('ID:', this.id);
    // No store means no support, make it the master
    if (!this.store) {
      return this.becomeMaster();
    }

    // Hook up storage event listener
    var self = this;
    function handler(event) { self.storageEvent(event); }
    if (window.addEventListener) {
      window.addEventListener('storage', handler, false);
    }
    else {
      window.attachEventListener('storage', handler);
    }

    // Check for ongoing election.
    var now = (new Date()).getTime();
    var lastHearbeat = this.store.getItem(HEARTBEAT_KEY) || 0;
    var election = this.store.getItem(ELECTION_KEY);
    var started = this.store.getItem(ELECTION_START_KEY);
    if (election && (now - started) < this.electionTime) {
      this.log('Ongoing election, casting vote');
      return this.castVote();
    }
    // Check for heartbeat, if fresh, become slave.
    else if (now - lastHearbeat < this.heartbeatTTL) {
      this.log('Found fresh heartbeat');
      return this.becomeSlave();
    }
    // Start election.
    else {
      return this.startElection();
    }
  };

  //
  // ## Handle Storage Event
  //
  // The storage event is used as a message bus between all open tabs. Thus
  // this method acts as kind of a message dispatcher.
  //
  Browbeat.prototype.storageEvent = function browbeatEventHandler(event) {
    var key = event.key;
    if (key.indexOf(KEY_PREFIX) !== 0) {
      return;
    }

    // Handle election events.
    if (key === ELECTION_KEY) {
      // No previous value means a new election was initiated, cast our vote.
      if (event.oldValue === null) {
        clearTimeout(this.heartbeatTimer);
        clearTimeout(this.sanityTimer);
        return this.castVote();
      }
    }

    if (key === CURRENT_KEY) {
      if (event.newValue === this.id.toString()) {
        return this.becomeMaster();
      }
      else {
        return this.becomeSlave();
      }
    }

    // Handle heartbeat events. Check for dead masters.
    if (!this.isMaster && key === HEARTBEAT_KEY) {
      var self = this;
      clearTimeout(this.heartbeatTimer);
      this.heartbeatTimer = setTimeout(function () {
        self.startElection();
      }, this.heartbeatTTL + this.heartbeatOffset);
      return;
    }

    if (key.indexOf(MSG_PREFIX) === 0) {
      var data = JSON.parse(event.newValue);
      switch (data.message) {
        case 'master':
          if (this.isMaster) { this.emit('message', data.data); }
          break;
        case 'slave':
          if (!this.isMaster) { this.emit('message', data.data); }
          break;
        case 'broadcast':
          this.emit('message', data.data);
          break;
        default:
          this.emit(data.message, data.data);
          break;
      }
      return;
    }
  };

  // -------------------------------------------------------------------------

  //
  // ## Become Master
  //
  // Becomes the master window. Initiate heartbeat and emit event.
  //
  Browbeat.prototype.becomeMaster = function browbeatElected() {
    this.log('Became master');
    var self = this;
    this.isMaster = true;
    this.emit('wonElection');

    if (this.store) {
      this.heartbeatTimer = setInterval(function heartbeat() {
        self.store.setItem(HEARTBEAT_KEY, (new Date()).getTime());
      }, this.heartbeatTTL / 2);

      // Garbage collect messages older then 2 seconds
      this.gcTimer = setInterval(function garbageCollect() {
        var now = (new Date()).getTime();
        var len = self.store.length;
        for (var i = len; i >= 0; i--) {
          if (i > self.gcLimit) break;
          var key = self.store.key(i);
          if (key && key.indexOf(MSG_PREFIX) === 0) {
            var parts = key.split('~');
            if (now - parseInt(parts[1], 10) > 2000) {
              self.store.removeItem(key);
              console.log('collected', key);
            }
          }
        }
      }, Math.random() * 20000);
    }
  };

  //
  // ## Resign
  //
  // Resigns presidency and let the other windows initiate a new election.
  // Assigns a new ID to avoid the same outcome after a vote.
  //
  Browbeat.prototype.resign = function browbeatResign() {
    if (!this.master) return;
    this.id = Math.random() * 1000;
    this.isMaster = false;
    this.emit('resigned');
    clearInterval(this.heartbeatTimer);
    this.becomeSlave();
  };

  //
  // ## Become Slave
  //
  // Did not win election. Monitor heartbeat and react to dead master.
  //
  Browbeat.prototype.becomeSlave = function browbeatBecomeSlave() {
    this.log('Became slave');
    this.isMaster = false;
    this.emit('lostElection');
    var self = this;
    clearTimeout(this.heartbeatTimer);
    this.heartbeatTimer = setTimeout(function () {
      self.startElection();
    }, this.heartbeatTTL + this.heartbeatOffset);
  };

  //
  // ## Cast Vote
  //
  // Register as a candidate in the election.
  //
  Browbeat.prototype.castVote = function browbeatVote() {
    clearTimeout(this.sanityTimer);
    this.log('Casting vote');
    this.emit('voting');
    var votes = this.store.getItem(ELECTION_KEY);
    votes = votes ? votes.split(',') : [];
    votes.push(this.id);
    this.store.setItem(ELECTION_KEY, votes);

    // Sometimes the initiating window will disappear before the election is
    // completed. To avoid a stalemate add a sanity check here.
    var self = this;
    this.sanity = setTimeout(function () {
      if (!self.store.getItem(CURRENT_KEY)) {
        self.startElection();
      }
    }, this.electionTime + this.heartbeatOffset);
  };

  //
  // ## Start Election
  //
  // Initiates a new election by writing to the localStorage. Since storage
  // events are not emitted to the window that initiated the event this method
  // also casts a vote.
  //
  Browbeat.prototype.startElection = function browbeatStartElection() {
    this.log('Initiating election');
    this.emit('electionInitiated');

    var self = this;
    this.store.removeItem(CURRENT_KEY);
    this.store.removeItem(HEARTBEAT_KEY);
    this.castVote();
    this.store.setItem(ELECTION_START_KEY, (new Date()).getTime());
    setTimeout(function endElection() {
      self.emit('electionConcluded');
      var candidates = self.store.getItem(ELECTION_KEY);
      candidates = candidates ? candidates.split(',') : [self.id];
      var winner = Math.max.apply(Math, candidates);
      self.store.setItem(CURRENT_KEY, winner);
      self.store.removeItem(ELECTION_KEY);
      self.store.removeItem(ELECTION_START_KEY);
      if (winner === self.id) {
        self.becomeMaster();
      }
    }, this.electionTime);
  };

  // -------------------------------------------------------------------------

  //
  // ## On Event
  //
  // Custom event emitter functionality. Attach a handler to the given event.
  //
  Browbeat.prototype.on = function browbeatEventOn(e, handler) {
    if (!this.listeners[e]) {
      this.listeners[e] = [];
    }

    this.listeners[e].push(handler);
  };

  //
  // ## Emit Event
  //
  // Emits an event to the registered listeners.
  //
  Browbeat.prototype.emit = function browbeatEventEmit(e, data) {
    if (!this.listeners[e]) return;
    data = data || {};
    data.eventName = e;
    for (var i in this.listeners[e]) {
      this.listeners[e][i](data);
    }
  };

  //
  // ## Remove Event Listener
  //
  // Removes the given event listener from the given event. The function
  // supplied here must be the exact same function supplied to `on()`.
  //
  Browbeat.prototype.off = function browbeatEventOff(e, handler) {
    if (!this.listeners[e]) {
      this.listeners[e] = [];
    }

    for (var i in this.listeners[e]) {
      if (this.listeners[e][i] === handler) {
        this.listeners[e].splice(i, 1);
        break;
      }
    }
  };

  // -------------------------------------------------------------------------

  //
  // ## Broadcast
  //
  // Broadcast a message to _all_ windows, including the sender.
  //
  Browbeat.prototype.broadcast = function browbeatBroadcast(message) {
    this.emit('message', message);
    this.sendMessage('broadcast', message);
  };

  //
  // ## Message Master
  //
  // Sends a message to the master only.
  //
  Browbeat.prototype.messageMaster = function browbeatMsgMaster(message) {
    if (this.isMaster) {
      this.emit('message', message);
    }
    else {
      this.sendMessage('master', message);
    }
  };

  //
  // ## Message Slaves
  //
  // Sends a message to the slaves only.
  //
  Browbeat.prototype.messageSlaves = function browbeatMsSlaves(message) {
    if (!this.isMaster) this.emit('message', message);
    this.sendMessage('slave', message);
  };

  //
  // ## Send Message
  //
  // Sends a message on the "bus" to other tabs. The message is written to the
  // `localStorage`. The message will be garbage collected by the master at
  // some point.
  //
  Browbeat.prototype.sendMessage = function browbeatSend(message, data) {
    var msg = {
      message: message,
      data: data,
      timestamp: (new Date()).getTime()
    };

    var key = MSG_PREFIX + '~' + msg.timestamp + '~' + Math.random();
    this.store.setItem(key, JSON.stringify(msg));
    this.emit('sentMessage', [message, data]);
  };

  // -------------------------------------------------------------------------

  //
  // ## Export Module
  //
  // Try to be a good citizen in whatever environment we find ourselves.
  //
  (function () {
    var hasDefine = typeof define === 'function' && define.amd;
    var hasExport = typeof exports !== 'undefined';

    if (hasDefine) {
      define('Browbeat', Browbeat);
    }
    else if (hasExport) {
      exports = Browbeat;
    }
    else {
      window.Browbeat = Browbeat;
    }
  }());
}());


text:
package com.alibaba.dubbo.remoting.transport.netty4;

import com.alibaba.dubbo.common.Constants;
import com.alibaba.dubbo.common.URL;
import com.alibaba.dubbo.common.logger.Logger;
import com.alibaba.dubbo.common.logger.LoggerFactory;
import com.alibaba.dubbo.common.utils.ExecutorUtil;
import com.alibaba.dubbo.common.utils.NetUtils;
import com.alibaba.dubbo.remoting.Channel;
import com.alibaba.dubbo.remoting.ChannelHandler;
import com.alibaba.dubbo.remoting.RemotingException;
import com.alibaba.dubbo.remoting.Server;
import com.alibaba.dubbo.remoting.transport.AbstractServer;
import com.alibaba.dubbo.remoting.transport.dispatcher.ChannelHandlers;
import com.alibaba.dubbo.remoting.transport.netty4.logging.NettyHelper;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.util.concurrent.DefaultThreadFactory;

import java.net.InetSocketAddress;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;

/**
 * NettyServer
 */
public class NettyServer extends AbstractServer implements Server {

    private static final Logger logger = LoggerFactory.getLogger(NettyServer.class);

    private Map<String, Channel> channels; // <ip:port, channel>

    private ServerBootstrap bootstrap;

    private io.netty.channel.Channel channel;

    private EventLoopGroup bossGroup;
    private EventLoopGroup workerGroup;

    public NettyServer(URL url, ChannelHandler handler) throws RemotingException {
        super(url, ChannelHandlers.wrap(handler, ExecutorUtil.setThreadName(url, SERVER_THREAD_POOL_NAME)));
    }

    @Override
    protected void doOpen() throws Throwable {
        NettyHelper.setNettyLoggerFactory();

        bootstrap = new ServerBootstrap();

        bossGroup = new NioEventLoopGroup(1, new DefaultThreadFactory("NettyServerBoss", true));
        workerGroup = new NioEventLoopGroup(getUrl().getPositiveParameter(Constants.IO_THREADS_KEY, Constants.DEFAULT_IO_THREADS),
                new DefaultThreadFactory("NettyServerWorker", true));

        final NettyServerHandler nettyServerHandler = new NettyServerHandler(getUrl(), this);
        channels = nettyServerHandler.getChannels();

        bootstrap.group(bossGroup, workerGroup)
                .channel(NioServerSocketChannel.class)
                .childOption(ChannelOption.TCP_NODELAY, Boolean.TRUE)
                .childOption(ChannelOption.SO_REUSEADDR, Boolean.TRUE)
                .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        NettyCodecAdapter adapter = new NettyCodecAdapter(getCodec(), getUrl(), NettyServer.this);
                        ch.pipeline()//.addLast("logging",new LoggingHandler(LogLevel.INFO))//for debug
                                .addLast("decoder", adapter.getDecoder())
                                .addLast("encoder", adapter.getEncoder())
                                .addLast("handler", nettyServerHandler);
                    }
                });
        // bind
        ChannelFuture channelFuture = bootstrap.bind(getBindAddress());
        channelFuture.syncUninterruptibly();
        channel = channelFuture.channel();

    }

    @Override
    protected void doClose() throws Throwable {
        try {
            if (channel != null) {
                // unbind.
                channel.close();
            }
        } catch (Throwable e) {
            logger.warn(e.getMessage(), e);
        }
        try {
            Collection<com.alibaba.dubbo.remoting.Channel> channels = getChannels();
            if (channels != null && channels.size() > 0) {
                for (com.alibaba.dubbo.remoting.Channel channel : channels) {
                    try {
                        channel.close();
                    } catch (Throwable e) {
                        logger.warn(e.getMessage(), e);
                    }
                }
            }
        } catch (Throwable e) {
            logger.warn(e.getMessage(), e);
        }
        try {
            if (bootstrap != null) {
                bossGroup.shutdownGracefully();
                workerGroup.shutdownGracefully();
            }
        } catch (Throwable e) {
            logger.warn(e.getMessage(), e);
        }
        try {
            if (channels != null) {
                channels.clear();
            }
        } catch (Throwable e) {
            logger.warn(e.getMessage(), e);
        }
    }

    public Collection<Channel> getChannels() {
        Collection<Channel> chs = new HashSet<Channel>();
        for (Channel channel : this.channels.values()) {
            if (channel.isConnected()) {
                chs.add(channel);
            } else {
                channels.remove(NetUtils.toAddressString(channel.getRemoteAddress()));
            }
        }
        return chs;
    }

    public Channel getChannel(InetSocketAddress remoteAddress) {
        return channels.get(NetUtils.toAddressString(remoteAddress));
    }

    public boolean isBound() {
        return channel.isActive();
    }

}


text:
package org.jetbrains.plugins.scala.testingSupport.test.ui

import com.intellij.execution.ExecutionBundle
import com.intellij.execution.ui.CommonProgramParametersPanel
import com.intellij.ide.`macro`.MacrosDialog
import com.intellij.openapi.ui.LabeledComponent
import com.intellij.ui.RawCommandLineEditor
import com.intellij.ui.components.fields.ExtendableTextField
import com.intellij.util.ui.UIUtil
import org.jetbrains.plugins.scala.testingSupport.test.testdata.TestConfigurationData

import java.awt.BorderLayout
import java.util

/** based on [[com.intellij.execution.ui.CommonJavaParametersPanel]] */
class CommonScalaParametersPanel extends CommonProgramParametersPanel {

  private var myVMParametersComponent: LabeledComponent[RawCommandLineEditor] = _

  override protected def addComponents(): Unit = {
    myVMParametersComponent = LabeledComponent.create(new RawCommandLineEditor, ExecutionBundle.message("run.configuration.java.vm.parameters.label"))
    copyDialogCaption(myVMParametersComponent)
    myVMParametersComponent.setLabelLocation(BorderLayout.WEST)
    add(myVMParametersComponent)

    super.addComponents()
  }

  override protected def setupAnchor(): Unit = {
    super.setupAnchor()
    myAnchor = UIUtil.mergeComponentsWithAnchor(this, myVMParametersComponent)
  }

  def reset(configurationData: TestConfigurationData): Unit = {
    super.reset(configurationData)
    myVMParametersComponent.getComponent.setText(configurationData.javaOptions)
  }

  /** @see [[org.jetbrains.plugins.scala.testingSupport.test.ScalaTestFrameworkCommandLineState.VariablesExpander]] */
  override protected def isMacroSupportEnabled = true

  /** @see [[org.jetbrains.plugins.scala.testingSupport.test.ScalaTestFrameworkCommandLineState.VariablesExpander]] */
  override protected def initMacroSupport(): Unit = {
    updatePathMacros()
    addMacroSupport(myProgramParametersComponent.getComponent.getEditorField, MacrosDialog.Filters.ALL)
    addMacroSupport(myWorkingDirectoryField.getTextField.asInstanceOf[ExtendableTextField], MacrosDialog.Filters.DIRECTORY_PATH)
    addMacroSupport(myVMParametersComponent.getComponent.getEditorField, MacrosDialog.Filters.ALL)
  }

  def getVMParameters: String = myVMParametersComponent.getComponent.getText

  def getEnvironmentVariables: util.Map[String, String] = myEnvVariablesComponent.getEnvs

  def isPassParentEnvs: Boolean = myEnvVariablesComponent.isPassParentEnvs

  def getProgramParameters: String = getProgramParametersComponent.getComponent.getText
}


text:
describe('Example Test', function() {
  beforeEach(function() {
    browser.ignoreSynchronization = true;
    browser.get('');
  });
  it('has the right title', function() {
    expect(browser.getTitle()).toEqual('Example Test');
  });
  describe('publisher', function() {
    it('loads the video', function() {
      browser.wait(function() {
        return element(by.css('.OT_publisher:not(.OT_loading)')).isPresent();
      }, 60000);
    });
  });
});


text:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Internal;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace Surpass.Web
{
    public class Program
    {
        ///
        /// Get website root directory

        /// 获取网站的根目录

        ///
        /// <returns></returns>
        private static string GetWebsiteRootDirectory()
        {
            var path = Path.GetDirectoryName(typeof(Program).Assembly.Location);
            while (!Directory.Exists(Path.Combine(path, "App_Data")))
            {
                path = Path.GetDirectoryName(path);
                if (string.IsNullOrEmpty(path))
                {
                    throw new DirectoryNotFoundException("Website root directory not found");
                }
            }
            return path;
        }

        public static void Main(string[] args)
        {
            BuildWebHost(args).Run();
        }

        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                //.UseConfiguration(new ConfigurationBuilder().SetBasePath(GetWebsiteRootDirectory())
                //    .AddJsonFile("appsettings.json", true)
                //    .Build()).ConfigureServices(
                //    x =>
                //    {
                //        Application.Initialize(GetWebsiteRootDirectory());
                //    }).Configure(app =>
                //    {
                //        var env = new HostingEnvironment();
                //        if (env.IsDevelopment())
                //        {
                //            app.UseDeveloperExceptionPage();
                //            app.UseBrowserLink();
                //        }
                //        else
                //        {
                //            app.UseExceptionHandler("/Home/Error");
                //        }

                //        app.UseStaticFiles();
                //        app.UseAuthentication();

                //        app.UseMvc(routes =>
                //        {
                //            routes.MapRoute(
                //                name: "default",
                //                template: "{controller=Home}/{action=Index}/{id?}");
                //        });
                //    })
                .UseUrls("http://*:80")
                    .UseStartup<Startup>()
                    .Build();
    }
}