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

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

Introduction

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

Prototype

public int getMaxIndex(String key) 

Source Link

Document

Returns the maximum defined index for the given key.

Usage

From source file:org.accada.reader.rprm.core.ReaderDevice.java

/**
 * Resets all internal state variables of the reader to a default
 * configuration. The documentation of the reader shall provide a definition
 * what the default settings are//from w  w  w. j ava2s  .  co  m
 * @param propFile
 *           The location of the property file
 * @throws ReaderProtocolException
 *            "Reader not found", "Failed to read properties file",
 *            "wrong valueTrigger in property file", "wrong edgeTrigger in
 *            property file"
 */
public final void resetToDefaultSettings(final String propFile, final String defaultPropFile)
        throws ReaderProtocolException {
    // operational status is DOWN before resetting
    setOperStatus(OperationalStatus.DOWN);

    //init lists
    sources = new Hashtable();
    dataSelectors = new Hashtable();
    notificationChannels = new Hashtable();
    triggers = new Hashtable();
    tagSelectors = new Hashtable();
    tagFields = new Hashtable();
    readPoints = new Hashtable();
    readers = new Hashtable<String, HardwareAbstraction>();
    valueTriggers = new Hashtable();
    edgeTriggers = new Hashtable();
    currentSource = null;
    alarmChannels = new Hashtable<String, AlarmChannel>();
    ioPorts = new Hashtable<String, IOPort>();

    // properties
    XMLConfiguration conf;
    URL fileurl = ResourceLocator.getURL(propFile, defaultPropFile, this.getClass());
    try {
        conf = new XMLConfiguration(fileurl);
    } catch (ConfigurationException e) {
        System.out.println(e.getMessage());
        throw new ReaderProtocolException("Failed to read properties file (" + propFile + ")",
                MessagingConstants.ERROR_UNKNOWN);
    }

    // get properties
    setEPC(conf.getString("epc"));
    setName(conf.getString("name"));
    setManufacturer(conf.getString("manufacturer"));
    setManufacturerDescription(conf.getString("manufacturerDescription"));
    setModel(conf.getString("model"));
    setHandle(conf.getInt("handle"));
    setRole(conf.getString("role"));

    setMaxSourceNumber(conf.getInt("maxSourceNumber"));
    setMaxTagSelectorNumber(conf.getInt("maxTagSelectorNumber"));
    setMaxTriggerNumber(conf.getInt("maxTriggerNumber"));

    // get readers
    SubnodeConfiguration readerConf = conf.configurationAt("readers");
    for (int i = 0; i <= readerConf.getMaxIndex("reader"); i++) {
        // key to current reader
        String key = "reader(" + i + ")";

        // reader's name
        String readerName = readerConf.getString(key + ".name");
        //System.out.println(readerName);

        // get reader
        if (!readers.containsKey(readerName)) {
            // reflection
            String rClass = readerConf.getString(key + ".class");
            String prop = readerConf.getString(key + ".properties");
            try {
                Class cls = Class.forName(rClass);
                Class[] partypes = new Class[] { String.class, String.class };
                Constructor ct = cls.getConstructor(partypes);
                Object[] arglist = new Object[] { readerName, prop };
                HardwareAbstraction ha = (HardwareAbstraction) ct.newInstance(arglist);
                readers.put(readerName, ha);

            } catch (Exception e) {
                log.error(e.getMessage() + "|" + e.getCause());
                throw new ReaderProtocolException("Reader not found", MessagingConstants.ERROR_UNKNOWN);
            }
        }
    }

    // configure readpoints
    SubnodeConfiguration rpConf = conf.configurationAt("readers");
    for (int i = 0; i <= rpConf.getMaxIndex("reader"); i++) {
        // key to current reader
        String key = "reader(" + i + ")";
        // name of the reader
        String readerName = rpConf.getString(key + ".name");
        // get reader
        HardwareAbstraction tempHardwareAbstraction = (HardwareAbstraction) readers.get(readerName);
        // get readpoints
        for (int j = 0; j <= rpConf.getMaxIndex(key + ".readpoint"); j++) {
            String rp = rpConf.getString(key + ".readpoint(" + j + ")");
            // System.out.println(" "+rps[j]);
            AntennaReadPoint.create(rp, this, tempHardwareAbstraction);
        }
    }

    // configure sources
    SubnodeConfiguration sourceConf = conf.configurationAt("sources");
    for (int i = 0; i <= sourceConf.getMaxIndex("source"); i++) {
        String key = "source(" + i + ")";
        // name of the source
        String sourceName = sourceConf.getString(key + ".name");
        // get source
        Source tempSource;
        if (!sources.containsKey(sourceName)) {
            tempSource = Source.create(sourceName, this);
        } else {
            tempSource = getSource(sourceName);
        }
        // fixed
        if (sourceConf.getString(key + ".fixed").equals("true")) {
            tempSource.setFixed(true);
        } else {
            tempSource.setFixed(false);
        }
        // reader's readpoints
        for (int j = 0; j <= sourceConf.getMaxIndex(key + ".readpoint"); j++) {
            String rp = sourceConf.getString(key + ".readpoint(" + j + ")");
            // System.out.println(" "+rp);
            tempSource.addReadPoints(new ReadPoint[] { getReadPoint(rp) });
        }
    }

    // set current Source
    setCurrentSource(getSource(conf.getString("currentSource")));

    // set defaults
    setDefaults();

    // get io triggers
    getIoTriggers(conf);

    // information used for the reader management implementation
    setDescription(conf.getString("description"));
    setLocationDescription(conf.getString("locationDescription"));
    setContact(conf.getString("contact"));
    serialNumber = conf.getString("serialNumber");
    dhcpServerFinder
            .setMacAddress(DHCPServerFinder.macAddressStringToByteArray(conf.getString("macAddress"), "-"));

    operStatusAlarmControl = new TTOperationalStatusAlarmControl("OperStatusAlarmControl", false,
            AlarmLevel.ERROR, 0, OperationalStatus.ANY, OperationalStatus.ANY);

    freeMemoryAlarmControl = new EdgeTriggeredAlarmControl("FreeMemoryAlarmControl", false, AlarmLevel.CRITICAL,
            0, 100, 1000, EdgeTriggeredAlarmDirection.FALLING);

    if ((alarmManager == null) && (mgmtAgent != null)) {
        AlarmProcessor alarmProcessor = null;
        switch (MessageLayer.mgmtAgentType) {
        case SNMP:
            alarmProcessor = new SnmpAlarmProcessor((SnmpAgent) mgmtAgent);
            break;
        // case ...:
        }
        alarmManager = new AlarmManager(alarmProcessor, this);
        alarmManager.start();
    }

    // configure alarm channels
    SubnodeConfiguration alarmChanConf = conf.configurationAt("alarmChannels");
    String host = "";
    int port = -1;
    for (int i = 0; i <= alarmChanConf.getMaxIndex("alarmChannel"); i++) {
        String key = "alarmChannel(" + i + ")";
        // name of the alarm channel
        String alarmChanName = alarmChanConf.getString(key + ".name");
        // get alarm channel
        try {
            host = alarmChanConf.getString(key + ".host");
            port = alarmChanConf.getInt(key + ".port");
            try {
                AlarmChannel.create(alarmChanName, new Address("udp://" + host + ":" + port), this);
            } catch (MalformedURLException mfue) {
                log.error(alarmChanName + ": invalid address");
            }
            host = "";
            port = -1;
        } catch (ReaderProtocolException rpe) {
            // next
        }
    }

    // operational status is UP after resetting
    setOperStatus(OperationalStatus.UP);

}

From source file:org.accada.reader.rprm.core.ReaderDevice.java

/**
 * Reads the configuration of the io triggers from property file.
 * @param conf//  w ww  . j a  va 2  s  . com
 *           The configuration
 * @throws ReaderProtocolException
 *            "wrong edgeTrigger in property file"
 */
private void getIoTriggers(final XMLConfiguration conf) throws ReaderProtocolException {
    // get io value triggers
    SubnodeConfiguration valueTriggerConf = conf.configurationAt("IOValueTriggerPortManager");
    String port;
    for (int i = 0; i <= valueTriggerConf.getMaxIndex("port"); i++) {
        port = valueTriggerConf.getString("port(" + i + ")");
        try {
            Class cls = Class.forName(valueTriggerConf.getString(port));
            Class[] partypes = new Class[] {};
            Constructor ct = cls.getConstructor(partypes);
            Class[] cl = ct.getParameterTypes();
            Object[] arglist = new Object[] {};
            IOValueTriggerPortManager manager = (IOValueTriggerPortManager) ct.newInstance(arglist);
            final int num = 4;
            valueTriggers.put(port.substring(num), manager);
        } catch (Exception e) {
            throw new ReaderProtocolException("wrong valueTrigger in property file",
                    MessagingConstants.ERROR_UNKNOWN);
        }
    }

    // get io edge triggers
    SubnodeConfiguration edgeTriggerConf = conf.configurationAt("IOEdgeTriggerPortManager");
    for (int i = 0; i <= valueTriggerConf.getMaxIndex("port"); i++) {
        port = edgeTriggerConf.getString("port(" + i + ")");
        try {
            Class cls = Class.forName(edgeTriggerConf.getString(port));
            Class[] partypes = new Class[] {};
            Constructor ct = cls.getConstructor(partypes);
            Class[] cl = ct.getParameterTypes();
            Object[] arglist = new Object[] {};
            IOEdgeTriggerPortManager manager = (IOEdgeTriggerPortManager) ct.newInstance(arglist);
            final int num = 4;
            edgeTriggers.put(port.substring(num), manager);
        } catch (Exception e) {
            throw new ReaderProtocolException("wrong edgeTrigger in property file",
                    MessagingConstants.ERROR_UNKNOWN);
        }
    }

}