Example usage for java.lang String equals

List of usage examples for java.lang String equals

Introduction

In this page you can find the example usage for java.lang String equals.

Prototype

public boolean equals(Object anObject) 

Source Link

Document

Compares this string to the specified object.

Usage

From source file:de.tudarmstadt.ukp.experiments.argumentation.convincingness.sampling.Step7aLearningDataProducer.java

@SuppressWarnings("unchecked")
public static void main(String[] args) throws IOException {
    String inputDir = args[0];//  w  ww.  j a  v a  2s  . c  om
    File outputDir = new File(args[1]);

    if (!outputDir.exists()) {
        outputDir.mkdirs();
    }

    Collection<File> files = IOHelper.listXmlFiles(new File(inputDir));

    // for generating ConvArgStrict use this
    String prefix = "no-eq_DescendingScoreArgumentPairListSorter";

    // for oversampling using graph transitivity properties use this
    //        String prefix = "generated_no-eq_AscendingScoreArgumentPairListSorter";

    Iterator<File> iterator = files.iterator();
    while (iterator.hasNext()) {
        File file = iterator.next();

        if (!file.getName().startsWith(prefix)) {
            iterator.remove();
        }
    }

    int totalGoldPairsCounter = 0;

    Map<String, Integer> goldDataDistribution = new HashMap<>();

    DescriptiveStatistics statsPerTopic = new DescriptiveStatistics();

    int totalPairsWithReasonSameAsGold = 0;

    DescriptiveStatistics ds = new DescriptiveStatistics();

    for (File file : files) {
        List<AnnotatedArgumentPair> argumentPairs = (List<AnnotatedArgumentPair>) XStreamTools.getXStream()
                .fromXML(file);

        int pairsPerTopicCounter = 0;

        String name = file.getName().replaceAll(prefix, "").replaceAll("\\.xml", "");

        PrintWriter pw = new PrintWriter(new File(outputDir, name + ".csv"), "utf-8");

        pw.println("#id\tlabel\ta1\ta2");

        for (AnnotatedArgumentPair argumentPair : argumentPairs) {
            String goldLabel = argumentPair.getGoldLabel();

            if (!goldDataDistribution.containsKey(goldLabel)) {
                goldDataDistribution.put(goldLabel, 0);
            }

            goldDataDistribution.put(goldLabel, goldDataDistribution.get(goldLabel) + 1);

            pw.printf(Locale.ENGLISH, "%s\t%s\t%s\t%s%n", argumentPair.getId(), goldLabel,
                    multipleParagraphsToSingleLine(argumentPair.getArg1().getText()),
                    multipleParagraphsToSingleLine(argumentPair.getArg2().getText()));

            pairsPerTopicCounter++;

            int sameInOnePair = 0;

            // get gold reason statistics
            for (AnnotatedArgumentPair.MTurkAssignment assignment : argumentPair.mTurkAssignments) {
                String label = assignment.getValue();

                if (goldLabel.equals(label)) {
                    sameInOnePair++;
                }
            }

            ds.addValue(sameInOnePair);
            totalPairsWithReasonSameAsGold += sameInOnePair;
        }

        totalGoldPairsCounter += pairsPerTopicCounter;
        statsPerTopic.addValue(pairsPerTopicCounter);

        pw.close();
    }

    System.out.println("Total reasons matching gold " + totalPairsWithReasonSameAsGold);
    System.out.println(ds);

    System.out.println("Total gold pairs: " + totalGoldPairsCounter);
    System.out.println(statsPerTopic);

    int totalPairs = 0;
    for (Integer pairs : goldDataDistribution.values()) {
        totalPairs += pairs;
    }
    System.out.println("Total pairs: " + totalPairs);
    System.out.println(goldDataDistribution);

}

From source file:com.entertailion.java.caster.Main.java

/**
 * @param args/*  w  ww.j  a  va 2 s  .  com*/
 */
public static void main(String[] args) {
    // http://commons.apache.org/proper/commons-cli/usage.html
    Option help = new Option("h", "help", false, "Print this help message");
    Option version = new Option("V", "version", false, "Print version information");
    Option list = new Option("l", "list", false, "List ChromeCast devices");
    Option verbose = new Option("v", "verbose", false, "Verbose debug logging");
    Option transcode = new Option("t", "transcode", false, "Transcode media; -f also required");
    Option rest = new Option("r", "rest", false, "REST API server");

    Option url = OptionBuilder.withLongOpt("stream").hasArg().withValueSeparator()
            .withDescription("HTTP URL for streaming content; -d also required").create("s");

    Option server = OptionBuilder.withLongOpt("device").hasArg().withValueSeparator()
            .withDescription("ChromeCast device IP address").create("d");

    Option id = OptionBuilder.withLongOpt("app-id").hasArg().withValueSeparator()
            .withDescription("App ID for whitelisted device").create("id");

    Option mediaFile = OptionBuilder.withLongOpt("file").hasArg().withValueSeparator()
            .withDescription("Local media file; -d also required").create("f");

    Option transcodingParameters = OptionBuilder.withLongOpt("transcode-parameters").hasArg()
            .withValueSeparator().withDescription("Transcode parameters; -t also required").create("tp");

    Option restPort = OptionBuilder.withLongOpt("rest-port").hasArg().withValueSeparator()
            .withDescription("REST API port; default 8080").create("rp");

    Options options = new Options();
    options.addOption(help);
    options.addOption(version);
    options.addOption(list);
    options.addOption(verbose);
    options.addOption(url);
    options.addOption(server);
    options.addOption(id);
    options.addOption(mediaFile);
    options.addOption(transcode);
    options.addOption(transcodingParameters);
    options.addOption(rest);
    options.addOption(restPort);
    // create the command line parser
    CommandLineParser parser = new PosixParser();

    //String[] arguments = new String[] { "-vr" };

    try {
        // parse the command line arguments
        CommandLine line = parser.parse(options, args);
        Option[] lineOptions = line.getOptions();
        if (lineOptions.length == 0) {
            System.out.println("caster: try 'java -jar caster.jar -h' for more information");
            System.exit(0);
        }

        Log.setVerbose(line.hasOption("v"));

        // Custom app-id
        if (line.hasOption("id")) {
            Log.d(LOG_TAG, line.getOptionValue("id"));
            appId = line.getOptionValue("id");
        }

        // Print version
        if (line.hasOption("V")) {
            System.out.println("Caster version " + VERSION);
        }

        // List ChromeCast devices
        if (line.hasOption("l")) {
            final DeviceFinder deviceFinder = new DeviceFinder(new DeviceFinderListener() {

                @Override
                public void discoveringDevices(DeviceFinder deviceFinder) {
                    Log.d(LOG_TAG, "discoveringDevices");
                }

                @Override
                public void discoveredDevices(DeviceFinder deviceFinder) {
                    Log.d(LOG_TAG, "discoveredDevices");
                    TrackedDialServers trackedDialServers = deviceFinder.getTrackedDialServers();
                    for (DialServer dialServer : trackedDialServers) {
                        System.out.println(dialServer.toString()); // keep system for output
                    }
                }

            });
            deviceFinder.discoverDevices();
        }

        // Stream media from internet
        if (line.hasOption("s") && line.hasOption("d")) {
            Log.d(LOG_TAG, line.getOptionValue("d"));
            Log.d(LOG_TAG, line.getOptionValue("s"));
            try {
                Playback playback = new Playback(platform, appId,
                        new DialServer(InetAddress.getByName(line.getOptionValue("d"))),
                        new PlaybackListener() {
                            private int time;
                            private int duration;
                            private int state;

                            @Override
                            public void updateTime(Playback playback, int time) {
                                Log.d(LOG_TAG, "updateTime: " + time);
                                this.time = time;
                            }

                            @Override
                            public void updateDuration(Playback playback, int duration) {
                                Log.d(LOG_TAG, "updateDuration: " + duration);
                                this.duration = duration;
                            }

                            @Override
                            public void updateState(Playback playback, int state) {
                                Log.d(LOG_TAG, "updateState: " + state);
                                // Stop the app if the video reaches the end
                                if (time > 0 && time == duration && state == 0) {
                                    playback.doStop();
                                    System.exit(0);
                                }
                            }

                            public int getTime() {
                                return time;
                            }

                            public int getDuration() {
                                return duration;
                            }

                            public int getState() {
                                return state;
                            }

                        });
                playback.stream(line.getOptionValue("s"));
            } catch (Exception e) {
                e.printStackTrace();
                System.exit(1);
            }
        }

        // Play local media file
        if (line.hasOption("f") && line.hasOption("d")) {
            Log.d(LOG_TAG, line.getOptionValue("d"));
            Log.d(LOG_TAG, line.getOptionValue("f"));

            final String file = line.getOptionValue("f");
            String device = line.getOptionValue("d");

            try {
                Playback playback = new Playback(platform, appId, new DialServer(InetAddress.getByName(device)),
                        new PlaybackListener() {
                            private int time;
                            private int duration;
                            private int state;

                            @Override
                            public void updateTime(Playback playback, int time) {
                                Log.d(LOG_TAG, "updateTime: " + time);
                                this.time = time;
                            }

                            @Override
                            public void updateDuration(Playback playback, int duration) {
                                Log.d(LOG_TAG, "updateDuration: " + duration);
                                this.duration = duration;
                            }

                            @Override
                            public void updateState(Playback playback, int state) {
                                Log.d(LOG_TAG, "updateState: " + state);
                                // Stop the app if the video reaches the end
                                if (time > 0 && time == duration && state == 0) {
                                    playback.doStop();
                                    System.exit(0);
                                }
                            }

                            public int getTime() {
                                return time;
                            }

                            public int getDuration() {
                                return duration;
                            }

                            public int getState() {
                                return state;
                            }

                        });
                if (line.hasOption("t") && line.hasOption("tp")) {
                    playback.setTranscodingParameters(line.getOptionValue("tp"));
                }
                playback.play(file, line.hasOption("t"));
            } catch (Exception e) {
                e.printStackTrace();
                System.exit(1);
            }
        }

        // REST API server
        if (line.hasOption("r")) {
            final DeviceFinder deviceFinder = new DeviceFinder(new DeviceFinderListener() {

                @Override
                public void discoveringDevices(DeviceFinder deviceFinder) {
                    Log.d(LOG_TAG, "discoveringDevices");
                }

                @Override
                public void discoveredDevices(DeviceFinder deviceFinder) {
                    Log.d(LOG_TAG, "discoveredDevices");
                    TrackedDialServers trackedDialServers = deviceFinder.getTrackedDialServers();
                    for (DialServer dialServer : trackedDialServers) {
                        Log.d(LOG_TAG, dialServer.toString());
                    }
                }

            });
            deviceFinder.discoverDevices();

            int port = 0;
            if (line.hasOption("rp")) {
                try {
                    port = Integer.parseInt(line.getOptionValue("rp"));
                } catch (NumberFormatException e) {
                    Log.e(LOG_TAG, "invalid rest port", e);
                }
            }

            Playback.startWebserver(port, new WebListener() {
                String[] prefixes = { "/playback", "/devices" };
                HashMap<String, Playback> playbackMap = new HashMap<String, Playback>();
                HashMap<String, RestPlaybackListener> playbackListenerMap = new HashMap<String, RestPlaybackListener>();

                final class RestPlaybackListener implements PlaybackListener {
                    private String device;
                    private int time;
                    private int duration;
                    private int state;

                    public RestPlaybackListener(String device) {
                        this.device = device;
                    }

                    @Override
                    public void updateTime(Playback playback, int time) {
                        Log.d(LOG_TAG, "updateTime: " + time);
                        this.time = time;
                    }

                    @Override
                    public void updateDuration(Playback playback, int duration) {
                        Log.d(LOG_TAG, "updateDuration: " + duration);
                        this.duration = duration;
                    }

                    @Override
                    public void updateState(Playback playback, int state) {
                        Log.d(LOG_TAG, "updateState: " + state);
                        this.state = state;
                        // Stop the app if the video reaches the end
                        if (this.time > 0 && this.time == this.duration && state == 0) {
                            playback.doStop();
                            playbackMap.remove(device);
                            playbackListenerMap.remove(device);
                        }
                    }

                    public int getTime() {
                        return time;
                    }

                    public int getDuration() {
                        return duration;
                    }

                    public int getState() {
                        return state;
                    }

                }

                @Override
                public Response handleRequest(String uri, String method, Properties header, Properties parms) {
                    Log.d(LOG_TAG, "handleRequest: " + uri);

                    if (method.equals("GET")) {
                        if (uri.startsWith(prefixes[0])) { // playback
                            String device = parms.getProperty("device");
                            if (device != null) {
                                RestPlaybackListener playbackListener = playbackListenerMap.get(device);
                                if (playbackListener != null) {
                                    // https://code.google.com/p/json-simple/wiki/EncodingExamples
                                    JSONObject obj = new JSONObject();
                                    obj.put("time", playbackListener.getTime());
                                    obj.put("duration", playbackListener.getDuration());
                                    switch (playbackListener.getState()) {
                                    case 0:
                                        obj.put("state", "idle");
                                        break;
                                    case 1:
                                        obj.put("state", "stopped");
                                        break;
                                    case 2:
                                        obj.put("state", "playing");
                                        break;
                                    default:
                                        obj.put("state", "idle");
                                        break;
                                    }
                                    return new Response(HttpServer.HTTP_OK, "text/plain", obj.toJSONString());
                                } else {
                                    // Nothing is playing
                                    JSONObject obj = new JSONObject();
                                    obj.put("time", 0);
                                    obj.put("duration", 0);
                                    obj.put("state", "stopped");
                                    return new Response(HttpServer.HTTP_OK, "text/plain", obj.toJSONString());
                                }
                            }
                        } else if (uri.startsWith(prefixes[1])) { // devices
                            // https://code.google.com/p/json-simple/wiki/EncodingExamples
                            JSONArray list = new JSONArray();
                            TrackedDialServers trackedDialServers = deviceFinder.getTrackedDialServers();
                            for (DialServer dialServer : trackedDialServers) {
                                JSONObject obj = new JSONObject();
                                obj.put("name", dialServer.getFriendlyName());
                                obj.put("ip_address", dialServer.getIpAddress().getHostAddress());
                                list.add(obj);
                            }
                            return new Response(HttpServer.HTTP_OK, "text/plain", list.toJSONString());
                        }
                    } else if (method.equals("POST")) {
                        if (uri.startsWith(prefixes[0])) { // playback
                            String device = parms.getProperty("device");
                            if (device != null) {
                                String stream = parms.getProperty("stream");
                                String file = parms.getProperty("file");
                                String state = parms.getProperty("state");
                                String transcode = parms.getProperty("transcode");
                                String transcodeParameters = parms.getProperty("transcode-parameters");
                                Log.d(LOG_TAG, "transcodeParameters=" + transcodeParameters);
                                if (stream != null) {
                                    try {
                                        if (playbackMap.get(device) == null) {
                                            DialServer dialServer = deviceFinder.getTrackedDialServers()
                                                    .findDialServer(InetAddress.getByName(device));
                                            if (dialServer != null) {
                                                RestPlaybackListener playbackListener = new RestPlaybackListener(
                                                        device);
                                                playbackMap.put(device, new Playback(platform, appId,
                                                        dialServer, playbackListener));
                                                playbackListenerMap.put(device, playbackListener);
                                            }
                                        }
                                        Playback playback = playbackMap.get(device);
                                        if (playback != null) {
                                            playback.stream(stream);
                                            return new Response(HttpServer.HTTP_OK, "text/plain", "Ok");
                                        }
                                    } catch (Exception e1) {
                                        Log.e(LOG_TAG, "playback", e1);
                                    }
                                } else if (file != null) {
                                    try {
                                        if (playbackMap.get(device) == null) {
                                            DialServer dialServer = deviceFinder.getTrackedDialServers()
                                                    .findDialServer(InetAddress.getByName(device));
                                            if (dialServer != null) {
                                                RestPlaybackListener playbackListener = new RestPlaybackListener(
                                                        device);
                                                playbackMap.put(device, new Playback(platform, appId,
                                                        dialServer, playbackListener));
                                                playbackListenerMap.put(device, playbackListener);
                                            }
                                        }
                                        Playback playback = playbackMap.get(device);
                                        if (transcodeParameters != null) {
                                            playback.setTranscodingParameters(transcodeParameters);
                                        }
                                        if (playback != null) {
                                            playback.play(file, transcode != null);
                                            return new Response(HttpServer.HTTP_OK, "text/plain", "Ok");
                                        }
                                    } catch (Exception e1) {
                                        Log.e(LOG_TAG, "playback", e1);
                                    }
                                } else if (state != null) {
                                    try {
                                        if (playbackMap.get(device) == null) {
                                            DialServer dialServer = deviceFinder.getTrackedDialServers()
                                                    .findDialServer(InetAddress.getByName(device));
                                            if (dialServer != null) {
                                                RestPlaybackListener playbackListener = new RestPlaybackListener(
                                                        device);
                                                playbackMap.put(device, new Playback(platform, appId,
                                                        dialServer, playbackListener));
                                                playbackListenerMap.put(device, playbackListener);
                                            }
                                        }
                                        Playback playback = playbackMap.get(device);
                                        if (playback != null) {
                                            // Handle case where current app wasn't started with caster
                                            playback.setDialServer(deviceFinder.getTrackedDialServers()
                                                    .findDialServer(InetAddress.getByName(device)));
                                            // Change the playback state
                                            if (state.equals("play")) {
                                                playback.doPlay();
                                                return new Response(HttpServer.HTTP_OK, "text/plain", "Ok");
                                            } else if (state.equals("pause")) {
                                                playback.doPause();
                                                return new Response(HttpServer.HTTP_OK, "text/plain", "Ok");
                                            } else if (state.equals("stop")) {
                                                playback.doStop();
                                                playbackMap.remove(device);
                                                playbackListenerMap.remove(device);
                                                return new Response(HttpServer.HTTP_OK, "text/plain", "Ok");
                                            } else {
                                                Log.e(LOG_TAG, "playback invalid state: " + state);
                                            }
                                        }
                                    } catch (Exception e1) {
                                        Log.e(LOG_TAG, "playback", e1);
                                    }
                                }
                            }
                        }
                    }

                    return new Response(HttpServer.HTTP_BADREQUEST, "text/plain", "Bad Request");
                }

                @Override
                public String[] uriPrefixes() {
                    return prefixes;
                }

            });
            Log.d(LOG_TAG, "REST server ready");

            // Run forever...
            while (true) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                }
            }
        }

        // Print help
        if (line.hasOption("h")) {
            printHelp(options);
        }
    } catch (ParseException exp) {
        System.out.println("ERROR: " + exp.getMessage());
        System.out.println();
        printHelp(options);
    }
}

From source file:br.com.dgimenes.jsonlibscomparison.JSONLibsTest.java

public static void main(String[] args)
        throws JSONException, JsonGenerationException, JsonMappingException, IOException {
    // initialize entities
    Sales sales = createSales();//from   w  w  w.j av a 2 s  . c  o  m
    SalesJAXB salesAnnotatedWithJAXB = createSalesAnnotatedWithJAXB(sales);
    SalesXStream salesAnnotatedWithXStream = createSalesAnnotatedWithXStream(sales);

    // JSON.org
    String jsonResultUsingJSONORG = null;
    jsonResultUsingJSONORG = normalizeOutput(encodeWithJSONORG(sales));

    // JACKSON (with JAX-B)
    String jsonResultUsingJACKSONJAXB = null;
    jsonResultUsingJACKSONJAXB = normalizeOutput(encodeWithJACKSONJAXB(salesAnnotatedWithJAXB));

    // XSTREAM
    String jsonResultUsingXSTREAM = null;
    jsonResultUsingXSTREAM = normalizeOutput(encodeWithXStream(salesAnnotatedWithXStream));

    // GSON
    String jsonResultUsingGSON = null;
    jsonResultUsingGSON = normalizeOutput(encodeWithGSON(sales));

    System.out.println("JSON.ORG\n" + jsonResultUsingJSONORG + "\n");
    System.out.println("JACKSON\n" + jsonResultUsingJACKSONJAXB + "\n");
    System.out.println("GSON\n" + jsonResultUsingGSON + "\n");
    System.out.println("XSTREAM\n" + jsonResultUsingXSTREAM + "\n");

    // Testing results
    if (jsonResultUsingJSONORG.equals(jsonResultUsingJACKSONJAXB)
            && jsonResultUsingJSONORG.equals(jsonResultUsingXSTREAM)
            && jsonResultUsingJSONORG.equals(jsonResultUsingGSON)) {
        System.out.println("TEST OK. All outputs are equal!");
    } else {
        System.err.println("TEST ERROR. Outputs are different!");
    }
}

From source file:com.linkedin.databus2.client.util.DbusClientClusterUtil.java

/**
 * @param args//  w w  w.  j av  a2s.c  o m
 * DbusClientClusterUtil -s <serverList> -n <namespace> -g <group> -d <dir>     members
 *                                                     leader 
 *                                                    keys 
 *                                                    readSCN <key> 
 *                                                    writeSCN <key> SCN [OFFSET]
 *                                                 remove <key>
 *                                                 readLastTS
 *                                                 writeLastTS TIMESTAMP            
 */
public static void main(String[] args) {
    try {
        GnuParser cmdLineParser = new GnuParser();
        Options options = new Options();
        options.addOption("n", true, "Zookeeper namespace  [/DatabusClient")
                .addOption("g", true, "Groupname [default-group-name] ")
                .addOption("d", true, "Shared directory name [shareddata] ")
                .addOption("s", true, "Zookeeper server list [localhost:2181] ").addOption("h", false, "help");
        CommandLine cmdLineArgs = cmdLineParser.parse(options, args, false);

        if (cmdLineArgs.hasOption('h')) {
            usage();
            System.exit(0);
        }

        String namespace = cmdLineArgs.getOptionValue('n');
        if (namespace == null || namespace.isEmpty()) {
            namespace = "/DatabusClient";
        }
        String groupname = cmdLineArgs.getOptionValue('g');
        if (groupname == null || groupname.isEmpty()) {
            groupname = "default-group-name";
        }
        String sharedDir = cmdLineArgs.getOptionValue('d');
        if (sharedDir == null || sharedDir.isEmpty()) {
            sharedDir = "shareddata";
        }
        String serverList = cmdLineArgs.getOptionValue('s');
        if (serverList == null || serverList.isEmpty()) {
            serverList = "localhost:2181";
        }
        String[] fns = cmdLineArgs.getArgs();
        if (fns.length < 1) {
            usage();
            System.exit(1);
        }
        String function = fns[0];
        String arg1 = (fns.length > 1) ? fns[1] : null;
        String arg2 = (fns.length > 2) ? fns[2] : null;
        try {
            String memberName = "cmd-line-tool";
            DatabusClientNode clusterNode = new DatabusClientNode(new DatabusClientNode.StaticConfig(true,
                    serverList, 2000, 5000, namespace, groupname, memberName, false, sharedDir));
            DatabusClientGroupMember member = clusterNode.getMember(namespace, groupname, memberName);
            if (member == null || !member.joinWithoutLeadershipDuties()) {
                System.err.println("Initialization failed for: " + member);
                System.exit(1);
            }

            if (function.equals("members")) {
                List<String> mlist = member.getMembers();
                for (String m : mlist) {
                    System.out.println(m);
                }

            } else if (function.equals("leader")) {
                String leader = member.getLeader();
                System.out.println(leader);

            } else if (function.equals("keys")) {
                List<String> keyList = member.getSharedKeys();
                if (keyList != null) {
                    for (String m : keyList) {
                        System.out.println(m);
                    }
                }
            } else if (function.equals("readSCN")) {
                List<String> keyList;
                if (arg1 == null) {
                    keyList = member.getSharedKeys();
                } else {
                    keyList = new ArrayList<String>();
                    keyList.add(arg1);
                }
                if (keyList != null) {
                    for (String k : keyList) {
                        if (!k.equals(DatabusClientDSCUpdater.DSCKEY)) {
                            Checkpoint cp = (Checkpoint) member.readSharedData(k);
                            if (cp != null) {
                                System.out.println(k + " " + cp.getWindowScn() + " " + cp.getWindowOffset());
                            } else {
                                System.err.println(k + " null null");
                            }
                        }
                    }
                }

            } else if (function.equals("writeSCN")) {
                if (arg1 != null && arg2 != null) {
                    Checkpoint cp = new Checkpoint();
                    cp.setConsumptionMode(DbusClientMode.ONLINE_CONSUMPTION);
                    cp.setWindowScn(Long.parseLong(arg2));
                    if (fns.length > 3) {
                        cp.setWindowOffset(Integer.parseInt(fns[3]));
                    } else {
                        cp.setWindowOffset(-1);
                    }
                    if (member.writeSharedData(arg1, cp)) {
                        System.out.println(arg1 + " " + cp.getWindowScn() + " " + cp.getWindowOffset());
                    } else {
                        System.err.println("Write failed! " + member + " couldn't write key=" + arg1);
                        System.exit(1);
                    }
                } else {
                    usage();
                    System.exit(1);
                }
            } else if (function.equals("readLastTs")) {
                Long timeInMs = (Long) member.readSharedData(DatabusClientDSCUpdater.DSCKEY);
                if (timeInMs != null) {
                    System.out.println(DatabusClientDSCUpdater.DSCKEY + " " + timeInMs.longValue());
                } else {
                    System.err.println(DatabusClientDSCUpdater.DSCKEY + " null");
                }
            } else if (function.equals("writeLastTs")) {
                if (arg1 != null) {
                    Long ts = Long.parseLong(arg1);
                    if (member.writeSharedData(DatabusClientDSCUpdater.DSCKEY, ts)) {
                        System.out.println(DatabusClientDSCUpdater.DSCKEY + " " + ts);
                    } else {
                        System.err.println("Write failed! " + member + " couldn't write key="
                                + DatabusClientDSCUpdater.DSCKEY);
                        System.exit(1);
                    }

                } else {
                    usage();
                    System.exit(1);
                }
            } else if (function.equals("remove")) {
                if (!member.removeSharedData(arg1)) {
                    System.err.println("Remove failed! " + arg1);
                    System.exit(1);
                }
            } else if (function.equals("create")) {
                if (!member.createPaths()) {
                    System.err.println("Create path failed!");
                    System.exit(1);
                }

            } else {
                usage();
                System.exit(1);
            }
        } catch (InvalidConfigException e) {
            e.printStackTrace();
            usage();
            System.exit(1);
        }

    } catch (ParseException e) {
        usage();
        System.exit(1);
    }

}

From source file:fr.bmartel.protocol.google.main.LaunchOauthApiServer.java

/**
 * The main method./*from   ww  w  .j  av  a 2  s. c om*/
 *
 * @param args the arguments
 */
public static void main(String[] args) {

    webPath = "";
    clientId = "";
    clientSecret = "";

    if (args.length == 3) {

        for (int i = 0; i < 3; i++) {
            if (args[i].toLowerCase().startsWith("webpath="))
                webPath = args[i].substring(args[i].indexOf("webpath=") + "webpath=".length() + 1,
                        args[i].length());
            if (args[i].toLowerCase().startsWith("clientid="))
                clientId = args[i].substring(args[i].indexOf("clientid=") + "clientid=".length() + 1,
                        args[i].length());
            if (args[i].toLowerCase().startsWith("clientsecret="))
                clientSecret = args[i].substring(
                        args[i].indexOf("clientsecret=") + "clientsecret=".length() + 1, args[i].length());
        }

        if (webPath.equals("")) {
            printHelp("Error web path is missing");
            return;
        } else if (clientId.equals("")) {
            printHelp("Error client Id is missing");
            return;
        } else if (clientSecret.equals("")) {
            printHelp("Error client secret is missing");
            return;
        }
    } else {
        printHelp("");
        return;
    }
    // start http server
    HttpServer server = new HttpServer(SERVER_PORT);

    websocketServer = new WebsocketServer(WEBSOCKET_SERVER_PORT);

    websocketServer.addServerEventListener(new IClientEventListener() {

        @SuppressWarnings("unchecked")
        @Override
        public void onMessageReceivedFromClient(final IWebsocketClient client, String message) {

            JSONObject obj = (JSONObject) JSONValue.parse(message);

            if (obj != null && obj.containsKey(JsonConstants.API_ACTION)) {

                System.out.println("[API] > " + obj.toJSONString());

                String action = obj.get(JsonConstants.API_ACTION).toString();

                if (action != null) {
                    if (action.equals(JsonConstants.API_REGISTRATION_STATE)) {

                        JSONObject registrationResponse = new JSONObject();

                        if (calendarNotifManager != null
                                && calendarNotifManager.getOauthRegistration() != null) {

                            registrationResponse.put(JsonConstants.GOOGLE_OAUTH_DEVICE_CODE,
                                    calendarNotifManager.getOauthRegistration().getDeviceCode());
                            registrationResponse.put(JsonConstants.GOOGLE_OAUTH_EXPIRING_BEFORE,
                                    calendarNotifManager.getOauthRegistration().getExpiringBefore());
                            registrationResponse.put(JsonConstants.GOOGLE_OAUTH_INTERVAL,
                                    calendarNotifManager.getOauthRegistration().getInterval());
                            registrationResponse.put(JsonConstants.GOOGLE_OAUTH_USERCODE,
                                    calendarNotifManager.getOauthRegistration().getUserCode());
                            registrationResponse.put(JsonConstants.GOOGLE_OAUTH_VERIFICATION_URL,
                                    calendarNotifManager.getOauthRegistration().getVerificationUrl());

                        }

                        System.out.println("[API] < " + registrationResponse.toJSONString());
                        client.sendMessage(registrationResponse.toJSONString());

                    } else if (action.equals(JsonConstants.API_TOKEN_STATE)) {

                        JSONObject requestTokenResponse = new JSONObject();

                        if (calendarNotifManager != null && calendarNotifManager.getCurrentToken() != null) {

                            requestTokenResponse.put(JsonConstants.GOOGLE_OAUTH_ACCESS_TOKEN,
                                    calendarNotifManager.getCurrentToken().getAccessToken());
                            requestTokenResponse.put(JsonConstants.GOOGLE_OAUTH_TOKEN_TYPE,
                                    calendarNotifManager.getCurrentToken().getTokenType());
                            requestTokenResponse.put(JsonConstants.GOOGLE_OAUTH_EXPIRE_IN,
                                    calendarNotifManager.getCurrentToken().getExpiresIn());

                        }
                        System.out.println("[API] < " + requestTokenResponse.toJSONString());
                        client.sendMessage(requestTokenResponse.toJSONString());

                    } else if (action.equals(JsonConstants.API_REGISTRATION)) {

                        calendarNotifManager = new CalendarNotifManager(clientId, clientSecret);

                        calendarNotifManager.requestDeviceAuth(new IOauthDeviceResponseListener() {

                            @Override
                            public void onResponseReceived(OauthForDeviceResponse response) {
                                if (response != null) {
                                    JSONObject registrationResponse = new JSONObject();
                                    registrationResponse.put(JsonConstants.GOOGLE_OAUTH_DEVICE_CODE,
                                            response.getDeviceCode());
                                    registrationResponse.put(JsonConstants.GOOGLE_OAUTH_EXPIRING_BEFORE,
                                            response.getExpiringBefore());
                                    registrationResponse.put(JsonConstants.GOOGLE_OAUTH_INTERVAL,
                                            response.getInterval());
                                    registrationResponse.put(JsonConstants.GOOGLE_OAUTH_USERCODE,
                                            response.getUserCode());
                                    registrationResponse.put(JsonConstants.GOOGLE_OAUTH_VERIFICATION_URL,
                                            response.getVerificationUrl());

                                    System.out.println("[API] < " + registrationResponse.toJSONString());

                                    client.sendMessage(registrationResponse.toJSONString());
                                }
                            }
                        });
                    } else if (action.equals(JsonConstants.API_REQUEST_TOKEN)) {

                        if (calendarNotifManager != null) {
                            calendarNotifManager.requestToken(new IRequestTokenListener() {

                                @Override
                                public void onRequestTokenReceived(OauthToken token) {
                                    if (token != null) {
                                        JSONObject requestTokenResponse = new JSONObject();
                                        requestTokenResponse.put(JsonConstants.GOOGLE_OAUTH_ACCESS_TOKEN,
                                                token.getAccessToken());
                                        requestTokenResponse.put(JsonConstants.GOOGLE_OAUTH_TOKEN_TYPE,
                                                token.getTokenType());
                                        requestTokenResponse.put(JsonConstants.GOOGLE_OAUTH_EXPIRE_IN,
                                                token.getExpiresIn());

                                        System.out.println("[API] < " + requestTokenResponse.toJSONString());

                                        client.sendMessage(requestTokenResponse.toJSONString());
                                    }
                                }

                                @Override
                                public void onRequestTokenError(String description) {
                                    String response = "{\"error\":\"request token error\",\"error_description\":\""
                                            + description + "\"}";

                                    System.out.println("[API] < " + response);

                                    client.sendMessage(response);
                                }
                            });
                        }
                    } else if (action.equals(JsonConstants.API_REVOKE_TOKEN)) {

                        if (calendarNotifManager != null) {
                            calendarNotifManager.revokeToken(new IRevokeTokenListener() {

                                @Override
                                public void onSuccess() {

                                    System.out.println("[API] < " + "{\"revokeToken\":\"success\"}");

                                    client.sendMessage("{\"revokeToken\":\"success\"}");
                                }

                                @Override
                                public void onError(String description) {

                                    String response = "{\"error\":\"request token error\",\"error_description\":\""
                                            + description + "\"}";

                                    System.out.println("[API] < " + response);

                                    client.sendMessage(response);
                                }
                            });
                        }
                    } else if (action.equals(JsonConstants.API_USER_PROFILE)) {
                        if (calendarNotifManager != null) {
                            calendarNotifManager.getUserProfileManager()
                                    .getUserProfile(new IUserProfileListener() {

                                        @Override
                                        public void onSuccess(UserProfile userProfile) {
                                            if (userProfile != null) {
                                                JSONObject userProfileResponse = new JSONObject();
                                                userProfileResponse.put(JsonConstants.GOOGLE_API_PROFILE_GENDER,
                                                        userProfile.getGender());
                                                userProfileResponse.put(
                                                        JsonConstants.GOOGLE_API_PROFILE_DISPLAY_NAME,
                                                        userProfile.getDisplayName());
                                                userProfileResponse.put(
                                                        JsonConstants.GOOGLE_API_PROFILE_FAMILY_NAME,
                                                        userProfile.getFamilyName());
                                                userProfileResponse.put(
                                                        JsonConstants.GOOGLE_API_PROFILE_GIVEN_NAME,
                                                        userProfile.getGivenName());
                                                userProfileResponse.put(
                                                        JsonConstants.GOOGLE_API_PROFILE_LANGUAGE,
                                                        userProfile.getLanguage());

                                                System.out.println(
                                                        "[API] < " + userProfileResponse.toJSONString());

                                                client.sendMessage(userProfileResponse.toJSONString());
                                            }
                                        }

                                        @Override
                                        public void onError(String description) {

                                            String response = "{\"error\":\"request token error\",\"error_description\":\""
                                                    + description + "\"}";

                                            System.out.println("[API] < " + response);

                                            client.sendMessage(response);
                                        }
                                    });
                        }
                    } else if (action.equals(JsonConstants.API_CREATE_EVENT)
                            && obj.containsKey(JsonConstants.API_DATE_BEGIN)
                            && obj.containsKey(JsonConstants.API_DATE_END)
                            && obj.containsKey(JsonConstants.API_SUMMARY)) {

                        String dateBegin = obj.get(JsonConstants.API_DATE_BEGIN).toString();
                        String dateEnd = obj.get(JsonConstants.API_DATE_END).toString();
                        String summary = obj.get(JsonConstants.API_SUMMARY).toString();

                        if (calendarNotifManager != null) {

                            calendarNotifManager.getCalendarManager().createEvent(dateBegin, dateEnd, summary,
                                    new ICreateEventListener() {

                                        @Override
                                        public void onError(String description) {

                                            String response = "{\"error\":\"request token error\",\"error_description\":\""
                                                    + description + "\"}";

                                            System.out.println("[API] < " + response);

                                            client.sendMessage(
                                                    "{\"error\":\"request token error\",\"error_description\":\""
                                                            + description + "\"}");
                                        }

                                        @Override
                                        public void onCreateSuccess(String id) {

                                            String response = "{\"createEvent\":\"success\",\"eventId\":\"" + id
                                                    + "\"}";

                                            System.out.println("[API] < " + response);

                                            client.sendMessage(response);
                                        }
                                    });
                        }
                    } else if (action.equals(JsonConstants.API_DELETE_EVENT)
                            && obj.containsKey(JsonConstants.API_EVENT_ID)) {

                        final String eventId = obj.get(JsonConstants.API_EVENT_ID).toString();

                        calendarNotifManager.getCalendarManager().deleteEvent(eventId,
                                new IDeleteEventListener() {

                                    @Override
                                    public void onSuccess() {

                                        String response = "{\"deleteEvent\":\"success\",\"eventId\":\""
                                                + eventId + "\"}";

                                        System.out.println("[API] < " + response);

                                        client.sendMessage(response);
                                    }

                                    @Override
                                    public void onError(String description) {

                                        String response = "{\"error\":\"request token error\",\"error_description\":\""
                                                + description + "\"}";

                                        System.out.println("[API] < " + response);

                                        client.sendMessage(response);
                                    }
                                });

                    } else if (action.equals(JsonConstants.API_GET_EVENTS)
                            && obj.containsKey(JsonConstants.API_DATE_BEGIN)
                            && obj.containsKey(JsonConstants.API_DATE_END) && obj.containsKey("searchText")
                            && calendarNotifManager != null) {

                        String dateBegin = obj.get(JsonConstants.API_DATE_BEGIN).toString();
                        String dateEnd = obj.get(JsonConstants.API_DATE_END).toString();
                        String searchText = obj.get(JsonConstants.API_SEARCH_TEXT).toString();

                        calendarNotifManager.getCalendarManager().getEventList(dateBegin, dateEnd, searchText,
                                new IEventListListener() {

                                    @Override
                                    public void onEventListReceived(List<CalendarEvents> calendarEventList) {

                                        String response = "{\"eventList\":" + CalendarUtils
                                                .convertCalendarListToJsonArray(calendarEventList)
                                                .toJSONString() + "}";

                                        System.out.println("[API] < " + response);

                                        client.sendMessage(response);
                                    }

                                    @Override
                                    public void onError(String description) {

                                        String response = "{\"error\":\"request token error\",\"error_description\":\""
                                                + description + "\"}";

                                        System.out.println("[API] < " + response);

                                        client.sendMessage(response);
                                    }
                                });

                    } else if (action.equals(JsonConstants.API_SUBSCRIBE_EVENT)
                            && obj.containsKey(JsonConstants.API_EVENT_ID)
                            && obj.containsKey(JsonConstants.API_TIME_ABOUT_TO_START)) {

                        String eventId = obj.get(JsonConstants.API_EVENT_ID).toString();
                        int timeAboutToStart = Integer
                                .parseInt(obj.get(JsonConstants.API_TIME_ABOUT_TO_START).toString());

                        calendarNotifManager.getNotificationManager().subscribeEvent(eventId, timeAboutToStart,
                                new IEventListener() {

                                    @Override
                                    public void onEventStart(String eventId, String summary) {

                                        String response = "{\"subscribedEvent\":\"" + eventId
                                                + "\",\"eventType\":\"started\",\"summary\":\"" + summary
                                                + "\"}";

                                        System.out.println("[API] < " + response);

                                        client.sendMessage(response);
                                    }

                                    @Override
                                    public void onEventAboutToStart(String eventId, String summary) {

                                        String response = "{\"subscribedEvent\":\"" + eventId
                                                + "\",\"eventType\":\"aboutToStart\",\"summary\":\"" + summary
                                                + "\"}";

                                        System.out.println("[API] < " + response);

                                        client.sendMessage(response);
                                    }
                                });

                    } else if (action.equals(JsonConstants.API_UNSUBSCRIBE_EVENT)
                            && obj.containsKey(JsonConstants.API_EVENT_ID)) {
                        String eventId = obj.get(JsonConstants.API_EVENT_ID).toString();

                        calendarNotifManager.getNotificationManager().unsubscribeEvent(eventId);
                    } else {
                        System.out.println("[API] Error api target is inconsistent");
                    }
                }
            }
        }

        @Override
        public void onClientConnection(IWebsocketClient client) {
            System.out.println("Websocket client connected");
        }

        @Override
        public void onClientClose(IWebsocketClient client) {
            System.out.println("Websocket client disconnected");
        }
    });

    Runnable websocketTask = new Runnable() {

        @Override
        public void run() {
            websocketServer.start();
        }
    };

    Thread thread = new Thread(websocketTask, "WEBSOCKET_THREAD");

    thread.start();

    server.addServerEventListener(new IHttpServerEventListener() {

        @Override
        public void onHttpFrameReceived(IHttpFrame httpFrame, HttpStates receptionStates,
                IHttpStream httpStream) {

            // check if http frame is OK
            if (receptionStates == HttpStates.HTTP_FRAME_OK) {

                // you can check here http frame type (response or request
                // frame)
                if (httpFrame.isHttpRequestFrame()) {

                    // we want to send a message to client for http GET
                    // request on page with uri /index
                    if (httpFrame.getMethod().equals("GET") && httpFrame.getUri().equals("/gcalendar")) {

                        String defaultPage = "";
                        try {
                            defaultPage = FileUtils.readFile(webPath + "/index.html", "UTF-8");
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                        // return default html page for this HTTP Server
                        httpStream.writeHttpFrame(new HttpResponseFrame(StatusCodeList.OK,
                                new HttpVersion(1, 1), new HashMap<String, String>(), defaultPage.getBytes())
                                        .toString().getBytes());

                    } else if (httpFrame.getMethod().equals("GET")
                            && (httpFrame.getUri().endsWith(".css") || httpFrame.getUri().endsWith(".js"))) {

                        String defaultPage = "";
                        try {
                            defaultPage = FileUtils.readFile(webPath + httpFrame.getUri(), "UTF-8");
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                        // return default html page for this HTTP Server
                        httpStream.writeHttpFrame(new HttpResponseFrame(StatusCodeList.OK,
                                new HttpVersion(1, 1), new HashMap<String, String>(), defaultPage.getBytes())
                                        .toString().getBytes());
                    }
                }
            }
        }
    });

    server.start();
    thread.interrupt();

}

From source file:com.emc.ecs.smart.SmartUploader.java

/**
 * Use commons-cli to parse command line arguments and start the upload.
 *///from   w  w w .jav a 2s . c  o  m
public static void main(String[] args) {
    Options opts = new Options();

    // Optional params
    opts.addOption(Option.builder().longOpt(THREADS_OPT).hasArg().argName("thread-count")
            .desc("Sets the number of threads to upload concurrently.  Default is " + DEFAULT_THREADS).build());
    opts.addOption(Option.builder().longOpt(SIMPLE_OPT)
            .desc("For comparison purposes, do a simple single-stream upload instead of a concurrent upload.")
            .build());
    opts.addOption(Option.builder().longOpt(SEGMENT_SIZE_OPT).hasArg().argName("bytes")
            .desc("Size of each upload segment in bytes.  Defaults to 2MB for uploads less than 128MB and "
                    + "128MB for objects greater than or equal to 128MB.")
            .build());
    opts.addOption(Option.builder().longOpt(FILE_OPT).hasArg().argName("path-to-file").required()
            .desc("(Required) The path to the file to upload.").build());
    opts.addOption(Option.builder().longOpt(URL_OPT).hasArg().argName("presigned-upload-url").required()
            .desc("URL used to PUT the file to create an object").build());
    opts.addOption(Option.builder().longOpt(CONTENT_TYPE_OPT).hasArg().argName("mimetype")
            .desc("Value to use for the Content-Type header.  Defaults to application/octet-stream.").build());
    opts.addOption(Option.builder().longOpt(VERIFY_URL_OPT).hasArg().argName("url")
            .desc("If specified, read back the object from this URL and compare with the local file.").build());
    opts.addOption(Option.builder().longOpt(LOG_LEVEL_OPT).hasArg().argName("level").type(Level.class)
            .desc("Sets the log level: DEBUG, INFO, WARN, ERROR, or FATAL.  Default is ERROR.").build());
    opts.addOption(Option.builder().longOpt(LOG_FILE_OPT).hasArg().argName("filename")
            .desc("Filename to write log messages.  Setting to STDOUT or STDERR will write log messages to the "
                    + "appropriate process stream.  Default is STDERR.")
            .build());
    opts.addOption(Option.builder().longOpt(LOG_PATTERN_OPT).hasArg().argName("log4j-pattern")
            .desc("Sets the " + "Log4J pattern to use when writing log messages.  Defaults to "
                    + "'%d{yyyy-MM-dd HH:mm:ss} %-5p [%t] %c{1}:%L - %m%n'")
            .build());
    opts.addOption(Option.builder().longOpt(STANDARD_MD5)
            .desc("Use standard Java IO for local checksum " + "instead of NIO").build());
    opts.addOption(Option.builder().longOpt(RETRY_DELAY_OPT).hasArg().argName("milliseconds")
            .desc("Initial retry delay in ms.  Subsequent retries will continue to double this exponentially.  "
                    + "The default value is " + RETRY_DELAY)
            .build());
    opts.addOption(Option.builder().longOpt(RETRY_COUNT_OPT).hasArg().argName("count")
            .desc("Number of times to "
                    + "retry failed transactions.  Set to zero to disable retries.  Defaults to " + RETRY_COUNT)
            .build());
    opts.addOption(Option.builder().longOpt(SAVE_MD5).hasArg().argName("path-to-file")
            .desc("Sets the file location where the local MD5 hash will be written.").build());

    DefaultParser dp = new DefaultParser();

    CommandLine cmd = null;
    try {
        cmd = dp.parse(opts, args);
    } catch (ParseException e) {
        System.err.println("Error: " + e.getMessage());
        HelpFormatter hf = new HelpFormatter();
        hf.printHelp("java -jar SmartUploader.jar", opts, true);
        System.exit(255);
    }

    // Required options.
    String url = cmd.getOptionValue(URL_OPT);
    String file = cmd.getOptionValue(FILE_OPT);

    SmartUploader uploader = null;
    try {
        uploader = new SmartUploader(new URL(url), new File(file).toPath());
    } catch (MalformedURLException e) {
        System.err.println("Could not parse upload URL: " + e.getMessage());
        System.exit(2);
    }

    if (cmd.hasOption(SEGMENT_SIZE_OPT)) {
        uploader.setSegmentSize(Integer.parseInt(cmd.getOptionValue(SEGMENT_SIZE_OPT)));
    }
    if (cmd.hasOption(THREADS_OPT)) {
        uploader.setThreadCount(Integer.parseInt(cmd.getOptionValue(THREADS_OPT)));
    }
    if (cmd.hasOption(SIMPLE_OPT)) {
        uploader.setSimpleMode(true);
    }
    if (cmd.hasOption(CONTENT_TYPE_OPT)) {
        uploader.setContentType(cmd.getOptionValue(CONTENT_TYPE_OPT));
    }

    if (cmd.hasOption(VERIFY_URL_OPT)) {
        try {
            uploader.setVerifyUrl(new URL(cmd.getOptionValue(VERIFY_URL_OPT)));
        } catch (MalformedURLException e) {
            System.err.println("Could not parse verify URL: " + e.getMessage());
            System.exit(3);
        }
    }

    if (cmd.hasOption(STANDARD_MD5)) {
        uploader.setStandardChecksum(true);
    }

    if (cmd.hasOption(RETRY_COUNT_OPT)) {
        uploader.setRetryCount(Integer.parseInt(cmd.getOptionValue(RETRY_COUNT_OPT)));
    }

    if (cmd.hasOption(RETRY_DELAY_OPT)) {
        uploader.setRetryDelay(Integer.parseInt(cmd.getOptionValue(RETRY_DELAY_OPT)));
    }

    if (cmd.hasOption(SAVE_MD5)) {
        uploader.setSaveMD5(cmd.getOptionValue(SAVE_MD5));
    }

    //
    // Configure Logging
    //

    // Pattern
    String layoutString = "%d{yyyy-MM-dd HH:mm:ss} %-5p [%t] %c{1}:%L - %m%n";
    if (cmd.hasOption(LOG_PATTERN_OPT)) {
        layoutString = cmd.getOptionValue(LOG_PATTERN_OPT);
    }
    PatternLayout layout = new PatternLayout(layoutString);

    // Appender
    String logFileName = "STDERR";
    if (cmd.hasOption(LOG_FILE_OPT)) {
        logFileName = cmd.getOptionValue(LOG_FILE_OPT);
    }
    Appender appender = null;
    if (logFileName.equals("STDERR")) {
        appender = new ConsoleAppender(layout, "System.err");
    } else if (logFileName.equals("STDOUT")) {
        appender = new ConsoleAppender(layout, "System.out");
    } else {
        // Just a regular file.
        try {
            appender = new FileAppender(layout, logFileName);
        } catch (IOException e) {
            System.err.println("FATAL: Could not configure appender");
            e.printStackTrace();
            System.exit(8);
        }
    }
    LogManager.getRootLogger().addAppender(appender);

    // Log level
    String logLevel = "ERROR";
    if (cmd.hasOption(LOG_LEVEL_OPT)) {
        logLevel = cmd.getOptionValue(LOG_LEVEL_OPT);
    }
    LogManager.getRootLogger().setLevel(Level.toLevel(logLevel));

    //
    // Start the upload
    //
    uploader.doUpload();

    System.exit(0);
}

From source file:com.oltpbenchmark.DBWorkload.java

/**
 * @param args/*w w  w .  ja va  2 s .co m*/
 * @throws Exception 
 */
public static void main(String[] args) throws Exception {
    // Initialize log4j
    String log4jPath = System.getProperty("log4j.configuration");
    if (log4jPath != null) {
        org.apache.log4j.PropertyConfigurator.configure(log4jPath);
    } else {
        throw new RuntimeException("Missing log4j.properties file");
    }

    // create the command line parser
    CommandLineParser parser = new PosixParser();
    XMLConfiguration pluginConfig = null;
    try {
        pluginConfig = new XMLConfiguration("config/plugin.xml");
    } catch (ConfigurationException e1) {
        LOG.info("Plugin configuration file config/plugin.xml is missing");
        e1.printStackTrace();
    }
    pluginConfig.setExpressionEngine(new XPathExpressionEngine());
    Options options = new Options();
    options.addOption("b", "bench", true,
            "[required] Benchmark class. Currently supported: " + pluginConfig.getList("/plugin//@name"));
    options.addOption("c", "config", true, "[required] Workload configuration file");
    options.addOption(null, "create", true, "Initialize the database for this benchmark");
    options.addOption(null, "clear", true, "Clear all records in the database for this benchmark");
    options.addOption(null, "load", true, "Load data using the benchmark's data loader");
    options.addOption(null, "execute", true, "Execute the benchmark workload");
    options.addOption(null, "runscript", true, "Run an SQL script");
    options.addOption(null, "upload", true, "Upload the result");

    options.addOption("v", "verbose", false, "Display Messages");
    options.addOption("h", "help", false, "Print this help");
    options.addOption("s", "sample", true, "Sampling window");
    options.addOption("ss", false, "Verbose Sampling per Transaction");
    options.addOption("o", "output", true, "Output file (default System.out)");
    options.addOption("d", "directory", true,
            "Base directory for the result files, default is current directory");
    options.addOption("t", "timestamp", false,
            "Each result file is prepended with a timestamp for the beginning of the experiment");
    options.addOption(null, "histograms", false, "Print txn histograms");
    options.addOption(null, "dialects-export", true, "Export benchmark SQL to a dialects file");

    // parse the command line arguments
    CommandLine argsLine = parser.parse(options, args);
    if (argsLine.hasOption("h")) {
        printUsage(options);
        return;
    } else if (argsLine.hasOption("c") == false) {
        LOG.error("Missing Configuration file");
        printUsage(options);
        return;
    } else if (argsLine.hasOption("b") == false) {
        LOG.fatal("Missing Benchmark Class to load");
        printUsage(options);
        return;
    }

    // If an output directory is used, store the information
    String outputDirectory = "";
    if (argsLine.hasOption("d")) {
        outputDirectory = argsLine.getOptionValue("d");
    }

    String timestampValue = "";
    if (argsLine.hasOption("t")) {
        timestampValue = String.valueOf(TimeUtil.getCurrentTime().getTime()) + "_";
    }

    //       -------------------------------------------------------------------
    //        GET PLUGIN LIST
    //       -------------------------------------------------------------------

    String plugins = argsLine.getOptionValue("b");

    String[] pluginList = plugins.split(",");
    List<BenchmarkModule> benchList = new ArrayList<BenchmarkModule>();

    // Use this list for filtering of the output
    List<TransactionType> activeTXTypes = new ArrayList<TransactionType>();

    String configFile = argsLine.getOptionValue("c");
    XMLConfiguration xmlConfig = new XMLConfiguration(configFile);
    xmlConfig.setExpressionEngine(new XPathExpressionEngine());
    int lastTxnId = 0;

    for (String plugin : pluginList) {

        // ----------------------------------------------------------------
        // WORKLOAD CONFIGURATION
        // ----------------------------------------------------------------

        String pluginTest = "";

        pluginTest = "[@bench='" + plugin + "']";

        WorkloadConfiguration wrkld = new WorkloadConfiguration();
        wrkld.setBenchmarkName(plugin);
        wrkld.setXmlConfig(xmlConfig);
        wrkld.setDBType(DatabaseType.get(xmlConfig.getString("dbtype")));
        wrkld.setDBDriver(xmlConfig.getString("driver"));
        wrkld.setDBConnection(xmlConfig.getString("DBUrl"));
        wrkld.setDBName(xmlConfig.getString("DBName"));
        wrkld.setDBUsername(xmlConfig.getString("username"));
        wrkld.setDBPassword(xmlConfig.getString("password"));
        int terminals = xmlConfig.getInt("terminals[not(@bench)]", 0);
        terminals = xmlConfig.getInt("terminals" + pluginTest, terminals);
        wrkld.setTerminals(terminals);
        wrkld.setIsolationMode(xmlConfig.getString("isolation", "TRANSACTION_SERIALIZABLE"));
        wrkld.setScaleFactor(xmlConfig.getDouble("scalefactor", 1.0));
        wrkld.setRecordAbortMessages(xmlConfig.getBoolean("recordabortmessages", false));

        int size = xmlConfig.configurationsAt("/works/work").size();
        for (int i = 1; i < size + 1; i++) {
            SubnodeConfiguration work = xmlConfig.configurationAt("works/work[" + i + "]");
            List<String> weight_strings;

            // use a workaround if there multiple workloads or single
            // attributed workload
            if (pluginList.length > 1 || work.containsKey("weights[@bench]")) {
                weight_strings = get_weights(plugin, work);
            } else {
                weight_strings = work.getList("weights[not(@bench)]");
            }
            int rate = 1;
            boolean rateLimited = true;
            boolean disabled = false;

            // can be "disabled", "unlimited" or a number
            String rate_string;
            rate_string = work.getString("rate[not(@bench)]", "");
            rate_string = work.getString("rate" + pluginTest, rate_string);
            if (rate_string.equals(RATE_DISABLED)) {
                disabled = true;
            } else if (rate_string.equals(RATE_UNLIMITED)) {
                rateLimited = false;
            } else if (rate_string.isEmpty()) {
                LOG.fatal(String.format("Please specify the rate for phase %d and workload %s", i, plugin));
                System.exit(-1);
            } else {
                try {
                    rate = Integer.parseInt(rate_string);
                    if (rate < 1) {
                        LOG.fatal("Rate limit must be at least 1. Use unlimited or disabled values instead.");
                        System.exit(-1);
                    }
                } catch (NumberFormatException e) {
                    LOG.fatal(String.format("Rate string must be '%s', '%s' or a number", RATE_DISABLED,
                            RATE_UNLIMITED));
                    System.exit(-1);
                }
            }
            Phase.Arrival arrival = Phase.Arrival.REGULAR;
            String arrive = work.getString("@arrival", "regular");
            if (arrive.toUpperCase().equals("POISSON"))
                arrival = Phase.Arrival.POISSON;

            int activeTerminals;
            activeTerminals = work.getInt("active_terminals[not(@bench)]", terminals);
            activeTerminals = work.getInt("active_terminals" + pluginTest, activeTerminals);
            if (activeTerminals > terminals) {
                System.out.println("Configuration error in work " + i + ": number of active terminals" + ""
                        + "is bigger than the total number of terminals");
                System.exit(-1);
            }
            wrkld.addWork(work.getInt("/time"), rate, weight_strings, rateLimited, disabled, activeTerminals,
                    arrival);
        } // FOR

        int numTxnTypes = xmlConfig.configurationsAt("transactiontypes" + pluginTest + "/transactiontype")
                .size();
        if (numTxnTypes == 0 && pluginList.length == 1) {
            //if it is a single workload run, <transactiontypes /> w/o attribute is used
            pluginTest = "[not(@bench)]";
            numTxnTypes = xmlConfig.configurationsAt("transactiontypes" + pluginTest + "/transactiontype")
                    .size();
        }
        wrkld.setNumTxnTypes(numTxnTypes);

        // CHECKING INPUT PHASES
        int j = 0;
        for (Phase p : wrkld.getAllPhases()) {
            j++;
            if (p.getWeightCount() != wrkld.getNumTxnTypes()) {
                LOG.fatal(String.format(
                        "Configuration files is inconsistent, phase %d contains %d weights but you defined %d transaction types",
                        j, p.getWeightCount(), wrkld.getNumTxnTypes()));
                System.exit(-1);
            }
        } // FOR

        // Generate the dialect map
        wrkld.init();

        assert (wrkld.getNumTxnTypes() >= 0);
        assert (xmlConfig != null);

        // ----------------------------------------------------------------
        // BENCHMARK MODULE
        // ----------------------------------------------------------------

        String classname = pluginConfig.getString("/plugin[@name='" + plugin + "']");

        if (classname == null) {
            throw new ParseException("Plugin " + plugin + " is undefined in config/plugin.xml");
        }
        BenchmarkModule bench = ClassUtil.newInstance(classname, new Object[] { wrkld },
                new Class<?>[] { WorkloadConfiguration.class });
        assert (benchList.get(0) != null);

        Map<String, Object> initDebug = new ListOrderedMap<String, Object>();
        initDebug.put("Benchmark", String.format("%s {%s}", plugin.toUpperCase(), classname));
        initDebug.put("Configuration", configFile);
        initDebug.put("Type", wrkld.getDBType());
        initDebug.put("Driver", wrkld.getDBDriver());
        initDebug.put("URL", wrkld.getDBConnection());
        initDebug.put("Isolation", xmlConfig.getString("isolation", "TRANSACTION_SERIALIZABLE [DEFAULT]"));
        initDebug.put("Scale Factor", wrkld.getScaleFactor());
        INIT_LOG.info(SINGLE_LINE + "\n\n" + StringUtil.formatMaps(initDebug));
        INIT_LOG.info(SINGLE_LINE);

        // Load TransactionTypes
        List<TransactionType> ttypes = new ArrayList<TransactionType>();

        // Always add an INVALID type for Carlo
        ttypes.add(TransactionType.INVALID);
        int txnIdOffset = lastTxnId;
        for (int i = 1; i < wrkld.getNumTxnTypes() + 1; i++) {
            String key = "transactiontypes" + pluginTest + "/transactiontype[" + i + "]";
            String txnName = xmlConfig.getString(key + "/name");
            int txnId = i + 1;
            if (xmlConfig.containsKey(key + "/id")) {
                txnId = xmlConfig.getInt(key + "/id");
            }
            TransactionType tmpType = bench.initTransactionType(txnName, txnId + txnIdOffset);
            // Keep a reference for filtering
            activeTXTypes.add(tmpType);
            // Add a reference for the active TTypes in this benchmark
            ttypes.add(tmpType);
            lastTxnId = i;
        } // FOR
        TransactionTypes tt = new TransactionTypes(ttypes);
        wrkld.setTransTypes(tt);
        LOG.debug("Using the following transaction types: " + tt);

        benchList.add(bench);
    }

    // Export StatementDialects
    if (isBooleanOptionSet(argsLine, "dialects-export")) {
        BenchmarkModule bench = benchList.get(0);
        if (bench.getStatementDialects() != null) {
            LOG.info("Exporting StatementDialects for " + bench);
            String xml = bench.getStatementDialects().export(bench.getWorkloadConfiguration().getDBType(),
                    bench.getProcedures().values());
            System.out.println(xml);
            System.exit(0);
        }
        throw new RuntimeException("No StatementDialects is available for " + bench);
    }

    @Deprecated
    boolean verbose = argsLine.hasOption("v");

    // Create the Benchmark's Database
    if (isBooleanOptionSet(argsLine, "create")) {
        for (BenchmarkModule benchmark : benchList) {
            CREATE_LOG.info("Creating new " + benchmark.getBenchmarkName().toUpperCase() + " database...");
            runCreator(benchmark, verbose);
            CREATE_LOG.info("Finished!");
            CREATE_LOG.info(SINGLE_LINE);
        }
    } else if (CREATE_LOG.isDebugEnabled()) {
        CREATE_LOG.debug("Skipping creating benchmark database tables");
        CREATE_LOG.info(SINGLE_LINE);
    }

    // Clear the Benchmark's Database
    if (isBooleanOptionSet(argsLine, "clear")) {
        for (BenchmarkModule benchmark : benchList) {
            CREATE_LOG.info("Resetting " + benchmark.getBenchmarkName().toUpperCase() + " database...");
            benchmark.clearDatabase();
            CREATE_LOG.info("Finished!");
            CREATE_LOG.info(SINGLE_LINE);
        }
    } else if (CREATE_LOG.isDebugEnabled()) {
        CREATE_LOG.debug("Skipping creating benchmark database tables");
        CREATE_LOG.info(SINGLE_LINE);
    }

    // Execute Loader
    if (isBooleanOptionSet(argsLine, "load")) {
        for (BenchmarkModule benchmark : benchList) {
            LOAD_LOG.info("Loading data into " + benchmark.getBenchmarkName().toUpperCase() + " database...");
            runLoader(benchmark, verbose);
            LOAD_LOG.info("Finished!");
            LOAD_LOG.info(SINGLE_LINE);
        }
    } else if (LOAD_LOG.isDebugEnabled()) {
        LOAD_LOG.debug("Skipping loading benchmark database records");
        LOAD_LOG.info(SINGLE_LINE);
    }

    // Execute a Script
    if (argsLine.hasOption("runscript")) {
        for (BenchmarkModule benchmark : benchList) {
            String script = argsLine.getOptionValue("runscript");
            SCRIPT_LOG.info("Running a SQL script: " + script);
            runScript(benchmark, script);
            SCRIPT_LOG.info("Finished!");
            SCRIPT_LOG.info(SINGLE_LINE);
        }
    }

    // Execute Workload
    if (isBooleanOptionSet(argsLine, "execute")) {
        // Bombs away!
        Results r = null;
        try {
            r = runWorkload(benchList, verbose);
        } catch (Throwable ex) {
            LOG.error("Unexpected error when running benchmarks.", ex);
            System.exit(1);
        }
        assert (r != null);

        PrintStream ps = System.out;
        PrintStream rs = System.out;
        ResultUploader ru = new ResultUploader(r, xmlConfig, argsLine);

        if (argsLine.hasOption("o")) {
            // Check if directory needs to be created
            if (outputDirectory.length() > 0) {
                FileUtil.makeDirIfNotExists(outputDirectory.split("/"));
            }

            // Build the complex path
            String baseFile = timestampValue + argsLine.getOptionValue("o");

            // Increment the filename for new results
            String nextName = FileUtil.getNextFilename(FileUtil.joinPath(outputDirectory, baseFile + ".res"));
            ps = new PrintStream(new File(nextName));
            EXEC_LOG.info("Output into file: " + nextName);

            nextName = FileUtil.getNextFilename(FileUtil.joinPath(outputDirectory, baseFile + ".raw"));
            rs = new PrintStream(new File(nextName));
            EXEC_LOG.info("Output Raw data into file: " + nextName);

            nextName = FileUtil.getNextFilename(FileUtil.joinPath(outputDirectory, baseFile + ".summary"));
            PrintStream ss = new PrintStream(new File(nextName));
            EXEC_LOG.info("Output summary data into file: " + nextName);
            ru.writeSummary(ss);
            ss.close();

            nextName = FileUtil.getNextFilename(FileUtil.joinPath(outputDirectory, baseFile + ".db.cnf"));
            ss = new PrintStream(new File(nextName));
            EXEC_LOG.info("Output db config into file: " + nextName);
            ru.writeDBParameters(ss);
            ss.close();

            nextName = FileUtil.getNextFilename(FileUtil.joinPath(outputDirectory, baseFile + ".ben.cnf"));
            ss = new PrintStream(new File(nextName));
            EXEC_LOG.info("Output benchmark config into file: " + nextName);
            ru.writeBenchmarkConf(ss);
            ss.close();
        } else if (EXEC_LOG.isDebugEnabled()) {
            EXEC_LOG.debug("No output file specified");
        }

        if (argsLine.hasOption("s")) {
            int windowSize = Integer.parseInt(argsLine.getOptionValue("s"));
            EXEC_LOG.info("Grouped into Buckets of " + windowSize + " seconds");
            r.writeCSV(windowSize, ps);

            if (isBooleanOptionSet(argsLine, "upload")) {
                ru.uploadResult();
            }

            // Allow more detailed reporting by transaction to make it easier to check
            if (argsLine.hasOption("ss")) {

                for (TransactionType t : activeTXTypes) {
                    PrintStream ts = ps;

                    if (ts != System.out) {
                        // Get the actual filename for the output
                        String baseFile = timestampValue + argsLine.getOptionValue("o") + "_" + t.getName();
                        String prepended = outputDirectory + timestampValue;
                        String nextName = FileUtil
                                .getNextFilename(FileUtil.joinPath(outputDirectory, baseFile + ".res"));
                        ts = new PrintStream(new File(nextName));
                        r.writeCSV(windowSize, ts, t);
                        ts.close();
                    }
                }
            }
        } else if (EXEC_LOG.isDebugEnabled()) {
            EXEC_LOG.warn("No bucket size specified");
        }
        if (argsLine.hasOption("histograms")) {
            EXEC_LOG.info(SINGLE_LINE);
            EXEC_LOG.info("Completed Transactions:\n" + r.getTransactionSuccessHistogram() + "\n");
            EXEC_LOG.info("Aborted Transactions:\n" + r.getTransactionAbortHistogram() + "\n");
            EXEC_LOG.info("Rejected Transactions:\n" + r.getTransactionRetryHistogram());
            EXEC_LOG.info("Unexpected Errors:\n" + r.getTransactionErrorHistogram());
            if (r.getTransactionAbortMessageHistogram().isEmpty() == false)
                EXEC_LOG.info(
                        "User Aborts:\n" + StringUtil.formatMaps(r.getTransactionAbortMessageHistogram()));
        } else if (EXEC_LOG.isDebugEnabled()) {
            EXEC_LOG.warn("No bucket size specified");
        }

        r.writeAllCSVAbsoluteTiming(rs);

        ps.close();
        rs.close();
    } else {
        EXEC_LOG.info("Skipping benchmark workload execution");
    }
}

From source file:com.github.horrorho.inflatabledonkey.Main.java

/**
 * @param args the command line arguments
 * @throws IOException/*w w  w .  j av  a 2s  .c om*/
 */
public static void main(String[] args) throws IOException {
    try {
        if (!PropertyLoader.instance().test(args)) {
            return;
        }
    } catch (IllegalArgumentException ex) {
        System.out.println("Argument error: " + ex.getMessage());
        System.out.println("Try '" + Property.APP_NAME.value() + " --help' for more information.");
        System.exit(-1);
    }

    // SystemDefault HttpClient.
    // TODO concurrent
    CloseableHttpClient httpClient = HttpClients.custom().setUserAgent("CloudKit/479 (13A404)")
            .useSystemProperties().build();

    // Auth
    // TODO rework when we have UncheckedIOException for Authenticator
    Auth auth = Property.AUTHENTICATION_TOKEN.value().map(Auth::new).orElse(null);

    if (auth == null) {
        auth = Authenticator.authenticate(httpClient, Property.AUTHENTICATION_APPLEID.value().get(),
                Property.AUTHENTICATION_PASSWORD.value().get());
    }
    logger.debug("-- main() - auth: {}", auth);
    logger.info("-- main() - dsPrsID:mmeAuthToken: {}:{}", auth.dsPrsID(), auth.mmeAuthToken());

    if (Property.ARGS_TOKEN.booleanValue().orElse(false)) {
        System.out.println("DsPrsID:mmeAuthToken " + auth.dsPrsID() + ":" + auth.mmeAuthToken());
        return;
    }

    logger.info("-- main() - Apple ID: {}", Property.AUTHENTICATION_APPLEID.value());
    logger.info("-- main() - password: {}", Property.AUTHENTICATION_PASSWORD.value());
    logger.info("-- main() - token: {}", Property.AUTHENTICATION_TOKEN.value());

    // Account
    Account account = Accounts.account(httpClient, auth);

    // Backup
    Backup backup = Backup.create(httpClient, account);

    // BackupAccount
    BackupAccount backupAccount = backup.backupAccount(httpClient);
    logger.debug("-- main() - backup account: {}", backupAccount);

    // Devices
    List<Device> devices = backup.devices(httpClient, backupAccount.devices());
    logger.debug("-- main() - device count: {}", devices.size());

    // Snapshots
    List<SnapshotID> snapshotIDs = devices.stream().map(Device::snapshots).flatMap(Collection::stream)
            .collect(Collectors.toList());
    logger.info("-- main() - total snapshot count: {}", snapshotIDs.size());

    Map<String, Snapshot> snapshots = backup.snapshot(httpClient, snapshotIDs).stream().collect(
            Collectors.toMap(s -> s.record().getRecordIdentifier().getValue().getName(), Function.identity()));

    boolean repeat = false;
    do {

        for (int i = 0; i < devices.size(); i++) {
            Device device = devices.get(i);
            List<SnapshotID> deviceSnapshotIDs = device.snapshots();

            System.out.println(i + " " + device.info());

            for (int j = 0; j < deviceSnapshotIDs.size(); j++) {
                SnapshotID sid = deviceSnapshotIDs.get(j);
                System.out.println("\t" + j + snapshots.get(sid.id()).info() + "   " + sid.timestamp());
            }
        }
        if (Property.PRINT_SNAPSHOTS.booleanValue().orElse(false)) {
            return;
        }
        // Selection
        Scanner input = new Scanner(System.in);

        int deviceIndex;
        int snapshotIndex = Property.SELECT_SNAPSHOT_INDEX.intValue().get();

        if (devices.size() > 1) {
            System.out.printf("Select a device [0 - %d]: ", devices.size() - 1);
            deviceIndex = input.nextInt();
        } else
            deviceIndex = Property.SELECT_DEVICE_INDEX.intValue().get();

        if (deviceIndex >= devices.size() || deviceIndex < 0) {
            System.out.println("No such device: " + deviceIndex);
            System.exit(-1);
        }

        Device device = devices.get(deviceIndex);
        System.out.println("Selected device: " + deviceIndex + ", " + device.info());

        if (device.snapshots().size() > 1) {
            System.out.printf("Select a snapshot [0 - %d]: ", device.snapshots().size() - 1);
            snapshotIndex = input.nextInt();
        } else
            snapshotIndex = Property.SELECT_SNAPSHOT_INDEX.intValue().get();

        if (snapshotIndex >= devices.get(deviceIndex).snapshots().size() || snapshotIndex < 0) {
            System.out.println("No such snapshot for selected device: " + snapshotIndex);
            System.exit(-1);
        }

        logger.info("-- main() - arg device index: {}", deviceIndex);
        logger.info("-- main() - arg snapshot index: {}", snapshotIndex);

        String selected = devices.get(deviceIndex).snapshots().get(snapshotIndex).id();
        Snapshot snapshot = snapshots.get(selected);
        System.out.println("Selected snapshot: " + snapshotIndex + ", " + snapshot.info());

        // Asset list.
        List<Assets> assetsList = backup.assetsList(httpClient, snapshot);
        logger.info("-- main() - assets count: {}", assetsList.size());

        // Domains filter --domain option
        String chosenDomain = Property.FILTER_DOMAIN.value().orElse("").toLowerCase(Locale.US);
        logger.info("-- main() - arg domain substring filter: {}", Property.FILTER_DOMAIN.value());
        // Output domains --domains option
        if (Property.PRINT_DOMAIN_LIST.booleanValue().orElse(false)) {
            System.out.println("Domains / file count:");
            assetsList.stream().filter(a -> a.domain().isPresent())
                    .map(a -> a.domain().get() + " / " + a.files().size()).sorted()
                    .forEach(System.out::println);

            System.out.print("Type a domain ('null' to exit): ");
            chosenDomain = input.next().toLowerCase(Locale.US);
            if (chosenDomain.equals("null"))
                return;
            // TODO check Assets without domain information.
        }

        String domainSubstring = chosenDomain;

        Predicate<Optional<String>> domainFilter = domain -> domain.map(d -> d.toLowerCase(Locale.US))
                .map(d -> d.contains(domainSubstring)).orElse(false);

        List<String> files = Assets.files(assetsList, domainFilter);
        logger.info("-- main() - domain filtered file count: {}", files.size());

        // Output folders.
        Path outputFolder = Paths.get(Property.OUTPUT_FOLDER.value().orElse("output"));
        Path assetOutputFolder = outputFolder.resolve("assets"); // TODO assets value injection
        Path chunkOutputFolder = outputFolder.resolve("chunks"); // TODO chunks value injection
        logger.info("-- main() - output folder chunks: {}", chunkOutputFolder);
        logger.info("-- main() - output folder assets: {}", assetOutputFolder);

        // Download tools.
        AuthorizeAssets authorizeAssets = AuthorizeAssets.backupd();
        DiskChunkStore chunkStore = new DiskChunkStore(chunkOutputFolder);
        StandardChunkEngine chunkEngine = new StandardChunkEngine(chunkStore);
        AssetDownloader assetDownloader = new AssetDownloader(chunkEngine);
        KeyBagManager keyBagManager = backup.newKeyBagManager();

        // Mystery Moo. 
        Moo moo = new Moo(authorizeAssets, assetDownloader, keyBagManager);

        // Filename extension filter.
        String filenameExtension = Property.FILTER_EXTENSION.value().orElse("").toLowerCase(Locale.US);
        logger.info("-- main() - arg filename extension filter: {}", Property.FILTER_EXTENSION.value());

        Predicate<Asset> assetFilter = asset -> asset.relativePath().map(d -> d.toLowerCase(Locale.US))
                .map(d -> d.endsWith(filenameExtension)).orElse(false);

        // Batch process files in groups of 100.
        // TODO group files into batches based on file size.
        List<List<String>> batches = ListUtils.partition(files, 100);

        for (List<String> batch : batches) {
            List<Asset> assets = backup.assets(httpClient, batch).stream().filter(assetFilter::test)
                    .collect(Collectors.toList());
            logger.info("-- main() - filtered asset count: {}", assets.size());
            moo.download(httpClient, assets, assetOutputFolder);
        }
        System.out.print("Download other snapshot (Y/N)? ");
        repeat = input.next().toLowerCase(Locale.US).charAt(0) == 'y';
    } while (repeat == true);
}

From source file:com.google.api.services.consumersurveys.App.java

public static void main(String[] args) throws Exception {
    ArgumentParser parser = ArgumentParsers.newArgumentParser("App").defaultHelp(true)
            .description("Create and modify surveys.");
    parser.addArgument("-o", "--option").choices(OPTIONS).help("The operation to perform.");
    parser.addArgument("-oe", "--owner_email").nargs("*")
            .help("Specify owners' email to use to create surveys.");
    parser.addArgument("-s", "--survey_id").help("survey id to start the survey");
    parser.addArgument("-rf", "--result_file").setDefault("results.xls")
            .help("filename to store excel results");
    parser.addArgument("-se", "--service_account_email").type(String.class)
            .help("Specify a bot email to use for auth.");

    Namespace ns = null;/*from www.  java 2s . c o  m*/
    try {
        ns = parser.parseArgs(args);
    } catch (ArgumentParserException e) {
        parser.handleError(e);
        System.exit(1);
    }

    String option = ns.getString("option");
    String serviceAccountEmail = ns.getString("service_account_email");
    List<String> owners = ns.<String>getList("owner_email");
    String resultFile = ns.getString("result_file");
    String surveyId = ns.getString("survey_id");

    if (serviceAccountEmail == null) {
        System.out.println(
                "\n\nMissing serviceAccountEmail. " + "serviceAccountEmail is necssary for authenication");
        System.exit(1);
    }

    if (option == null) {
        System.out.println("\n\nMissing option. " + "You must use one of these options: " + OPTIONS);
        System.exit(1);
    }

    Consumersurveys cs = getConsumerSurverysService(serviceAccountEmail);

    try {
        if (option.equals(CREATE)) {
            if (owners == null) {
                System.out.println("\n\nMissing owners. " + "You must specify owners to create a survey.");
                System.exit(1);
            }
            Survey survey = createSurvey(cs, owners);
        }
        if (option.equals(START)) {
            if (surveyId == null) {
                System.out.println("\n\nMissing surveyId. " + "You must specify surveyId to start a survey.");
                System.exit(1);
            }
            startSurvey(cs, surveyId);
        }
        if (option.equals(SET_RESPONSE_COUNT)) {
            if (surveyId == null) {
                System.out.println(
                        "\n\nMissing surveyId. " + "You must specify surveyId to set a response count.");
                System.exit(1);
            }
            updateSurveyResponseCount(cs, surveyId, 120);
        }
        if (option.equals(FETCH)) {
            if (surveyId == null) {
                System.out.println("\n\nMissing surveyId. " + "You must specify surveyId to get the results.");
                System.exit(1);
            }
            getSurveyResults(cs, surveyId, resultFile);
        }
        if (option.equals(LIST)) {
            listSurveys(cs);
        }
    } catch (GoogleJsonResponseException e) {
        System.err.println(e.getDetails());
    }
}

From source file:com.github.vatbub.awsvpnlauncher.Main.java

public static void main(String[] args) {
    Common.getInstance().setAppName("awsVpnLauncher");
    FOKLogger.enableLoggingOfUncaughtExceptions();
    prefs = new Preferences(Main.class.getName());

    // enable the shutdown hook
    Runtime.getRuntime().addShutdownHook(new Thread(() -> {
        if (session != null) {
            if (session.isConnected()) {
                session.disconnect();/*from  w w  w.  j  a  va 2s.  c  o m*/
            }
        }
    }));

    UpdateChecker.completeUpdate(args, (oldVersion, oldFile) -> {
        if (oldVersion != null) {
            FOKLogger.info(Main.class.getName(), "Successfully upgraded " + Common.getInstance().getAppName()
                    + " from v" + oldVersion.toString() + " to v" + Common.getInstance().getAppVersion());
        }
    });
    List<String> argsAsList = new ArrayList<>(Arrays.asList(args));

    for (String arg : args) {
        if (arg.toLowerCase().matches("mockappversion=.*")) {
            // Set the mock version
            String version = arg.substring(arg.toLowerCase().indexOf('=') + 1);
            Common.getInstance().setMockAppVersion(version);
            argsAsList.remove(arg);
        } else if (arg.toLowerCase().matches("mockbuildnumber=.*")) {
            // Set the mock build number
            String buildnumber = arg.substring(arg.toLowerCase().indexOf('=') + 1);
            Common.getInstance().setMockBuildNumber(buildnumber);
            argsAsList.remove(arg);
        } else if (arg.toLowerCase().matches("mockpackaging=.*")) {
            // Set the mock packaging
            String packaging = arg.substring(arg.toLowerCase().indexOf('=') + 1);
            Common.getInstance().setMockPackaging(packaging);
            argsAsList.remove(arg);
        }
    }

    args = argsAsList.toArray(new String[0]);

    try {
        mvnRepoConfig = new Config(
                new URL("https://www.dropbox.com/s/vnhs4nax2lczccf/mavenRepoConfig.properties?dl=1"),
                Main.class.getResource("mvnRepoFallbackConfig.properties"), true, "mvnRepoCachedConfig", true);
        projectConfig = new Config(
                new URL("https://www.dropbox.com/s/d36hwrrufoxfmm7/projectConfig.properties?dl=1"),
                Main.class.getResource("projectFallbackConfig.properties"), true, "projectCachedConfig", true);
    } catch (IOException e) {
        FOKLogger.log(Main.class.getName(), Level.SEVERE, "Could not load the remote config", e);
    }

    try {
        installUpdates(args);
    } catch (Exception e) {
        FOKLogger.log(Main.class.getName(), Level.SEVERE, "Could not install updates", e);
    }

    if (args.length == 0) {
        // not enough arguments
        printHelpMessage();
        throw new NotEnoughArgumentsException();
    }

    switch (args[0].toLowerCase()) {
    case "setup":
        setup();
        break;
    case "launch":
        initAWSConnection();
        launch();
        break;
    case "terminate":
        initAWSConnection();
        terminate();
        break;
    case "config":
        // require a second arg
        if (args.length == 2) {
            // not enough arguments
            printHelpMessage();
            throw new NotEnoughArgumentsException();
        }

        config(Property.valueOf(args[1]), args[2]);
        break;
    case "getconfig":
        // require a second arg
        if (args.length == 1) {
            // not enough arguments
            printHelpMessage();
            throw new NotEnoughArgumentsException();
        }

        getConfig(Property.valueOf(args[1]));
        break;
    case "printconfig":
        printConfig();
        break;
    case "deleteconfig":
        // require a second arg
        if (args.length == 1) {
            // not enough arguments
            printHelpMessage();
            throw new NotEnoughArgumentsException();
        }

        deleteConfig(Property.valueOf(args[1]));
        break;
    case "ssh":
        String sshInstanceId;
        if (args.length == 2) {
            // a instanceID is specified
            sshInstanceId = args[1];
        } else {
            String instanceIdsPrefValue = prefs.getPreference("instanceIDs", "");
            if (instanceIdsPrefValue.equals("")) {
                throw new NotEnoughArgumentsException(
                        "No instanceId was specified to connect to and no instanceId was saved in the preference file. Please either start another instance using the launch command or specify the instance id of the instance to connect to as a additional parameter.");
            }

            List<String> instanceIds = Arrays.asList(instanceIdsPrefValue.split(";"));
            if (instanceIds.size() == 1) {
                // exactly one instance found
                sshInstanceId = instanceIds.get(0);
            } else {
                FOKLogger.severe(Main.class.getName(), "Multiple instance ids found:");

                for (String instanceId : instanceIds) {
                    FOKLogger.severe(Main.class.getName(), instanceId);
                }
                throw new NotEnoughArgumentsException(
                        "Multiple instance ids were found in the preference file. Please specify the instance id of the instance to connect to as a additional parameter.");
            }
        }

        initAWSConnection();
        ssh(sshInstanceId);
        break;
    default:
        printHelpMessage();
    }
}