Example usage for com.google.common.base Preconditions checkNotNull

List of usage examples for com.google.common.base Preconditions checkNotNull

Introduction

In this page you can find the example usage for com.google.common.base Preconditions checkNotNull.

Prototype

public static <T> T checkNotNull(T reference, @Nullable Object errorMessage) 

Source Link

Document

Ensures that an object reference passed as a parameter to the calling method is not null.

Usage

From source file:fr.xebia.demo.amazon.aws.AmazonAwsSesEmailVerifier.java

public static void main(String[] args) throws Exception {
    InputStream credentialsAsStream = Thread.currentThread().getContextClassLoader()
            .getResourceAsStream("AwsCredentials.properties");
    Preconditions.checkNotNull(credentialsAsStream,
            "File 'AwsCredentials.properties' NOT found in the classpath");
    AWSCredentials awsCredentials = new PropertiesCredentials(credentialsAsStream);

    AmazonSimpleEmailService ses = new AmazonSimpleEmailServiceClient(awsCredentials);

    URL emailsToVerifyURL = Thread.currentThread().getContextClassLoader().getResource("emails-to-verify.txt");
    List<String> emailsToVerify = Resources.readLines(emailsToVerifyURL, Charsets.ISO_8859_1);

    for (String emailToVerify : emailsToVerify) {
        System.out.println(emailToVerify);
        Thread.sleep(10 * 1000);//from  www.ja v  a  2  s.c om
        ses.verifyEmailAddress(new VerifyEmailAddressRequest().withEmailAddress(emailToVerify));
    }
}

From source file:org.b1.pack.cli.Main.java

public static void main(String[] args) throws Exception {
    boolean debugMode = Boolean.getBoolean(Main.class.getName() + ".debug");
    try {//from w  w w .j a v a  2  s .  co m
        long startTime = System.currentTimeMillis();
        ArgSet argSet = new ArgSet(args);
        String command = argSet.getCommand();
        if (argSet.isHelp()) {
            printHelp();
            Preconditions.checkArgument(command == null, "Command ignored");
        } else {
            Preconditions.checkNotNull(command, "No command");
            Preconditions.checkNotNull(COMAND_MAP.get(command), "Invalid command: %s", command).execute(argSet);
        }
        if (debugMode) {
            System.out.println("Executed in " + (System.currentTimeMillis() - startTime) + " ms.");
        }
    } catch (Exception e) {
        printError(e);
        if (debugMode) {
            throw e;
        } else {
            System.exit(1);
        }
    }
}

From source file:com.cloudbees.api.Main.java

public static void main(String[] args) throws Exception {

    File beesCredentialsFile = new File(System.getProperty("user.home"), ".bees/bees.config");
    Preconditions.checkArgument(beesCredentialsFile.exists(), "File %s not found", beesCredentialsFile);
    Properties beesCredentials = new Properties();
    beesCredentials.load(new FileInputStream(beesCredentialsFile));
    String apiUrl = "https://api.cloudbees.com/api";
    String apiKey = beesCredentials.getProperty("bees.api.key");
    String secret = beesCredentials.getProperty("bees.api.secret");
    BeesClient client = new BeesClient(apiUrl, apiKey, secret, "xml", "1.0");
    client.setVerbose(false);/*from w  w w. j av a 2s . c om*/

    URL databasesUrl = Thread.currentThread().getContextClassLoader().getResource("databases.txt");
    Preconditions.checkNotNull(databasesUrl, "File 'databases.txt' NOT found in the classpath");

    Collection<String> databaseNames;
    try {
        databaseNames = Sets.newTreeSet(Resources.readLines(databasesUrl, Charsets.ISO_8859_1));
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }

    databaseNames = Collections2.transform(databaseNames, new Function<String, String>() {
        @Nullable
        @Override
        public String apply(@Nullable String input) {
            // {host_db_create,<<"tco_q5rm">>,<<"TCO_q5rm">>,

            if (input == null)
                return null;

            if (input.startsWith("#"))
                return null;

            if (input.indexOf('"') == -1) {
                logger.warn("Skip invalid line {}", input);
                return null;
            }
            input = input.substring(input.indexOf('"') + 1);
            if (input.indexOf('"') == -1) {
                logger.warn("Skip invalid line {}", input);
                return null;
            }
            return input.substring(0, input.indexOf('"'));

        }
    });
    databaseNames = Collections2.filter(databaseNames, new Predicate<String>() {
        @Override
        public boolean apply(@Nullable String s) {
            return !Strings.isNullOrEmpty(s);
        }
    });

    Multimap<String, String> databasesByAccount = ArrayListMultimap.create();

    Class.forName("com.mysql.jdbc.Driver");

    for (String databaseName : databaseNames) {
        try {
            DatabaseInfo databaseInfo = client.databaseInfo(databaseName, true);
            databasesByAccount.put(databaseInfo.getOwner(), databaseInfo.getName());
            logger.debug("Evaluate " + databaseInfo.getName());

            if (true == false) {
                // Hibernate
                logger.info("Hibernate {}", databaseName);
                Map<String, String> params = new HashMap<String, String>();
                params.put("database_id", databaseName);
                String url = client.getRequestURL("database.hibernate", params);
                String response = client.executeRequest(url);
                DatabaseInfoResponse apiResponse = (DatabaseInfoResponse) client.readResponse(response);
                logger.info("DB {} status: {}", apiResponse.getDatabaseInfo().getName(),
                        apiResponse.getDatabaseInfo().getStatus());

            }
            if (true == false) {
                // Hibernate
                logger.info("Activate {}", databaseName);
                Map<String, String> params = new HashMap<String, String>();
                params.put("database_id", databaseName);
                String url = client.getRequestURL("database.activate", params);
                String response = client.executeRequest(url);
                DatabaseInfoResponse apiResponse = (DatabaseInfoResponse) client.readResponse(response);
                logger.info("DB {} status: {}", apiResponse.getDatabaseInfo().getName(),
                        apiResponse.getDatabaseInfo().getStatus());
            }

            String dbUrl = "jdbc:mysql://" + databaseInfo.getMaster() + "/" + databaseInfo.getName();
            logger.info("Connect to {} user={}", dbUrl, databaseInfo.getUsername());
            Connection cnn = DriverManager.getConnection(dbUrl, databaseInfo.getUsername(),
                    databaseInfo.getPassword());
            cnn.setAutoCommit(false);
            cnn.close();

        } catch (Exception e) {
            logger.warn("Exception for {}", databaseName, e);
        }
    }

    System.out.println("OWNERS");
    for (String account : databasesByAccount.keySet()) {
        System.out.println(account + ": " + Joiner.on(", ").join(databasesByAccount.get(account)));
    }

}

From source file:ezbake.thriftrunner.ThriftRunner.java

public static void main(String[] args) throws Exception {
    ThriftStarter thriftStarter = null;/*from w ww.j av a  2 s .com*/
    if (OpenShiftUtil.inOpenShiftContainer()) {
        thriftStarter = new OpenShiftStarter();
    } else {
        thriftStarter = new SimpleStarter();
    }

    final CommonOptions commonOptions = parseArgs(args, thriftStarter);

    final File jarFile = Preconditions.checkNotNull(commonOptions.jarFile, "A jar file must be specified!");
    Preconditions.checkState(jarFile.exists() && jarFile.isFile(),
            jarFile + " does not exist or is not a file!");

    final Properties configuration = getMergedConfig(commonOptions);

    setupLogging(configuration, commonOptions.loggingConfig);

    thriftStarter.initialize();

    /* args4j for boolean  will set to true if we enable an option, in this case we want to disable something, hence
     the '!' */
    final EzBakeBaseThriftService service = getServiceFromJar(jarFile, thriftStarter, configuration);

    System.out.println("Thrift server starting on " + thriftStarter.getPrivateHostInfo());

    boolean serviceDiscoveryEnabled = !commonOptions.serviceDiscoveryDisabled;
    // Will start the server and block until the JVM is shut down, thus stopping the service, unregistering, etc.
    new EZBakeBaseThriftServiceRunner(service, thriftStarter, serviceDiscoveryEnabled).run();
}

From source file:edu.byu.nlp.data.app.AnnotationStream2Csv.java

public static void main(String[] args) throws IOException {
    // parse CLI arguments
    new ArgumentParser(AnnotationStream2Csv.class).parseArgs(args);
    Preconditions.checkNotNull(jsonStream, "You must provide a valid --json-stream!");

    Dataset data = readData(jsonStream);

    // optionally aggregate by instance
    String header = "annotator,start,end,annotation,label,source,num_correct_annotations,num_annotations,cum_num_annotations,num_annotators,cum_num_annotators\n";

    // iterate over instances and (optionally) annotations
    final StringBuilder bld = new StringBuilder();

    switch (row) {
    case ANNOTATION:

        // sort all annotations by end time
        Map<FlatInstance<SparseFeatureVector, Integer>, DatasetInstance> ann2InstMap = Maps
                .newIdentityHashMap();//from   w  w w  .j  av a  2s.c om
        List<FlatInstance<SparseFeatureVector, Integer>> annotationList = Lists.newArrayList();
        for (DatasetInstance inst : data) {
            for (FlatInstance<SparseFeatureVector, Integer> ann : inst.getAnnotations().getRawAnnotations()) {
                ann2InstMap.put(ann, inst); // record instance of each annotations
                annotationList.add(ann);
            }
        }
        Collections.sort(annotationList, new Comparator<FlatInstance<SparseFeatureVector, Integer>>() {
            @Override
            public int compare(FlatInstance<SparseFeatureVector, Integer> o1,
                    FlatInstance<SparseFeatureVector, Integer> o2) {
                // no null checking since we want to fail if annotation time is not set. 
                return Long.compare(o1.getEndTimestamp(), o2.getEndTimestamp());
            }
        });

        Set<Integer> annotators = Sets.newHashSet();
        for (Enumeration<FlatInstance<SparseFeatureVector, Integer>> item : Iterables2
                .enumerate(annotationList)) {
            FlatInstance<SparseFeatureVector, Integer> ann = item.getElement();
            DatasetInstance inst = ann2InstMap.get(ann);
            annotators.add(ann.getAnnotator());

            bld.append(ann.getAnnotator() + ",");
            bld.append(ann.getStartTimestamp() + ",");
            bld.append(ann.getEndTimestamp() + ",");
            bld.append(ann.getAnnotation() + ",");
            bld.append(inst.getLabel() + ",");
            bld.append(
                    data.getInfo().getIndexers().getInstanceIdIndexer().get(inst.getInfo().getSource()) + ",");
            bld.append((!inst.hasLabel() ? "NA" : ann.getAnnotation() == inst.getLabel() ? 1 : 0) + ","); // num correct
            bld.append(1 + ","); // num annotations
            bld.append((item.getIndex() + 1) + ","); // cumulative num annotations
            bld.append(1 + ","); // num annotators
            bld.append(annotators.size() + ""); // cumulative num annotators
            bld.append("\n");
        }
        break;
    case INSTANCE:
        int cumNumAnnotations = 0;
        for (DatasetInstance inst : data) {
            cumNumAnnotations += inst.getInfo().getNumAnnotations();

            int numCorrectAnnotations = 0;
            // sum over all the annotators who put the correct answer (if available)
            if (inst.hasLabel()) {
                Integer correctLabel = inst.getLabel();
                for (int j = 0; j < data.getInfo().getNumAnnotators(); j++) {
                    numCorrectAnnotations += inst.getAnnotations().getLabelAnnotations()
                            .getRow(j)[correctLabel];
                }
            }

            bld.append("NA,");
            bld.append("NA,");
            bld.append("NA,");
            bld.append("NA,");
            bld.append(inst.getLabel() + ",");
            bld.append(inst.getInfo().getSource() + ",");
            bld.append(numCorrectAnnotations + ",");
            bld.append(inst.getInfo().getNumAnnotations() + ",");
            bld.append(cumNumAnnotations + ",");
            bld.append(inst.getInfo().getNumAnnotators() + ",");
            bld.append("NA"); // cumulative num annotators
            bld.append("\n");
        }
        break;

    case ANNOTATOR:
        Multiset<Integer> perAnnotatorAnnotationCounts = HashMultiset.create();
        Multiset<Integer> perAnnotatorCorrectAnnotationCounts = HashMultiset.create();
        for (DatasetInstance inst : data) {
            for (FlatInstance<SparseFeatureVector, Integer> ann : inst.getAnnotations().getRawAnnotations()) {
                int annotatorId = ann.getAnnotator();

                perAnnotatorAnnotationCounts.add(annotatorId);

                if (inst.getLabel() == ann.getAnnotation()) {
                    perAnnotatorCorrectAnnotationCounts.add(annotatorId);
                }

            }
        }

        for (String annotatorId : data.getInfo().getAnnotatorIdIndexer()) {

            bld.append(annotatorId + ",");
            bld.append("NA,");
            bld.append("NA,");
            bld.append("NA,");
            bld.append("NA,");
            bld.append("NA,");
            bld.append(perAnnotatorCorrectAnnotationCounts.count(annotatorId) + ",");
            bld.append(perAnnotatorAnnotationCounts.count(annotatorId) + ",");
            bld.append("NA,");
            bld.append("1,"); // num annotators
            bld.append("NA"); // cumulative num annotators
            bld.append("\n");
        }

        break;

    default:
        Preconditions.checkArgument(false, "unknown row type: " + row);
        break;
    }

    // output to console
    if (out == null) {
        System.out.println(header);
        System.out.println(bld.toString());
    } else {
        File outfile = new File(out);
        Files.write(header, outfile, Charsets.UTF_8);
        Files.append(bld, outfile, Charsets.UTF_8);
    }

}

From source file:edu.byu.nlp.data.app.AnnotationStream2Annotators.java

public static void main(String[] args) throws IOException {
    // parse CLI arguments
    new ArgumentParser(AnnotationStream2Annotators.class).parseArgs(args);
    Preconditions.checkNotNull(jsonStream, "You must provide a valid --json-stream!");
    Preconditions.checkArgument(smooth >= 0, "invalid smoothing value=" + smooth);
    Preconditions.checkArgument(k > 0, "invalid number of clusters=" + k);

    // compile annotation stream data into a dataset
    RandomGenerator rnd = new MersenneTwister(seed);
    Dataset data = readData(jsonStream);

    // create confusion matrices for each annotator wrt some truth
    int[][][] confusionMatrices; // confusionMatrices[annotator][true label][annotation] = count
    logger.info("dataset=" + data);
    switch (confusionMatrixTruth) {
    case GOLD://  w  w w. ja  v  a  2 s  . c om
        confusionMatrices = Datasets.confusionMatricesWrtGoldLabels(data);
        break;
    case MAJORITY:
        confusionMatrices = Datasets.confusionMatricesWrtMajorityVoteLabels(data, rnd);
        break;
    default:
        throw new IllegalArgumentException(
                "unknown truth standard for constructing confusion matrices: " + confusionMatrixTruth);
    }

    // aggregate annotators based on their confusion matrices
    double[][][] annotatorParameters = confusionMatrices2AnnotatorParameters(confusionMatrices);
    int[] clusterAssignments = clusterAnnotatorParameters(annotatorParameters, aggregate, k, maxIterations,
            smooth, rnd);
    double[][][] clusteredAnnotatorParameters = aggregateAnnotatorParameterClusters(annotatorParameters,
            clusterAssignments);

    // aggregate annotator rates
    double[] annotationRates = new double[clusteredAnnotatorParameters.length];
    for (int j = 0; j < confusionMatrices.length; j++) {
        long numAnnotationsPerJ = Matrices.sum(confusionMatrices[j]);
        // add this annotator's annotation count to the cluster total
        annotationRates[clusterAssignments[j]] += numAnnotationsPerJ;
    }
    DoubleArrays.normalizeToSelf(annotationRates);

    // output to console 
    logger.info("aggregated annotators=\n" + Matrices.toString(clusteredAnnotatorParameters, 10, 10, 20, 3));
    for (int c = 0; c < clusteredAnnotatorParameters.length; c++) {
        logger.info("aggregated annotator #" + c + " accuracy=" + accuracyOf(clusteredAnnotatorParameters[c]));
        logger.info("aggregated annotator #" + c + " rate=" + annotationRates[c]);
    }

    // output to file 
    if (output != null) {
        List<SimulatedAnnotator> annotators = SimulatedAnnotators.from(clusteredAnnotatorParameters,
                annotationRates);
        Files2.write(SimulatedAnnotators.serialize(annotators), output);
    }
}

From source file:io.warp10.standalone.Warp.java

public static void main(String[] args) throws Exception {

    System.setProperty("java.awt.headless", "true");

    setProperties(args[0]);/*from w ww .j  a va2  s .c  o  m*/

    boolean nullbackend = "true".equals(WarpConfig.getProperties().getProperty(NULL));

    boolean plasmabackend = "true".equals(WarpConfig.getProperties().getProperty(Configuration.PURE_PLASMA));

    boolean inmemory = "true".equals(WarpConfig.getProperties().getProperty(Configuration.IN_MEMORY));

    Properties properties = getProperties();

    for (String property : REQUIRED_PROPERTIES) {
        // Don't check LEVELDB_HOME when in-memory
        if (inmemory && Configuration.LEVELDB_HOME.equals(property)) {
            continue;
        }
        Preconditions.checkNotNull(properties.getProperty(property),
                "Property '" + property + "' MUST be set.");
    }

    //
    // Initialize KeyStore
    //

    KeyStore keystore;

    if (properties.containsKey(Configuration.OSS_MASTER_KEY)) {
        keystore = new OSSKeyStore(properties.getProperty(Configuration.OSS_MASTER_KEY));
    } else {
        keystore = new UnsecureKeyStore();
    }

    extractKeys(keystore, properties);

    keystore.setKey(KeyStore.SIPHASH_CLASS,
            keystore.decodeKey(properties.getProperty(Configuration.WARP_HASH_CLASS)));
    Preconditions.checkArgument(16 == keystore.getKey(KeyStore.SIPHASH_CLASS).length,
            Configuration.WARP_HASH_CLASS + " MUST be 128 bits long.");
    keystore.setKey(KeyStore.SIPHASH_LABELS,
            keystore.decodeKey(properties.getProperty(Configuration.WARP_HASH_LABELS)));
    Preconditions.checkArgument(16 == keystore.getKey(KeyStore.SIPHASH_LABELS).length,
            Configuration.WARP_HASH_LABELS + " MUST be 128 bits long.");

    //
    // Generate secondary keys. We use the ones' complement of the primary keys
    //

    keystore.setKey(KeyStore.SIPHASH_CLASS_SECONDARY,
            CryptoUtils.invert(keystore.getKey(KeyStore.SIPHASH_CLASS)));
    keystore.setKey(KeyStore.SIPHASH_LABELS_SECONDARY,
            CryptoUtils.invert(keystore.getKey(KeyStore.SIPHASH_LABELS)));

    keystore.setKey(KeyStore.SIPHASH_INDEX,
            keystore.decodeKey(properties.getProperty(Configuration.CONTINUUM_HASH_INDEX)));
    Preconditions.checkArgument(16 == keystore.getKey(KeyStore.SIPHASH_INDEX).length,
            Configuration.CONTINUUM_HASH_INDEX + " MUST be 128 bits long.");
    keystore.setKey(KeyStore.SIPHASH_TOKEN,
            keystore.decodeKey(properties.getProperty(Configuration.WARP_HASH_TOKEN)));
    Preconditions.checkArgument(16 == keystore.getKey(KeyStore.SIPHASH_TOKEN).length,
            Configuration.WARP_HASH_TOKEN + " MUST be 128 bits long.");
    keystore.setKey(KeyStore.SIPHASH_APPID,
            keystore.decodeKey(properties.getProperty(Configuration.WARP_HASH_APP)));
    Preconditions.checkArgument(16 == keystore.getKey(KeyStore.SIPHASH_APPID).length,
            Configuration.WARP_HASH_APP + " MUST be 128 bits long.");
    keystore.setKey(KeyStore.AES_TOKEN,
            keystore.decodeKey(properties.getProperty(Configuration.WARP_AES_TOKEN)));
    Preconditions.checkArgument(
            (16 == keystore.getKey(KeyStore.AES_TOKEN).length)
                    || (24 == keystore.getKey(KeyStore.AES_TOKEN).length)
                    || (32 == keystore.getKey(KeyStore.AES_TOKEN).length),
            Configuration.WARP_AES_TOKEN + " MUST be 128, 192 or 256 bits long.");
    keystore.setKey(KeyStore.AES_SECURESCRIPTS,
            keystore.decodeKey(properties.getProperty(Configuration.WARP_AES_SCRIPTS)));
    Preconditions.checkArgument(
            (16 == keystore.getKey(KeyStore.AES_SECURESCRIPTS).length)
                    || (24 == keystore.getKey(KeyStore.AES_SECURESCRIPTS).length)
                    || (32 == keystore.getKey(KeyStore.AES_SECURESCRIPTS).length),
            Configuration.WARP_AES_SCRIPTS + " MUST be 128, 192 or 256 bits long.");

    if (null != properties.getProperty(Configuration.WARP_AES_LOGGING,
            Configuration.WARP_DEFAULT_AES_LOGGING)) {
        keystore.setKey(KeyStore.AES_LOGGING, keystore.decodeKey(properties
                .getProperty(Configuration.WARP_AES_LOGGING, Configuration.WARP_DEFAULT_AES_LOGGING)));
        Preconditions.checkArgument(
                (16 == keystore.getKey(KeyStore.AES_LOGGING).length)
                        || (24 == keystore.getKey(KeyStore.AES_LOGGING).length)
                        || (32 == keystore.getKey(KeyStore.AES_LOGGING).length),
                Configuration.WARP_AES_LOGGING + " MUST be 128, 192 or 256 bits long.");
    }

    setKeyStore(keystore);

    //
    // Initialize levelDB
    //

    Options options = new Options();

    options.createIfMissing(false);

    if (properties.containsKey(Configuration.LEVELDB_MAXOPENFILES)) {
        int maxOpenFiles = Integer.parseInt(properties.getProperty(Configuration.LEVELDB_MAXOPENFILES));
        options.maxOpenFiles(maxOpenFiles);
    }

    if (null != properties.getProperty(Configuration.LEVELDB_CACHE_SIZE)) {
        options.cacheSize(Long.parseLong(properties.getProperty(Configuration.LEVELDB_CACHE_SIZE)));
    }

    if (null != properties.getProperty(Configuration.LEVELDB_COMPRESSION_TYPE)) {
        if ("snappy".equalsIgnoreCase(properties.getProperty(Configuration.LEVELDB_COMPRESSION_TYPE))) {
            options.compressionType(CompressionType.SNAPPY);
        } else {
            options.compressionType(CompressionType.NONE);
        }
    }

    //
    // Attempt to load JNI library, fallback to pure java in case of error
    //

    if (!inmemory && !nullbackend && !plasmabackend) {
        try {
            db = JniDBFactory.factory.open(new File(properties.getProperty(Configuration.LEVELDB_HOME)),
                    options);
        } catch (UnsatisfiedLinkError ule) {
            System.out.println("WARNING: falling back to pure java implementation of LevelDB.");
            db = Iq80DBFactory.factory.open(new File(properties.getProperty(Configuration.LEVELDB_HOME)),
                    options);
        }
    }

    // Register shutdown hook to close the DB.
    Runtime.getRuntime().addShutdownHook(new Thread(new Warp()));

    //
    // Initialize the backup manager
    //

    if (null != db) {
        String triggerPath = properties.getProperty(Configuration.STANDALONE_SNAPSHOT_TRIGGER);
        String signalPath = properties.getProperty(Configuration.STANDALONE_SNAPSHOT_SIGNAL);

        if (null != triggerPath && null != signalPath) {
            Thread backupManager = new StandaloneSnapshotManager(triggerPath, signalPath);
            backupManager.setDaemon(true);
            backupManager.setName("[Snapshot Manager]");
            backupManager.start();
        }
    }

    WarpScriptLib.registerExtensions();

    //
    // Initialize ThrottlingManager
    //

    ThrottlingManager.init();

    //
    // Create Jetty server
    //

    Server server = new Server();

    int acceptors = Integer.valueOf(properties.getProperty(Configuration.STANDALONE_ACCEPTORS));
    int selectors = Integer.valueOf(properties.getProperty(Configuration.STANDALONE_SELECTORS));
    port = Integer.valueOf(properties.getProperty(Configuration.STANDALONE_PORT));
    host = properties.getProperty(Configuration.STANDALONE_HOST);

    ServerConnector connector = new ServerConnector(server, acceptors, selectors);

    connector.setPort(port);
    if (null != host) {
        connector.setHost(host);
    }

    String idle = properties.getProperty(Configuration.STANDALONE_IDLE_TIMEOUT);

    if (null != idle) {
        connector.setIdleTimeout(Long.parseLong(idle));
    }

    connector.setName("Continuum Standalone Egress");

    server.setConnectors(new Connector[] { connector });

    HandlerList handlers = new HandlerList();

    Handler cors = new CORSHandler();
    handlers.addHandler(cors);

    StandaloneDirectoryClient sdc = null;
    StoreClient scc = null;

    if (inmemory) {
        sdc = new StandaloneDirectoryClient(null, keystore);
        scc = new StandaloneMemoryStore(keystore,
                Long.valueOf(WarpDist.getProperties().getProperty(Configuration.IN_MEMORY_DEPTH,
                        Long.toString(60 * 60 * 1000 * Constants.TIME_UNITS_PER_MS))),
                Long.valueOf(
                        WarpDist.getProperties().getProperty(Configuration.IN_MEMORY_HIGHWATERMARK, "100000")),
                Long.valueOf(
                        WarpDist.getProperties().getProperty(Configuration.IN_MEMORY_LOWWATERMARK, "80000")));
        ((StandaloneMemoryStore) scc).setDirectoryClient((StandaloneDirectoryClient) sdc);
        if ("true".equals(WarpDist.getProperties().getProperty(Configuration.IN_MEMORY_EPHEMERAL))) {
            ((StandaloneMemoryStore) scc).setEphemeral(true);
        }
        ((StandaloneMemoryStore) scc).load();
    } else if (plasmabackend) {
        sdc = new StandaloneDirectoryClient(null, keystore);
        scc = new PlasmaStoreClient();
    } else if (nullbackend) {
        sdc = new NullDirectoryClient(keystore);
        scc = new NullStoreClient();
    } else {
        sdc = new StandaloneDirectoryClient(db, keystore);
        scc = new StandaloneStoreClient(db, keystore, properties);
    }

    StandaloneGeoDirectory geodir = new StandaloneGeoDirectory(keystore.clone(), scc, sdc, properties);

    if (properties.containsKey(Configuration.RUNNER_ROOT)) {
        if (!properties.containsKey(Configuration.RUNNER_ENDPOINT)) {
            properties.setProperty(Configuration.RUNNER_ENDPOINT, "");
            StandaloneScriptRunner runner = new StandaloneScriptRunner(properties, keystore.clone(), scc, sdc,
                    geodir, properties);
        } else {
            //
            // Allocate a normal runner
            //
            ScriptRunner runner = new ScriptRunner(keystore.clone(), properties);
        }

    }

    //
    // Enable the ThrottlingManager (not 
    //

    ThrottlingManager.enable();

    QuasarTokenFilter tf = new QuasarTokenFilter(properties, keystore);

    GzipHandler gzip = new GzipHandler();
    EgressExecHandler egressExecHandler = new EgressExecHandler(keystore, properties, sdc, geodir.getClient(),
            scc);
    gzip.setHandler(egressExecHandler);
    gzip.setBufferSize(65536);
    gzip.setMinGzipSize(0);
    handlers.addHandler(gzip);
    setEgress(true);

    gzip = new GzipHandler();
    gzip.setHandler(new StandaloneIngressHandler(keystore, sdc, scc));
    gzip.setBufferSize(65536);
    gzip.setMinGzipSize(0);
    handlers.addHandler(gzip);

    gzip = new GzipHandler();
    gzip.setHandler(new EgressFetchHandler(keystore, properties, sdc, scc));
    gzip.setBufferSize(65536);
    gzip.setMinGzipSize(0);
    handlers.addHandler(gzip);

    gzip = new GzipHandler();
    gzip.setHandler(new EgressFindHandler(keystore, sdc));
    gzip.setBufferSize(65536);
    gzip.setMinGzipSize(0);
    handlers.addHandler(gzip);

    gzip = new GzipHandler();
    gzip.setHandler(new StandaloneDeleteHandler(keystore, sdc, scc));
    gzip.setBufferSize(65536);
    gzip.setMinGzipSize(0);
    handlers.addHandler(gzip);

    handlers.addHandler(geodir);

    //ContextHandler context = new ContextHandler();
    StandalonePlasmaHandler plasmaHandler = new StandalonePlasmaHandler(keystore, properties, sdc);
    scc.addPlasmaHandler(plasmaHandler);
    scc.addPlasmaHandler(geodir);

    //context.setHandler(plasmaHandler);
    //handlers.addHandler(context);
    handlers.addHandler(plasmaHandler);

    StandaloneStreamUpdateHandler streamUpdateHandler = new StandaloneStreamUpdateHandler(keystore, properties,
            sdc, scc);
    handlers.addHandler(streamUpdateHandler);

    EgressMobiusHandler mobiusHandler = new EgressMobiusHandler(scc, sdc, properties);
    handlers.addHandler(mobiusHandler);

    server.setHandler(handlers);

    JettyUtil.setSendServerVersion(server, false);

    // Clear master key from memory
    keystore.forget();

    try {
        server.start();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    // Retrieve actual local port
    port = connector.getLocalPort();

    // Indicate standalone mode is on
    standaloneMode = true;

    WarpDist.setInitialized(true);

    try {
        while (true) {
            try {
                Thread.sleep(60000L);
            } catch (InterruptedException ie) {
            }
        }
    } catch (Throwable t) {
        System.err.println(t.getMessage());
        server.stop();
    }
}

From source file:io.warp10.WarpDist.java

public static void main(String[] args) throws Exception {

    System.setProperty("java.awt.headless", "true");

    setProperties(args[0]);//from  w  ww  . ja v  a2s .com

    //
    // Extract components to spawn
    //

    String[] components = properties.getProperty(Configuration.WARP_COMPONENTS).split(",");

    Set<String> subprocesses = new HashSet<String>();

    for (String component : components) {
        component = component.trim();

        subprocesses.add(component);
    }

    if (properties.containsKey(Configuration.OSS_MASTER_KEY)) {
        keystore = new OSSKeyStore(properties.getProperty(Configuration.OSS_MASTER_KEY));
    } else {
        keystore = new UnsecureKeyStore();
    }

    //
    // Set SIPHASH keys for class/labels/index
    //

    for (String property : REQUIRED_PROPERTIES) {
        Preconditions.checkNotNull(properties.getProperty(property),
                "Property '" + property + "' MUST be set.");
    }

    keystore.setKey(KeyStore.SIPHASH_CLASS,
            keystore.decodeKey(properties.getProperty(Configuration.WARP_HASH_CLASS)));
    Preconditions.checkArgument(16 == keystore.getKey(KeyStore.SIPHASH_CLASS).length,
            Configuration.WARP_HASH_CLASS + " MUST be 128 bits long.");
    keystore.setKey(KeyStore.SIPHASH_LABELS,
            keystore.decodeKey(properties.getProperty(Configuration.WARP_HASH_LABELS)));
    Preconditions.checkArgument(16 == keystore.getKey(KeyStore.SIPHASH_LABELS).length,
            Configuration.WARP_HASH_LABELS + " MUST be 128 bits long.");

    //
    // Generate secondary keys. We use the ones' complement of the primary keys
    //

    keystore.setKey(KeyStore.SIPHASH_CLASS_SECONDARY,
            CryptoUtils.invert(keystore.getKey(KeyStore.SIPHASH_CLASS)));
    keystore.setKey(KeyStore.SIPHASH_LABELS_SECONDARY,
            CryptoUtils.invert(keystore.getKey(KeyStore.SIPHASH_LABELS)));

    keystore.setKey(KeyStore.SIPHASH_INDEX,
            keystore.decodeKey(properties.getProperty(Configuration.CONTINUUM_HASH_INDEX)));
    Preconditions.checkArgument(16 == keystore.getKey(KeyStore.SIPHASH_INDEX).length,
            Configuration.CONTINUUM_HASH_INDEX + " MUST be 128 bits long.");
    keystore.setKey(KeyStore.SIPHASH_TOKEN,
            keystore.decodeKey(properties.getProperty(Configuration.WARP_HASH_TOKEN)));
    Preconditions.checkArgument(16 == keystore.getKey(KeyStore.SIPHASH_TOKEN).length,
            Configuration.WARP_HASH_TOKEN + " MUST be 128 bits long.");
    keystore.setKey(KeyStore.SIPHASH_APPID,
            keystore.decodeKey(properties.getProperty(Configuration.WARP_HASH_APP)));
    Preconditions.checkArgument(16 == keystore.getKey(KeyStore.SIPHASH_APPID).length,
            Configuration.WARP_HASH_APP + " MUST be 128 bits long.");
    keystore.setKey(KeyStore.AES_TOKEN,
            keystore.decodeKey(properties.getProperty(Configuration.WARP_AES_TOKEN)));
    Preconditions.checkArgument(
            (16 == keystore.getKey(KeyStore.AES_TOKEN).length)
                    || (24 == keystore.getKey(KeyStore.AES_TOKEN).length)
                    || (32 == keystore.getKey(KeyStore.AES_TOKEN).length),
            Configuration.WARP_AES_TOKEN + " MUST be 128, 192 or 256 bits long.");
    keystore.setKey(KeyStore.AES_SECURESCRIPTS,
            keystore.decodeKey(properties.getProperty(Configuration.WARP_AES_SCRIPTS)));
    Preconditions.checkArgument(
            (16 == keystore.getKey(KeyStore.AES_SECURESCRIPTS).length)
                    || (24 == keystore.getKey(KeyStore.AES_SECURESCRIPTS).length)
                    || (32 == keystore.getKey(KeyStore.AES_SECURESCRIPTS).length),
            Configuration.WARP_AES_SCRIPTS + " MUST be 128, 192 or 256 bits long.");

    if (null != properties.getProperty(Configuration.WARP_AES_LOGGING,
            Configuration.WARP_DEFAULT_AES_LOGGING)) {
        keystore.setKey(KeyStore.AES_LOGGING, keystore.decodeKey(properties
                .getProperty(Configuration.WARP_AES_LOGGING, Configuration.WARP_DEFAULT_AES_LOGGING)));
        Preconditions.checkArgument(
                (16 == keystore.getKey(KeyStore.AES_LOGGING).length)
                        || (24 == keystore.getKey(KeyStore.AES_LOGGING).length)
                        || (32 == keystore.getKey(KeyStore.AES_LOGGING).length),
                Configuration.WARP_AES_LOGGING + " MUST be 128, 192 or 256 bits long.");
    }

    if (null != properties.getProperty(Configuration.CONFIG_FETCH_PSK)) {
        keystore.setKey(KeyStore.SIPHASH_FETCH_PSK,
                keystore.decodeKey(properties.getProperty(Configuration.CONFIG_FETCH_PSK)));
        Preconditions.checkArgument((16 == keystore.getKey(KeyStore.SIPHASH_FETCH_PSK).length),
                Configuration.CONFIG_FETCH_PSK + " MUST be 128 bits long.");
    }

    WarpScriptLib.registerExtensions();

    KafkaWebCallService.initKeys(keystore, properties);

    //
    // Initialize ThrottlingManager
    //

    ThrottlingManager.init();

    if (subprocesses.contains("egress") && subprocesses.contains("fetcher")) {
        throw new RuntimeException("'fetcher' and 'egress' cannot be specified together as components to run.");
    }

    for (String subprocess : subprocesses) {
        if ("ingress".equals(subprocess)) {
            Ingress ingress = new Ingress(getKeyStore(), getProperties());
            Map<String, String> labels = new HashMap<String, String>();
            labels.put(SensisionConstants.SENSISION_LABEL_COMPONENT, "ingress");
            Sensision.set(SensisionConstants.SENSISION_CLASS_WARP_REVISION, labels, Revision.REVISION);
        } else if ("egress".equals(subprocess)) {
            Egress egress = new Egress(getKeyStore(), getProperties(), false);
            Map<String, String> labels = new HashMap<String, String>();
            labels.put(SensisionConstants.SENSISION_LABEL_COMPONENT, "egress");
            Sensision.set(SensisionConstants.SENSISION_CLASS_WARP_REVISION, labels, Revision.REVISION);
            hasEgress = true;
        } else if ("fetcher".equals(subprocess)) {
            Egress egress = new Egress(getKeyStore(), getProperties(), true);
            Map<String, String> labels = new HashMap<String, String>();
            labels.put(SensisionConstants.SENSISION_LABEL_COMPONENT, "fetcher");
            Sensision.set(SensisionConstants.SENSISION_CLASS_WARP_REVISION, labels, Revision.REVISION);
        } else if ("store".equals(subprocess)) {
            int nthreads = Integer.valueOf(properties.getProperty(Configuration.STORE_NTHREADS));
            for (int i = 0; i < nthreads; i++) {
                //Store store = new Store(getKeyStore(), getProperties(), null);
                Store store = new Store(getKeyStore(), getProperties(), 1);
            }
            Map<String, String> labels = new HashMap<String, String>();
            labels.put(SensisionConstants.SENSISION_LABEL_COMPONENT, "store");
            Sensision.set(SensisionConstants.SENSISION_CLASS_WARP_REVISION, labels, Revision.REVISION);
        } else if ("directory".equals(subprocess)) {
            Directory directory = new Directory(getKeyStore(), getProperties());
            Map<String, String> labels = new HashMap<String, String>();
            labels.put(SensisionConstants.SENSISION_LABEL_COMPONENT, "directory");
            Sensision.set(SensisionConstants.SENSISION_CLASS_WARP_REVISION, labels, Revision.REVISION);
            //} else if ("index".equals(subprocess)) {
            //  Index index = new Index(getKeyStore(), getProperties());
        } else if ("plasmaFE".equalsIgnoreCase(subprocess)) {
            PlasmaFrontEnd plasmaFE = new PlasmaFrontEnd(getKeyStore(), getProperties());
            Map<String, String> labels = new HashMap<String, String>();
            labels.put(SensisionConstants.SENSISION_LABEL_COMPONENT, "plasmafe");
            Sensision.set(SensisionConstants.SENSISION_CLASS_WARP_REVISION, labels, Revision.REVISION);
        } else if ("plasmaBE".equalsIgnoreCase(subprocess)) {
            PlasmaBackEnd plasmaBE = new PlasmaBackEnd(getKeyStore(), getProperties());
            Map<String, String> labels = new HashMap<String, String>();
            labels.put(SensisionConstants.SENSISION_LABEL_COMPONENT, "plasmabe");
            Sensision.set(SensisionConstants.SENSISION_CLASS_WARP_REVISION, labels, Revision.REVISION);
        } else if ("webcall".equals(subprocess)) {
            KafkaWebCallBroker webcall = new KafkaWebCallBroker(getKeyStore(), getProperties());
            Map<String, String> labels = new HashMap<String, String>();
            labels.put(SensisionConstants.SENSISION_LABEL_COMPONENT, "webcall");
            Sensision.set(SensisionConstants.SENSISION_CLASS_WARP_REVISION, labels, Revision.REVISION);
        } else if ("geodir".equals(subprocess)) {
            GeoDirectory geodir = new GeoDirectory(getKeyStore(), getProperties());
            Map<String, String> labels = new HashMap<String, String>();
            labels.put(SensisionConstants.SENSISION_LABEL_COMPONENT, "geodir");
            Sensision.set(SensisionConstants.SENSISION_CLASS_WARP_REVISION, labels, Revision.REVISION);
        } else if ("runner".equals(subprocess)) {
            ScriptRunner runner = new ScriptRunner(getKeyStore(), getProperties());
            Map<String, String> labels = new HashMap<String, String>();
            labels.put(SensisionConstants.SENSISION_LABEL_COMPONENT, "runner");
            Sensision.set(SensisionConstants.SENSISION_CLASS_WARP_REVISION, labels, Revision.REVISION);
        } else {
            System.err.println("Unknown component '" + subprocess + "', skipping.");
            continue;
        }
    }

    // Clear master key from memory
    keystore.forget();

    setInitialized(true);

    //
    // We're done, let's sleep endlessly
    //

    try {
        while (true) {
            try {
                Thread.sleep(60000L);
            } catch (InterruptedException ie) {
            }
        }
    } catch (Throwable t) {
        System.err.println(t.getMessage());
    }
}

From source file:app.philm.in.util.ViewUtils.java

public static boolean isEmpty(TextView textView) {
    Preconditions.checkNotNull(textView, "textView cannot be null");
    return TextUtils.isEmpty(textView.getText());
}

From source file:io.pravega.controller.store.host.HostStoreFactory.java

public static HostControllerStore createStore(final HostMonitorConfig hostMonitorConfig,
        final StoreClient storeClient) {

    Preconditions.checkNotNull(hostMonitorConfig, "hostMonitorConfig");
    Preconditions.checkNotNull(storeClient, "storeClient");

    if (hostMonitorConfig.isHostMonitorEnabled()) {
        Preconditions.checkArgument(storeClient.getType() == StoreType.Zookeeper,
                "If host monitor is enabled then the store type should be Zookeeper");
        log.info("Creating Zookeeper based host store");
        return new ZKHostStore((CuratorFramework) storeClient.getClient(),
                hostMonitorConfig.getContainerCount());
    } else {//from   w ww .j  av a2s  .com
        // We create an in-memory host store using the configuration passed in hostMonitorConfig.
        log.info("Creating in-memory host store");
        return createInMemoryStore(hostMonitorConfig);
    }
}