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

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

Introduction

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

Prototype

public String getString(String key) 

Source Link

Usage

From source file:io.datalayer.conf.HierarchicalIniConfigurationTest.java

/**
 * Tests querying the properties of an existing section.
 *//*from w  ww.j a v a2  s . co  m*/
@Test
public void testGetSectionExisting() throws ConfigurationException {
    HierarchicalINIConfiguration config = setUpConfig(INI_DATA);
    SubnodeConfiguration section = config.getSection("section1");
    assertEquals("Wrong value of var1", "foo", section.getString("var1"));
    assertEquals("Wrong value of var2", "451", section.getString("var2"));
}

From source file:io.datalayer.conf.HierarchicalIniConfigurationTest.java

/**
 * Tests querying the properties of a section that was merged from two
 * sections with the same name./* ww  w  .  java2s  .  c om*/
 */
@Test
public void testGetSectionMerged() throws ConfigurationException {
    final String data = INI_DATA + "[section1]" + LINE_SEPARATOR + "var3 = merged" + LINE_SEPARATOR;
    HierarchicalINIConfiguration config = setUpConfig(data);
    SubnodeConfiguration section = config.getSection("section1");
    assertEquals("Wrong value of var1", "foo", section.getString("var1"));
    assertEquals("Wrong value of var2", "451", section.getString("var2"));
    assertEquals("Wrong value of var3", "merged", section.getString("var3"));
}

From source file:io.datalayer.conf.HierarchicalIniConfigurationTest.java

/**
 * Tests whether a duplicate session is merged.
 *//*  w  w w  . j a v  a  2s  .c  o  m*/
@Test
public void testMergeDuplicateSection() throws ConfigurationException {
    final String data = "[section]\nvar1 = sec1\n\n" + "[section]\nvar2 = sec2\n";
    HierarchicalINIConfiguration config = setUpConfig(data);
    assertEquals("Wrong value 1", "sec1", config.getString("section.var1"));
    assertEquals("Wrong value 2", "sec2", config.getString("section.var2"));
    SubnodeConfiguration sub = config.getSection("section");
    assertEquals("Wrong sub value 1", "sec1", sub.getString("var1"));
    assertEquals("Wrong sub value 2", "sec2", sub.getString("var2"));
    StringWriter writer = new StringWriter();
    config.save(writer);
    String content = writer.toString();
    int pos = content.indexOf("[section]");
    assertTrue("Section not found: " + content, pos >= 0);
    assertTrue("Section found multiple times: " + content, content.indexOf("[section]", pos + 1) < 0);
}

From source file:io.datalayer.conf.HierarchicalIniConfigurationTest.java

/**
 * Tests querying the content of the global section.
 *//*from  w  w  w.  ja v  a2 s  . c o  m*/
@Test
public void testGetSectionGlobal() throws ConfigurationException {
    HierarchicalINIConfiguration config = setUpConfig(INI_DATA_GLOBAL);
    SubnodeConfiguration section = config.getSection(null);
    assertEquals("Wrong value of global variable", "testGlobal", section.getString("globalVar"));
}

From source file:io.datalayer.conf.HierarchicalIniConfigurationTest.java

/**
 * Tests whether a section that was created by getSection() can be
 * manipulated./*from  w  w  w. j a va  2s .  co m*/
 */
@Test
public void testGetSectionNonExistingManipulate() throws ConfigurationException {
    HierarchicalINIConfiguration config = setUpConfig(INI_DATA);
    SubnodeConfiguration section = config.getSection("newSection");
    section.addProperty("test", "success");
    assertEquals("Main config not updated", "success", config.getString("newSection.test"));
    StringWriter writer = new StringWriter();
    config.save(writer);
    HierarchicalINIConfiguration config2 = setUpConfig(writer.toString());
    section = config2.getSection("newSection");
    assertEquals("Wrong value", "success", section.getString("test"));
}

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  ww . j a  v a2s  . 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. ja  v a  2 s .co  m
 *           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);
        }
    }

}

From source file:org.apache.tamaya.commons.IniConfigurationFormat.java

@Override
public ConfigurationData readConfiguration(String name, InputStream inputStream) {
    PropertyValue data = PropertyValue.createObject();
    data.setMeta("name", name);
    try {// w  ww. ja va 2 s.  com
        HierarchicalINIConfiguration commonIniConfiguration;
        File file = new File(name);
        if (file.exists()) {
            commonIniConfiguration = new HierarchicalINIConfiguration(file);
        } else {
            commonIniConfiguration = new HierarchicalINIConfiguration(new URL(name));
        }
        for (String section : commonIniConfiguration.getSections()) {
            SubnodeConfiguration sectionConfig = commonIniConfiguration.getSection(section);
            PropertyValue sectionNode = ((ObjectValue) data).getOrSetValue(section,
                    () -> PropertyValue.createObject(section));
            Map<String, String> properties = new HashMap<>();
            Iterator<String> keyIter = sectionConfig.getKeys();
            while (keyIter.hasNext()) {
                String key = keyIter.next();
                ((ObjectValue) sectionNode).setValue(key, sectionConfig.getString(key));
            }
        }
    } catch (Exception e) {
        throw new ConfigException("Failed to parse ini-file format from " + name, e);
    }
    return new ConfigurationData(name, this, data);
}

From source file:org.apache.tamaya.format.IniConfigurationFormat.java

@Override
public Map<String, Map<String, String>> readConfiguration(URL url) {
    Map<String, Map<String, String>> result = new HashMap<>();
    try {/*from   w  w w .  j a v a  2  s . c  om*/
        HierarchicalINIConfiguration commonIniConfiguration = new HierarchicalINIConfiguration(url);
        for (String section : commonIniConfiguration.getSections()) {
            SubnodeConfiguration sectionConfig = commonIniConfiguration.getSection(section);
            Map<String, String> properties = new HashMap<>();
            Iterator<String> keyIter = sectionConfig.getKeys();
            while (keyIter.hasNext()) {
                String key = keyIter.next();
                properties.put(key, sectionConfig.getString(key));
            }
            result.put(section, properties);
        }
        return result;
    } catch (ConfigurationException e) {
        throw new ConfigException("Failed to parse ini-file format from " + url, e);
    }
}

From source file:org.apache.tamaya.integration.commons.IniConfigurationFormat.java

public ConfigurationData readConfiguration(URL url) {
    ConfigurationDataBuilder builder = ConfigurationDataBuilder.of(url.toString(), this);
    try {/*  w w  w  .  j a v  a2  s  .c  o  m*/
        HierarchicalINIConfiguration commonIniConfiguration = new HierarchicalINIConfiguration(url);
        for (String section : commonIniConfiguration.getSections()) {
            SubnodeConfiguration sectionConfig = commonIniConfiguration.getSection(section);
            Map<String, String> properties = new HashMap<>();
            Iterator<String> keyIter = sectionConfig.getKeys();
            while (keyIter.hasNext()) {
                String key = keyIter.next();
                properties.put(key, sectionConfig.getString(key));
            }
            builder.addProperties(section, properties);
        }
    } catch (ConfigurationException e) {
        throw new ConfigException("Failed to parse ini-file format from " + url, e);
    }
    return builder.build();
}