text:
package org.apache.hadoop.hbase.client;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import org.apache.hadoop.hbase.ClusterMetrics.Option;
import org.apache.hadoop.hbase.HBaseClassTestRule;
import org.apache.hadoop.hbase.HConstants;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MiniHBaseCluster;
import org.apache.hadoop.hbase.ServerName;
import org.apache.hadoop.hbase.TableExistsException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.TableNotDisabledException;
import org.apache.hadoop.hbase.TableNotEnabledException;
import org.apache.hadoop.hbase.TableNotFoundException;
import org.apache.hadoop.hbase.UnknownRegionException;
import org.apache.hadoop.hbase.Waiter.Predicate;
import org.apache.hadoop.hbase.constraint.ConstraintException;
import org.apache.hadoop.hbase.master.HMaster;
import org.apache.hadoop.hbase.master.assignment.AssignmentManager;
import org.apache.hadoop.hbase.regionserver.HRegion;
import org.apache.hadoop.hbase.regionserver.HRegionServer;
import org.apache.hadoop.hbase.regionserver.HStore;
import org.apache.hadoop.hbase.testclassification.ClientTests;
import org.apache.hadoop.hbase.testclassification.LargeTests;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.util.FutureUtils;
import org.apache.hadoop.hbase.wal.AbstractFSWALProvider;
import org.junit.Assert;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
import org.apache.hadoop.hbase.shaded.protobuf.RequestConverter;

/**
 * Class to test HBaseAdmin.
 * Spins up the minicluster once at test start and then takes it down afterward.
 * Add any testing of HBaseAdmin functionality here.
 */
@Category({LargeTests.class, ClientTests.class})
public class TestAdmin2 extends TestAdminBase {

  @ClassRule
  public static final HBaseClassTestRule CLASS_RULE =
      HBaseClassTestRule.forClass(TestAdmin2.class);

  private static final Logger LOG = LoggerFactory.getLogger(TestAdmin2.class);

  @Test
  public void testCreateBadTables() throws IOException {
    String msg = null;
    try {
      ADMIN.createTable(new HTableDescriptor(TableName.META_TABLE_NAME));
    } catch(TableExistsException e) {
      msg = e.toString();
    }
    assertTrue("Unexcepted exception message " + msg, msg != null &&
      msg.startsWith(TableExistsException.class.getName()) &&
      msg.contains(TableName.META_TABLE_NAME.getNameAsString()));

    // Now try and do concurrent creation with a bunch of threads.
    TableDescriptorBuilder.ModifyableTableDescriptor tableDescriptor =
      new TableDescriptorBuilder.ModifyableTableDescriptor(
        TableName.valueOf(name.getMethodName()));
    ColumnFamilyDescriptor familyDescriptor =
      new ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor(
        HConstants.CATALOG_FAMILY);
    tableDescriptor.setColumnFamily(familyDescriptor);
    int count = 10;
    Thread [] threads = new Thread [count];
    final AtomicInteger successes = new AtomicInteger(0);
    final AtomicInteger failures = new AtomicInteger(0);
    final Admin localAdmin = ADMIN;
    for (int i = 0; i < count; i++) {
      threads[i] = new Thread(Integer.toString(i)) {
        @Override
        public void run() {
          try {
            localAdmin.createTable(tableDescriptor);
            successes.incrementAndGet();
          } catch (TableExistsException e) {
            failures.incrementAndGet();
          } catch (IOException e) {
            throw new RuntimeException("Failed threaded create" + getName(), e);
          }
        }
      };
    }
    for (int i = 0; i < count; i++) {
      threads[i].start();
    }
    for (int i = 0; i < count; i++) {
      while(threads[i].isAlive()) {
        try {
          Thread.sleep(100);
        } catch (InterruptedException e) {
          // continue
        }
      }
    }
    // All threads are now dead.  Count up how many tables were created and
    // how many failed w/ appropriate exception.
    assertEquals(1, successes.get());
    assertEquals(count - 1, failures.get());
  }

  /**
   * Test for hadoop-1581 'HBASE: Unopenable tablename bug'.
   * @throws Exception
   */
  @Test
  public void testTableNameClash() throws Exception {
    final String name = this.name.getMethodName();
    TableDescriptorBuilder.ModifyableTableDescriptor tableDescriptor1 =
      new TableDescriptorBuilder.ModifyableTableDescriptor(
        TableName.valueOf(name + "SOMEUPPERCASE"));
    TableDescriptorBuilder.ModifyableTableDescriptor tableDescriptor2 =
      new TableDescriptorBuilder.ModifyableTableDescriptor(TableName.valueOf(name));
    tableDescriptor1.setColumnFamily(
      new ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor(
        HConstants.CATALOG_FAMILY));
    tableDescriptor2.setColumnFamily(
      new ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor(
        HConstants.CATALOG_FAMILY));
    ADMIN.createTable(tableDescriptor1);
    ADMIN.createTable(tableDescriptor2);
    // Before fix, below would fail throwing a NoServerForRegionException.
    TEST_UTIL.getConnection().getTable(tableDescriptor2.getTableName()).close();
  }

  /***
   * HMaster.createTable used to be kind of synchronous call
   * Thus creating of table with lots of regions can cause RPC timeout
   * After the fix to make createTable truly async, RPC timeout shouldn't be an
   * issue anymore
   */
  @Test
  public void testCreateTableRPCTimeOut() throws Exception {
    final String name = this.name.getMethodName();
    int oldTimeout = TEST_UTIL.getConfiguration().
      getInt(HConstants.HBASE_RPC_TIMEOUT_KEY, HConstants.DEFAULT_HBASE_RPC_TIMEOUT);
    TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_RPC_TIMEOUT_KEY, 1500);
    try {
      int expectedRegions = 100;
      // Use 80 bit numbers to make sure we aren't limited
      byte [] startKey = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
      byte [] endKey =   { 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 };
      Admin hbaseadmin = TEST_UTIL.getAdmin();
      TableDescriptorBuilder.ModifyableTableDescriptor tableDescriptor =
        new TableDescriptorBuilder.ModifyableTableDescriptor(TableName.valueOf(name));
      tableDescriptor.setColumnFamily(
        new ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor(
          HConstants.CATALOG_FAMILY));
      hbaseadmin.createTable(tableDescriptor, startKey, endKey, expectedRegions);
    } finally {
      TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_RPC_TIMEOUT_KEY, oldTimeout);
    }
  }

  /**
   * Test read only tables
   * @throws Exception
   */
  @Test
  public void testReadOnlyTable() throws Exception {
    final TableName name = TableName.valueOf(this.name.getMethodName());
    Table table = TEST_UTIL.createTable(name, HConstants.CATALOG_FAMILY);
    byte[] value = Bytes.toBytes("somedata");
    // This used to use an empty row... That must have been a bug
    Put put = new Put(value);
    put.addColumn(HConstants.CATALOG_FAMILY, HConstants.CATALOG_FAMILY, value);
    table.put(put);
    table.close();
  }

  /**
   * Test that user table names can contain '-' and '.' so long as they do not
   * start with same. HBASE-771
   */
  @Test
  public void testTableNames() throws IOException {
    byte[][] illegalNames = new byte[][] {
        Bytes.toBytes("-bad"),
        Bytes.toBytes(".bad")
    };
    for (byte[] illegalName : illegalNames) {
      try {
        new HTableDescriptor(TableName.valueOf(illegalName));
        throw new IOException("Did not detect '" +
            Bytes.toString(illegalName) + "' as an illegal user table name");
      } catch (IllegalArgumentException e) {
        // expected
      }
    }
    byte[] legalName = Bytes.toBytes("g-oo.d");
    try {
      new HTableDescriptor(TableName.valueOf(legalName));
    } catch (IllegalArgumentException e) {
      throw new IOException("Legal user table name: '" +
        Bytes.toString(legalName) + "' caused IllegalArgumentException: " +
        e.getMessage());
    }
  }

  /**
   * For HADOOP-2579
   */
  @Test (expected=TableExistsException.class)
  public void testTableExistsExceptionWithATable() throws IOException {
    final TableName name = TableName.valueOf(this.name.getMethodName());
    TEST_UTIL.createTable(name, HConstants.CATALOG_FAMILY).close();
    TEST_UTIL.createTable(name, HConstants.CATALOG_FAMILY);
  }

  /**
   * Can't disable a table if the table isn't in enabled state
   */
  @Test (expected=TableNotEnabledException.class)
  public void testTableNotEnabledExceptionWithATable() throws IOException {
    final TableName name = TableName.valueOf(this.name.getMethodName());
    TEST_UTIL.createTable(name, HConstants.CATALOG_FAMILY).close();
    ADMIN.disableTable(name);
    ADMIN.disableTable(name);
  }

  /**
   * Can't enable a table if the table isn't in disabled state
   */
  @Test(expected = TableNotDisabledException.class)
  public void testTableNotDisabledExceptionWithATable() throws IOException {
    final TableName name = TableName.valueOf(this.name.getMethodName());
    try (Table t = TEST_UTIL.createTable(name, HConstants.CATALOG_FAMILY)) {
      ADMIN.enableTable(name);
    }
  }

  /**
   * For HADOOP-2579
   */
  @Test(expected = TableNotFoundException.class)
  public void testTableNotFoundExceptionWithoutAnyTables() throws IOException {
    TableName tableName = TableName.valueOf("testTableNotFoundExceptionWithoutAnyTables");
    try (Table ht = TEST_UTIL.getConnection().getTable(tableName)) {
      ht.get(new Get(Bytes.toBytes("e")));
    }
  }

  @Test
  public void testShouldUnassignTheRegion() throws Exception {
    final TableName tableName = TableName.valueOf(name.getMethodName());
    createTableWithDefaultConf(tableName);

    RegionInfo info = null;
    HRegionServer rs = TEST_UTIL.getRSForFirstRegionInTable(tableName);
    List<RegionInfo> onlineRegions = ProtobufUtil.getOnlineRegions(rs.getRSRpcServices());
    for (RegionInfo regionInfo : onlineRegions) {
      if (!regionInfo.getTable().isSystemTable()) {
        info = regionInfo;
        ADMIN.unassign(regionInfo.getRegionName(), true);
      }
    }
    boolean isInList = ProtobufUtil.getOnlineRegions(
      rs.getRSRpcServices()).contains(info);
    long timeout = System.currentTimeMillis() + 10000;
    while ((System.currentTimeMillis() < timeout) && (isInList)) {
      Thread.sleep(100);
      isInList = ProtobufUtil.getOnlineRegions(
        rs.getRSRpcServices()).contains(info);
    }

    assertFalse("The region should not be present in online regions list.",
      isInList);
  }

  @Test
  public void testCloseRegionIfInvalidRegionNameIsPassed() throws Exception {
    final String name = this.name.getMethodName();
    byte[] tableName = Bytes.toBytes(name);
    createTableWithDefaultConf(tableName);

    RegionInfo info = null;
    HRegionServer rs = TEST_UTIL.getRSForFirstRegionInTable(TableName.valueOf(tableName));
    List<RegionInfo> onlineRegions = ProtobufUtil.getOnlineRegions(rs.getRSRpcServices());
    for (RegionInfo regionInfo : onlineRegions) {
      if (!regionInfo.isMetaRegion()) {
        if (regionInfo.getRegionNameAsString().contains(name)) {
          info = regionInfo;
          try {
            ADMIN.unassign(Bytes.toBytes("sample"), true);
          } catch (UnknownRegionException nsre) {
            // expected, ignore it
          }
        }
      }
    }
    onlineRegions = ProtobufUtil.getOnlineRegions(rs.getRSRpcServices());
    assertTrue("The region should be present in online regions list.",
        onlineRegions.contains(info));
  }

  @Test
  public void testCloseRegionThatFetchesTheHRIFromMeta() throws Exception {
    final TableName tableName = TableName.valueOf(name.getMethodName());
    createTableWithDefaultConf(tableName);

    RegionInfo info = null;
    HRegionServer rs = TEST_UTIL.getRSForFirstRegionInTable(tableName);
    List<RegionInfo> onlineRegions = ProtobufUtil.getOnlineRegions(rs.getRSRpcServices());
    for (RegionInfo regionInfo : onlineRegions) {
      if (!regionInfo.isMetaRegion()) {
        if (regionInfo.getRegionNameAsString().contains("TestHBACloseRegion2")) {
          info = regionInfo;
          ADMIN.unassign(regionInfo.getRegionName(), true);
        }
      }
    }

    boolean isInList = ProtobufUtil.getOnlineRegions(
      rs.getRSRpcServices()).contains(info);
    long timeout = System.currentTimeMillis() + 10000;
    while ((System.currentTimeMillis() < timeout) && (isInList)) {
      Thread.sleep(100);
      isInList = ProtobufUtil.getOnlineRegions(
        rs.getRSRpcServices()).contains(info);
    }

    assertFalse("The region should not be present in online regions list.",
      isInList);
  }

  private Admin createTable(TableName tableName) throws IOException {
    Admin admin = TEST_UTIL.getAdmin();

    TableDescriptorBuilder tableDescriptorBuilder =
      TableDescriptorBuilder.newBuilder(tableName);
    ColumnFamilyDescriptor columnFamilyDescriptor =
      ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("value")).build();

    tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor);
    admin.createTable(tableDescriptorBuilder.build());
    return admin;
  }

  private void createTableWithDefaultConf(byte[] TABLENAME) throws IOException {
    createTableWithDefaultConf(TableName.valueOf(TABLENAME));
  }

  private void createTableWithDefaultConf(TableName TABLENAME) throws IOException {
    TableDescriptorBuilder tableDescriptorBuilder =
      TableDescriptorBuilder.newBuilder(TABLENAME);
    ColumnFamilyDescriptor columnFamilyDescriptor =
      ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("value")).build();
    tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor);

    ADMIN.createTable(tableDescriptorBuilder.build());
  }

  /**
   * For HBASE-2556
   */
  @Test
  public void testGetTableRegions() throws IOException {
    final TableName tableName = TableName.valueOf(name.getMethodName());

    int expectedRegions = 10;

    // Use 80 bit numbers to make sure we aren't limited
    byte [] startKey = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
    byte [] endKey =   { 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 };

    TableDescriptorBuilder.ModifyableTableDescriptor tableDescriptor =
      new TableDescriptorBuilder.ModifyableTableDescriptor(tableName);
    tableDescriptor.setColumnFamily(
      new ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor(
        HConstants.CATALOG_FAMILY));
    ADMIN.createTable(tableDescriptor, startKey, endKey, expectedRegions);

    List<RegionInfo> RegionInfos = ADMIN.getRegions(tableName);

    assertEquals("Tried to create " + expectedRegions + " regions " +
        "but only found " + RegionInfos.size(),
        expectedRegions, RegionInfos.size());
 }

  @Test
  public void testMoveToPreviouslyAssignedRS() throws IOException, InterruptedException {
    MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
    HMaster master = cluster.getMaster();
    final TableName tableName = TableName.valueOf(name.getMethodName());
    Admin localAdmin = createTable(tableName);
    List<RegionInfo> tableRegions = localAdmin.getRegions(tableName);
    RegionInfo hri = tableRegions.get(0);
    AssignmentManager am = master.getAssignmentManager();
    ServerName server = am.getRegionStates().getRegionServerOfRegion(hri);
    localAdmin.move(hri.getEncodedNameAsBytes(), server);
    assertEquals("Current region server and region server before move should be same.", server,
      am.getRegionStates().getRegionServerOfRegion(hri));
  }

  @Test
  public void testWALRollWriting() throws Exception {
    setUpforLogRolling();
    String className = this.getClass().getName();
    StringBuilder v = new StringBuilder(className);
    while (v.length() < 1000) {
      v.append(className);
    }
    byte[] value = Bytes.toBytes(v.toString());
    HRegionServer regionServer = startAndWriteData(TableName.valueOf(name.getMethodName()), value);
    LOG.info("after writing there are "
        + AbstractFSWALProvider.getNumRolledLogFiles(regionServer.getWAL(null)) + " log files");

    // flush all regions
    for (HRegion r : regionServer.getOnlineRegionsLocalContext()) {
      r.flush(true);
    }
    ADMIN.rollWALWriter(regionServer.getServerName());
    int count = AbstractFSWALProvider.getNumRolledLogFiles(regionServer.getWAL(null));
    LOG.info("after flushing all regions and rolling logs there are " +
        count + " log files");
    assertTrue(("actual count: " + count), count <= 2);
  }

  private void setUpforLogRolling() {
    // Force a region split after every 768KB
    TEST_UTIL.getConfiguration().setLong(HConstants.HREGION_MAX_FILESIZE,
        768L * 1024L);

    // We roll the log after every 32 writes
    TEST_UTIL.getConfiguration().setInt("hbase.regionserver.maxlogentries", 32);

    TEST_UTIL.getConfiguration().setInt(
        "hbase.regionserver.logroll.errors.tolerated", 2);
    TEST_UTIL.getConfiguration().setInt("hbase.rpc.timeout", 10 * 1000);

    // For less frequently updated regions flush after every 2 flushes
    TEST_UTIL.getConfiguration().setInt(
        "hbase.hregion.memstore.optionalflushcount", 2);

    // We flush the cache after every 8192 bytes
    TEST_UTIL.getConfiguration().setInt(HConstants.HREGION_MEMSTORE_FLUSH_SIZE,
        8192);

    // Increase the amount of time between client retries
    TEST_UTIL.getConfiguration().setLong("hbase.client.pause", 10 * 1000);

    // Reduce thread wake frequency so that other threads can get
    // a chance to run.
    TEST_UTIL.getConfiguration().setInt(HConstants.THREAD_WAKE_FREQUENCY,
        2 * 1000);

    /**** configuration for testLogRollOnDatanodeDeath ****/
    // lower the namenode & datanode heartbeat so the namenode
    // quickly detects datanode failures
    TEST_UTIL.getConfiguration().setInt("dfs.namenode.heartbeat.recheck-interval", 5000);
    TEST_UTIL.getConfiguration().setInt("dfs.heartbeat.interval", 1);
    // the namenode might still try to choose the recently-dead datanode
    // for a pipeline, so try to a new pipeline multiple times
    TEST_UTIL.getConfiguration().setInt("dfs.client.block.write.retries", 30);
    TEST_UTIL.getConfiguration().setInt(
        "hbase.regionserver.hlog.tolerable.lowreplication", 2);
    TEST_UTIL.getConfiguration().setInt(
        "hbase.regionserver.hlog.lowreplication.rolllimit", 3);
  }

  private HRegionServer startAndWriteData(TableName tableName, byte[] value)
  throws IOException, InterruptedException {
    // When the hbase:meta table can be opened, the region servers are running
    TEST_UTIL.getConnection().getTable(TableName.META_TABLE_NAME).close();

    // Create the test table and open it
    TableDescriptorBuilder.ModifyableTableDescriptor tableDescriptor =
      new TableDescriptorBuilder.ModifyableTableDescriptor(tableName);
    tableDescriptor.setColumnFamily(
      new ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor(
        HConstants.CATALOG_FAMILY));
    ADMIN.createTable(tableDescriptor);
    Table table = TEST_UTIL.getConnection().getTable(tableName);

    HRegionServer regionServer = TEST_UTIL.getRSForFirstRegionInTable(tableName);
    for (int i = 1; i <= 256; i++) { // 256 writes should cause 8 log rolls
      Put put = new Put(Bytes.toBytes("row" + String.format("%1$04d", i)));
      put.addColumn(HConstants.CATALOG_FAMILY, null, value);
      table.put(put);
      if (i % 32 == 0) {
        // After every 32 writes sleep to let the log roller run
        try {
          Thread.sleep(2000);
        } catch (InterruptedException e) {
          // continue
        }
      }
    }

    table.close();
    return regionServer;
  }

  @Test
  public void testDisableCatalogTable() throws Exception {
    try {
      ADMIN.disableTable(TableName.META_TABLE_NAME);
      fail("Expected to throw ConstraintException");
    } catch (ConstraintException e) {
    }
    // Before the fix for HBASE-6146, the below table creation was failing as the hbase:meta table
    // actually getting disabled by the disableTable() call.
    TableDescriptorBuilder.ModifyableTableDescriptor tableDescriptor =
      new TableDescriptorBuilder.ModifyableTableDescriptor(
        TableName.valueOf(Bytes.toBytes(name.getMethodName())));
    ColumnFamilyDescriptor familyDescriptor =
      new ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor(
        Bytes.toBytes("cf1"));

    tableDescriptor.setColumnFamily(familyDescriptor);
    TEST_UTIL.getAdmin().createTable(tableDescriptor);
  }

  @Test
  public void testIsEnabledOrDisabledOnUnknownTable() throws Exception {
    try {
      ADMIN.isTableEnabled(TableName.valueOf(name.getMethodName()));
      fail("Test should fail if isTableEnabled called on unknown table.");
    } catch (IOException e) {
    }

    try {
      ADMIN.isTableDisabled(TableName.valueOf(name.getMethodName()));
      fail("Test should fail if isTableDisabled called on unknown table.");
    } catch (IOException e) {
    }
  }

  @Test
  public void testBalancer() throws Exception {
    boolean initialState = ADMIN.isBalancerEnabled();

    // Start the balancer, wait for it.
    boolean prevState = ADMIN.balancerSwitch(!initialState, true);

    // The previous state should be the original state we observed
    assertEquals(initialState, prevState);

    // Current state should be opposite of the original
    assertEquals(!initialState, ADMIN.isBalancerEnabled());

    // Reset it back to what it was
    prevState = ADMIN.balancerSwitch(initialState, true);

    // The previous state should be the opposite of the initial state
    assertEquals(!initialState, prevState);
    // Current state should be the original state again
    assertEquals(initialState, ADMIN.isBalancerEnabled());
  }

  @Test
  public void testRegionNormalizer() throws Exception {
    boolean initialState = ADMIN.isNormalizerEnabled();

    // flip state
    boolean prevState = ADMIN.normalizerSwitch(!initialState);

    // The previous state should be the original state we observed
    assertEquals(initialState, prevState);

    // Current state should be opposite of the original
    assertEquals(!initialState, ADMIN.isNormalizerEnabled());

    // Reset it back to what it was
    prevState = ADMIN.normalizerSwitch(initialState);

    // The previous state should be the opposite of the initial state
    assertEquals(!initialState, prevState);
    // Current state should be the original state again
    assertEquals(initialState, ADMIN.isNormalizerEnabled());
  }

  @Test
  public void testAbortProcedureFail() throws Exception {
    Random randomGenerator = new Random();
    long procId = randomGenerator.nextLong();

    boolean abortResult = ADMIN.abortProcedure(procId, true);
    assertFalse(abortResult);
  }

  @Test
  public void testGetProcedures() throws Exception {
    String procList = ADMIN.getProcedures();
    assertTrue(procList.startsWith("["));
  }

  @Test
  public void testGetLocks() throws Exception {
    String lockList = ADMIN.getLocks();
    assertTrue(lockList.startsWith("["));
  }

  @Test
  public void testDecommissionRegionServers() throws Exception {
    List<ServerName> decommissionedRegionServers = ADMIN.listDecommissionedRegionServers();
    assertTrue(decommissionedRegionServers.isEmpty());

    final TableName tableName = TableName.valueOf(name.getMethodName());
    TEST_UTIL.createMultiRegionTable(tableName, Bytes.toBytes("f"), 6);

    ArrayList<ServerName> clusterRegionServers =
        new ArrayList<>(ADMIN.getClusterMetrics(EnumSet.of(Option.LIVE_SERVERS))
          .getLiveServerMetrics().keySet());

    assertEquals(3, clusterRegionServers.size());

    HashMap<ServerName, List<RegionInfo>> serversToDecommssion = new HashMap<>();
    // Get a server that has meta online. We will decommission two of the servers,
    // leaving one online.
    int i;
    for (i = 0; i < clusterRegionServers.size(); i++) {
      List<RegionInfo> regionsOnServer = ADMIN.getRegions(clusterRegionServers.get(i));
      if (ADMIN.getRegions(clusterRegionServers.get(i)).stream().anyMatch(p -> p.isMetaRegion())) {
        serversToDecommssion.put(clusterRegionServers.get(i), regionsOnServer);
        break;
      }
    }

    clusterRegionServers.remove(i);
    // Get another server to decommission.
    serversToDecommssion.put(clusterRegionServers.get(0),
      ADMIN.getRegions(clusterRegionServers.get(0)));

    ServerName remainingServer = clusterRegionServers.get(1);

    // Decommission
    ADMIN.decommissionRegionServers(new ArrayList<ServerName>(serversToDecommssion.keySet()), true);
    assertEquals(2, ADMIN.listDecommissionedRegionServers().size());

    // Verify the regions have been off the decommissioned servers, all on the one
    // remaining server.
    for (ServerName server : serversToDecommssion.keySet()) {
      for (RegionInfo region : serversToDecommssion.get(server)) {
        TEST_UTIL.assertRegionOnServer(region, remainingServer, 10000);
      }
    }

    // Recommission and load the regions.
    for (ServerName server : serversToDecommssion.keySet()) {
      List<byte[]> encodedRegionNames = serversToDecommssion.get(server).stream()
          .map(region -> region.getEncodedNameAsBytes()).collect(Collectors.toList());
      ADMIN.recommissionRegionServer(server, encodedRegionNames);
    }
    assertTrue(ADMIN.listDecommissionedRegionServers().isEmpty());
    // Verify the regions have been moved to the recommissioned servers
    for (ServerName server : serversToDecommssion.keySet()) {
      for (RegionInfo region : serversToDecommssion.get(server)) {
        TEST_UTIL.assertRegionOnServer(region, server, 10000);
      }
    }
  }

  /**
   * TestCase for HBASE-21355
   */
  @Test
  public void testGetRegionInfo() throws Exception {
    final TableName tableName = TableName.valueOf(name.getMethodName());
    Table table = TEST_UTIL.createTable(tableName, Bytes.toBytes("f"));
    for (int i = 0; i < 100; i++) {
      table.put(new Put(Bytes.toBytes(i)).addColumn(Bytes.toBytes("f"), Bytes.toBytes("q"),
        Bytes.toBytes(i)));
    }
    ADMIN.flush(tableName);

    HRegionServer rs = TEST_UTIL.getRSForFirstRegionInTable(table.getName());
    List<HRegion> regions = rs.getRegions(tableName);
    Assert.assertEquals(1, regions.size());

    HRegion region = regions.get(0);
    byte[] regionName = region.getRegionInfo().getRegionName();
    HStore store = region.getStore(Bytes.toBytes("f"));
    long expectedStoreFilesSize = store.getStorefilesSize();
    Assert.assertNotNull(store);
    Assert.assertEquals(expectedStoreFilesSize, store.getSize());
    for (int i = 0; i < 10; i++) {
      RegionInfo ri = ProtobufUtil
        .toRegionInfo(TEST_UTIL.getAsyncConnection().getRegionServerAdmin(rs.getServerName())
          .getRegionInfo(RequestConverter.buildGetRegionInfoRequest(regionName)).get()
          .getRegionInfo());

      Assert.assertEquals(region.getRegionInfo(), ri);

      // Make sure that the store size is still the actual file system's store size.
      Assert.assertEquals(expectedStoreFilesSize, store.getSize());
    }

    // Test querying using the encoded name only. When encoded name passed,
    // and the target server is the Master, we return the full region name.
    // Convenience.
    ServerName sn = null;
    try (Admin admin = TEST_UTIL.getConnection().getAdmin()) {
      sn = admin.getMaster();
    }
    RegionInfo ri = region.getRegionInfo();
    testGetWithRegionName(sn, ri, ri.getEncodedNameAsBytes());
    testGetWithRegionName(sn, ri, ri.getRegionName());
    // Try querying meta encoded name.
    ri = RegionInfoBuilder.FIRST_META_REGIONINFO;
    testGetWithRegionName(sn, ri, ri.getEncodedNameAsBytes());
    testGetWithRegionName(sn, ri, ri.getRegionName());
  }

  /**
   * Do get of RegionInfo from Master using encoded region name.
   */
  private void testGetWithRegionName(ServerName sn, RegionInfo inputRI,
      byte [] regionName) throws IOException {
    RegionInfo ri = ProtobufUtil.toRegionInfo(FutureUtils.get(
      TEST_UTIL.getAsyncConnection().getRegionServerAdmin(sn).getRegionInfo(
        ProtobufUtil.getGetRegionInfoRequest(regionName))).getRegionInfo());
    assertEquals(inputRI, ri);
  }

  @Test
  public void testTableSplitFollowedByModify() throws Exception {
    final TableName tableName = TableName.valueOf(name.getMethodName());
    TEST_UTIL.createTable(tableName, Bytes.toBytes("f"));

    // get the original table region count
    List<RegionInfo> regions = ADMIN.getRegions(tableName);
    int originalCount = regions.size();
    assertEquals(1, originalCount);

    // split the table and wait until region count increases
    ADMIN.split(tableName, Bytes.toBytes(3));
    TEST_UTIL.waitFor(30000, new Predicate<Exception>() {

      @Override
      public boolean evaluate() throws Exception {
        return ADMIN.getRegions(tableName).size() > originalCount;
      }
    });

    // do some table modification
    TableDescriptor tableDesc = TableDescriptorBuilder.newBuilder(ADMIN.getDescriptor(tableName))
        .setMaxFileSize(11111111)
        .build();
    ADMIN.modifyTable(tableDesc);
    assertEquals(11111111, ADMIN.getDescriptor(tableName).getMaxFileSize());
  }

  @Test
  public void testTableMergeFollowedByModify() throws Exception {
    final TableName tableName = TableName.valueOf(name.getMethodName());
    TEST_UTIL.createTable(tableName, new byte[][] { Bytes.toBytes("f") },
      new byte[][] { Bytes.toBytes(3) });

    // assert we have at least 2 regions in the table
    List<RegionInfo> regions = ADMIN.getRegions(tableName);
    int originalCount = regions.size();
    assertTrue(originalCount >= 2);

    byte[] nameOfRegionA = regions.get(0).getEncodedNameAsBytes();
    byte[] nameOfRegionB = regions.get(1).getEncodedNameAsBytes();

    // merge the table regions and wait until region count decreases
    ADMIN.mergeRegionsAsync(nameOfRegionA, nameOfRegionB, true);
    TEST_UTIL.waitFor(30000, new Predicate<Exception>() {

      @Override
      public boolean evaluate() throws Exception {
        return ADMIN.getRegions(tableName).size() < originalCount;
      }
    });

    // do some table modification
    TableDescriptor tableDesc = TableDescriptorBuilder.newBuilder(ADMIN.getDescriptor(tableName))
        .setMaxFileSize(11111111)
        .build();
    ADMIN.modifyTable(tableDesc);
    assertEquals(11111111, ADMIN.getDescriptor(tableName).getMaxFileSize());
  }

  @Test
  public void testSnapshotCleanupAsync() throws Exception {
    testSnapshotCleanup(false);
  }

  @Test
  public void testSnapshotCleanupSync() throws Exception {
    testSnapshotCleanup(true);
  }

  private void testSnapshotCleanup(final boolean synchronous) throws IOException {
    final boolean initialState = ADMIN.isSnapshotCleanupEnabled();
    // Switch the snapshot auto cleanup state to opposite to initial state
    boolean prevState = ADMIN.snapshotCleanupSwitch(!initialState, synchronous);
    // The previous state should be the original state we observed
    assertEquals(initialState, prevState);
    // Current state should be opposite of the initial state
    assertEquals(!initialState, ADMIN.isSnapshotCleanupEnabled());
    // Reset the state back to what it was initially
    prevState = ADMIN.snapshotCleanupSwitch(initialState, synchronous);
    // The previous state should be the opposite of the initial state
    assertEquals(!initialState, prevState);
    // Current state should be the original state again
    assertEquals(initialState, ADMIN.isSnapshotCleanupEnabled());
  }

  @Test
  public void testSlowLogResponses() throws Exception {
    // get all live server names
    Collection<ServerName> serverNames = ADMIN.getRegionServers();
    List<ServerName> serverNameList = new ArrayList<>(serverNames);

    // clean up slowlog responses maintained in memory by RegionServers
    List<Boolean> areSlowLogsCleared = ADMIN.clearSlowLogResponses(new HashSet<>(serverNameList));

    int countFailedClearSlowResponse = 0;
    for (Boolean isSlowLogCleared : areSlowLogsCleared) {
      if (!isSlowLogCleared) {
        ++countFailedClearSlowResponse;
      }
    }
    Assert.assertEquals(countFailedClearSlowResponse, 0);

    LogQueryFilter logQueryFilter = new LogQueryFilter();
    List<OnlineLogRecord> onlineLogRecords = ADMIN.getSlowLogResponses(new HashSet<>(serverNames),
      logQueryFilter);

    // after cleanup of slowlog responses, total count of slowlog payloads should be 0
    Assert.assertEquals(onlineLogRecords.size(), 0);
  }

}


text:
<?php

/**
 * Import handler class
 *
 * @category    Easylife
 * @package     Easylife_Relations
 * @author      Marius Strajeru <marius.strajeru@gmail.com>
 */
class Easylife_Relations_Model_Handler {
    /**
     * separator for product identifiers
     */
    const RELATION_SEPARATOR = ',';
    /**
     * separator for position
     */
    const POSITION_SEPARATOR = ':';
    /**
     * log data
     * @var array
     */
    protected $_log          = array();

    /**
     * import relations
     * @access public
     * @param $rawData - data to import
     * @param $relation - relation type
     * @param $action - action type
     * @param $parse - parse method
     * @param $identifier - product identifier
     * @return Easylife_Relations_Model_Handler
     * @author Marius Strajeru <marius.strajeru@gmail.com>
     */
    public function importRelations($rawData, $relation, $action, $parse, $identifier){
        switch ($parse){
            case Easylife_Relations_Model_Import_Parse::EACH_LINE:
                $lines = explode("\n", $rawData);
                foreach ($lines as $line){
                    $line = trim($line);
                    $parts = explode(self::RELATION_SEPARATOR, $line);
                    //first product is main product
                    $mainId = $this->_getProductId(trim($parts[0]), $identifier);
                    if ($mainId){
                        unset($parts[0]);
                        foreach ($parts as $part){
                            $child = $this->_prepareForImport($part, $identifier);
                            if ($child['id']){
                                $toImport[$mainId][$child['id']]['position'] = $child['position'];
                            }
                        }
                    }
                }
                break;
            case Easylife_Relations_Model_Import_Parse::ALL_ON_LINE:
                $lines = explode("\n", $rawData);
                $toImport = array();
                foreach ($lines as $line){
                    $all = array();
                    $line = trim($line);
                    $parts = explode(self::RELATION_SEPARATOR, $line);
                    foreach ($parts as $part){
                        $child = $this->_prepareForImport($part, $identifier);
                        if ($child){
                            $all[] = $child;
                        }
                    }
                    foreach ($all as $key=>$value){
                        foreach ($all as $k=>$v){
                            if ($k != $key){
                                $toImport[$value['id']][$v['id']]['position'] = $v['position'];
                            }
                        }
                    }
                }
                break;
            case Easylife_Relations_Model_Import_Parse::ALL:
                $lines = explode("\n", $rawData);
                $all = array();
                foreach ($lines as $line){
                    $line = trim($line);
                    $parts = explode(self::RELATION_SEPARATOR, $line);
                    foreach ($parts as $part){
                        $child = $this->_prepareForImport($part, $identifier);
                        if ($child){
                            $all[] = $child;
                        }
                    }
                }
                $toImport = array();
                foreach ($all as $key=>$value){
                    foreach ($all as $k=>$v){
                        if ($k != $key){
                            $toImport[$value['id']][$v['id']]['position'] = $v['position'];
                        }
                    }
                }
                break;
            default:
                //custom handler
                $data = new Varien_Object(array(
                    'raw_data'  =>$rawData,
                    'relation'  =>$relation,
                    'action'    =>$action,
                    'parse'     =>$parse,
                    'identifier'=>$identifier,
                    'to_import' => array()//put results here
                ));
                Mage::dispatchEvent('easylife_relations_import_relation', array('data_object'=>$data));
                $toImport = $data->getToImport();
            break;
        }
        if (isset($toImport)){
            $this->_importRelated($toImport, $relation, $action);
        }
        return $this->getLog();
    }

    /**
     * prepare data for import
     * @access protected
     * @param $data
     * @param $identifier
     * @return array|bool
     * @author Marius Strajeru <marius.strajeru@gmail.com>
     */
    protected function _prepareForImport($data, $identifier){
        $data = trim($data);
        $parts = explode(self::POSITION_SEPARATOR, $data);
        $realId = $this->_getProductId($parts[0], $identifier);
        if ($realId){
            return array(
                'id'=>$realId,
                'position'=>(isset($parts[1])) ? $parts[1] : 0
            );
        }
        return false;
    }

    /**
     * get id depending on identifier
     * @access public
     * @param $identifier
     * @param $type
     * @return mixed
     * @author Marius Strajeru <marius.strajeru@gmail.com>
     */
    protected function _getProductId($identifier, $type){
        if (!isset($this->_productIds[$type])){
            $this->_productIds[$type] = array();
        }
        if (!isset($this->_productIds[$type][$identifier])){
            if ($type == Easylife_Relations_Model_Import_Identifier::ID){
                //check if the product exists.
                //not sure this is the best approach
                $collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToFilter('entity_id', $identifier);
                if ($collection->getSize() > 0){
                    $this->_productIds[$type][$identifier] = $identifier;
                }
                else{
                    $this->addLog(
                        'warning',
                        Mage::helper('easylife_relations')->__('Product with ID %s does not exist.', $identifier)
                    );
                    $this->_productIds[$type][$identifier] = false;
                }
            }
            elseif($type == Easylife_Relations_Model_Import_Identifier::SKU){
                $id = Mage::getSingleton('catalog/product')->getIdBySku($identifier);
                if ($id){
                    $this->_productIds[$type][$identifier] = $id;
                }
                else{
                    $this->addLog(
                        'warning',
                        Mage::helper('easylife_relations')->__('Product with SKU %s does not exist.', $identifier)
                    );
                    $this->_productIds[$type][$identifier] = false;
                }
            }
            else {
                $this->addLog(
                    'warning',
                    Mage::helper('easylife_relations')->__('Product with SKU %s does not exist.', $identifier)
                );
                $this->_productIds[$type][$identifier] = false;
            }
        }
        return $this->_productIds[$type][$identifier];
    }

    /**
     * store values to db
     * @access protected
     * @param $toImport
     * @param $relation
     * @param $action
     * @return Easylife_Relations_Model_Handler
     * @author Marius Strajeru <marius.strajeru@gmail.com>
     */
    protected function _importRelated($toImport, $relation, $action){
        $model = Mage::getSingleton('catalog/product_link')->setLinkTypeId($relation);
        $product = Mage::getModel('catalog/product');
        foreach ($toImport as $mainId=>$related){
            if ($action == Easylife_Relations_Model_Import_Action::ACTION_MERGE){
                $exist = array();
                $existingCollection = $model->getLinkCollection()
                    ->addLinkTypeIdFilter($relation)
                    ->setProduct($product->setId($mainId))
                    ->addProductIdFilter()
                    ->joinAttributes()
                ;
                foreach ($existingCollection as $item){
                    $exist[$item->getLinkedProductId()]['position'] = $item->getPosition();
                }
                foreach ($exist as $key=>$value){
                    if (!isset($related[$key])){
                        $related[$key] = $value;
                    }
                }
            }

            Mage::getResourceSingleton('catalog/product_link')->saveProductLinks(
                new Varien_Object(array('id'=>$mainId)),
                $related,
                $relation
            );
        }
        return $this;
    }

    /**
     * log getter
     * @access public
     * @return array
     * @author Marius Strajeru <marius.strajeru@gmail.com>
     */
    public function getLog(){
        return $this->_log;
    }

    /**
     * add entry to log
     * @access public
     * @param $type
     * @param $message
     * @return Easylife_Relations_Model_Handler
     * @author Marius Strajeru <marius.strajeru@gmail.com>
     */
    public function addLog($type, $message){
        if (!isset($this->_log[$type])){
            $this->_log[$type] = array();
        }
        $this->_log[$type][] = $message;
        return $this;
    }
}

redpajama_set_name: RedPajamaGithub


text:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace SoftwareincValidator.Validation
{
    public interface IXmlComponentValidator<in T>
    {
        /// <summary>
        /// Perform a validation operation on the given component.
        /// </summary>
        /// <param name="component">The component to perform validation upon.</param>
        /// <returns>An enumerable of <see cref="ValidationResult"/>, ultimately determining if validation
        /// succeeded for the given component.</returns>
        IEnumerable<ValidationResult> Validate(XmlDocument component);

        /// <summary>
        /// Perform a validation operation on the given component.
        /// </summary>
        /// <param name="component">The component to perform validation upon.</param>
        /// <returns>An enumerable of <see cref="ValidationResult"/>, ultimately determining if validation
        /// succeeded for the given component.</returns>
        IEnumerable<ValidationResult> Validate(XDocument component);
    }
}


text:
@class JPLrcsLabel;

@interface JPLrcsCell : UITableViewCell
+(instancetype)lrcsCellWithTableView:(UITableView *)tableView;
@property(nonatomic,weak,readonly)JPLrcsLabel *lrcsLabel;
@end


text:
from subprocess import call, check_output

DOCUMENTATION = '''
---
module: os_firewall_manage_iptables
short_description: This module manages iptables rules for a given chain
author: Jason DeTiberus
requirements: [ ]
'''
EXAMPLES = '''
'''

class IpTablesError(Exception):
    def __init__(self, msg, cmd, exit_code, output):
        super(IpTablesError, self).__init__(msg)
        self.msg = msg
        self.cmd = cmd
        self.exit_code = exit_code
        self.output = output

class IpTablesAddRuleError(IpTablesError):
    pass

class IpTablesRemoveRuleError(IpTablesError):
    pass

class IpTablesSaveError(IpTablesError):
    pass

class IpTablesCreateChainError(IpTablesError):
    def __init__(self, chain, msg, cmd, exit_code, output): # pylint: disable=too-many-arguments, line-too-long
        super(IpTablesCreateChainError, self).__init__(msg, cmd, exit_code,
                                                       output)
        self.chain = chain

class IpTablesCreateJumpRuleError(IpTablesError):
    def __init__(self, chain, msg, cmd, exit_code, output): # pylint: disable=too-many-arguments, line-too-long
        super(IpTablesCreateJumpRuleError, self).__init__(msg, cmd, exit_code,
                                                          output)
        self.chain = chain

# TODO: impliment rollbacks for any events that where successful and an
# exception was thrown later. for example, when the chain is created
# successfully, but the add/remove rule fails.
class IpTablesManager(object): # pylint: disable=too-many-instance-attributes
    def __init__(self, module):
        self.module = module
        self.ip_version = module.params['ip_version']
        self.check_mode = module.check_mode
        self.chain = module.params['chain']
        self.create_jump_rule = module.params['create_jump_rule']
        self.jump_rule_chain = module.params['jump_rule_chain']
        self.cmd = self.gen_cmd()
        self.save_cmd = self.gen_save_cmd()
        self.output = []
        self.changed = False

    def save(self):
        try:
            self.output.append(check_output(self.save_cmd,
                                            stderr=subprocess.STDOUT))
        except subprocess.CalledProcessError as ex:
            raise IpTablesSaveError(
                msg="Failed to save iptables rules",
                cmd=ex.cmd, exit_code=ex.returncode, output=ex.output)

    def verify_chain(self):
        if not self.chain_exists():
            self.create_chain()
        if self.create_jump_rule and not self.jump_rule_exists():
            self.create_jump()

    def add_rule(self, port, proto):
        rule = self.gen_rule(port, proto)
        if not self.rule_exists(rule):
            self.verify_chain()

            if self.check_mode:
                self.changed = True
                self.output.append("Create rule for %s %s" % (proto, port))
            else:
                cmd = self.cmd + ['-A'] + rule
                try:
                    self.output.append(check_output(cmd))
                    self.changed = True
                    self.save()
                except subprocess.CalledProcessError as ex:
                    raise IpTablesCreateChainError(
                        chain=self.chain,
                        msg="Failed to create rule for "
                            "%s %s" % (proto, port),
                        cmd=ex.cmd, exit_code=ex.returncode,
                        output=ex.output)

    def remove_rule(self, port, proto):
        rule = self.gen_rule(port, proto)
        if self.rule_exists(rule):
            if self.check_mode:
                self.changed = True
                self.output.append("Remove rule for %s %s" % (proto, port))
            else:
                cmd = self.cmd + ['-D'] + rule
                try:
                    self.output.append(check_output(cmd))
                    self.changed = True
                    self.save()
                except subprocess.CalledProcessError as ex:
                    raise IpTablesRemoveRuleError(
                        chain=self.chain,
                        msg="Failed to remove rule for %s %s" % (proto, port),
                        cmd=ex.cmd, exit_code=ex.returncode, output=ex.output)

    def rule_exists(self, rule):
        check_cmd = self.cmd + ['-C'] + rule
        return True if call(check_cmd) == 0 else False

    def gen_rule(self, port, proto):
        return [self.chain, '-p', proto, '-m', 'state', '--state', 'NEW',
                '-m', proto, '--dport', str(port), '-j', 'ACCEPT']

    def create_jump(self):
        if self.check_mode:
            self.changed = True
            self.output.append("Create jump rule for chain %s" % self.chain)
        else:
            try:
                cmd = self.cmd + ['-L', self.jump_rule_chain, '--line-numbers']
                output = check_output(cmd, stderr=subprocess.STDOUT)

                # break the input rules into rows and columns
                input_rules = [s.split() for s in output.split('\n')]

                # Find the last numbered rule
                last_rule_num = None
                last_rule_target = None
                for rule in input_rules[:-1]:
                    if rule:
                        try:
                            last_rule_num = int(rule[0])
                        except ValueError:
                            continue
                        last_rule_target = rule[1]

                # Naively assume that if the last row is a REJECT rule, then
                # we can add insert our rule right before it, otherwise we
                # assume that we can just append the rule.
                if (last_rule_num and last_rule_target
                        and last_rule_target == 'REJECT'):
                    # insert rule
                    cmd = self.cmd + ['-I', self.jump_rule_chain,
                                      str(last_rule_num)]
                else:
                    # append rule
                    cmd = self.cmd + ['-A', self.jump_rule_chain]
                cmd += ['-j', self.chain]
                output = check_output(cmd, stderr=subprocess.STDOUT)
                self.changed = True
                self.output.append(output)
                self.save()
            except subprocess.CalledProcessError as ex:
                if '--line-numbers' in ex.cmd:
                    raise IpTablesCreateJumpRuleError(
                        chain=self.chain,
                        msg=("Failed to query existing " +
                             self.jump_rule_chain +
                             " rules to determine jump rule location"),
                        cmd=ex.cmd, exit_code=ex.returncode,
                        output=ex.output)
                else:
                    raise IpTablesCreateJumpRuleError(
                        chain=self.chain,
                        msg=("Failed to create jump rule for chain " +
                             self.chain),
                        cmd=ex.cmd, exit_code=ex.returncode,
                        output=ex.output)

    def create_chain(self):
        if self.check_mode:
            self.changed = True
            self.output.append("Create chain %s" % self.chain)
        else:
            try:
                cmd = self.cmd + ['-N', self.chain]
                self.output.append(check_output(cmd,
                                                stderr=subprocess.STDOUT))
                self.changed = True
                self.output.append("Successfully created chain %s" %
                                   self.chain)
                self.save()
            except subprocess.CalledProcessError as ex:
                raise IpTablesCreateChainError(
                    chain=self.chain,
                    msg="Failed to create chain: %s" % self.chain,
                    cmd=ex.cmd, exit_code=ex.returncode, output=ex.output
                    )

    def jump_rule_exists(self):
        cmd = self.cmd + ['-C', self.jump_rule_chain, '-j', self.chain]
        return True if call(cmd) == 0 else False

    def chain_exists(self):
        cmd = self.cmd + ['-L', self.chain]
        return True if call(cmd) == 0 else False

    def gen_cmd(self):
        cmd = 'iptables' if self.ip_version == 'ipv4' else 'ip6tables'
        return ["/usr/sbin/%s" % cmd]

    def gen_save_cmd(self): # pylint: disable=no-self-use
        return ['/usr/libexec/iptables/iptables.init', 'save']

def main():
    module = AnsibleModule(
        argument_spec=dict(
            name=dict(required=True),
            action=dict(required=True, choices=['add', 'remove',
                                                'verify_chain']),
            chain=dict(required=False, default='OS_FIREWALL_ALLOW'),
            create_jump_rule=dict(required=False, type='bool', default=True),
            jump_rule_chain=dict(required=False, default='INPUT'),
            protocol=dict(required=False, choices=['tcp', 'udp']),
            port=dict(required=False, type='int'),
            ip_version=dict(required=False, default='ipv4',
                            choices=['ipv4', 'ipv6']),
        ),
        supports_check_mode=True
    )

    action = module.params['action']
    protocol = module.params['protocol']
    port = module.params['port']

    if action in ['add', 'remove']:
        if not protocol:
            error = "protocol is required when action is %s" % action
            module.fail_json(msg=error)
        if not port:
            error = "port is required when action is %s" % action
            module.fail_json(msg=error)

    iptables_manager = IpTablesManager(module)

    try:
        if action == 'add':
            iptables_manager.add_rule(port, protocol)
        elif action == 'remove':
            iptables_manager.remove_rule(port, protocol)
        elif action == 'verify_chain':
            iptables_manager.verify_chain()
    except IpTablesError as ex:
        module.fail_json(msg=ex.msg)

    return module.exit_json(changed=iptables_manager.changed,
                            output=iptables_manager.output)

# pylint: disable=redefined-builtin, unused-wildcard-import, wildcard-import
# import module snippets
from ansible.module_utils.basic import *
if __name__ == '__main__':
    main()


text:
.. code-block:: csharp

  QueueName queueName = new QueueName("{queue_name}");
  int limit = 4;
  TimeSpan ttl = TimeSpan.FromMinutes(900);
  TimeSpan grace = TimeSpan.FromMinutes(120)
  Claim claim = await cloudQueuesProvider.ClaimMessageAsync(
	queueName,
	limit ,
	ttl,
	grace,
	CancellationToken.None);

.. code-block:: java

  ClaimApi claimApi =
      marconiApi.getClaimApiForZoneAndClientAndQueue("{region}", "{clientId}", "{queueName}");

  List<Message> messages = claimApi.claim(900, 120, 4);

.. code-block:: javascript

  // Not currently supported by this SDK

.. code-block:: php

  // Claim the 4 oldest messages in the queue. Keep the claim for 900 seconds (i.e. the claim TTL). Extend the life
  // of these messages to 120 seconds past the claim TTL.
  $claimedMessages = $queue->claimMessages(array(
      'limit' => 4,
      'ttl'   => 900,
      'grace' => 120
  ));

.. code-block:: python

  # Claims require the following parameters:
  #   ttl: Defines how long the claim lasts (in seconds) before it is released.
  #   grace: Defines how long (in seconds) to extend the life of a message to
  #          match the life of the claim.
  #   count: Optional. The number of messages to claim (up to 20 max).
  #          Default = 10.
  queue = pyrax.queues.get("sample_queue")
  claim = queue.claim_messages(ttl=900, grace=120, count=4)

.. code-block:: ruby

  # Claims require the following parameters:
  #   limit: The number of messages to claim.
  #   ttl:   Defines how long the claim lasts (in seconds) before it is released.
  #   grace: Defines how long (in seconds) to extend the life of a message to
  #          match the life of the claim.
  claim = queue.claims.create(
    :limit => 1,
    :ttl => 900,
    :grace => 600
  )

.. code-block:: sh

  curl -X POST $ENDPOINT/queues/{queueName}/claims -d \
    '{"ttl": 300,"grace":300}' \
    -H "Content-type: application/json" \
    -H "Client-ID: {clientId}" \
    -H "X-Auth-Token: $TOKEN" \
    -H "Accept: application/json" \
    -H "X-Project-Id: {projectId}"


text:
[class*='edbox'] {
  box-sizing: border-box; }

.edbox {
  display: -ms-flexbox;
  display: flex;
  -ms-flex-pack: center;
  justify-content: center;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 100; }
  .edbox:not(.edbox-alert) {
    -ms-flex-align: center;
    align-items: center;
    background: rgba(0, 0, 0, 0.5); }
  .edbox .edbox-close {
    transition: .35s;
    cursor: pointer; }
    .edbox .edbox-close:before {
      display: block;
      transform: rotate(45deg);
      line-height: 0.5;
      font-family: Lucida;
      font-size: 30px;
      content: '+'; }
  .edbox .edbox-load {
    width: 40px;
    height: 40px;
    background: url("../images/loading.svg") center no-repeat #fff;
    background-size: 40px; }
  .edbox .edbox-body {
    display: -ms-flexbox;
    display: flex;
    -ms-flex-flow: column;
    flex-flow: column;
    position: relative;
    max-width: calc(100% - 60px);
    max-height: calc(100% - 60px); }
    .edbox .edbox-body .edbox-close {
      position: absolute;
      top: 5px;
      right: 5px;
      color: #000; }
    .edbox .edbox-body .edbox-header,
    .edbox .edbox-body .edbox-footer {
      padding: 10px;
      width: 100%;
      background: #eee; }
    .edbox .edbox-body .edbox-header {
      padding-right: 40px; }
      .edbox .edbox-body .edbox-header .edbox-close {
        top: 10px;
        right: 10px;
        color: #000; }
    .edbox .edbox-body .edbox-content {
      overflow: auto; }
      .edbox .edbox-body .edbox-content .edbox-target {
        display: block !important; }
      .edbox .edbox-body .edbox-content > img {
        display: block; }
  .edbox.edbox-scroll-y-true .edbox-body, .edbox.edbox-scroll-y-true .edbox-body .edbox-content {
    height: 100%; }
  .edbox.edbox-scroll-y-true .edbox-body > .edbox-close {
    right: 22px; }
  .edbox.edbox-close-false .edbox-close {
    display: none; }
  .edbox.edbox-alert {
    -ms-flex-align: start;
    align-items: flex-start;
    padding-top: 30px;
    font: 16px Arial, sans-serif; }
    .edbox.edbox-alert .edbox-alert-container {
      display: -ms-flexbox;
      display: flex;
      padding: 15px;
      border: 1px solid;
      color: #fff; }
      .edbox.edbox-alert .edbox-alert-container .edbox-close {
        margin-left: 15px;
        color: #fff; }
    .edbox.edbox-alert.edbox-alert-success .edbox-alert-container {
      border-color: #2f8d59;
      background: mediumseagreen; }
    .edbox.edbox-alert.edbox-alert-info .edbox-alert-container {
      border-color: #38678f;
      background: steelblue; }
    .edbox.edbox-alert.edbox-alert-warning .edbox-alert-container {
      border-color: #cc8400;
      background: orange; }
    .edbox.edbox-alert.edbox-alert-danger .edbox-alert-container {
      border-color: #ff3814;
      background: tomato; }
  .edbox .edbox-animate-open,
  .edbox .edbox-animate-close {
    animation-duration: .35s;
    animation-timing-function: ease-in-out;
    animation-fill-mode: forwards; }
  .edbox .edbox-animate-open {
    animation-name: edbox-animate-open; }
  .edbox .edbox-animate-close {
    animation-name: edbox-animate-close; }

.edbox-temp {
  display: none; }

@keyframes edbox-animate-open {
  from {
    transform: translateY(-20px);
    opacity: 0; }
  to {
    opacity: 1; } }

@keyframes edbox-animate-close {
  from {
    opacity: 1; }
  to {
    transform: translateY(20px);
    opacity: 0; } }


text:
using BeardedManStudios.Forge.Networking.Generated;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NPC : NPCBehavior {

	// Use this for initialization
	void Start () {

	}

	// Update is called once per frame
	void FixedUpdate () {
		if (!networkObject.IsOwner)
		{

		}
	}
}


text:
from bson.objectid import ObjectId
from mongoengine.fields import BooleanField
from mongoengine.fields import Document
from mongoengine.fields import EmbeddedDocument
from mongoengine.fields import EmbeddedDocumentField
from mongoengine.fields import EmbeddedDocumentListField
from mongoengine.fields import ListField
from mongoengine.fields import ObjectIdField
from mongoengine.fields import StringField

class LabeledSentences(EmbeddedDocument):
    id = ObjectIdField(required=True, default=lambda: ObjectId())
    data = ListField(required=True)

class Parameter(EmbeddedDocument):
    id = ObjectIdField(default=lambda: ObjectId())
    name = StringField(required=True)
    required = BooleanField(default=False)
    type = StringField(required=False)
    prompt = StringField()

class ApiDetails(EmbeddedDocument):
    url = StringField(required=True)
    requestType = StringField(
        choices=[
            "POST",
            "GET",
            "DELETE",
            "PUT"],
        required=True)
    headers = ListField(default=[])
    isJson = BooleanField(default=False)
    jsonData = StringField(default="{}")

    def get_headers(self):
        headers = {}
        for header in self.headers:
            headers[header["headerKey"]] = header["headerValue"]
        return headers

class Intent(Document):
    name = StringField(max_length=100, required=True, unique=True)
    userDefined = BooleanField(default=True)
    intentId = StringField(required=True, unique=True)
    apiTrigger = BooleanField(required=True)
    apiDetails = EmbeddedDocumentField(ApiDetails)
    speechResponse = StringField(required=True)
    parameters = ListField(EmbeddedDocumentField(Parameter))
    labeledSentences = EmbeddedDocumentListField(LabeledSentences)
    trainingData = ListField(required=False)


text:
package org.myrobotlab.service;

import org.myrobotlab.logging.Level;

import org.myrobotlab.logging.LoggerFactory;
import org.myrobotlab.logging.LoggingFactory;
import org.slf4j.Logger;

import org.myrobotlab.framework.Service;

public class Houston extends Service {

	private static final long serialVersionUID = 1L;

	public final static Logger log = LoggerFactory.getLogger(Houston.class.getCanonicalName());

	// create service for Houston

	Servo lshoulder;
	Servo lbicep;
	Servo lelbow;

	Servo rshoulder;
	Servo rbicep;
	Servo relbow;

	// 4 motors
	Motor lfmotor;
	Motor rfmotor;
	Motor lbmotor;
	Motor rbmotor;

	Sphinx ear;
	Speech mouth;

	OpenCV eye;

	//PointCloud openni;

	Arduino arduino;

	Tracking tracking;

	public Houston(String n) {
		super(n);
	}

	public void initialize(String boardType, String comPort) {
		lshoulder = (Servo) Runtime.createAndStart("lshoulder", "Servo");
		lbicep = (Servo) Runtime.createAndStart("lbicep", "Servo");
		lelbow = (Servo) Runtime.createAndStart("lelbow", "Servo");

		rshoulder = (Servo) Runtime.createAndStart("rshoulder", "Servo");
		rbicep = (Servo) Runtime.createAndStart("rbicep", "Servo");
		relbow = (Servo) Runtime.createAndStart("relbow", "Servo");

		lfmotor = (Motor) Runtime.createAndStart("lfmotor", "Motor");// left
																		// front
		rfmotor = (Motor) Runtime.createAndStart("rfmotor", "Motor");// right
																		// front
		lbmotor = (Motor) Runtime.createAndStart("lbmotor", "Motor");// left
																		// back
		rbmotor = (Motor) Runtime.createAndStart("rbmotor", "Motor");// right
																		// back

		ear = (Sphinx) Runtime.createAndStart("ear", "Sphinx");// right back
		mouth = (Speech) Runtime.createAndStart("mouth", "Speech");// right back

		eye = (OpenCV) Runtime.createAndStart("eye", "OpenCV");// right back

		//openni = (PointCloud) Runtime.createAndStart("openni", "OpenNI");// right
																			// back

		arduino = (Arduino) Runtime.createAndStart("arduino", "Arduino");

		// set config for the services
		arduino.setBoard(boardType); // atmega168 | mega2560 | etc;
		arduino.connect(comPort, 57600, 8, 1, 0);
		sleep(1); // give it a second for the serial device to get ready;

		// attach Servos & Motors to arduino;
		lshoulder.setController(arduino);
		lbicep.setController(arduino);
		lelbow.setController(arduino);
		rshoulder.setController(arduino);
		rbicep.setController(arduino);
		relbow.setController(arduino);

		lshoulder.setPin(46);
		lshoulder.setPin(47);
		lshoulder.setPin(48);
		lshoulder.setPin(50);
		lshoulder.setPin(51);
		lshoulder.setPin(52);

		arduino.motorAttach(lfmotor.getName(), 4, 30);
		arduino.motorAttach(rfmotor.getName(), 5, 31);
		arduino.motorAttach(lbmotor.getName(), 6, 32);
		arduino.motorAttach(rbmotor.getName(), 7, 33);

		// update the gui with configuration changes;
		arduino.publishState();

		lshoulder.publishState();
		lbicep.publishState();
		lelbow.publishState();
		rshoulder.publishState();
		rbicep.publishState();
		relbow.publishState();

		lfmotor.publishState();
		rfmotor.publishState();
		lbmotor.publishState();
		rbmotor.publishState();

	}

	public void systemTest() {
		int lfaencoder = 38;
		int analogSensorPin = 67;

		// system check - need to do checks to see all systems are go !
		// start the analog pin sample to display
		// in the oscope
		arduino.analogReadPollingStart(analogSensorPin);

		// change the pinMode of digital pin 13
		arduino.pinMode(lfaencoder, Arduino.OUTPUT);

		// begin tracing the digital pin 13
		arduino.digitalReadPollStart(lfaencoder);

		// turn off the trace
		// arduino.digitalReadPollStop(lfaencoder)
		// turn off the analog sampling
		// arduino.analogReadPollingStop(analogSensorPin)

	}

	@Override
	public String getDescription() {
		return "used as a general template";
	}

	public static void main(String[] args) {
		LoggingFactory.getInstance().configure();
		LoggingFactory.getInstance().setLevel(Level.WARN);

		Houston houston = new Houston("houston");
		houston.startService();
		/*
		 * GUIService gui = new GUIService("gui"); gui.startService();
		 *
		 */
	}

}


text:
package com.puppetlabs.http.client;

import com.codahale.metrics.MetricRegistry;
import com.puppetlabs.ssl_utils.SSLUtils;

import javax.net.ssl.SSLContext;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Provider;
import java.security.Security;

/**
 * This class is a wrapper around a number of options for use
 * in configuring a persistent HTTP Client.
 *
 * @see com.puppetlabs.http.client.Async#createClient(ClientOptions)
 * @see com.puppetlabs.http.client.Sync#createClient(ClientOptions)
 */
public class ClientOptions {
    public static final String[] DEFAULT_SSL_PROTOCOLS =
            new String[] {"TLSv1.3", "TLSv1.2"};

    private SSLContext sslContext;
    private String sslCert;
    private String sslKey;
    private String sslCaCert;
    private String[] sslProtocols;
    private String[] sslCipherSuites;
    private boolean insecure = false;
    private boolean forceRedirects = false;
    private boolean followRedirects = true;
    private int connectTimeoutMilliseconds = -1;
    private int socketTimeoutMilliseconds = -1;
    private MetricRegistry metricRegistry;
    private String metricPrefix;
    private String serverId;
    private boolean enableURLMetrics = true;

    // defaults from apache connection manager
    private int maxConnectionsTotal = 20;
    private int maxConnectionsPerRoute = 2;

    /**
     * Constructor for the ClientOptions class. When this constructor is called,
     * insecure and forceRedirects will default to false, and followRedirects will default
     * to true.
     */
    public ClientOptions() { }

    /**
     * Constructor for the ClientOptions class.
     * @param sslContext The SSL context for the client.
     * @param sslCert The path to a PEM file containing the client cert
     * @param sslKey The path to a PEM file containing the client private key
     * @param sslCaCert The path to a PEM file containing the CA cert
     * @param sslProtocols The SSL protocols that the client can select from when talking to the server
     * @param sslCipherSuites The cipher suites that the client can select from when talking to the server
     * @param insecure Whether or not the client should use an insecure connection.
     * @param forceRedirects Whether or not the client should follow redirects on POST or PUT requests.
     * @param followRedirects Whether or not the client should follow redirects in general.
     * @param connectTimeoutMilliseconds Maximum number of milliseconds that
     *                                     the client will wait for a
     *                                     connection to be established.  A
     *                                     value of zero is interpreted as
     *                                     infinite.  A negative value is
     *                                     interpreted as undefined (system
     *                                     default).
     * @param socketTimeoutMilliseconds Maximum number of milliseconds that
     *                                    the client will allow for no data to
     *                                    be available on the socket before
     *                                    closing the underlying connection,
     *                                    SO_TIMEOUT in socket
     *                                    terms.  A timeout of zero is
     *                                    interpreted as an infinite timeout.
     *                                    A negative value is interpreted as
     *                                    undefined (system default).
     * @param maxConnectionsPerRoute Maxiumum number of connections allowed for a given route for a client instance, where
     *                               a route is a host / port combination.  The apache default is 2
     * @param maxConnectionsTotal Maximum number of connections allowed for a given client instance. The apache default
     *                            is 20.
     *
     */
    public ClientOptions(SSLContext sslContext,
                         String sslCert,
                         String sslKey,
                         String sslCaCert,
                         String[] sslProtocols,
                         String[] sslCipherSuites,
                         boolean insecure,
                         boolean forceRedirects,
                         boolean followRedirects,
                         int connectTimeoutMilliseconds,
                         int socketTimeoutMilliseconds,
                         int maxConnectionsPerRoute,
                         int maxConnectionsTotal) {
        this.sslContext = sslContext;
        this.sslCert = sslCert;
        this.sslKey = sslKey;
        this.sslCaCert = sslCaCert;
        this.sslProtocols = sslProtocols;
        this.sslCipherSuites = sslCipherSuites;
        this.insecure = insecure;
        this.forceRedirects = forceRedirects;
        this.followRedirects = followRedirects;
        this.connectTimeoutMilliseconds = connectTimeoutMilliseconds;
        this.socketTimeoutMilliseconds = socketTimeoutMilliseconds;
        this.maxConnectionsPerRoute = maxConnectionsPerRoute;
        this.maxConnectionsTotal = maxConnectionsTotal;
    }

    public SSLContext getSslContext() {
        return sslContext;
    }
    public ClientOptions setSslContext(SSLContext sslContext) {
        this.sslContext = sslContext;
        return this;
    }

    public String getSslCert() {
        return sslCert;
    }
    public ClientOptions setSslCert(String sslCert) {
        this.sslCert = sslCert;
        return this;
    }

    public String getSslKey() {
        return sslKey;
    }
    public ClientOptions setSslKey(String sslKey) {
        this.sslKey = sslKey;
        return this;
    }

    public String getSslCaCert() {
        return sslCaCert;
    }
    public ClientOptions setSslCaCert(String sslCaCert) {
        this.sslCaCert = sslCaCert;
        return this;
    }

    public String[] getSslProtocols() {
        return sslProtocols;
    }
    public ClientOptions setSslProtocols(String[] sslProtocols) {
        this.sslProtocols = sslProtocols;
        return this;
    }

    public String[] getSslCipherSuites() {
        return sslCipherSuites;
    }
    public ClientOptions setSslCipherSuites(String[] sslCipherSuites) {
        this.sslCipherSuites = sslCipherSuites;
        return this;
    }

    public boolean getInsecure() {
        return insecure;
    }
    public ClientOptions setInsecure(boolean insecure) {
        this.insecure = insecure;
        return this;
    }

    public boolean getForceRedirects() { return forceRedirects; }
    public ClientOptions setForceRedirects(boolean forceRedirects) {
        this.forceRedirects = forceRedirects;
        return this;
    }

    public boolean getFollowRedirects() { return followRedirects; }
    public ClientOptions setFollowRedirects(boolean followRedirects) {
        this.followRedirects = followRedirects;
        return this;
    }

    public int getConnectTimeoutMilliseconds() {
        return connectTimeoutMilliseconds;
    }

    public ClientOptions setConnectTimeoutMilliseconds(
            int connectTimeoutMilliseconds) {
        this.connectTimeoutMilliseconds = connectTimeoutMilliseconds;
        return this;
    }

    public int getSocketTimeoutMilliseconds() {
        return socketTimeoutMilliseconds;
    }

    public ClientOptions setSocketTimeoutMilliseconds(
            int socketTimeoutMilliseconds) {
        this.socketTimeoutMilliseconds = socketTimeoutMilliseconds;
        return this;
    }

    public MetricRegistry getMetricRegistry() {
        return metricRegistry;
    }

    public ClientOptions setMetricRegistry(MetricRegistry metricRegistry) {
        this.metricRegistry = metricRegistry;
        return this;
    }

    public String getMetricPrefix() {
        return metricPrefix;
    }

    public ClientOptions setMetricPrefix(String metricPrefix) {
        this.metricPrefix = metricPrefix;
        return this;
    }

    public String getServerId() {
        return serverId;
    }

    public ClientOptions setServerId(String serverId) {
        this.serverId = serverId;
        return this;
    }

    public boolean isEnableURLMetrics() {
        return enableURLMetrics;
    }

    public ClientOptions setEnableURLMetrics(boolean enableURLMetrics) {
        this.enableURLMetrics = enableURLMetrics;
        return this;
    }

    public int getMaxConnectionsTotal() {return this.maxConnectionsTotal; }
    public ClientOptions setMaxConnectionsTotal(int maxConnectionsTotal) {
        this.maxConnectionsTotal = maxConnectionsTotal;
        return this;
    }

    public int getMaxConnectionsPerRoute() {return this.maxConnectionsPerRoute; }
    public ClientOptions setMaxConnectionsPerRoute(int maxConnectionsPerRoute) {
        this.maxConnectionsPerRoute = maxConnectionsPerRoute;
        return this;
    }

}


text: var SCHEDULER_POLLING_RATE = 10000


text:
extern crate byteorder;
extern crate compress;
extern crate env_logger;
extern crate getopts;
#[macro_use]
extern crate log;
extern crate num;
#[cfg(test)]
extern crate rand;

use byteorder::{LittleEndian, WriteBytesExt, ReadBytesExt};
use std::{env, io};
use std::fs::File;
use std::path;
use model::Model;

/// Block encoding/decoding logic
pub mod block;
/// Entropy compression (last stage)
pub mod entropy;
/// Compression models
pub mod model;
/// Suffix Array Construction Algorithm (SACA)
pub mod saca;

const EXTENSION: &'static str = "dark";

/// Program entry point
pub fn main() {
    env_logger::init().unwrap();
    let mut options = getopts::Options::new();
    options.optopt("m", "model", "set compression model", "dark|exp|raw|rawdc|simple|ybs");
    //options.optopt("o", "output", "set output file name", "NAME");
    options.optflag("h", "help", "print this help info");

    let args: Vec<_> = env::args().collect();
    let matches = match options.parse(&args[1..]) {
        Ok(m)   => m,
        Err(f)  => panic!(f.to_string())
    };
    if matches.opt_present("h") || matches.free.is_empty() {
        let brief = format!("Dark compressor usage:\n{} [options] input_file[.dark]", args[0]);
        println!("{}", options.usage(&brief));
        return
    }

    let model = matches.opt_str("m").unwrap_or("exp".to_string());
    info!("Using model: {}", model);
    let input_path = path::Path::new(&matches.free[0]);
    let input_ext = input_path.extension();
    if input_ext.is_some() && input_ext.unwrap() == EXTENSION {
        use block::Decoder;
        let mut in_file = match File::open(&input_path) {
            Ok(file) => io::BufReader::new(file),
            Err(e) => {
                println!("Input {:?} can not be read: {:?}", input_path, e);
                return;
            }
        };
        let mut out_path = path::PathBuf::new();
        out_path.set_file_name(input_path.file_name().unwrap());
        out_path.set_extension("orig");
        let out_file = io::BufWriter::new(File::create(&out_path).unwrap());
        // decode the block size
        let n = in_file.read_u32::<LittleEndian>().unwrap() as usize;
        info!("Decoding N: {}", n);
        // decode the block
        let (_, _, err) = match model.as_ref() {
            "bbb"   => block::raw::Decoder::new(n, model::bbb::Model  ::new()).decode(in_file, out_file),
            "dark"  => block::dc::Decoder::new(n, model::dark::Model  ::new()).decode(in_file, out_file),
            "exp"   => block::dc::Decoder::new(n, model::exp::Model   ::new()).decode(in_file, out_file),
            "raw"   => block::raw::Decoder::new(n, model::raw::Out    ::new()).decode(in_file, out_file),
            "rawdc" => block::dc::Decoder::new(n, model::raw::DcOut   ::new()).decode(in_file, out_file),
            "simple"=> block::dc::Decoder::new(n, model::simple::Model::new()).decode(in_file, out_file),
            "ybs"   => block::dc::Decoder::new(n, model::ybs::Model   ::new()).decode(in_file, out_file),
            _       => panic!("Unknown decoding model: {}", model)
        };
        err.unwrap();
    }else {
        use std::io::Read;
        use block::Encoder;
        let mut input = Vec::new();
        let mut file = match File::open(&input_path) {
            Ok(f) => f,
            Err(e) => {
                println!("Input {:?} can not be read: {}", input_path, e);
                return;
            }
        };
        let n = file.read_to_end(&mut input).unwrap();
        // write the block size
        let mut out_path = path::PathBuf::new();
        out_path.set_file_name(input_path.file_name().unwrap());
        out_path.set_extension(EXTENSION);
        let mut out_file = io::BufWriter::new(File::create(&out_path).unwrap());
        info!("Encoding N: {}", n);
        out_file.write_u32::<LittleEndian>(n as u32).unwrap();
        // encode the block
        let (_, err) = match model.as_ref() {
            "bbb"   => block::raw::Encoder::new(n, model::bbb::Model  ::new()).encode(&input, out_file),
            "dark"  => block::dc::Encoder::new(n, model::dark::Model  ::new()).encode(&input, out_file),
            "exp"   => block::dc::Encoder::new(n, model::exp::Model   ::new()).encode(&input, out_file),
            "raw"   => block::raw::Encoder::new(n, model::raw::Out    ::new()).encode(&input, out_file),
            "rawdc" => block::dc::Encoder::new(n, model::raw::DcOut   ::new()).encode(&input, out_file),
            "simple"=> block::dc::Encoder::new(n, model::simple::Model::new()).encode(&input, out_file),
            "ybs"   => block::dc::Encoder::new(n, model::ybs::Model   ::new()).encode(&input, out_file),
            _       => panic!("Unknown encoding model: {}", model)
        };
        err.unwrap();
    }
}


text:
<?php

class GenreController extends CController
{

    public function filters()
    {
        return array(
            'accessControl', // perform access control for CRUD operations
            array(
                'ext.starship.RestfullYii.filters.ERestFilter +
                REST.GET, REST.PUT, REST.POST, REST.DELETE'
            ),
        );
    }

    public function actions()
    {
        return array(
            'REST.' => 'ext.starship.RestfullYii.actions.ERestActionProvider',
        );
    }

    public function accessRules()
    {
        return array(
            array('allow', 'actions' => array('REST.GET', 'REST.PUT', 'REST.POST', 'REST.DELETE'),
                'users' => array('*'),
            ),
            array('deny', // deny all users
                'users' => array('*'),
            ),
        );
    }

    /**
     * Returns the model assosiated with this controller.
     * The assumption is that the model name matches your controller name
     * If this is not the case you should override this method in your controller
     */
    public function getModel()
    {
        if ($this->model === null) {
            /*	$modelName = str_replace('Controller', '', get_class($this));*/
            $modelName = 'Genre';
            $this->model = new $modelName;
        }
        $this->_attachBehaviors($this->model);
        return $this->model;
    }

    /**
     * Helper for loading a single model
     */
    protected function loadOneModel($id)
    {
        return $this->getModel()->with($this->nestedRelations)->findByPk($id);
    }

    public function restEvents()
    {

        /**
         * req.get.resources.render
         *
         * Called when a GET request for when a list resources is to be rendered
         *
         * @param (Array) (data) this is an array of models representing the resources
         * @param (String) (model_name) the name of the resources model
         * @param (Array) (relations) the list of relations to include with the data
         * @param (Int) (count) the count of records to return
         */
        $this->onRest('req.get.resources.render', function ($data, $model_name, $relations, $count) {
            //Handler for GET (list resources) request
            $this->setHttpStatus((($count > 0) ? 200 : 204));
            $this->renderJSON(array(
                'type' => 'rest',
                'success' => (($count > 0) ? true : false),
                'message' => (($count > 0) ? "Record(s) Found" : "No Record(s) Found"),
                'totalCount' => $count,
                'modelName' => $model_name,
                //  'relations'         => $relations,
                'data' => $data,
            ));
        });

        /**
         * req.get.resource.render
         *
         * Called when a GET request for a single resource is to be rendered
         * @param (Object) (data) this is the resources model
         * @param (String) (model_name) the name of the resources model
         * @param (Array) (relations) the list of relations to include with the data
         * @param (Int) (count) the count of records to return (will be either 1 or 0)
         */
        $this->onRest('req.get.resource.render', function ($data, $model_name, $relations, $count) {
            //Handler for GET (single resource) request
            $this->setHttpStatus((($count > 0) ? 200 : 204));
            $this->renderJSON(array(
                'type' => 'rest',
                'success' => (($count > 0) ? true : false),
                'message' => (($count > 0) ? "Record Found" : "No Record Found"),
                'totalCount' => $count,
                'modelName' => $model_name,
                'relations' => $relations,
                'data' => $data,
            ));
        });
    }
}


text:
<?php

//if (!$modx->hasPermission('quip.comment_approve')) return $modx->error->failure($modx->lexicon('access_denied'));
$config = $modx->migx->customconfigs;
$prefix = isset($config['prefix']) && !empty($config['prefix']) ? $config['prefix'] : null;
if (isset($config['use_custom_prefix']) && !empty($config['use_custom_prefix'])) {
    $prefix = isset($config['prefix']) ? $config['prefix'] : '';
}

if (!empty($config['packageName'])) {
    $packageNames = explode(',', $config['packageName']);

    if (count($packageNames) == '1') {
        //for now connecting also to foreign databases, only with one package by default possible
        $xpdo = $modx->migx->getXpdoInstanceAndAddPackage($config);
    } else {
        //all packages must have the same prefix for now!
        foreach ($packageNames as $packageName) {
            $packagepath = $modx->getOption('core_path') . 'components/' . $packageName . '/';
            $modelpath = $packagepath . 'model/';
            if (is_dir($modelpath)) {
                $modx->addPackage($packageName, $modelpath, $prefix);
            }

        }
        $xpdo = &$modx;
    }
}else{
    $xpdo = &$modx;
}
$classname = $config['classname'];

if (empty($scriptProperties['objects'])) {
    return $modx->error->failure($modx->lexicon('quip.comment_err_ns'));
}

$objectIds = explode(',',$scriptProperties['objects']);

foreach ($objectIds as $id) {
    $object = $xpdo->getObject($classname,$id);
    if ($object == null) continue;
switch ($scriptProperties['task']) {
	case 'publish':
        $object->set('published','1');
        $object->set('publishedon',strftime('%Y-%m-%d %H:%M:%S'));
        $object->set('publishedby',$modx->user->get('id'));
	    break;
	case 'delete':
        $object->set('deleted','1');
        $object->set('deletedon',strftime('%Y-%m-%d %H:%M:%S'));
        $object->set('deletedby',$modx->user->get('id'));
	    break;
	case 'unpublish':
        $object->set('unpublishedon', strftime('%Y-%m-%d %H:%M:%S'));
        $object->set('published', '0');
		$object->set('unpublishedby',$modx->user->get('id'));//feld fehlt noch
	    break;
    default:
	break;
	}

    if ($object->save() === false) {
        return $modx->error->failure($modx->lexicon('quip.comment_err_save'));
    }
}

//clear cache for all contexts
$collection = $modx->getCollection('modContext');
foreach ($collection as $context) {
    $contexts[] = $context->get('key');
}
$modx->cacheManager->refresh(array(
    'db' => array(),
    'auto_publish' => array('contexts' => $contexts),
    'context_settings' => array('contexts' => $contexts),
    'resource' => array('contexts' => $contexts),
    ));

return $modx->error->success();


text:
class IntNode
{
    private int data;
    private IntNode link;

    public IntNode(int data, IntNode link)
    {
        this.data = data;
        this.link = link;
    }

    public int getData() { return data; }
    public IntNode getLink() { return link; }
    public void setData(int data) { this.data = data; }
    public void setLink(IntNode link) { this.link = link; }
}


text:
package org.apereo.cas.web.security.authentication;

import org.apereo.cas.authorization.LdapUserAttributesToRolesAuthorizationGenerator;
import org.apereo.cas.authorization.LdapUserGroupsToRolesAuthorizationGenerator;
import org.apereo.cas.configuration.model.core.monitor.MonitorProperties;
import org.apereo.cas.util.CollectionUtils;
import org.apereo.cas.util.HttpRequestUtils;
import org.apereo.cas.util.LdapUtils;
import org.apereo.cas.util.LoggingUtils;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.apache.commons.lang3.StringUtils;
import org.ldaptive.ConnectionFactory;
import org.ldaptive.Credential;
import org.ldaptive.ReturnAttributes;
import org.ldaptive.SearchOperation;
import org.ldaptive.auth.AuthenticationRequest;
import org.ldaptive.auth.Authenticator;
import org.pac4j.core.authorization.authorizer.RequireAnyRoleAuthorizer;
import org.pac4j.core.authorization.generator.AuthorizationGenerator;
import org.pac4j.core.authorization.generator.DefaultRolesPermissionsAuthorizationGenerator;
import org.pac4j.core.context.JEEContext;
import org.pac4j.core.context.session.JEESessionStore;
import org.pac4j.core.profile.CommonProfile;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.InsufficientAuthenticationException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * This is {@link MonitorEndpointLdapAuthenticationProvider}.
 *
 * @author Misagh Moayyed
 * @since 5.1.0
 */
@Slf4j
@RequiredArgsConstructor
public class MonitorEndpointLdapAuthenticationProvider implements AuthenticationProvider {
    private final MonitorProperties.Endpoints.LdapSecurity ldapProperties;
    private final SecurityProperties securityProperties;
    private final ConnectionFactory connectionFactory;
    private final Authenticator authenticator;

    public void destroy() {
        this.connectionFactory.close();
        this.authenticator.close();
    }

    @Override
    public Authentication authenticate(final Authentication authentication) throws AuthenticationException {
        try {
            val username = authentication.getPrincipal().toString();
            val credentials = authentication.getCredentials();
            val password = Optional.ofNullable(credentials).map(Object::toString).orElse(null);
            if (StringUtils.isBlank(password)) {
                throw new IllegalArgumentException("Password cannot be blank");
            }
            LOGGER.debug("Preparing LDAP authentication request for user [{}]", username);
            val request = new AuthenticationRequest(username, new Credential(password), ReturnAttributes.ALL.value());
            LOGGER.debug("Executing LDAP authentication request for user [{}]", username);

            val response = this.authenticator.authenticate(request);
            LOGGER.debug("LDAP response: [{}]", response);

            if (response.isSuccess()) {

                val roles = securityProperties.getUser().getRoles();
                if (roles.isEmpty()) {
                    LOGGER.info("No user security roles are defined for CAS to enable authorization. User [{}] is considered authorized", username);
                    return generateAuthenticationToken(authentication, new ArrayList<>(0));
                }

                val entry = response.getLdapEntry();
                val profile = new CommonProfile();
                profile.setId(username);
                entry.getAttributes().forEach(a -> profile.addAttribute(a.getName(), a.getStringValues()));

                LOGGER.debug("Collected user profile [{}]", profile);

                val context = new JEEContext(HttpRequestUtils.getHttpServletRequestFromRequestAttributes(),
                    HttpRequestUtils.getHttpServletResponseFromRequestAttributes(),
                    new JEESessionStore());
                val authZGen = buildAuthorizationGenerator();
                authZGen.generate(context, profile);
                LOGGER.debug("Assembled user profile with roles after generating authorization claims [{}]", profile);

                val authorities = profile.getRoles()
                    .stream()
                    .map(SimpleGrantedAuthority::new)
                    .collect(Collectors.toCollection(ArrayList::new));
                LOGGER.debug("List of authorities remapped from profile roles are [{}]", authorities);
                val authorizer = new RequireAnyRoleAuthorizer(roles);
                LOGGER.debug("Executing authorization for expected admin roles [{}]", authorizer.getElements());

                if (authorizer.isAllAuthorized(context, CollectionUtils.wrap(profile))) {
                    return generateAuthenticationToken(authentication, authorities);
                }
                LOGGER.warn("User [{}] is not authorized to access the requested resource allowed to roles [{}]",
                    username, authorizer.getElements());
            } else {
                LOGGER.warn("LDAP authentication response produced no results for [{}]", username);
            }

        } catch (final Exception e) {
            LoggingUtils.error(LOGGER, e);
            throw new InsufficientAuthenticationException("Unexpected LDAP error", e);
        }
        throw new BadCredentialsException("Could not authenticate provided credentials");
    }

    private static Authentication generateAuthenticationToken(final Authentication authentication, final List<SimpleGrantedAuthority> authorities) {
        val username = authentication.getPrincipal().toString();
        val credentials = authentication.getCredentials();
        return new UsernamePasswordAuthenticationToken(username, credentials, authorities);
    }

    @Override
    public boolean supports(final Class<?> aClass) {
        return UsernamePasswordAuthenticationToken.class.isAssignableFrom(aClass);
    }

    @SuppressWarnings("java:S2095")
    private AuthorizationGenerator buildAuthorizationGenerator() {
        val ldapAuthz = this.ldapProperties.getLdapAuthz();

        if (isGroupBasedAuthorization()) {
            LOGGER.debug("Handling LDAP authorization based on groups");
            return new LdapUserGroupsToRolesAuthorizationGenerator(
                ldapAuthorizationGeneratorUserSearchOperation(this.connectionFactory),
                ldapAuthz.isAllowMultipleResults(),
                ldapAuthz.getGroupAttribute(),
                ldapAuthz.getGroupPrefix(),
                ldapAuthorizationGeneratorGroupSearchOperation(connectionFactory));
        }
        if (isUserBasedAuthorization()) {
            LOGGER.debug("Handling LDAP authorization based on attributes and roles");
            return new LdapUserAttributesToRolesAuthorizationGenerator(
                ldapAuthorizationGeneratorUserSearchOperation(this.connectionFactory),
                ldapAuthz.isAllowMultipleResults(),
                ldapAuthz.getRoleAttribute(),
                ldapAuthz.getRolePrefix());
        }
        val roles = securityProperties.getUser().getRoles();
        LOGGER.info("Could not determine authorization generator based on users or groups. Authorization will generate static roles based on [{}]", roles);
        return new DefaultRolesPermissionsAuthorizationGenerator(roles, new ArrayList<>(0));
    }

    private boolean isGroupBasedAuthorization() {
        val ldapAuthz = this.ldapProperties.getLdapAuthz();
        return StringUtils.isNotBlank(ldapAuthz.getGroupFilter()) && StringUtils.isNotBlank(ldapAuthz.getGroupAttribute());
    }

    private boolean isUserBasedAuthorization() {
        val ldapAuthz = this.ldapProperties.getLdapAuthz();
        return StringUtils.isNotBlank(ldapAuthz.getBaseDn()) && StringUtils.isNotBlank(ldapAuthz.getSearchFilter());
    }

    private SearchOperation ldapAuthorizationGeneratorUserSearchOperation(final ConnectionFactory factory) {
        val ldapAuthz = this.ldapProperties.getLdapAuthz();
        val searchOperation = LdapUtils.newLdaptiveSearchOperation(ldapAuthz.getBaseDn(), ldapAuthz.getSearchFilter(),
            new ArrayList<>(0), CollectionUtils.wrap(ldapAuthz.getRoleAttribute()));
        searchOperation.setConnectionFactory(factory);
        return searchOperation;
    }

    private SearchOperation ldapAuthorizationGeneratorGroupSearchOperation(final ConnectionFactory factory) {
        val ldapAuthz = this.ldapProperties.getLdapAuthz();
        val searchOperation = LdapUtils.newLdaptiveSearchOperation(ldapAuthz.getGroupBaseDn(), ldapAuthz.getGroupFilter(),
            new ArrayList<>(0), CollectionUtils.wrap(ldapAuthz.getGroupAttribute()));
        searchOperation.setConnectionFactory(factory);
        return searchOperation;
    }
}


text:
package org.keycloak.models.jpa.entities;

import org.keycloak.models.AuthenticationExecutionModel;

import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

/**
 * @author Bill Burke
 * @version $Revision: 1 $
 */
@Table(name="AUTHENTICATION_EXECUTION")
@Entity
public class AuthenticationExecutionEntity {
    @Id
    @Column(name="ID", length = 36)
    @Access(AccessType.PROPERTY) // we do this because relationships often fetch id, but not entity.  This avoids an extra SQL
    protected String id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "REALM_ID")
    protected RealmEntity realm;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "FLOW_ID")
    protected AuthenticationFlowEntity parentFlow;

    @Column(name="AUTHENTICATOR")
    protected String authenticator;

    @Column(name="AUTH_CONFIG")
    protected String authenticatorConfig;

    @Column(name="AUTH_FLOW_ID")
    protected String flowId;

    @Column(name="REQUIREMENT")
    protected AuthenticationExecutionModel.Requirement requirement;

    @Column(name="PRIORITY")
    protected int priority;

    @Column(name="AUTHENTICATOR_FLOW")
    private boolean autheticatorFlow;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public RealmEntity getRealm() {
        return realm;
    }

    public void setRealm(RealmEntity realm) {
        this.realm = realm;
    }

    public String getAuthenticator() {
        return authenticator;
    }

    public void setAuthenticator(String authenticator) {
        this.authenticator = authenticator;
    }

    public AuthenticationExecutionModel.Requirement getRequirement() {
        return requirement;
    }

    public void setRequirement(AuthenticationExecutionModel.Requirement requirement) {
        this.requirement = requirement;
    }

    public int getPriority() {
        return priority;
    }

    public void setPriority(int priority) {
        this.priority = priority;
    }

    public boolean isAutheticatorFlow() {
        return autheticatorFlow;
    }

    public void setAutheticatorFlow(boolean autheticatorFlow) {
        this.autheticatorFlow = autheticatorFlow;
    }

    public AuthenticationFlowEntity getParentFlow() {
        return parentFlow;
    }

    public void setParentFlow(AuthenticationFlowEntity flow) {
        this.parentFlow = flow;
    }

    public String getFlowId() {
        return flowId;
    }

    public void setFlowId(String flowId) {
        this.flowId = flowId;
    }

    public String getAuthenticatorConfig() {
        return authenticatorConfig;
    }

    public void setAuthenticatorConfig(String authenticatorConfig) {
        this.authenticatorConfig = authenticatorConfig;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null) return false;
        if (!(o instanceof AuthenticationExecutionEntity)) return false;

        AuthenticationExecutionEntity that = (AuthenticationExecutionEntity) o;

        if (!id.equals(that.getId())) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return id.hashCode();
    }

}


text:
/**
 * A maven plugin that allows integration tests to run with a mini-hbase cluster.
 */
package org.kiji.maven.plugins.hbase;