Example usage for org.apache.commons.configuration HierarchicalConfiguration getMaxIndex

List of usage examples for org.apache.commons.configuration HierarchicalConfiguration getMaxIndex

Introduction

In this page you can find the example usage for org.apache.commons.configuration HierarchicalConfiguration getMaxIndex.

Prototype

public int getMaxIndex(String key) 

Source Link

Document

Returns the maximum defined index for the given key.

Usage

From source file:Cresendo.java

public static void main(String[] args) {
    String cfgFileReceiver = null; // Path to config file for eif receiver agent
    String cfgFileEngine = null; // Path to config file for xml event engine
    Options opts = null; // Command line options
    HelpFormatter hf = null; // Command line help formatter

    // Setup the message record which will contain text written to the log file
    ////from   ww  w  . j a v  a2 s  .com
    // The message logger object is created when the "-l" is processed
    // as this object need to be associated with a log file
    //
    LogRecord msg = new LogRecord(LogRecord.TYPE_INFO, "Cresendo", "main", "", "", "", "", "");

    // Get the directory separator (defaults to "/")
    //
    dirSep = System.getProperty("file.separator", "/");

    // Initialise the structure containing the event handler objects
    //
    Vector<IEventHandler> eventHandler = new Vector<IEventHandler>(10, 10);

    // Process the command line arguments
    //
    try {
        opts = new Options();
        hf = new HelpFormatter();

        opts.addOption("h", "help", false, "Command line arguments help");
        opts.addOption("i", "instance name", true, "Name of cresendo instance");
        opts.addOption("l", "log dir", true, "Path to log file directory");
        opts.addOption("c", "config dir", true, "Path to configuarion file directory");

        opts.getOption("l").setRequired(true);
        opts.getOption("c").setRequired(true);

        BasicParser parser = new BasicParser();
        CommandLine cl = parser.parse(opts, args);

        // Print out some help and exit
        //
        if (cl.hasOption('h')) {
            hf.printHelp("Options", opts);
            System.exit(0);
        }

        // Set the instance name
        //
        if (cl.hasOption('i')) {
            instanceName = cl.getOptionValue('i'); // Set to something other than "default"
        }

        // Setup the message and trace logging objects for the EventEngine
        //
        if (cl.hasOption('l')) {
            // Setup the the paths to the message, trace and status log files
            //
            logDir = cl.getOptionValue("l");

            logPath = logDir + dirSep + instanceName + "-engine.log";
            tracePath = logDir + dirSep + instanceName + "-engine.trace";
            statusPath = logDir + dirSep + instanceName + "-engine.status";
        } else {
            // NOTE:  This should be picked up by the MissingOptionException catch below
            //        but I couldn't get this to work so I added the following code:
            //
            hf.printHelp("Option 'l' is a required option", opts);
            System.exit(1);
        }

        // Read the receiver and engine config files in the config directory
        //
        if (cl.hasOption('c')) {
            // Setup and check path to eif config file for TECAgent receiver object
            //
            configDir = cl.getOptionValue("c");
            cfgFileReceiver = configDir + dirSep + instanceName + ".conf";
            checkConfigFile(cfgFileReceiver);

            // Setup and check path to xml config file for the EventEngine
            //
            cfgFileEngine = cl.getOptionValue("c") + dirSep + instanceName + ".xml";
            checkConfigFile(cfgFileEngine);

        } else {
            // NOTE:  This should be picked up by the MissingOptionException catch below
            //        but I couldn't get this to work so I added the following code:
            //
            hf.printHelp("Option 'c' is a required option", opts);
            System.exit(1);
        }
    } catch (UnrecognizedOptionException e) {
        hf.printHelp(e.toString(), opts);
        System.exit(1);
    } catch (MissingOptionException e) {
        hf.printHelp(e.toString(), opts);
        System.exit(1);
    } catch (MissingArgumentException e) {
        hf.printHelp(e.toString(), opts);
        System.exit(1);
    } catch (ParseException e) {
        e.printStackTrace();
        System.exit(1);
    } catch (Exception e) {
        System.err.println(e.toString());
        System.exit(1);
    }

    // Main program
    //
    try {
        // =====================================================================
        // Setup the message, trace and status logger objects
        //
        try {
            msgHandler = new FileHandler("cresendo", "message handler", logPath);
            msgHandler.openDevice();

            msgLogger = new MessageLogger("cresendo", "message log");
            msgLogger.addHandler(msgHandler);

            trcHandler = new FileHandler("cresendo", "trace handler", tracePath);
            trcHandler.openDevice();

            trcLogger = new TraceLogger("cresendo", "trace log");
            trcLogger.addHandler(trcHandler);

            statLogger = new StatusLogger(statusPath);
        } catch (Exception e) {
            System.err.println(e.toString());
            System.exit(1);
        }

        // Add the shutdown hook
        //
        Runtime.getRuntime().addShutdownHook(new ShutdownThread(msgLogger, instanceName));

        // ---------------------------------------------------------------------
        // =====================================================================
        // Load and parse the xml event engine configuration file
        //
        //
        msg.setText("Loading xml engine from: '" + cfgFileEngine + "'");

        try {
            XMLConfiguration xmlProcessor = new XMLConfiguration();
            xmlProcessor.setFileName(cfgFileEngine);

            // Validate the xml against a document type declaration
            //
            xmlProcessor.setValidating(true);

            // Don't interpolate the tag contents by splitting them on a delimiter
            // (ie by default a comma)
            //
            xmlProcessor.setDelimiterParsingDisabled(true);

            // This will throw a ConfigurationException if the xml document does not
            // conform to its dtd.  By doing this we hopefully catch any errors left
            // behind after the xml configuration file has been edited.
            //
            xmlProcessor.load();

            // Setup the trace flag
            //
            ConfigurationNode engine = xmlProcessor.getRootNode();
            List rootAttribute = engine.getAttributes();

            for (Iterator it = rootAttribute.iterator(); it.hasNext();) {
                ConfigurationNode attr = (ConfigurationNode) it.next();

                String attrName = attr.getName();
                String attrValue = (String) attr.getValue();

                if (attrValue == null || attrValue == "") {
                    System.err.println("\n  Error: The value of the attribute '" + attrName + "'"
                            + "\n         in the xml file '" + cfgFileEngine + "'" + "\n         is not set");
                    System.exit(1);
                }

                if (attrName.matches("trace")) {
                    if (attrValue.matches("true") || attrValue.matches("on")) {
                        trcLogger.setLogging(true);
                    }
                }

                if (attrName.matches("status")) {
                    if (attrValue.matches("true") || attrValue.matches("on")) {
                        statLogger.setLogging(true);
                    } else {
                        statLogger.setLogging(false);
                    }
                }

                if (attrName.matches("interval")) {
                    if (!attrValue.matches("[0-9]+")) {
                        System.err.println("\n  Error: The value of the interval attribute in: '"
                                + cfgFileEngine + "'" + "\n         should only contain digits from 0 to 9."
                                + "\n         It currently contains: '" + attrValue + "'");
                        System.exit(1);
                    }

                    statLogger.setInterval(Integer.parseInt(attrValue));
                }
            }

            // Now build and instantiate the list of classes that will process events
            // received by the TECAgent receiver in a chain like manner.
            //
            List classes = xmlProcessor.configurationsAt("class");

            for (Iterator it = classes.iterator(); it.hasNext();) {
                HierarchicalConfiguration sub = (HierarchicalConfiguration) it.next();

                // sub contains now all data contained in a single <class></class> tag set
                //
                String className = sub.getString("name");

                // Log message
                //
                msg.setText(msg.getText() + "\n  Instantiated event handler class: '" + className + "'");

                // The angle brackets describing the class of object held by the
                // Vector are implemented by Java 1.5 and have 2 effects.
                //
                // 1. The list accepts only elements of that class and nothing else
                // (Of course thanks to Auto-Wrap you can also add double-values)
                //
                // 2. the get(), firstElement() ... Methods don't return a Object, but
                //    they deliver an element of the class.
                //
                Vector<Class> optTypes = new Vector<Class>(10, 10);
                Vector<Object> optValues = new Vector<Object>(10, 10);

                for (int i = 0; i <= sub.getMaxIndex("option"); i++) {
                    Object optValue = null;
                    String optVarName = sub.getString("option(" + i + ")[@varname]");
                    String optJavaType = sub.getString("option(" + i + ")[@javatype]");

                    // Use the specified java type in order to make the method call
                    // to the heirarchical sub object [painful :-((]
                    //
                    if (optJavaType.matches("byte")) {
                        optTypes.addElement(byte.class);
                        optValue = sub.getByte("option(" + i + ")");

                        if (optValue == null) // Catch nulls
                        {
                            optValue = 0; // Set to something nullish
                        }
                    } else if (optJavaType.matches("short")) {
                        optTypes.addElement(byte.class);
                        optValue = sub.getShort("option(" + i + ")");

                        if (optValue == null) // Catch nulls
                        {
                            optValue = 0; // Set to something nullish
                        }
                    } else if (optJavaType.matches("int")) {
                        optTypes.addElement(int.class);
                        optValue = sub.getInt("option(" + i + ")");

                        if (optValue == null) // Catch nulls
                        {
                            optValue = 0; // Set to something nullish
                        }
                    } else if (optJavaType.matches("long")) {
                        optTypes.addElement(long.class);
                        optValue = sub.getLong("option(" + i + ")");

                        if (optValue == null) // Catch nulls
                        {
                            optValue = 0; // Set to something nullish
                        }
                    } else if (optJavaType.matches("float")) {
                        optTypes.addElement(float.class);
                        optValue = sub.getFloat("option(" + i + ")");

                        if (optValue == null) // Catch nulls
                        {
                            optValue = 0.0; // Set to something nullish
                        }
                    } else if (optJavaType.matches("double")) {
                        optTypes.addElement(double.class);
                        optValue = sub.getDouble("option(" + i + ")");

                        if (optValue == null) // Catch nulls
                        {
                            optValue = 0.0; // Set to something nullish
                        }
                    } else if (optJavaType.matches("boolean")) {
                        optTypes.addElement(boolean.class);
                        optValue = sub.getBoolean("option(" + i + ")");

                        if (optValue == null) // Catch nulls
                        {
                            optValue = false; // Set to something nullish
                        }
                    } else if (optJavaType.matches("String")) {
                        optTypes.addElement(String.class);
                        optValue = sub.getString("option(" + i + ")");

                        if (optValue == null) // Catch nulls
                        {
                            optValue = ""; // Set it to something nullish
                        }
                    } else {
                        System.err.println(
                                "Error: Unsupported java type found in xml config: '" + optJavaType + "'");
                        System.exit(1);
                    }

                    // Add option value element
                    //
                    //              System.out.println("Option value is: '" + optValue.toString() + "'\n");
                    //
                    optValues.addElement(optValue);

                    // Append to message text
                    //
                    String msgTemp = msg.getText();
                    msgTemp += "\n      option name: '" + optVarName + "'";
                    msgTemp += "\n      option type: '" + optJavaType + "'";
                    msgTemp += "\n     option value: '" + optValues.lastElement().toString() + "'";
                    msg.setText(msgTemp);
                }

                try {
                    // Instantiate the class with the java reflection api
                    //
                    Class klass = Class.forName(className);

                    // Setup an array of paramater types in order to retrieve the matching constructor
                    //
                    Class[] types = optTypes.toArray(new Class[optTypes.size()]);

                    // Get the constructor for the class which matches the parameter types
                    //
                    Constructor konstruct = klass.getConstructor(types);

                    // Create an instance of the event handler
                    //
                    IEventHandler eventProcessor = (IEventHandler) konstruct.newInstance(optValues.toArray());

                    // Add the instance to the list of event handlers
                    //
                    eventHandler.addElement(eventProcessor);

                } catch (InvocationTargetException e) {
                    System.err.println("Error: " + e.toString());
                    System.exit(1);
                } catch (ClassNotFoundException e) {
                    System.err.println("Error: class name not found: '" + className + "' \n" + e.toString());
                    System.exit(1);
                } catch (Exception e) {
                    System.err.println(
                            "Error: failed to instantiate class: '" + className + "' \n" + e.toString());
                    System.exit(1);
                }
            }
        } catch (ConfigurationException cex) // Something went wrong loading the xml file
        {
            System.err.println("\n" + "Error loading XML file: " + cfgFileEngine + "\n" + cex.toString());
            System.exit(1);
        } catch (Exception e) {
            System.err.println(e.toString());
            System.exit(1);
        }

        // ---------------------------------------------------------------------
        // =====================================================================
        // Setup the TECAgent receiver 
        // 
        Reader cfgIn = null;

        try {
            cfgIn = new FileReader(cfgFileReceiver);
        } catch (Exception e) {
            System.err.println(e.toString());
            System.exit(1);
        }

        // Start the TECAgent receiver and register the event engine handler
        //
        TECAgent receiver = new TECAgent(cfgIn, TECAgent.RECEIVER_MODE, false);

        EventEngine ee = new EventEngine(eventHandler, msgLogger, trcLogger);

        receiver.registerListener(ee);

        // Construct message and send it to the message log
        //
        String text = "\n  Cresendo instance '" + instanceName + "' listening for events on port '"
                + receiver.getConfigVal("ServerPort") + "'";

        msg.setText(msg.getText() + text);
        msgLogger.log(msg); // Send message to log

        // ---------------------------------------------------------------------
        // =====================================================================
        // Initiate status logging
        //
        if (statLogger.isLogging()) {
            int seconds = statLogger.getInterval();

            while (true) {
                try {
                    statLogger.log();
                } catch (Exception ex) {
                    System.err.println("\n  An error occurred while writing to '" + statusPath + "'" + "\n  '"
                            + ex.toString() + "'");
                }

                Thread.sleep(seconds * 1000); // Convert sleep time to milliseconds
            }
        }

        // ---------------------------------------------------------------------
    } catch (Exception e) {
        System.err.println(e.toString());
        System.exit(1);
    }
}

From source file:elaborate.editor.config.Configuration.java

private static void extractRenditions(HierarchicalConfiguration config) {
    int n = config.getMaxIndex("rendition");
    for (int i = 0; i <= n; i++) {
        String messageId = config.getString("rendition(" + i + ")[@id]");
        if (messageId != null) {
            String message = config.getString("rendition(" + i + ")");
            renditions.put(messageId, message);
        }//from  w  w  w.j a va2s. co m
    }
}

From source file:elaborate.editor.config.Configuration.java

private static void extractMessages(String parentId, HierarchicalConfiguration config) {
    int n = config.getMaxIndex("message");
    for (int i = 0; i <= n; i++) {
        String messageId = config.getString("message(" + i + ")[@id]");
        if (messageId != null) {
            String message = config.getString("message(" + i + ")");
            if (message != null) {
                message = message.replaceAll("\\{", "<").replaceAll("\\}", ">");
            }/*  www  . j  a v  a2 s . c om*/
            messages.put(parentId + "." + messageId, message);
        }
    }
}

From source file:com.easyvalidation.xml.processor.XMLProcessor.java

/**
 * Parse the XML & return list of rules.
 * //from ww  w.  j  a v a2  s.c o  m
 * @param fileNames
 * @return
 * @throws ValidationException
 */
public static Map<String, List<Rule>> parseXML(String[] fileNames) throws ValidationException {
    Map<String, List<Rule>> validationMap = new HashMap<String, List<Rule>>();

    com.easyvalidation.xml.config.EasyValidationXmlConfiguration configuration = new com.easyvalidation.xml.config.EasyValidationXmlConfiguration();
    //System.out.println("Have we came here?");
    configuration.setAutoSave(false);
    //System.out.println("Have we came here?");
    configuration.clear();
    //System.out.println("Have we came here?");
    configuration.setValidating(true);

    try {
        for (String fileName : fileNames) {
            //System.out.println("Have we came here4?");
            configuration.load(fileName);
            //System.out.println("Have we came here5?");
        }
        //System.out.println("Have we came here?");
        boolean isMessageFromKeyAllowed = false;

        Map<String, PropertiesConfiguration> propertiesMap = new HashMap<String, PropertiesConfiguration>();
        int propertiesSize = configuration.getMaxIndex(Nodes.PROPERTIES);
        for (int propertiesIndex = 0; propertiesIndex <= propertiesSize; propertiesIndex++) {

            String locale = configuration.getString(RuleElementPathUtil.getPropsFileLocale(propertiesIndex));

            PropertiesConfiguration propertiesConfiguration = new PropertiesConfiguration();
            propertiesConfiguration.setAutoSave(false);
            propertiesConfiguration.clear();

            HierarchicalConfiguration subConfig = configuration
                    .configurationAt(RuleElementPathUtil.getPropertiesPath(propertiesIndex));
            //System.out.println("Have we came here6?");
            int propFileSize = subConfig.getMaxIndex(Nodes.FILE);
            for (int propFileIndex = 0; propFileIndex <= propFileSize; propFileIndex++) {
                String propFileName = subConfig.getString(RuleElementPathUtil.getPropsFilePath(propFileIndex));
                if (!Utils.isEmpty(propFileName)) {
                    propertiesConfiguration.load(propFileName);
                }
            }
            propertiesMap.put(locale, propertiesConfiguration);
        }
        //System.out.println("Have we came here7?");
        if (!propertiesMap.isEmpty()) {
            isMessageFromKeyAllowed = true;
        }

        int validationSize = configuration.getMaxIndex(Nodes.VALIDATION);

        for (int validationIndex = 0; validationIndex <= validationSize; validationIndex++) {
            String name = configuration.getString(RuleElementPathUtil.getValidationName(validationIndex));

            HierarchicalConfiguration subConfig = configuration
                    .configurationAt(RuleElementPathUtil.getValidation(validationIndex));

            int ruleSize = subConfig.getMaxIndex(Nodes.RULE);
            List<Rule> ruleList = new ArrayList<Rule>();
            for (int ruleIndex = 0; ruleIndex <= ruleSize; ruleIndex++) {
                String ruleType = subConfig.getString(RuleElementPathUtil.getRuleType(ruleIndex));

                String property = subConfig.getString(RuleElementPathUtil.getRuleFieldName(ruleIndex));

                String message = subConfig.getString(RuleElementPathUtil.getRuleMessage(ruleIndex));

                String min = subConfig.getString(RuleElementPathUtil.getRuleMin(ruleIndex));

                String max = subConfig.getString(RuleElementPathUtil.getRuleMax(ruleIndex));

                String dateFormat = subConfig.getString(RuleElementPathUtil.getRuleDateFormat(ruleIndex));

                String key = subConfig.getString(RuleElementPathUtil.getMessageKey(ruleIndex));

                String useAttributePlaceHolder = subConfig
                        .getString(RuleElementPathUtil.getMessageUseAttributePlaceHolder(ruleIndex));

                String regEx = subConfig.getString(RuleElementPathUtil.getRuleRegEx(ruleIndex));

                String expression = subConfig.getString(RuleElementPathUtil.getRuleExpression(ruleIndex));

                Rule rule = new Rule(ruleType);

                rule.setProperty(property);
                rule.setMessage(message);
                rule.setMin(min);
                rule.setMax(max);
                rule.setDateFormat(dateFormat);
                rule.setKey(key);
                rule.setUseAttributePlaceHolder(Boolean.valueOf(useAttributePlaceHolder));
                rule.setRegEx(Boolean.valueOf(regEx));
                rule.setExpression(expression);
                if (isMessageFromKeyAllowed) {
                    rule.setPropertiesMap(propertiesMap);
                }

                rule.populateRule();
                ruleList.add(rule);
            }
            validationMap.put(name, ruleList);
        }
    } catch (Exception ex) {
        throw new ValidationException(ex);
    }

    return validationMap;
}

From source file:elaborate.editor.config.Configuration.java

private static void processConfiguration(HierarchicalConfiguration h_config) {
    renditions = Maps.newTreeMap();//  ww  w  .  ja va2s. c o m
    HierarchicalConfiguration config = configurationAt(h_config, "renditions");
    if (config != null) {
        extractRenditions(config);
    }
    messages = Maps.newTreeMap();
    config = configurationAt(h_config, "messages");
    if (config != null) {
        int n = config.getMaxIndex("group");
        for (int i = 0; i <= n; i++) {
            String groupId = config.getString("group(" + i + ")[@id]");
            if (groupId != null) {
                HierarchicalConfiguration group = config.configurationAt("group(" + i + ")");
                extractMessages(groupId, group);
            }
        }
    }
}

From source file:playground.michalm.jtrrouter.JTRRouter.java

protected void readConfigs(String dir, String flowsFile, String turnsFile) {

    // process flows.xml
    ///*from w  w  w . j  a v  a2s.  c  o m*/
    // <flows startTime="0" stopTime="3600">
    // ....
    // ....
    // </flows>

    try {
        HierarchicalConfiguration flowCfg = new XMLConfiguration(dir + "\\" + flowsFile);

        genStartTime = flowCfg.getInt("[@startTime]");
        genStopTime = flowCfg.getInt("[@stopTime]");
        genPeriod = genStopTime - genStartTime;

        flowFactor = flowCfg.getDouble("[@flowFactor]");

        int count = flowCfg.getMaxIndex("flow") + 1;
        for (int i = 0; i < count; i++) {
            initFlow((HierarchicalConfiguration) flowCfg.subset("flow(" + i + ')'));
        }

        // process turns.xml
        HierarchicalConfiguration nodeCfg = new XMLConfiguration(dir + "\\" + turnsFile);

        count = nodeCfg.getMaxIndex("turn") + 1;
        for (int i = 0; i < count; i++) {
            initTurn((HierarchicalConfiguration) nodeCfg.subset("turn(" + i + ')'));
        }
    } catch (ConfigurationException e) {
        throw new RuntimeException(e);
    }
}

From source file:playground.michalm.jtrrouter.JTRRouter.java

protected void initTurn(HierarchicalConfiguration nodeCfg) {
    int id = nodeCfg.getInt("[@id]");
    int prev = nodeCfg.getInt("[@prev]");
    int length = nodeCfg.getMaxIndex("next") + 1;

    int[] nodes = new int[length];
    double[] probs = new double[length];

    for (int i = 0; i < length; i++) {
        Configuration nextCfg = nodeCfg.subset("next(" + i + ')');
        nodes[i] = nextCfg.getInt("[@node]");
        probs[i] = nextCfg.getDouble("[@probability]");
    }/*  www  .  jav  a2s . c  om*/

    turns[prev][id] = new Turn(id, prev, nodes, probs);
}

From source file:playground.michalm.jtrrouter.transims.TransimsJTRRouter.java

protected void initFlow(HierarchicalConfiguration flowCfg) {
    int node = flowCfg.getInt("[@node]");
    int next = flowCfg.getInt("[@next]");

    int in = flowCfg.getInt("[@inParking]", -1);
    int out = flowCfg.getInt("[@outParking]", -1);

    int length = flowCfg.getMaxIndex("vehicle") + 1;

    int[] types = new int[length];
    int[] subTypes = new int[length];
    int[] nos = new int[length];

    for (int i = 0; i < length; i++) {
        Configuration vehCfg = flowCfg.subset("vehicle(" + i + ')');
        types[i] = vehCfg.getInt("[@type]");
        subTypes[i] = vehCfg.getInt("[@subtype]");
        nos[i] = vehCfg.getInt("[@no]");
    }//from ww  w. j  av a2  s  . c o m

    flows[node] = new TransimsFlow(node, in, out, next, types, subTypes, nos);
}