Example usage for org.apache.commons.configuration2 PropertiesConfiguration PropertiesConfiguration

List of usage examples for org.apache.commons.configuration2 PropertiesConfiguration PropertiesConfiguration

Introduction

In this page you can find the example usage for org.apache.commons.configuration2 PropertiesConfiguration PropertiesConfiguration.

Prototype

public PropertiesConfiguration() 

Source Link

Document

Creates an empty PropertyConfiguration object which can be used to synthesize a new Properties file by adding values and then saving().

Usage

From source file:org.powertac.samplebroker.core.BrokerRunner.java

public void processCmdLine(String[] args) {
    OptionParser parser = new OptionParser();
    OptionSpec<String> jmsUrlOption = parser.accepts("jms-url").withRequiredArg().ofType(String.class);
    OptionSpec<File> configOption = parser.accepts("config").withRequiredArg().ofType(File.class);
    OptionSpec<Integer> repeatCountOption = parser.accepts("repeat-count").withRequiredArg()
            .ofType(Integer.class);
    OptionSpec<Integer> repeatHoursOption = parser.accepts("repeat-hours").withRequiredArg()
            .ofType(Integer.class);
    OptionSpec<String> queueNameOption = parser.accepts("queue-name").withRequiredArg().ofType(String.class);
    OptionSpec<String> serverQueueOption = parser.accepts("server-queue").withRequiredArg()
            .ofType(String.class);
    parser.accepts("ipc-adapter");
    //parser.accepts("no-ntp");
    parser.accepts("interactive");
    OptionSpec<String> propOption = parser.accepts("prop").withRequiredArg().ofType(String.class);

    // do the parse
    OptionSet options = parser.parse(args);

    File configFile = null;//  www . j av  a 2s.c o  m
    //String jmsUrl = null;
    //boolean noNtp = false;
    //boolean interactive = false;
    //String queueName = null;
    //String serverQueue = null;
    Integer repeatCount = 1;
    Integer repeatHours = 0;
    long end = 0l;
    PropertiesConfiguration cliProps = new PropertiesConfiguration();

    try {
        // process broker options
        System.out.println("<options");
        if (options.has(configOption)) {
            configFile = options.valueOf(configOption);
            System.out.println(" config=\"" + configFile.getName() + "\"");
        }
        if (options.has(jmsUrlOption)) {
            cliProps.setProperty("samplebroker.core.powerTacBroker.jmsBrokerUrl",
                    options.valueOf(jmsUrlOption));
            System.out.print(" jms-url=\"" + options.valueOf(jmsUrlOption) + "\"");
        }
        //if (options.has("no-ntp")) {
        //  noNtp = true;
        //  System.out.println("  no ntp - estimate offset");
        //}
        if (options.has(repeatCountOption)) {
            repeatCount = options.valueOf(repeatCountOption);
            System.out.print(" repeat-count=\"" + repeatCount + "\"");
        } else if (options.has(repeatHoursOption)) {
            repeatHours = options.valueOf(repeatHoursOption);
            System.out.print(" repeat-hours=\"" + repeatHours + "\"");
            long now = new Date().getTime();
            end = now + 1000 * 3600 * repeatHours;
        }
        if (options.has(queueNameOption)) {
            cliProps.setProperty("samplebroker.core.powerTacBroker.brokerQueueName",
                    options.valueOf(queueNameOption));
            System.out.print(" queue-name=\"" + options.valueOf(queueNameOption) + "\"");
        }
        if (options.has(serverQueueOption)) {
            cliProps.setProperty("samplebroker.core.powerTacBroker.serverQueueName",
                    options.valueOf(serverQueueOption));
            System.out.print(" server-queue=\"" + options.valueOf(serverQueueOption) + "\"");
        }
        if (options.has("interactive")) {
            cliProps.setProperty("samplebroker.core.powerTacBroker.interactive", "true");
            System.out.print(" interactive=\"true\"");
        }
        if (options.has("ipc-adapter")) {
            cliProps.setProperty("samplebroker.core.brokerMessageReceiver.ipcAdapter", "true");
            System.out.println("Using ipc-adapter to pass raw xml.");
        }
        if (options.has(propOption)) {
            List<String> values = options.valuesOf(propOption);
            for (String value : values) {
                int colon = value.indexOf(":");
                if (colon > 0) {
                    String name = value.substring(0, colon);
                    String val = value.substring(colon + 1);
                    cliProps.setProperty(name, val);
                    System.out.print(" " + name + "=" + val);
                }
            }
        }
        System.out.println("/>");

        // at this point, we are either done, or we need to repeat
        int counter = 0;
        while ((null != repeatCount && repeatCount > 0) || (new Date().getTime() < end)) {
            counter += 1;

            // initialize and run
            if (null == context) {
                context = new ClassPathXmlApplicationContext("broker.xml");
            } else {
                context.close();
                context.refresh();
            }

            // Re-open the logfiles
            reopenLogs(counter);
            IdGenerator.recycle();

            // get the broker reference and delegate the rest
            context.registerShutdownHook();
            broker = (PowerTacBroker) context.getBeansOfType(PowerTacBroker.class).values().toArray()[0];
            System.out.println("Starting session " + counter);
            log.info("Starting session {}", counter);
            broker.startSession(cliProps, configFile, end);
            if (null != repeatCount)
                repeatCount -= 1;
        }
    } catch (OptionException e) {
        System.err.println("Bad command argument: " + e.toString());
    }
}