text:
package org.musicmount.server;

import java.io.File;
import java.io.FileInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.logging.Logger;

import javax.xml.bind.DatatypeConverter;

import fi.iki.elonen.NanoHTTPD;
import fi.iki.elonen.NanoHTTPD.Response;

/**
 * Experimental!
 *
 * not really working...
 */
public class MusicMountServerNano implements MusicMountServer {
	static final Logger LOGGER = Logger.getLogger(MusicMountServerNano.class.getName());

	/**
	 * Hack: nanohttpd uses available() to determine Content-Length!
	 * See https://github.com/NanoHttpd/nanohttpd/issues/51
	 */
	static class MusicMountResponseData extends FilterInputStream {
		int available;
		MusicMountResponseData(InputStream input, int available) {
			super(input);
			this.available = available;
		}
		@Override
		public synchronized int available() throws IOException {
			return available > 0 ? available : super.available();
		}
		@Override
		public synchronized int read() throws IOException {
			available = 0;
			return super.read();
		}
		@Override
		public synchronized int read(byte[] b, int off, int len) throws IOException {
			available = 0;
			return super.read(b, off, len);
		}
	}

	static class MusicMountResponse extends Response {
		final Long contentLength;

		MusicMountResponse(Response.Status status, String mimeType, String message) {
			super(status, mimeType, message);
			this.contentLength = null;
	        addHeader("Accept-Ranges", "bytes");
		}
		MusicMountResponse(Response.Status status, String mimeType, InputStream data, long contentLength) {
			super(status, mimeType, new MusicMountResponseData(data, (int)contentLength));
			this.contentLength = contentLength;
	        addHeader("Accept-Ranges", "bytes");
		}
	}

	private static String mimeType(File file) {
        int dot = file.getName().lastIndexOf('.');
        String fileType =  dot > 0 ? file.getName().substring(dot + 1).toLowerCase() : null;
        if (fileType != null) {
        	switch (fileType) {
        	case "json" :
        		return "text/json";
        	case "m4a" :
        		return "audio/mp4";
        	case "mp3" :
        		return "audio/mpeg";
        	case "jpg" :
        	case "jpeg":
        		return "image/jpeg";
        	case "png" :
        		return "image/png";
        	}
        }
        return null;
	}

	private FolderContext mountContext;
	private FolderContext musicContext;
	private String basicAuth;
	private NanoHTTPD nano;
	private final AccessLog accessLog;

	public MusicMountServerNano(AccessLog accessLog) {
		this.accessLog = accessLog;
//      Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
//			@Override
//			public void run() {
//				if (isAlive()) {
//					stop();
//				}
//			}
//		}));
	}

	@Override
	public void start(FolderContext music, FolderContext mount, int port, String user, String password) throws Exception {
		this.mountContext = mount;
		this.musicContext = music;
		this.basicAuth = user != null ? "Basic " + DatatypeConverter.printBase64Binary((user + ":" + password).getBytes()) : null;
		this.nano = new NanoHTTPD(port) {
			@Override
		    public Response serve(final IHTTPSession session) {
				final long requestTimestamp = System.currentTimeMillis();
		    	final MusicMountResponse response = MusicMountServerNano.this.serve(session.getUri(), session.getHeaders());
				if (accessLog != null) {
					final long responseTimestamp = System.currentTimeMillis();
					accessLog.log(new AccessLog.Entry() {
						@Override
						public long getResponseTimestamp() {
							return responseTimestamp;
						}
						@Override
						public int getResponseStatus() {
							return response.getStatus() != null ? response.getStatus().getRequestStatus() : 0;
						}
						@Override
						public String getResponseHeader(String header) {
							if (header.equalsIgnoreCase("Content-Length")) {
								if (response.contentLength != null) {
									return String.valueOf(response.contentLength);
								}
							}
							return null;
						}
						@Override
						public String getRequestURI() {
							return session.getUri();
						}
						@Override
						public long getRequestTimestamp() {
							return requestTimestamp;
						}
						@Override
						public String getRequestMethod() {
							return session.getMethod().name();
						}
					});
					int methodAndURIFormatLength = 50;
					StringBuilder builder = new StringBuilder();
					String uri = session.getUri();
			        int maxURILength = methodAndURIFormatLength - 1 - session.getMethod().name().length();
			        if (uri.length() > maxURILength) {
			        	uri = "..." +  uri.substring(uri.length() - maxURILength + 3);
			        }
			        String methodAndURI = String.format("%s %s", session.getMethod().name(), uri);
			        builder.append(String.format(String.format("%%-%ds", methodAndURIFormatLength), methodAndURI));
					builder.append(String.format("%4d", response.getStatus().getRequestStatus()));
			        if (response.contentLength != null) {
			        	builder.append(String.format("%,11dB", response.contentLength));
			        } else {
			            builder.append("            ");
			        }
			        LOGGER.finer(builder.toString());
				}
		    	return response;
		    }
		};
		nano.start();
	}

	@Override
	public void start(FolderContext music, MountContext mount, int port, String user, String password) throws Exception {
		throw new UnsupportedOperationException();
	}

	@Override
	public boolean isStarted() {
		return nano != null && nano.isAlive();
	}

	@Override
	public void await() {
		try {
			Thread.sleep(Long.MAX_VALUE);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} // sleep forever
	}

	@Override
	public void stop() {
		nano.stop();
		nano = null;
	}

    MusicMountResponse serve(String uri, Map<String, String> headers) {
        // strip down URI to relevant path
        uri = uri.trim().replace(File.separatorChar, '/');
        if (uri.lastIndexOf('?') >= 0) {
            uri = uri.substring(0, uri.lastIndexOf('?'));
        }

        // apply basic authentication
		if (basicAuth != null && !basicAuth.equals(headers.get("authorization"))) {
	        MusicMountResponse response = new MusicMountResponse(Response.Status.UNAUTHORIZED, NanoHTTPD.MIME_PLAINTEXT, "Needs authentication.");
			response.addHeader("WWW-Authenticate", "Basic realm=\"MusicMount\"");
			return response;
		}

        FolderContext context = null;
        if (uri.startsWith(musicContext.getPath())) {
        	context = musicContext;
        } else if (uri.startsWith(mountContext.getPath())) {
        	context = mountContext;
        } else {
            return new MusicMountResponse(Response.Status.NOT_FOUND, NanoHTTPD.MIME_PLAINTEXT, "File not found.");
        }

    	File file = new File(context.getFolder(), uri.substring(context.getPath().length()));
    	if (context == mountContext && file.isDirectory()) {
            if (!uri.endsWith("/")) {
                uri += "/";
            }
            uri += "index.json";
    		file = new File(file, "index.json");
    	}

        String mimeType = mimeType(file);

        if (mimeType == null || !file.getAbsolutePath().startsWith(context.getFolder().getAbsolutePath())) {
            return new MusicMountResponse(Response.Status.FORBIDDEN, NanoHTTPD.MIME_PLAINTEXT, "Won't serve.");
        }
        if (!file.exists()) {
            return new MusicMountResponse(Response.Status.NOT_FOUND, NanoHTTPD.MIME_PLAINTEXT, "File not found.");
        }
        if (!file.canRead()) {
            return new MusicMountResponse(Response.Status.FORBIDDEN, NanoHTTPD.MIME_PLAINTEXT, "Cannot read file.");
        }
        if (file.isDirectory()) {
            return new MusicMountResponse(Response.Status.FORBIDDEN, NanoHTTPD.MIME_PLAINTEXT, "No directory listing.");
        }

        try {
			return serveFile(file, headers, (mimeType + "; charset=utf-8"));
		} catch (IOException e) {
            return new MusicMountResponse(Response.Status.INTERNAL_ERROR, NanoHTTPD.MIME_PLAINTEXT, "Could not serve file.");
		}
    }

    MusicMountResponse serveFile(File file, Map<String, String> headers, String contentType) throws IOException {
        // Support (simple) skipping:
        long startFrom = 0;
        long endAt = -1;
        String range = headers.get("range");
        if (range != null) {
            if (range.startsWith("bytes=")) {
                range = range.substring("bytes=".length());
                int minus = range.indexOf('-');
                try {
                    if (minus > 0) {
                        startFrom = Long.parseLong(range.substring(0, minus));
                        endAt = Long.parseLong(range.substring(minus + 1));
                    }
                } catch (NumberFormatException ignored) {
                	LOGGER.warning("Could not parse range: " + range);
                }
            }
        }

        // Calculate etag
        String etag = Integer.toHexString((file.getAbsolutePath() + file.lastModified() + "" + file.length()).hashCode());

    	MusicMountResponse res;

        // Change return code and add Content-Range header when skipping is requested
        long fileLen = file.length();
        if (range != null && startFrom >= 0) {
            if (startFrom >= fileLen) {
                res = new MusicMountResponse(Response.Status.RANGE_NOT_SATISFIABLE, NanoHTTPD.MIME_PLAINTEXT, null);
                res.addHeader("Content-Range", "bytes */" + fileLen);
                res.addHeader("ETag", etag);
            } else {
                if (endAt < 0) {
                    endAt = fileLen - 1;
                }
                long newLen = endAt - startFrom + 1;
                if (newLen < 0) {
                    newLen = 0;
                }

                FileInputStream fis = new FileInputStream(file);
                long position = 0;
                while (position < startFrom) {
                	long skipped = fis.skip(startFrom - position);
                	if (skipped <= 0) {
                		fis.close();
                		throw new IOException("Could not skip to start position");
                	}
                	position += skipped;
                }

                res = new MusicMountResponse(Response.Status.PARTIAL_CONTENT, contentType, fis, newLen);
                res.addHeader("Content-Range", "bytes " + startFrom + "-" + endAt + "/" + fileLen);
                res.addHeader("ETag", etag);
            }
        } else {
            if (etag.equals(headers.get("if-none-match"))) {
                res = new MusicMountResponse(Response.Status.NOT_MODIFIED, contentType, null);
                res.addHeader("ETag", etag);
            } else {
                FileInputStream fis = new FileInputStream(file);
                res = new MusicMountResponse(Response.Status.OK, contentType, fis, fileLen);
                res.addHeader("ETag", etag);
            }
        }

        return res;
    }
}


text:
<?xml version="1.0" encoding="UTF-8"?>
<billStatus>
  <bill>
    <createDate>2017-02-14T05:20:37Z</createDate>
    <summaries>
      <billSummaries>
        <item>
          <text><![CDATA[
Improving Access to Farm Conservation Act of 2017

 This bill amends the Food Security Act of 1985 to exempt certain recipients of Department of Agriculture (USDA) conservation assistance from specified reporting requirements. The bill exempts producers or landowners eligible to participate in any USDA conservation program from reporting requirements related to subawards, executive compensation, the Universal Identifier, and the System for Award Management.
]]></text>
          <actionDate>2017-02-13</actionDate>
          <name>Introduced in Senate</name>
          <updateDate>2017-02-13T05:00:00Z</updateDate>
          <actionDesc>Introduced in Senate</actionDesc>
          <lastSummaryUpdateDate>2017-03-01T22:28:29Z</lastSummaryUpdateDate>
          <versionCode>00</versionCode>
        </item>
      </billSummaries>
    </summaries>
    <actions>
      <actionTypeCounts>
        <introducedInSenate>1</introducedInSenate>
        <referredToCommittee>1</referredToCommittee>
      </actionTypeCounts>
      <actionByCounts>
        <senate>2</senate>
      </actionByCounts>
      <item>
        <actionDate>2017-02-13</actionDate>
        <committee>
          <systemCode>ssga00</systemCode>
          <name>Homeland Security and Governmental Affairs Committee</name>
        </committee>
        <links />
        <sourceSystem>
          0
          <name>Senate</name>
        </sourceSystem>
        <text>Read twice and referred to the Committee on Homeland Security and Governmental Affairs.</text>
        <type>IntroReferral</type>
      </item>
      <item>
        <text>Introduced in Senate</text>
        <actionCode>10000</actionCode>
        <actionDate>2017-02-13</actionDate>
        <links />
        <committee />
        <sourceSystem>
          9
          <name>Library of Congress</name>
        </sourceSystem>
        <type>IntroReferral</type>
      </item>
    </actions>
    <title>Improving Access to Farm Conservation Act of 2017</title>
    <cboCostEstimates />
    <notes />
    <recordedVotes />
    <version>1.0.0</version>
    <congress>115</congress>
    <billType>S</billType>
    <cosponsors>
      <item>
        <lastName>Boozman</lastName>
        <state>AR</state>
        <isOriginalCosponsor>True</isOriginalCosponsor>
        <bioguideId>B001236</bioguideId>
        <firstName>John</firstName>
        <sponsorshipDate>2017-02-13</sponsorshipDate>
        <sponsorshipWithdrawnDate />
        <fullName>Sen. Boozman, John [R-AR]</fullName>
        <middleName />
        <party>R</party>
        <identifiers>
          <gpoId>8247</gpoId>
          <bioguideId>B001236</bioguideId>
          <lisID>1687</lisID>
        </identifiers>
      </item>
      <item>
        <lastName>Ernst</lastName>
        <state>IA</state>
        <isOriginalCosponsor>False</isOriginalCosponsor>
        <bioguideId>E000295</bioguideId>
        <firstName>Joni</firstName>
        <sponsorshipDate>2017-03-27</sponsorshipDate>
        <sponsorshipWithdrawnDate />
        <fullName>Sen. Ernst, Joni  [R-IA]</fullName>
        <middleName />
        <party>R</party>
        <identifiers>
          <lisID>2283</lisID>
          <gpoId />
          <bioguideId>E000295</bioguideId>
        </identifiers>
      </item>
      <item>
        <lastName>LEAHY</lastName>
        <state>VT</state>
        <isOriginalCosponsor>False</isOriginalCosponsor>
        <bioguideId>L000174</bioguideId>
        <firstName>PATRICK</firstName>
        <sponsorshipDate>2017-07-26</sponsorshipDate>
        <sponsorshipWithdrawnDate />
        <fullName>Sen. Leahy, Patrick J. [D-VT]</fullName>
        <middleName>JOSEPH</middleName>
        <party>D</party>
        <identifiers>
          <gpoId>8244</gpoId>
          <lisID>1383</lisID>
          <bioguideId>L000174</bioguideId>
        </identifiers>
      </item>
      <item>
        <lastName>Bennet</lastName>
        <state>CO</state>
        <isOriginalCosponsor>False</isOriginalCosponsor>
        <bioguideId>B001267</bioguideId>
        <firstName>Michael</firstName>
        <sponsorshipDate>2017-08-01</sponsorshipDate>
        <sponsorshipWithdrawnDate />
        <fullName>Sen. Bennet, Michael F. [D-CO]</fullName>
        <middleName>F.</middleName>
        <party>D</party>
        <identifiers>
          <lisID>1965</lisID>
          <gpoId>8302</gpoId>
          <bioguideId>B001267</bioguideId>
        </identifiers>
      </item>
      <item>
        <lastName>Franken</lastName>
        <state>MN</state>
        <isOriginalCosponsor>False</isOriginalCosponsor>
        <bioguideId>F000457</bioguideId>
        <firstName>Al</firstName>
        <sponsorshipDate>2017-09-07</sponsorshipDate>
        <sponsorshipWithdrawnDate />
        <fullName>Sen. Franken, Al [D-MN]</fullName>
        <middleName />
        <party>D</party>
        <identifiers>
          <lisID>1969</lisID>
          <gpoId>8339</gpoId>
          <bioguideId>F000457</bioguideId>
        </identifiers>
      </item>
      <item>
        <lastName>Murphy</lastName>
        <state>CT</state>
        <isOriginalCosponsor>False</isOriginalCosponsor>
        <bioguideId>M001169</bioguideId>
        <firstName>Christopher</firstName>
        <sponsorshipDate>2017-09-27</sponsorshipDate>
        <sponsorshipWithdrawnDate />
        <fullName>Sen. Murphy, Christopher [D-CT]</fullName>
        <middleName />
        <party>D</party>
        <identifiers>
          <bioguideId>M001169</bioguideId>
          <gpoId>7870</gpoId>
          <lisID>1837</lisID>
        </identifiers>
      </item>
    </cosponsors>
    <originChamber>Senate</originChamber>
    <committeeReports />
    <titles>
      <item>
        <parentTitleType />
        <titleType>(Extracted from GPO) Short Titles as Introduced</titleType>
        <title>Improving Access to Farm Conservation Act of 2017</title>
        <chamberCode />
        <chamberName />
      </item>
      <item>
        <parentTitleType />
        <titleType>Short Titles as Introduced</titleType>
        <title>Improving Access to Farm Conservation Act of 2017</title>
        <chamberCode />
        <chamberName />
      </item>
      <item>
        <parentTitleType />
        <titleType>Official Title as Introduced</titleType>
        <title>A bill to amend the Food Security Act of 1985 to exempt certain recipients of Department of Agriculture conservation assistance from certain reporting requirements, and for other purposes.</title>
        <chamberCode />
        <chamberName />
      </item>
      <item>
        <parentTitleType />
        <titleType>Display Title</titleType>
        <title>Improving Access to Farm Conservation Act of 2017</title>
        <chamberCode />
        <chamberName />
      </item>
    </titles>
    <updateDate>2017-09-28T05:02:13Z</updateDate>
    <relatedBills>
      <item>
        <latestAction>
          <text>Referred to the Subcommittee on Conservation and Forestry.</text>
          <actionDate>2017-03-10</actionDate>
        </latestAction>
        <latestTitle>Improving Access to Farm Conservation Act of 2017</latestTitle>
        <number>1163</number>
        <type>HR</type>
        <congress>115</congress>
        <relationshipDetails>
          <item>
            <type>Identical bill</type>
            <identifiedBy>CRS</identifiedBy>
          </item>
        </relationshipDetails>
      </item>
    </relatedBills>
    <latestAction>
      <links />
      <actionDate>2017-02-13</actionDate>
      <text>Read twice and referred to the Committee on Homeland Security and Governmental Affairs.</text>
    </latestAction>
    <introducedDate>2017-02-13</introducedDate>
    <sponsors>
      <item>
        <bioguideId>K000367</bioguideId>
        <middleName />
        <fullName>Sen. Klobuchar, Amy [D-MN]</fullName>
        <identifiers>
          <bioguideId>K000367</bioguideId>
          <gpoId>8249</gpoId>
          <lisID>1826</lisID>
        </identifiers>
        <lastName>Klobuchar</lastName>
        <party>D</party>
        <byRequestType />
        <state>MN</state>
        <firstName>Amy</firstName>
      </item>
    </sponsors>
    <policyArea>
      <name>Agriculture and Food</name>
    </policyArea>
    <laws />
    <committees>
      <billCommittees>
        <item>
          <name>Homeland Security and Governmental Affairs Committee</name>
          <activities>
            <item>
              <date>2017-02-13T23:17:04Z</date>
              <name>Referred to</name>
            </item>
          </activities>
          <chamber>Senate</chamber>
          <systemCode>ssga00</systemCode>
          <subcommittees />
          <type>Standing</type>
        </item>
      </billCommittees>
    </committees>
    <subjects>
      <billSubjects>
        <policyArea>
          <name>Agriculture and Food</name>
        </policyArea>
        <legislativeSubjects>
          <item>
            <name>Agricultural conservation and pollution</name>
          </item>
          <item>
            <name>Agricultural prices, subsidies, credit</name>
          </item>
          <item>
            <name>Government information and archives</name>
          </item>
        </legislativeSubjects>
      </billSubjects>
    </subjects>
    <amendments />
    <calendarNumbers />
    <billNumber>364</billNumber>
  </bill>
  <dublinCore xmlns:dc="http://purl.org/dc/elements/1.1/">
    <dc:format>text/xml</dc:format>
    <dc:language>EN</dc:language>
    <dc:rights>Pursuant to Title 17 Section 105 of the United States Code, this file is not subject to copyright protection and is in the public domain.</dc:rights>
    <dc:contributor>Congressional Research Service, Library of Congress</dc:contributor>
    <dc:description>This file contains bill summaries and statuses for federal legislation. A bill summary describes the most significant provisions of a piece of legislation and details the effects the legislative text may have on current law and federal programs. Bill summaries are authored by the Congressional Research Service (CRS) of the Library of Congress. As stated in Public Law 91-510 (2 USC 166 (d)(6)), one of the duties of CRS is "to prepare summaries and digests of bills and resolutions of a public general nature introduced in the Senate or House of Representatives". For more information, refer to the User Guide that accompanies this file.</dc:description>
  </dublinCore>
</billStatus>

redpajama_set_name: RedPajamaGithub


text:
package org.apache.ignite.spi.discovery.zk.internal;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Paths;
import javax.security.auth.login.Configuration;
import org.apache.ignite.IgniteCheckedException;
import org.apache.ignite.configuration.IgniteConfiguration;
import org.apache.ignite.internal.util.typedef.internal.U;
import org.apache.ignite.spi.discovery.zk.ZookeeperDiscoverySpi;
import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
import org.apache.zookeeper.client.ZooKeeperSaslClient;
import org.apache.zookeeper.server.ServerCnxnFactory;
import org.apache.zookeeper.server.ZKDatabase;
import org.apache.zookeeper.server.ZooKeeperServer;

import static org.apache.curator.test.DirectoryUtils.deleteRecursively;

/**
 * Implements methods to prepare SASL tests infrastructure: jaas.conf files, starting up ZooKeeper server,
 * clean up procedures when the test has finished etc.
 */
public abstract class ZookeeperDiscoverySpiSaslAuthAbstractTest extends GridCommonAbstractTest {
    /** */
    private File tmpDir = createTmpDir();

    /** */
    private static final String JAAS_CONF_FILE = "jaas.conf";

    /** */
    private static final String AUTH_PROVIDER = "zookeeper.authProvider.1";

    /** */
    private static final String SASL_CONFIG = "java.security.auth.login.config";

    /** */
    private long joinTimeout = 2_000;

    /** */
    private long sesTimeout = 10_000;

    /** */
    private ServerCnxnFactory serverFactory;

    /** */
    private String hostPort = "localhost:2181";

    /** */
    private int maxCnxns;

    /** {@inheritDoc} */
    @Override protected IgniteConfiguration getConfiguration(String instanceName) throws Exception {
        IgniteConfiguration cfg = super.getConfiguration(instanceName);

        ZookeeperDiscoverySpi zkSpi = new ZookeeperDiscoverySpi();

        if (joinTimeout != 0)
            zkSpi.setJoinTimeout(joinTimeout);

        zkSpi.setSessionTimeout(sesTimeout > 0 ? sesTimeout : 10_000);

        zkSpi.setZkConnectionString(hostPort);

        cfg.setDiscoverySpi(zkSpi);

        return cfg;
    }

    /** {@inheritDoc} */
    @Override protected void beforeTest() throws Exception {
        prepareJaasConfigFile();

        prepareSaslSystemProperties();

        startZooKeeperServer();
    }

    /** {@inheritDoc} */
    @Override protected void afterTest() throws Exception {
        stopZooKeeperServer();

        stopAllGrids();

        clearSaslSystemProperties();

        clearTmpDir();
    }

    /** */
    private void clearTmpDir() throws Exception {
        deleteRecursively(tmpDir);
    }

    /** */
    protected void clearSaslSystemProperties() {
        Configuration.setConfiguration(null);

        System.clearProperty(AUTH_PROVIDER);

        System.clearProperty(SASL_CONFIG);

        System.clearProperty(ZooKeeperSaslClient.LOGIN_CONTEXT_NAME_KEY);
    }

    /**
     * @throws Exception If failed.
     */
    private void prepareJaasConfigFile() throws Exception {
        U.ensureDirectory(tmpDir, "Temp directory for JAAS configuration file wasn't created", null);

        File saslConfFile = new File(tmpDir, JAAS_CONF_FILE);

        FileWriter fwriter = new FileWriter(saslConfFile);

        writeServerConfigSection(fwriter, "validPassword");

        writeClientConfigSection(fwriter, "ValidZookeeperClient", "validPassword");

        writeClientConfigSection(fwriter, "InvalidZookeeperClient", "invalidPassword");

        fwriter.close();
    }

    /** */
    private void prepareSaslSystemProperties() {
        Configuration.setConfiguration(null);

        System.setProperty(SASL_CONFIG, Paths.get(tmpDir.getPath().toString(), JAAS_CONF_FILE).toString());

        System.setProperty(AUTH_PROVIDER, "org.apache.zookeeper.server.auth.SASLAuthenticationProvider");
    }

    /** */
    private void writeClientConfigSection(FileWriter fwriter, String clientName, String pass) throws IOException {
        fwriter.write(clientName + "{\n" +
            "       org.apache.zookeeper.server.auth.DigestLoginModule required\n" +
            "       username=\"zkUser\"\n" +
            "       password=\"" + pass + "\";\n" +
            "};" + "\n");
    }

    /** */
    private void writeServerConfigSection(FileWriter fwriter, String pass) throws IOException {
        fwriter.write("Server {\n" +
            "          org.apache.zookeeper.server.auth.DigestLoginModule required\n" +
            "          user_zkUser=\"" + pass + "\";\n" +
            "};\n");
    }

    /** */
    private File createTmpDir() {
        File jaasConfDir = Paths.get(System.getProperty("java.io.tmpdir"), "zk_disco_spi_test").toFile();

        try {
            U.ensureDirectory(jaasConfDir, "", null);
        }
        catch (IgniteCheckedException e) {
            // ignored
        }

        return jaasConfDir;
    }

    /** */
    private void stopZooKeeperServer() throws Exception {
        shutdownServerInstance(serverFactory);
        serverFactory = null;
    }

    /** */
    private void shutdownServerInstance(ServerCnxnFactory factory)
    {
        if (factory != null) {
            ZKDatabase zkDb = null;
            {
                ZooKeeperServer zs = getServer(factory);
                if (zs != null)
                    zkDb = zs.getZKDatabase();
            }
            factory.shutdown();
            try {
                if (zkDb != null)
                    zkDb.close();
            } catch (IOException ie) {
                // ignore
            }
        }
    }

    /** */
    private ZooKeeperServer getServer(ServerCnxnFactory fac) {
        ZooKeeperServer zs = U.field(fac, "zkServer");

        return zs;
    }

    /** */
    private void startZooKeeperServer() throws Exception {
        serverFactory = createNewServerInstance(serverFactory, hostPort,
            maxCnxns);
        startServerInstance(tmpDir, serverFactory);
    }

    /** */
    private ServerCnxnFactory createNewServerInstance(
        ServerCnxnFactory factory, String hostPort, int maxCnxns)
        throws IOException {
        final int port = getPort(hostPort);

        if (factory == null)
            factory = ServerCnxnFactory.createFactory(port, maxCnxns);

        return factory;
    }

    /** */
    private void startServerInstance(File dataDir,
        ServerCnxnFactory factory) throws IOException,
        InterruptedException {
        ZooKeeperServer zks = new ZooKeeperServer(dataDir, dataDir, 3000);
        factory.startup(zks);
    }

    /** */
    private int getPort(String hostPort) {
        String[] split = hostPort.split(":");
        String portstr = split[split.length-1];
        String[] pc = portstr.split("/");

        if (pc.length > 1)
            portstr = pc[0];

        return Integer.parseInt(portstr);
    }
}


text:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.cnodejs.android.md">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.CAMERA" />

    <application
        android:name="org.cnodejs.android.md.app.AppController"
        android:allowBackup="true"
        android:fullBackupContent="true"
        android:icon="@mipmap/ic_launcher"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:label="@string/app_name"
        android:theme="@style/AppThemeLight">

        <activity
            android:name="org.cnodejs.android.md.ui.activity.LaunchActivity"
            android:label="@string/app_name"
            android:theme="@style/AppThemeLaunch"
            android:screenOrientation="portrait"
            android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name="org.cnodejs.android.md.ui.activity.MainActivity"
            android:launchMode="singleTask"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:configChanges="keyboard|keyboardHidden|orientation|screenSize" />

        <activity
            android:name="org.cnodejs.android.md.ui.activity.LoginActivity"
            android:label="@string/login"
            android:screenOrientation="portrait"
            android:configChanges="keyboard|keyboardHidden|orientation|screenSize" />

        <activity
            android:name="org.cnodejs.android.md.ui.activity.ScanQRCodeActivity"
            android:label="@string/scan_qr_code"
            android:screenOrientation="portrait"
            android:configChanges="keyboard|keyboardHidden|orientation|screenSize" />

        <activity
            android:name="org.cnodejs.android.md.ui.activity.TopicActivity"
            android:label="@string/topic"
            android:screenOrientation="portrait"
            android:configChanges="keyboard|keyboardHidden|orientation|screenSize" />

        <activity
            android:name="org.cnodejs.android.md.ui.activity.TopicCompatActivity"
            android:label="@string/topic"
            android:screenOrientation="portrait"
            android:configChanges="keyboard|keyboardHidden|orientation|screenSize" />

        <activity
            android:name="org.cnodejs.android.md.ui.activity.CreateTopicActivity"
            android:label="@string/new_topic"
            android:screenOrientation="portrait"
            android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
            android:windowSoftInputMode="stateHidden|adjustResize" />

        <activity
            android:name="org.cnodejs.android.md.ui.activity.MarkdownPreviewActivity"
            android:label="@string/markdown_preview"
            android:screenOrientation="portrait"
            android:configChanges="keyboard|keyboardHidden|orientation|screenSize" />

        <activity
            android:name="org.cnodejs.android.md.ui.activity.ImagePreviewActivity"
            android:label="@string/image_preview"
            android:screenOrientation="portrait"
            android:configChanges="keyboard|keyboardHidden|orientation|screenSize" />

        <activity
            android:name="org.cnodejs.android.md.ui.activity.UserDetailActivity"
            android:label="@string/user_detail"
            android:screenOrientation="portrait"
            android:configChanges="keyboard|keyboardHidden|orientation|screenSize" />

        <activity
            android:name="org.cnodejs.android.md.ui.activity.NotificationActivity"
            android:label="@string/notification"
            android:screenOrientation="portrait"
            android:configChanges="keyboard|keyboardHidden|orientation|screenSize" />

        <activity
            android:name="org.cnodejs.android.md.ui.activity.NotificationCompatActivity"
            android:label="@string/notification"
            android:screenOrientation="portrait"
            android:configChanges="keyboard|keyboardHidden|orientation|screenSize" />

        <activity
            android:name="org.cnodejs.android.md.ui.activity.SettingActivity"
            android:label="@string/setting"
            android:screenOrientation="portrait"
            android:configChanges="keyboard|keyboardHidden|orientation|screenSize" />

        <activity
            android:name="org.cnodejs.android.md.ui.activity.ModifyTopicSignActivity"
            android:label="@string/modify_topic_sign"
            android:screenOrientation="portrait"
            android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
            android:windowSoftInputMode="stateHidden|adjustResize" />

        <activity
            android:name="org.cnodejs.android.md.ui.activity.AboutActivity"
            android:label="@string/about"
            android:screenOrientation="portrait"
            android:configChanges="keyboard|keyboardHidden|orientation|screenSize" />

        <activity
            android:name="org.cnodejs.android.md.ui.activity.LicenseActivity"
            android:label="@string/open_source_license"
            android:screenOrientation="portrait"
            android:configChanges="keyboard|keyboardHidden|orientation|screenSize" />

        <activity
            android:name="org.cnodejs.android.md.ui.activity.CrashLogActivity"
            android:label="@string/app_crash"
            android:screenOrientation="portrait"
            android:configChanges="keyboard|keyboardHidden|orientation|screenSize" />

        <activity
            android:name="org.cnodejs.android.md.ui.activity.StandardLinkRouterActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
            <intent-filter android:label="@string/app_name">
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data
                    android:scheme="https"
                    android:host="cnodejs.org"
                    android:pathPrefix="/topic/" />
            </intent-filter>
            <intent-filter android:label="@string/app_name">
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data
                    android:scheme="https"
                    android:host="cnodejs.org"
                    android:pathPrefix="/user/" />
            </intent-filter>
        </activity>

    </application>

</manifest>


text:
package types

// StatsAggregation type.
//
// https://github.com/elastic/elasticsearch-specification/blob/4ca0cc05d3ae3fa06c2cd7be91905b656a474334/specification/_types/aggregations/metric.ts#L145-L145
type StatsAggregation struct {
	Field   *string  `json:"field,omitempty"`
	Format  *string  `json:"format,omitempty"`
	Missing *Missing `json:"missing,omitempty"`
	Script  *Script  `json:"script,omitempty"`
}

// NewStatsAggregation returns a StatsAggregation.
func NewStatsAggregation() *StatsAggregation {
	r := &StatsAggregation{}

	return r
}


text:
<?php

namespace YaoiTests\PHPUnit\Http\Client;

use Yaoi\Test\PHPUnit\TestCase;

class MirrorTestBase extends TestCase
{
    public function setUp() {
        if (empty(TestCase::$settings['envHttpMirrorServer'])) {
            $this->markTestSkipped('HttpMirror tests disabled');
            return;
        }

    }

}


text:
require "spec_helper"

describe Humanoid::Extensions::Hash::CriteriaHelpers do

  describe "#expand_complex_criteria" do

    before do
      @hash = {:age.gt => 40, :title => "Title"}
    end

    it "expands complex criteria to form a valid `where` hash" do
      @hash.expand_complex_criteria.should == {:age => {"$gt" => 40}, :title => "Title"}
    end

  end

end


text:
FOUNDATION_EXPORT double Pods_QuickShotUtils_ExampleVersionNumber;
FOUNDATION_EXPORT const unsigned char Pods_QuickShotUtils_ExampleVersionString[];


text:
package jeex.ejb.beans;

import jeex.ejb.domain.Discussion;
import jeex.ejb.domain.Topic;
import jeex.ejb.entities.DiscussionEntity;

import javax.persistence.TypedQuery;
import java.util.List;

public class DiscussionManagerBean extends ManagerBean<Discussion, DiscussionEntity, Long>
      implements DiscussionManagerLocal, DiscussionManagerRemote {

   public DiscussionManagerBean() {
      super(Discussion.class, DiscussionEntity.class);
   }

   @Override
   public List<Discussion> listByTopic(Topic topic) {
      TypedQuery<DiscussionEntity> q = createQuery(DiscussionEntity.findByTopic);
      q.setParameter("topicId", topic.getId());
      return query(q);
   }
}


text:
package io.leopard.web.mvc.test;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ser.DefaultSerializerProvider;
import com.fasterxml.jackson.databind.ser.std.NullSerializer;

public class CustomObjectMapper extends ObjectMapper {

	private static final long serialVersionUID = 1L;

	public CustomObjectMapper() {
		DefaultSerializerProvider sp = new DefaultSerializerProvider.Impl();
		sp.setNullValueSerializer(NullSerializer.instance);
		this.setSerializerProvider(sp);
	}

}


text:
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

package com.cloud.bridge.util;

import java.text.SimpleDateFormat;

import java.text.DateFormat;
import java.text.FieldPosition;
import java.text.ParsePosition;
import java.util.Date;
import java.util.Calendar;
import java.util.TimeZone;

/**
 * Format and parse a date string which is expected to be in ISO 8601 DateTimeFormat especially for
 * use in XML documents.
 * An example is for use with GMTDateTimeUserType to provide parsing of DateTime format strings into
 * accurate Java Date representations based on UTC.
 * The purpose of this class is to allow the creation of accurate date time representations following
 * the ISO 8601 format YYYY-MM-DDThh:MM:ss
 * using the letter "T" as the date/time separator
 * This representation may be immediately followed by a "Z" (Zulu i.e. at zero offset from GMT) to indicate UTC
 * or, otherwise, to a specific time zone.  If a time zone (tz) is encoded then this is held as the difference
 * between the local time in the tz and UCT, expressed as a positive(+) or negative(-) offset (hhMM) appended
 * to the format.
 * The default case holds no tz information and assumes that a date time representation referenced to Zulu
 * (i.e. zero offset from GMT) is required.  When formatting an existing Date transform it into the Zulu timezone
 * so that it is explicitly at GMT with zero offset.  This provides the default representation for the encoding
 * of AWS datetime values.
 * For testing, it may be useful to note that, as at 2012, a city whose time is always in the Zulu timezone is
 * Reykjavik, Iceland.
 * The parsing and formatting methods provided by this class are GMT-referenced and locale insensitive.
 */

public class ISO8601SimpleDateTimeFormat extends SimpleDateFormat {

	private static final long serialVersionUID = 7388260211953189670L;

	protected static TimeZone defaultTimeZone = TimeZone.getTimeZone("Z");

	  /**
	   * Construct a new ISO8601DateTimeFormat using the default time zone.
	   * Initializes calendar inherited from java.text.DateFormat.calendar
	   *
	   */
	  public ISO8601SimpleDateTimeFormat() {
	    setCalendar(Calendar.getInstance(defaultTimeZone));
	  }

	  /**
	   * Construct a new ISO8601DateTimeFormat using a specific time zone.
	   * Initializes calendar inherited from java.text.DateFormat.calendar
	   * @param tz The time zone used to format and parse the date.
	   */
	  public ISO8601SimpleDateTimeFormat(TimeZone tz) {
	    setCalendar(Calendar.getInstance(tz));
	  }

      /**
       * The abstract superclass DateFormat has two business methods to override.  These are
       * public StringBuffer format(Date arg0, StringBuffer arg1, FieldPosition arg2)
       * public Date parse(String arg0, ParsePosition arg1)
       */

	  /**
	   * @see DateFormat#format(Date, StringBuffer, FieldPosition)
	   */
      @Override
	  public StringBuffer format(Date date, StringBuffer stringBuffer, FieldPosition fieldPosition) {
         calendar.setTime(date);
         calendar.setTimeZone(defaultTimeZone);
         writeYYYYMM(stringBuffer);
         stringBuffer.append('T');
         writehhMMss(stringBuffer);
         stringBuffer.append(".000Z");
         return stringBuffer;
	  }

	  /* @see DateFormat#parse(String, ParsePosition)
	   * Assigns the values of YYYY-MM-DDThh:MM:ss fields between the delimiters of dateString
	   * or a near approximation using the superclass SimpleDateFormat if not formatted exactly as ISO8601
	   */
	  @Override
	  public Date parse(String dateString, ParsePosition pos) {
		ParsePosition startpos = pos;
	    int p = pos.getIndex();
	    // Assign value of YYYY
	    try {
	      int YYYY = Integer.valueOf(dateString.substring(p, p + 4)).intValue();
	      p += 4;
	      if (dateString.charAt(p) != '-') {
	        throw new IllegalArgumentException();
	      }
	      p++;
	    // Assign value of MM
	      int MM = Integer.valueOf(dateString.substring(p, p + 2)).intValue() - 1;
	      p += 2;
	      if (dateString.charAt(p) != '-') {
		        throw new IllegalArgumentException();
	      }
	      p++;
	    // Asign value of dd
	      int DD = Integer.valueOf(dateString.substring(p, p + 2)).intValue();
	      p += 2;
	      if (dateString.charAt(p) != 'T') {
		        throw new IllegalArgumentException();
	      }
	      p++;
	    // Assign value of hh
	      int hh = Integer.valueOf(dateString.substring(p, p + 2)).intValue();
	      p += 2;
	      if (dateString.charAt(p) != ':') {
		        throw new IllegalArgumentException();
	      }
	      p++;
       // Assign value of mm
	      int mm = Integer.valueOf(dateString.substring(p, p + 2)).intValue();
	      p += 2;
	      if (dateString.charAt(p) != ':') {
		        throw new IllegalArgumentException();
	      }
		  p++;
	   // Assign value of ss
	      int ss = 0;
	   //   if (p < dateString.length() && dateString.charAt(p) == ':') {
       // Allow exactly two ss digits after final : delimiter
	        ss = Integer.valueOf(dateString.substring(p, p + 2)).intValue();
	        p += 2;
		// Set calendar inherited from java.text.DateFormat.calendar
	      calendar.set(YYYY, MM, DD, hh, mm, ss);
	      calendar.set(Calendar.MILLISECOND, 0); // Since java.util.Date holds none, zeroize milliseconds
		// process appended timezone if any or Z otherwise
	    p = parseTZ(p, dateString);
	    }
	    catch (IllegalArgumentException ex) {
	      super.setTimeZone(TimeZone.getTimeZone("GMT"));
	      super.applyPattern("yyyy-MM-dd HH:mm:ss");
	      return super.parse(dateString, startpos);
	    }
	    catch (Exception ex) {
		super.setTimeZone(TimeZone.getTimeZone("GMT"));
		return super.parse(dateString, startpos);    // default pattern
	    }
	    finally
	    {
	     pos.setIndex(p);
	    }
	   // Return the Calendar instance's Date representation of its value
		return calendar.getTime();
	 }

		  /**
		   * Write the time zone string.  Remember that in the default TimeZone there is no offset and the
		   * convention to supply the TimeZone string constant "Z" is applicable.  As an optimization there
		   * is no need to call this method where the default TimeZone has been set.
		   * @param stringBuffer The buffer to append the time zone.
		   */
		  protected final void writeTZ(StringBuffer stringBuffer) {
		    int offset =
		      calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET);
		    if (offset == 0) {
		      stringBuffer.append('Z');
		    }
		    else {
		      int offsetHour = offset / 3600000;
		      int offsetMin = (offset % 3600000) / 60000;
		      if (offset >= 0) {
		        stringBuffer.append('+');
		      }
		      else {
		        stringBuffer.append('-');
		        offsetHour = 0 - offsetHour;
		        offsetMin = 0 - offsetMin;
		      }
		      appendInt(stringBuffer, offsetHour, 2);
		      stringBuffer.append(':');
		      appendInt(stringBuffer, offsetMin, 2);
		    }
		  }

		  /**
		   * Write hour, minutes, and seconds.
		   * @param stringBuffer The buffer to append the string.
		   */
		  protected final void writehhMMss(StringBuffer stringBuffer) {
		    int hh = calendar.get(Calendar.HOUR_OF_DAY);
		    appendInt(stringBuffer, hh, 2);
		    stringBuffer.append(':');

		    int mm = calendar.get(Calendar.MINUTE);
		    appendInt(stringBuffer, mm, 2);
		    stringBuffer.append(':');

		    int ss = calendar.get(Calendar.SECOND);
		    appendInt(stringBuffer, ss, 2);
		  }

		  /**
		   * Write YYYY, and MMs.
		   * @param stringBuffer The buffer to append the string.
		   */
		  protected final void writeYYYYMM(StringBuffer stringBuffer) {
		    int YYYY = calendar.get(Calendar.YEAR);
		    appendInt(stringBuffer, YYYY, 4);

		    String MM;
		    switch (calendar.get(Calendar.MONTH)) {
		      case Calendar.JANUARY :
		        MM = "-01-";
		        break;
		      case Calendar.FEBRUARY :
		        MM = "-02-";
		        break;
		      case Calendar.MARCH :
		        MM = "-03-";
		        break;
		      case Calendar.APRIL :
		        MM = "-04-";
		        break;
		      case Calendar.MAY :
		        MM = "-05-";
		        break;
		      case Calendar.JUNE :
		        MM = "-06-";
		        break;
		      case Calendar.JULY :
		        MM = "-07-";
		        break;
		      case Calendar.AUGUST :
		        MM = "-08-";
		        break;
		      case Calendar.SEPTEMBER :
		        MM = "-09-";
		        break;
		      case Calendar.OCTOBER :
		        MM = "-10-";
		        break;
		      case Calendar.NOVEMBER :
		        MM = "-11-";
		        break;
		      case Calendar.DECEMBER :
		        MM = "-12-";
		        break;
		      default :
		        MM = "-NA-";
		        break;
		    }
		    stringBuffer.append(MM);

		    int DD = calendar.get(Calendar.DAY_OF_MONTH);
		    appendInt(stringBuffer, DD, 2);
		  }

		  /**
		   * Write an integer value with leading zeros.
		   * @param stringBuffer The buffer to append the string.
		   * @param value The value to write.
		   * @param length The length of the string to write.
		   */
		  protected final void appendInt(StringBuffer stringBuffer, int value, int length) {
		    int len1 = stringBuffer.length();
		    stringBuffer.append(value);
		    int len2 = stringBuffer.length();
		    for (int i = len2; i < len1 + length; ++i) {
		      stringBuffer.insert(len1, '0');
		    }
		  }

		  /**
		   * Parse the time zone.
		   * @param i The position to start parsing.
		   * @param dateString The dateString to parse.
		   * @return The position after parsing has finished.
		   */

		  protected final int parseTZ(int i, String dateString)  {
		    if (i < dateString.length()) {
		      // check and handle the zone/dst offset
		      int offset = 0;
		      if (dateString.charAt(i) == 'Z') {
		        offset = 0;
		        i++;
		      }
		      else {
		        int sign = 1;
		        if (dateString.charAt(i) == '-') {
		          sign = -1;
		        }
		        else if (dateString.charAt(i) != '+') {
		          throw new IllegalArgumentException();
		        }
		        i++;

		        int offsetHour = Integer.valueOf(dateString.substring(i, i + 2)).intValue();
		        i += 2;

		        if (dateString.charAt(i) != ':') {
		          throw new IllegalArgumentException();
		        }
		        i++;

		        int offsetMin = Integer.valueOf(dateString.substring(i, i + 2)).intValue();
		        i += 2;
		        offset = ((offsetHour * 60) + offsetMin) * 60000 * sign;
		      }
		      int offsetCal =
		        calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET);

		      calendar.add(Calendar.MILLISECOND, offsetCal - offset);
		    }
		    return i;

		  }

		  @Override
		     public int hashCode() {
		         return (calendar.get(2)+calendar.get(16));
		         // numberFormat (used by superclass) will not distribute, so use calendar third and penultimate fields
		         // (i.e. dd and ss) instead
		         // in Java 6 (Calendar.FIELD_COUNT-1) returns 16
		     }

		  @Override
		     public boolean equals(Object obj) {
		         if (this == obj) return true;
		         if (obj == null || getClass() != obj.getClass()) return false;
		         DateFormat other = (DateFormat) obj;
		         Calendar otherCalendar = other.getCalendar();
		         for (int i = 0; i < Calendar.FIELD_COUNT; i++)
		        	 if ( calendar.get(i) != (otherCalendar.get(i)) ) return false;
		         return true;
		     }

	}


text:
<?php

namespace Acme\DemoBundle\Controller;

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

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;

use Acme\DemoBundle\Entity\Contact;

use Ivory\GoogleMap\Overlays\Animation;
use Ivory\GoogleMap\Overlays\Marker;

/**
 * Index controller.
 *
 */
class ExceptionController extends Controller
{
    /**
     * @return type
     */
    public function exceptionAction(Request $request)
    {
        return $this->render('AcmeDemoBundle:Index:exception.html.twig');
    }
}


text:
// Define gulp before we start
var gulp = require('gulp');

// Define Sass and the autoprefixer
var sass = require('gulp-sass');
var prefix = require('gulp-autoprefixer');
var nodemon = require('gulp-nodemon');

var environment;
if (process.platform === 'linux') {
  environment = 'production';
} else {
  environment = 'development';
}
// This is an object which defines paths for the styles.
// Can add paths for javascript or images for example
// The folder, files to look for and destination are all required for sass
var paths = {
  styles: {
    src: './content/themes/casper/assets/sass',
    files: './content/themes/casper/assets/sass/input.scss',
    watch: './content/themes/casper/assets/sass/**/*.scss',
    dest: './content/themes/casper/assets/css'
  }
}

// A display error function, to format and make custom errors more uniform
// Could be combined with gulp-util or npm colors for nicer output
var displayError = function(error) {

  // Initial building up of the error
  var errorString = '[' + error.plugin + ']';
  errorString += ' ' + error.message.replace("\n",''); // Removes new line at the end

  // If the error contains the filename or line number add it to the string
  if(error.fileName)
    errorString += ' in ' + error.fileName;

  if(error.lineNumber)
    errorString += ' on line ' + error.lineNumber;

  // This will output an error like the following:
  // [gulp-sass] error message in file_name on line 1
  console.error(errorString);
}

// Setting up the sass task
gulp.task('sass', function (){
  // Taking the path from the above object
  gulp.src(paths.styles.files)
  // Sass options - make the output compressed and add the source map
  // Also pull the include path from the paths object
  .pipe(sass({
    outputStyle: 'compressed',
    sourceComments: 'map',
    includePaths : [paths.styles.src]
  }))
  // If there is an error, don't stop compiling but use the custom displayError function
  .on('error', function(err){
    displayError(err);
  })
  // Pass the compiled sass through the prefixer with defined
  .pipe(prefix(
    'last 2 version', 'safari 5', 'ie 8', 'ie 9', 'opera 12.1', 'ios 6', 'android 4'
  ))
  // Funally put the compiled sass into a css file
  .pipe(gulp.dest(paths.styles.dest))
});

gulp.task('develop', function () {
  nodemon({
    script: 'server.js',
    ext: 'html js',
    env: { 'NODE_ENV': environment }
  })
    .on('restart', function () {
      console.log('restarted!')
    })
});

// This is the default task - which is run when `gulp` is run
// The tasks passed in as an array are run before the tasks within the function
gulp.task('default', ['develop', 'sass'], function() {
  // Watch the files in the paths object, and when there is a change, fun the functions in the array
  gulp.watch(paths.styles.watch, ['sass'])
  // Also when there is a change, display what file was changed, only showing the path after the 'sass folder'
  .on('change', function(evt) {
    console.log(
      '[watcher] File ' + evt.path.replace(/.*(?=sass)/,'') + ' was ' + evt.type + ', compiling...'
    );
  });
});


text:
package etri.sdn.controller.module.routing;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.projectfloodlight.openflow.types.OFPort;

import etri.sdn.controller.MessageContext;
import etri.sdn.controller.module.devicemanager.IDevice;
import etri.sdn.controller.module.devicemanager.SwitchPort;

public class RoutingDecision implements IRoutingDecision {

    protected RoutingAction action;
    protected Integer wildcards;
    protected SwitchPort srcPort;
    protected IDevice srcDevice;
    protected List<IDevice> destDevices;
    protected List<SwitchPort> broadcastIntertfaces;

    public RoutingDecision(long swDipd,
                                  OFPort inPort,
                                  IDevice srcDevice,
                                  RoutingAction action) {
        this.srcPort = new SwitchPort(swDipd, inPort);
        this.srcDevice = srcDevice;
        this.destDevices =
                Collections.synchronizedList(new ArrayList<IDevice>());
        this.broadcastIntertfaces =
                Collections.synchronizedList(new ArrayList<SwitchPort>());
        this.action = action;
        this.wildcards = null;
    }

    @Override
    public RoutingAction getRoutingAction() {
        return this.action;
    }

    @Override
    public void setRoutingAction(RoutingAction action) {
        this.action = action;
    }

    @Override
    public SwitchPort getSourcePort() {
        return this.srcPort;
    }

    @Override
    public IDevice getSourceDevice() {
        return this.srcDevice;
    }

    @Override
    public List<IDevice> getDestinationDevices() {
        return this.destDevices;
    }

    @Override
    public void addDestinationDevice(IDevice d) {
        if (!destDevices.contains(d)) {
            destDevices.add(d);
        }
    }

    @Override
    public void setMulticastInterfaces(List<SwitchPort> lspt) {
        this.broadcastIntertfaces = lspt;
    }

    @Override
    public List<SwitchPort> getMulticastInterfaces() {
        return this.broadcastIntertfaces;
    }

    @Override
    public Integer getWildcards() {
        return this.wildcards;
    }

    @Override
    public void setWildcards(Integer wildcards) {
        this.wildcards = wildcards;
    }

    @Override
    public void addToContext(MessageContext cntx) {
    	cntx.put(MessageContext.ROUTING_DECISION, this);
    }

    public String toString() {
        return "action " + action +
               " wildcard " +
               ((wildcards == null) ? null : "0x"+Integer.toHexString(wildcards.intValue()));
    }
}


text:
import assert = require("power-assert")
import request = require('superagent');
import nock = require('nock');
import sinon = require('sinon');
import TagAction = require('../../../src/actions/tag_action');
import Api = require('../../../src/api');

(nock ? describe : describe.skip)('stores/tag_action', () => {
    afterEach(() => nock.cleanAll());

    describe('Index', () => {
        var response = {
            status: 200,
            body: <any>[{
                name: 'tag1',
                count: 2
            }]
        };

        beforeEach(() => nock('http://localhost')
            .get('/api/tags')
            .reply(response.status, response.body)
        );

        it('should dispatch with "tag:set" and the tag data', (done) => {
            var action = new TagAction.Index();
            var spy = sinon.spy(action, 'dispatch');
            action.handler();
            setTimeout(() => {
                assert(spy.withArgs("tag:set", response.body).calledOnce);
                done();
            }, 10);
        });

        context('when not authenticated', () => {
            before(() => {
                response = {
                    status: 401,
                    body: <any>{
                        message: 'Not authenticated'
                    }
                };
            });

            it('should dispatch with "session:unset" action and the error message', (done) => {
                var action = new TagAction.Index();
                var spy = sinon.spy(action, 'dispatch');
                action.handler();
                setTimeout(() => {
                    assert(spy.withArgs("session:unset", response.body).calledOnce);
                    done();
                }, 10);
            });
        });
    });

    describe('Entry', () => {
        var response = {
            status: 200,
            body: <any>[{
                video: {video_id: 'sm9'}
            }]
        };
        var name = 'tag1';

        beforeEach(() => nock('http://localhost')
            .get(`/api/tags/${name}/entries`)
            .reply(response.status, response.body)
        );

        it('should dispatch with "entry:set" and the entry data', (done) => {
            var action = new TagAction.Entry();
            var spy = sinon.spy(action, 'dispatch');
            action.handler(name);
            setTimeout(() => {
                assert(spy.withArgs("entry:set", {records: response.body}).calledOnce);
                done();
            }, 10);
        });
    });
});


text:
package generic

import (
	"context"
	"database/sql"
	"fmt"
	"regexp"
	"strconv"
	"strings"
	"sync"
	"time"

	"github.com/Rican7/retry/backoff"
	"github.com/Rican7/retry/strategy"
	"github.com/sirupsen/logrus"
)

const (
	defaultMaxIdleConns = 2 // copied from database/sql
)

var (
	columns = "kv.id AS theid, kv.name, kv.created, kv.deleted, kv.create_revision, kv.prev_revision, kv.lease, kv.value, kv.old_value"
	revSQL  = `
		SELECT MAX(rkv.id) AS id
		FROM kine AS rkv`

	compactRevSQL = `
		SELECT MAX(crkv.prev_revision) AS prev_revision
		FROM kine AS crkv
		WHERE crkv.name = 'compact_rev_key'`

	idOfKey = `
		AND
		mkv.id <= ? AND
		mkv.id > (
			SELECT MAX(ikv.id) AS id
			FROM kine AS ikv
			WHERE
				ikv.name = ? AND
				ikv.id <= ?)`

	listSQL = fmt.Sprintf(`
		SELECT (%s), (%s), %s
		FROM kine AS kv
		JOIN (
			SELECT MAX(mkv.id) AS id
			FROM kine AS mkv
			WHERE
				mkv.name LIKE ?
				%%s
			GROUP BY mkv.name) maxkv
	    ON maxkv.id = kv.id
		WHERE
			  (kv.deleted = 0 OR ?)
		ORDER BY kv.id ASC
		`, revSQL, compactRevSQL, columns)
)

type Stripped string

func (s Stripped) String() string {
	str := strings.ReplaceAll(string(s), "\n", "")
	return regexp.MustCompile("[\t ]+").ReplaceAllString(str, " ")
}

type ErrRetry func(error) bool
type TranslateErr func(error) error

type ConnectionPoolConfig struct {
	MaxIdle     int           // zero means defaultMaxIdleConns; negative means 0
	MaxOpen     int           // <= 0 means unlimited
	MaxLifetime time.Duration // maximum amount of time a connection may be reused
}

type Generic struct {
	sync.Mutex

	LockWrites            bool
	LastInsertID          bool
	DB                    *sql.DB
	GetCurrentSQL         string
	GetRevisionSQL        string
	RevisionSQL           string
	ListRevisionStartSQL  string
	GetRevisionAfterSQL   string
	CountSQL              string
	AfterSQL              string
	DeleteSQL             string
	CompactSQL            string
	UpdateCompactSQL      string
	InsertSQL             string
	FillSQL               string
	InsertLastInsertIDSQL string
	Retry                 ErrRetry
	TranslateErr          TranslateErr
}

func q(sql, param string, numbered bool) string {
	if param == "?" && !numbered {
		return sql
	}

	regex := regexp.MustCompile(`\?`)
	n := 0
	return regex.ReplaceAllStringFunc(sql, func(string) string {
		if numbered {
			n++
			return param + strconv.Itoa(n)
		}
		return param
	})
}

func (d *Generic) Migrate(ctx context.Context) {
	var (
		count     = 0
		countKV   = d.queryRow(ctx, "SELECT COUNT(*) FROM key_value")
		countKine = d.queryRow(ctx, "SELECT COUNT(*) FROM kine")
	)

	if err := countKV.Scan(&count); err != nil || count == 0 {
		return
	}

	if err := countKine.Scan(&count); err != nil || count != 0 {
		return
	}

	logrus.Infof("Migrating content from old table")
	_, err := d.execute(ctx,
		`INSERT INTO kine(deleted, create_revision, prev_revision, name, value, created, lease)
					SELECT 0, 0, 0, kv.name, kv.value, 1, CASE WHEN kv.ttl > 0 THEN 15 ELSE 0 END
					FROM key_value kv
						WHERE kv.id IN (SELECT MAX(kvd.id) FROM key_value kvd GROUP BY kvd.name)`)
	if err != nil {
		logrus.Errorf("Migration failed: %v", err)
	}
}

func configureConnectionPooling(connPoolConfig ConnectionPoolConfig, db *sql.DB, driverName string) {
	// behavior copied from database/sql - zero means defaultMaxIdleConns; negative means 0
	if connPoolConfig.MaxIdle < 0 {
		connPoolConfig.MaxIdle = 0
	} else if connPoolConfig.MaxIdle == 0 {
		connPoolConfig.MaxIdle = defaultMaxIdleConns
	}

	logrus.Infof("Configuring %s database connection pooling: maxIdleConns=%d, maxOpenConns=%d, connMaxLifetime=%s", driverName, connPoolConfig.MaxIdle, connPoolConfig.MaxOpen, connPoolConfig.MaxLifetime)
	db.SetMaxIdleConns(connPoolConfig.MaxIdle)
	db.SetMaxOpenConns(connPoolConfig.MaxOpen)
	db.SetConnMaxLifetime(connPoolConfig.MaxLifetime)
}

func openAndTest(driverName, dataSourceName string) (*sql.DB, error) {
	db, err := sql.Open(driverName, dataSourceName)
	if err != nil {
		return nil, err
	}

	for i := 0; i < 3; i++ {
		if err := db.Ping(); err != nil {
			db.Close()
			return nil, err
		}
	}

	return db, nil
}

func Open(ctx context.Context, driverName, dataSourceName string, connPoolConfig ConnectionPoolConfig, paramCharacter string, numbered bool) (*Generic, error) {
	var (
		db  *sql.DB
		err error
	)

	for i := 0; i < 300; i++ {
		db, err = openAndTest(driverName, dataSourceName)
		if err == nil {
			break
		}

		logrus.Errorf("failed to ping connection: %v", err)
		select {
		case <-ctx.Done():
			return nil, ctx.Err()
		case <-time.After(time.Second):
		}
	}

	configureConnectionPooling(connPoolConfig, db, driverName)

	return &Generic{
		DB: db,

		GetRevisionSQL: q(fmt.Sprintf(`
			SELECT
			0, 0, %s
			FROM kine AS kv
			WHERE kv.id = ?`, columns), paramCharacter, numbered),

		GetCurrentSQL:        q(fmt.Sprintf(listSQL, ""), paramCharacter, numbered),
		ListRevisionStartSQL: q(fmt.Sprintf(listSQL, "AND mkv.id <= ?"), paramCharacter, numbered),
		GetRevisionAfterSQL:  q(fmt.Sprintf(listSQL, idOfKey), paramCharacter, numbered),

		CountSQL: q(fmt.Sprintf(`
			SELECT (%s), COUNT(c.theid)
			FROM (
				%s
			) c`, revSQL, fmt.Sprintf(listSQL, "")), paramCharacter, numbered),

		AfterSQL: q(fmt.Sprintf(`
			SELECT (%s), (%s), %s
			FROM kine AS kv
			WHERE
				kv.name LIKE ? AND
				kv.id > ?
			ORDER BY kv.id ASC`, revSQL, compactRevSQL, columns), paramCharacter, numbered),

		DeleteSQL: q(`
			DELETE FROM kine AS kv
			WHERE kv.id = ?`, paramCharacter, numbered),

		UpdateCompactSQL: q(`
			UPDATE kine
			SET prev_revision = ?
			WHERE name = 'compact_rev_key'`, paramCharacter, numbered),

		InsertLastInsertIDSQL: q(`INSERT INTO kine(name, created, deleted, create_revision, prev_revision, lease, value, old_value)
			values(?, ?, ?, ?, ?, ?, ?, ?)`, paramCharacter, numbered),

		InsertSQL: q(`INSERT INTO kine(name, created, deleted, create_revision, prev_revision, lease, value, old_value)
			values(?, ?, ?, ?, ?, ?, ?, ?) RETURNING id`, paramCharacter, numbered),

		FillSQL: q(`INSERT INTO kine(id, name, created, deleted, create_revision, prev_revision, lease, value, old_value)
			values(?, ?, ?, ?, ?, ?, ?, ?, ?)`, paramCharacter, numbered),
	}, err
}

func (d *Generic) query(ctx context.Context, sql string, args ...interface{}) (*sql.Rows, error) {
	logrus.Tracef("QUERY %v : %s", args, Stripped(sql))
	return d.DB.QueryContext(ctx, sql, args...)
}

func (d *Generic) queryRow(ctx context.Context, sql string, args ...interface{}) *sql.Row {
	logrus.Tracef("QUERY ROW %v : %s", args, Stripped(sql))
	return d.DB.QueryRowContext(ctx, sql, args...)
}

func (d *Generic) execute(ctx context.Context, sql string, args ...interface{}) (result sql.Result, err error) {
	if d.LockWrites {
		d.Lock()
		defer d.Unlock()
	}

	wait := strategy.Backoff(backoff.Linear(100 + time.Millisecond))
	for i := uint(0); i < 20; i++ {
		logrus.Tracef("EXEC (try: %d) %v : %s", i, args, Stripped(sql))
		result, err = d.DB.ExecContext(ctx, sql, args...)
		if err != nil && d.Retry != nil && d.Retry(err) {
			wait(i)
			continue
		}
		return result, err
	}
	return
}

func (d *Generic) GetCompactRevision(ctx context.Context) (int64, error) {
	var id int64
	row := d.queryRow(ctx, compactRevSQL)
	err := row.Scan(&id)
	if err == sql.ErrNoRows {
		return 0, nil
	}
	return id, err
}

func (d *Generic) SetCompactRevision(ctx context.Context, revision int64) error {
	logrus.Tracef("SETCOMPACTREVISION %v", revision)
	_, err := d.execute(ctx, d.UpdateCompactSQL, revision)
	return err
}

func (d *Generic) Compact(ctx context.Context, revision int64) (int64, error) {
	logrus.Tracef("COMPACT %v", revision)
	res, err := d.execute(ctx, d.CompactSQL, revision, revision)
	if err != nil {
		return 0, err
	}
	return res.RowsAffected()
}

func (d *Generic) GetRevision(ctx context.Context, revision int64) (*sql.Rows, error) {
	return d.query(ctx, d.GetRevisionSQL, revision)
}

func (d *Generic) DeleteRevision(ctx context.Context, revision int64) error {
	logrus.Tracef("DELETEREVISION %v", revision)
	_, err := d.execute(ctx, d.DeleteSQL, revision)
	return err
}

func (d *Generic) ListCurrent(ctx context.Context, prefix string, limit int64, includeDeleted bool) (*sql.Rows, error) {
	sql := d.GetCurrentSQL
	if limit > 0 {
		sql = fmt.Sprintf("%s LIMIT %d", sql, limit)
	}
	return d.query(ctx, sql, prefix, includeDeleted)
}

func (d *Generic) List(ctx context.Context, prefix, startKey string, limit, revision int64, includeDeleted bool) (*sql.Rows, error) {
	if startKey == "" {
		sql := d.ListRevisionStartSQL
		if limit > 0 {
			sql = fmt.Sprintf("%s LIMIT %d", sql, limit)
		}
		return d.query(ctx, sql, prefix, revision, includeDeleted)
	}

	sql := d.GetRevisionAfterSQL
	if limit > 0 {
		sql = fmt.Sprintf("%s LIMIT %d", sql, limit)
	}
	return d.query(ctx, sql, prefix, revision, startKey, revision, includeDeleted)
}

func (d *Generic) Count(ctx context.Context, prefix string) (int64, int64, error) {
	var (
		rev sql.NullInt64
		id  int64
	)

	row := d.queryRow(ctx, d.CountSQL, prefix, false)
	err := row.Scan(&rev, &id)
	return rev.Int64, id, err
}

func (d *Generic) CurrentRevision(ctx context.Context) (int64, error) {
	var id int64
	row := d.queryRow(ctx, revSQL)
	err := row.Scan(&id)
	if err == sql.ErrNoRows {
		return 0, nil
	}
	return id, err
}

func (d *Generic) After(ctx context.Context, prefix string, rev, limit int64) (*sql.Rows, error) {
	sql := d.AfterSQL
	if limit > 0 {
		sql = fmt.Sprintf("%s LIMIT %d", sql, limit)
	}
	return d.query(ctx, sql, prefix, rev)
}

func (d *Generic) Fill(ctx context.Context, revision int64) error {
	_, err := d.execute(ctx, d.FillSQL, revision, fmt.Sprintf("gap-%d", revision), 0, 1, 0, 0, 0, nil, nil)
	return err
}

func (d *Generic) IsFill(key string) bool {
	return strings.HasPrefix(key, "gap-")
}

func (d *Generic) Insert(ctx context.Context, key string, create, delete bool, createRevision, previousRevision int64, ttl int64, value, prevValue []byte) (id int64, err error) {
	if d.TranslateErr != nil {
		defer func() {
			if err != nil {
				err = d.TranslateErr(err)
			}
		}()
	}

	cVal := 0
	dVal := 0
	if create {
		cVal = 1
	}
	if delete {
		dVal = 1
	}

	if d.LastInsertID {
		row, err := d.execute(ctx, d.InsertLastInsertIDSQL, key, cVal, dVal, createRevision, previousRevision, ttl, value, prevValue)
		if err != nil {
			return 0, err
		}
		return row.LastInsertId()
	}

	row := d.queryRow(ctx, d.InsertSQL, key, cVal, dVal, createRevision, previousRevision, ttl, value, prevValue)
	err = row.Scan(&id)
	return id, err
}


text:
#ifndef VICE_DRV_RAW_H
#define VICE_DRV_RAW_H

extern int drv_raw_init_resources(void);
extern void drv_raw_init(void);

#endif


text:
import { Component, EventEmitter, Input, Output } from '@angular/core';

import { Villain } from '../../core';

@Component({
  selector: 'app-villain-list',
  templateUrl: './villain-list.component.html',
  styleUrls: ['./villain-list.component.scss']
})
export class VillainListComponent {
  @Input() villains: Villain[];
  @Input() selectedVillain: Villain;
  @Output() deleted = new EventEmitter<Villain>();
  @Output() selected = new EventEmitter<Villain>();

  byId(villain: Villain) {
    return villain.id;
  }

  onSelect(villain: Villain) {
    this.selected.emit(villain);
  }

  deleteVillain(villain: Villain) {
    this.deleted.emit(villain);
  }
}


text:
/*
 *  THIS FILE IS AUTOGENERATED. DO NOT MODIFY IT; ALL CHANGES WILL BE LOST IN
 *  VAIN.
 *
 *  @generated
 */
#pragma once

#include <exception>
#include <memory>

#include <mcrouter/lib/network/RpcStatsContext.h>
#include <mcrouter/lib/network/ThriftTransport.h>
#include <mcrouter/McrouterFiberContext.h>
#include <thrift/lib/cpp/TApplicationException.h>
#include <thrift/lib/cpp/transport/TTransportException.h>
#include <thrift/lib/cpp2/async/RequestChannel.h>

#include "mcrouter/lib/carbon/example/gen/gen-cpp2/HelloGoodbyeAsyncClient.h"

namespace facebook {
namespace memcache {

template <>
class ThriftTransportMethods<hellogoodbye::HelloGoodbyeRouterInfo> : public ThriftTransportUtil {
 public:
  ThriftTransportMethods() = default;
  virtual ~ThriftTransportMethods() override = default;

folly::Try<apache::thrift::RpcResponseComplete<hellogoodbye::GoodbyeReply>> sendSyncHelper(
    typename hellogoodbye::HelloGoodbyeRouterInfo::RouteHandleAsyncClient* thriftClient,
    const hellogoodbye::GoodbyeRequest& request,
    apache::thrift::RpcOptions& rpcOptions,
    RpcStatsContext* rpcStatsContext = nullptr) {
  bool needServerLoad = mcrouter::fiber_local<hellogoodbye::HelloGoodbyeRouterInfo>::getThriftServerLoadEnabled();
  if (UNLIKELY(needServerLoad)) {
    rpcOptions.setWriteHeader(kLoadHeader, kDefaultLoadCounter);
  }
  if (UNLIKELY(request.getCryptoAuthToken().has_value())) {
    rpcOptions.setWriteHeader(
        std::string{carbon::MessageCommon::kCryptoAuthTokenHeader}, request.getCryptoAuthToken().value());
  }
  rpcOptions.setContextPropMask(0);

#ifndef LIBMC_FBTRACE_DISABLE
  traceRequest(request, rpcOptions);
#endif
  auto reply = thriftClient->sync_complete_goodbye(
      std::move(rpcOptions), request);
  if (rpcStatsContext && reply.hasValue()) {
      auto& stats = reply->responseContext.rpcSizeStats;
      rpcStatsContext->requestBodySize = stats.requestSerializedSizeBytes;
      rpcStatsContext->replySizeBeforeCompression = stats.responseSerializedSizeBytes;
      rpcStatsContext->replySizeAfterCompression = stats.responseWireSizeBytes;
      if (UNLIKELY(needServerLoad && reply->responseContext.serverLoad)) {
        rpcStatsContext->serverLoad = ServerLoad(
            static_cast<int32_t>(*reply->responseContext.serverLoad));
      }
  }
#ifndef LIBMC_FBTRACE_DISABLE
  traceResponse(request, reply);
#endif
  return reply;
}

folly::Try<apache::thrift::RpcResponseComplete<hellogoodbye::HelloReply>> sendSyncHelper(
    typename hellogoodbye::HelloGoodbyeRouterInfo::RouteHandleAsyncClient* thriftClient,
    const hellogoodbye::HelloRequest& request,
    apache::thrift::RpcOptions& rpcOptions,
    RpcStatsContext* rpcStatsContext = nullptr) {
  bool needServerLoad = mcrouter::fiber_local<hellogoodbye::HelloGoodbyeRouterInfo>::getThriftServerLoadEnabled();
  if (UNLIKELY(needServerLoad)) {
    rpcOptions.setWriteHeader(kLoadHeader, kDefaultLoadCounter);
  }
  if (UNLIKELY(request.getCryptoAuthToken().has_value())) {
    rpcOptions.setWriteHeader(
        std::string{carbon::MessageCommon::kCryptoAuthTokenHeader}, request.getCryptoAuthToken().value());
  }
  rpcOptions.setContextPropMask(0);

#ifndef LIBMC_FBTRACE_DISABLE
  traceRequest(request, rpcOptions);
#endif
        rpcOptions.setWriteHeader("shardId", folly::to<std::string>(*request.shardId_ref()));
        rpcOptions.setWriteHeader("message", folly::to<std::string>(*request.message_ref()));
        rpcOptions.setWriteHeader("priority", folly::to<std::string>(*request.priority_ref()));
  auto reply = thriftClient->sync_complete_hello(
      std::move(rpcOptions), request);
  if (rpcStatsContext && reply.hasValue()) {
      auto& stats = reply->responseContext.rpcSizeStats;
      rpcStatsContext->requestBodySize = stats.requestSerializedSizeBytes;
      rpcStatsContext->replySizeBeforeCompression = stats.responseSerializedSizeBytes;
      rpcStatsContext->replySizeAfterCompression = stats.responseWireSizeBytes;
      if (UNLIKELY(needServerLoad && reply->responseContext.serverLoad)) {
        rpcStatsContext->serverLoad = ServerLoad(
            static_cast<int32_t>(*reply->responseContext.serverLoad));
      }
  }
#ifndef LIBMC_FBTRACE_DISABLE
  traceResponse(request, reply);
#endif
  return reply;
}

folly::Try<apache::thrift::RpcResponseComplete<McVersionReply>> sendSyncHelper(
    typename hellogoodbye::HelloGoodbyeRouterInfo::RouteHandleAsyncClient* thriftClient,
    const McVersionRequest& request,
    apache::thrift::RpcOptions& rpcOptions,
    RpcStatsContext* rpcStatsContext = nullptr) {
  bool needServerLoad = mcrouter::fiber_local<hellogoodbye::HelloGoodbyeRouterInfo>::getThriftServerLoadEnabled();
  if (UNLIKELY(needServerLoad)) {
    rpcOptions.setWriteHeader(kLoadHeader, kDefaultLoadCounter);
  }
  if (UNLIKELY(request.getCryptoAuthToken().has_value())) {
    rpcOptions.setWriteHeader(
        std::string{carbon::MessageCommon::kCryptoAuthTokenHeader}, request.getCryptoAuthToken().value());
  }
  rpcOptions.setContextPropMask(0);

#ifndef LIBMC_FBTRACE_DISABLE
  traceRequest(request, rpcOptions);
#endif
  auto reply = thriftClient->sync_complete_mcVersion(
      std::move(rpcOptions), request);
  if (rpcStatsContext && reply.hasValue()) {
      auto& stats = reply->responseContext.rpcSizeStats;
      rpcStatsContext->requestBodySize = stats.requestSerializedSizeBytes;
      rpcStatsContext->replySizeBeforeCompression = stats.responseSerializedSizeBytes;
      rpcStatsContext->replySizeAfterCompression = stats.responseWireSizeBytes;
      if (UNLIKELY(needServerLoad && reply->responseContext.serverLoad)) {
        rpcStatsContext->serverLoad = ServerLoad(
            static_cast<int32_t>(*reply->responseContext.serverLoad));
      }
  }
#ifndef LIBMC_FBTRACE_DISABLE
  traceResponse(request, reply);
#endif
  return reply;
}

 protected:
  std::optional<apache::thrift::Client<hellogoodbye::thrift::HelloGoodbye>> thriftClient_;
};

template <>
class ThriftTransport<hellogoodbye::HelloGoodbyeRouterInfo> : public ThriftTransportMethods<hellogoodbye::HelloGoodbyeRouterInfo>,
                                       public ThriftTransportBase {
 public:
  ThriftTransport(folly::EventBase& eventBase, ConnectionOptions options)
      : ThriftTransportBase(eventBase, std::move(options)) {}
  ThriftTransport(folly::VirtualEventBase& eventBase, ConnectionOptions options)
      : ThriftTransportBase(eventBase.getEventBase(), std::move(options)) {}
  ~ThriftTransport() override {
    resetClient();
  }

  void setFlushList(FlushList* flushList) override final {
    flushList_ = flushList;
    if (thriftClient_) {
      auto* channel = static_cast<apache::thrift::RocketClientChannel*>(
          thriftClient_->getChannel());
      channel->setFlushList(flushList_);
    }
  }

  hellogoodbye::GoodbyeReply sendSync(
      const hellogoodbye::GoodbyeRequest& request,
      std::chrono::milliseconds timeout,
      RpcStatsContext* rpcStatsContext = nullptr) {

    return sendSyncImpl([this, &request, timeout, rpcStatsContext] {
      auto* thriftClient = getThriftClient();

      if (connectionOptions_.thriftCompression) {
        // Enable compression for this request type
        apache::thrift::CodecConfig codec;
        codec.zstdConfig_ref() = apache::thrift::ZstdCompressionCodecConfig();
        apache::thrift::CompressionConfig compressionConfig;
        if (connectionOptions_.thriftCompressionThreshold > 0) {
          compressionConfig.compressionSizeLimit_ref() =
              connectionOptions_.thriftCompressionThreshold;
        }
        compressionConfig.codecConfig_ref() = std::move(codec);
        auto* channel = static_cast<apache::thrift::RocketClientChannel*>(thriftClient_->getChannel());
        channel->setDesiredCompressionConfig(std::move(compressionConfig));
      }

      if (LIKELY(thriftClient != nullptr)) {
        auto rpcOptions = getRpcOptions(timeout);
        return sendSyncHelper(thriftClient, request, rpcOptions, rpcStatsContext);
      } else {
        return folly::Try<apache::thrift::RpcResponseComplete<hellogoodbye::GoodbyeReply>>(
            folly::make_exception_wrapper<apache::thrift::transport::TTransportException>(
              apache::thrift::transport::TTransportException::NOT_OPEN,
              "Error creating thrift client."));
      }
    });
  }

  hellogoodbye::HelloReply sendSync(
      const hellogoodbye::HelloRequest& request,
      std::chrono::milliseconds timeout,
      RpcStatsContext* rpcStatsContext = nullptr) {

    return sendSyncImpl([this, &request, timeout, rpcStatsContext] {
      auto* thriftClient = getThriftClient();

      if (connectionOptions_.thriftCompression) {
        // Disable compression for this request type
        apache::thrift::CompressionConfig compressionConfig;
        auto* channel = static_cast<apache::thrift::RocketClientChannel*>(thriftClient_->getChannel());
        channel->setDesiredCompressionConfig(std::move(compressionConfig));
      }

      if (LIKELY(thriftClient != nullptr)) {
        auto rpcOptions = getRpcOptions(timeout);
        return sendSyncHelper(thriftClient, request, rpcOptions, rpcStatsContext);
      } else {
        return folly::Try<apache::thrift::RpcResponseComplete<hellogoodbye::HelloReply>>(
            folly::make_exception_wrapper<apache::thrift::transport::TTransportException>(
              apache::thrift::transport::TTransportException::NOT_OPEN,
              "Error creating thrift client."));
      }
    });
  }

  McVersionReply sendSync(
      const McVersionRequest& request,
      std::chrono::milliseconds timeout,
      RpcStatsContext* rpcStatsContext = nullptr) {

    return sendSyncImpl([this, &request, timeout, rpcStatsContext] {
      auto* thriftClient = getThriftClient();
      if (LIKELY(thriftClient != nullptr)) {
        auto rpcOptions = getRpcOptions(timeout);
        return sendSyncHelper(thriftClient, request, rpcOptions, rpcStatsContext);
      } else {
        return folly::Try<apache::thrift::RpcResponseComplete<McVersionReply>>(
            folly::make_exception_wrapper<apache::thrift::transport::TTransportException>(
              apache::thrift::transport::TTransportException::NOT_OPEN,
              "Error creating thrift client."));
      }
    });
  }

 private:
  FlushList* flushList_{nullptr};

  apache::thrift::Client<hellogoodbye::thrift::HelloGoodbye>* getThriftClient() {
    if (UNLIKELY(!thriftClient_)) {
      thriftClient_ = createThriftClient<apache::thrift::Client<hellogoodbye::thrift::HelloGoodbye>>();
      if (flushList_) {
        auto* channel = static_cast<apache::thrift::RocketClientChannel*>(
            thriftClient_->getChannel());
        channel->setFlushList(flushList_);
      }
    }
    if (LIKELY(thriftClient_.has_value())) {
      return &thriftClient_.value();
    }
    return nullptr;
  }

  void resetClient() override final {
    if (thriftClient_) {
      if (auto channel = thriftClient_->getChannel()) {
        // Reset the callback to avoid the following cycle:
        //  ~ThriftAsyncClient() -> ~RocketClientChannel() ->
        //  channelClosed() -> ~ThriftAsyncClient()
        channel->setCloseCallback(nullptr);
      }
      thriftClient_.reset();
    }
  }
};

} // namespace memcache
} // namespace facebook


text:
package watcher

import (
	"fmt"
	"sync"
	"time"

	k8sv1 "k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/fields"
	"k8s.io/apimachinery/pkg/labels"
	"k8s.io/client-go/tools/cache"
	kvv1 "kubevirt.io/api/core/v1"
	"kubevirt.io/client-go/kubecli"
	"kubevirt.io/client-go/log"

	"kubevirt.io/kubevirt/pkg/controller"
	objUtils "kubevirt.io/kubevirt/tools/perfscale-load-generator/object"
)

const (
	informerTimeout = time.Minute
)

type ObjListWatcher struct {
	sync.Mutex

	// runningObjs is a set to avoid recount duplicated events of an object
	runningObjs            map[string]bool
	desiredObjRunningCount int
	updateChannel          chan bool

	virtCli      kubecli.KubevirtClient
	listOpts     metav1.ListOptions
	ResourceKind string

	informer    cache.SharedInformer
	stopChannel chan struct{}
}

func NewObjListWatcher(virtCli kubecli.KubevirtClient, resourceKind string, objCount int, listOpts metav1.ListOptions) *ObjListWatcher {
	w := &ObjListWatcher{
		stopChannel:            make(chan struct{}),
		updateChannel:          make(chan bool),
		runningObjs:            make(map[string]bool),
		virtCli:                virtCli,
		ResourceKind:           resourceKind,
		desiredObjRunningCount: objCount,
		listOpts:               listOpts,
	}

	labelSelector, err := labels.Parse(w.listOpts.LabelSelector)
	if err != nil {
		panic(err)
	}
	objListWatcher := controller.NewListWatchFromClient(
		virtCli.RestClient(),
		w.ResourceKind,
		k8sv1.NamespaceAll,
		fields.Everything(),
		labelSelector)

	w.informer = cache.NewSharedInformer(objListWatcher, nil, 0)
	w.stopChannel = make(chan struct{})
	w.informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
		UpdateFunc: func(oldObj, newObj interface{}) {
			w.handleUpdate(newObj)
		},
		DeleteFunc: func(obj interface{}) {
			w.handleDeleted(obj)
		},
	})

	return w
}

func (w *ObjListWatcher) Run() {
	go w.informer.Run(w.stopChannel)
	timeoutCh := make(chan struct{})
	timeoutTimer := time.AfterFunc(informerTimeout, func() {
		close(timeoutCh)
	})
	defer timeoutTimer.Stop()
	if !cache.WaitForCacheSync(timeoutCh, w.informer.HasSynced) {
		panic(fmt.Errorf("watcher timed out waiting for caches to sync"))
	}
	// waiting 1s to prevent the watcher miss events from the test
	time.Sleep(1 * time.Second)
}

func (w *ObjListWatcher) Stop() {
	close(w.stopChannel)
}

func (w *ObjListWatcher) handleUpdate(obj interface{}) {
	switch w.ResourceKind {
	case objUtils.VMIResource:
		vmi, ok := obj.(*kvv1.VirtualMachineInstance)
		if !ok {
			log.Log.V(2).Errorf("Could not convert VirtualMachineInstance obj: %v", w.ResourceKind)
		}
		if vmi.Status.Phase == kvv1.Running {
			w.addObj(w.getID(vmi))
		}
		if vmi.Status.Phase == kvv1.Succeeded || vmi.Status.Phase == kvv1.Failed {
			log.Log.V(4).Errorf("VirtualMachineInstance obj: %s is a final state, waiting for garbage collection", fmt.Sprintf("%s/%s", vmi.Namespace, vmi.Name))
		}

	case objUtils.VMResource:
		vm, ok := obj.(*kvv1.VirtualMachine)
		if !ok {
			log.Log.V(2).Errorf("Could not convert VirtualMachine obj: %v", w.ResourceKind)
		}
		if vm.Status.Ready {
			w.addObj(w.getID(vm))
		}

	case objUtils.VMIReplicaSetResource:
		replicaSet, ok := obj.(*kvv1.VirtualMachineInstanceReplicaSet)
		if !ok {
			log.Log.V(2).Errorf("Could not convert VirtualMachineInstanceReplicaSet obj: %v", w.ResourceKind)
		}
		w.addObj(w.getID(replicaSet))

	default:
		log.Log.V(2).Errorf("Watcher does not support object type %s", w.ResourceKind)
		return
	}
}

func (w *ObjListWatcher) handleDeleted(obj interface{}) {
	switch w.ResourceKind {
	case objUtils.VMResource:
		vm, ok := obj.(*kvv1.VirtualMachine)
		if !ok {
			log.Log.V(2).Errorf("Could not convert VirtualMachine obj: %v", w.ResourceKind)
			return
		}
		isGarbageCollected, err := w.isGarbageCollected(vm)
		if err != nil {
			log.Log.V(2).Errorf("Could not get VirtualMachine: %v, err: %#v", fmt.Sprintf("%s/%s", vm.Namespace, vm.Name), err)
		}
		if !isGarbageCollected {
			log.Log.V(4).Errorf("VirtualMachine obj: %v has not been garbage collected yet", fmt.Sprintf("%s/%s", vm.Namespace, vm.Name))
			return
		}
		w.deleteObj(w.getID(vm))

	case objUtils.VMIReplicaSetResource:
		replicaSet, ok := obj.(*kvv1.VirtualMachineInstanceReplicaSet)
		if !ok {
			log.Log.V(2).Errorf("Could not convert VirtualMachineInstanceReplicaSet obj: %v", w.ResourceKind)
			return
		}
		isGarbageCollected, err := w.isGarbageCollected(replicaSet)
		if err != nil {
			log.Log.V(2).Errorf("Could not get VirtualMachineInstanceReplicaSet: %v, err: %#v", fmt.Sprintf("%s/%s", replicaSet.Namespace, replicaSet.Name), err)
		}
		if !isGarbageCollected {
			log.Log.V(4).Errorf("VirtualMachineInstanceReplicaSet obj: %v has not been garbage collected yet", fmt.Sprintf("%s/%s", replicaSet.Namespace, replicaSet.Name))
			return
		}
		w.deleteObj(w.getID(replicaSet))
	case objUtils.VMIResource:
		vmi, ok := obj.(*kvv1.VirtualMachineInstance)
		if !ok {
			log.Log.V(2).Errorf("Could not convert VirtualMachineInstance obj: %v", w.ResourceKind)
			return
		}
		isGarbageCollected, err := w.isGarbageCollected(vmi)
		if err != nil {
			log.Log.V(2).Errorf("Could not get VirtualMachineInstance: %v, err: %#v", fmt.Sprintf("%s/%s", vmi.Namespace, vmi.Name), err)
		}
		if !isGarbageCollected {
			log.Log.V(4).Errorf("VirtualMachineInstance obj: %v has not been garbage collected yet", fmt.Sprintf("%s/%s", vmi.Namespace, vmi.Name))
			return
		}
		w.deleteObj(w.getID(vmi))

	default:
		return
	}
}

func (w *ObjListWatcher) WaitRunning(timeout time.Duration) error {
	if w.getRunningCount() == w.desiredObjRunningCount {
		return nil
	}
	for {
		select {
		case <-time.After(timeout):
			return fmt.Errorf("timeout after %s waiting for objects be running", timeout)

		case <-w.updateChannel:
			count := w.getRunningCount()
			if count == w.desiredObjRunningCount {
				return nil
			}
			log.Log.V(6).Infof("Waiting %d %v to be Running", (w.desiredObjRunningCount - count), w.ResourceKind)
		}
	}
}

func (w *ObjListWatcher) WaitDeletion(timeout time.Duration) error {
	runningCount := w.getRunningCount()
	if runningCount == 0 || runningCount == w.desiredObjRunningCount {
		return nil
	}
	for {
		select {
		case <-time.After(timeout):
			return fmt.Errorf("timeout after %s waiting for objects be deleted", timeout)

		case <-w.updateChannel:
			count := w.getRunningCount()
			if count == 0 {
				return nil
			}
			log.Log.V(6).Infof("Still %d %v waiting to be Garbage Collected", count, w.ResourceKind)
		}
	}
}

func (w *ObjListWatcher) addObj(id string) {
	w.Lock()
	w.runningObjs[id] = true
	w.Unlock()
	w.updateChannel <- true
}

func (w *ObjListWatcher) deleteObj(id string) {
	w.Lock()
	delete(w.runningObjs, id)
	w.Unlock()
	w.updateChannel <- true
}

func (w *ObjListWatcher) getRunningCount() int {
	var count int
	w.Lock()
	count = len(w.runningObjs)
	w.Unlock()
	return count
}

func (w *ObjListWatcher) isGarbageCollected(obj interface{}) (bool, error) {
	objKey, err := controller.KeyFunc(obj)
	if err != nil {
		log.Log.V(2).Errorf("Error getting obj key for %s err: %v", obj, err)
		return false, err
	}
	_, exists, err := w.informer.GetStore().GetByKey(objKey)
	switch {
	case err != nil:
		log.Log.V(2).Errorf("Error getting obj %s from cache err: %v", obj, err)
		return false, err
	case !exists:
		log.Log.V(4).Errorf("Obj %s not found in cache", objKey)
		return true, nil
	default:
		log.Log.V(4).Errorf("Obj %s found in cache", objKey)
		return false, nil
	}
}

func (w *ObjListWatcher) getID(obj metav1.Object) string {
	return fmt.Sprintf("%s/%s/%s", w.ResourceKind, obj.GetNamespace(), obj.GetName())
}