Example usage for org.apache.commons.configuration CompositeConfiguration getKeys

List of usage examples for org.apache.commons.configuration CompositeConfiguration getKeys

Introduction

In this page you can find the example usage for org.apache.commons.configuration CompositeConfiguration getKeys.

Prototype

public Iterator getKeys(String key) 

Source Link

Usage

From source file:org.soaplab.services.Config.java

/**************************************************************************
 * Get all properties contained in the configuration whose keys
 * (names) match the specified prefix. <p>
 *
 * The remaining parameters are used in the similar way as in
 * {@link #getString getString} method. Meaning if they are not
 * null, the following prefixes are actually tried:
 *
 *<pre>/*from  w ww . j av  a2 s  .c o m*/
 * &lt;prefix&gt;.
 * &lt;class-of-owner&gt;.&lt;prefix&gt;.
 * &lt;serviceName&gt;.&lt;prefix&gt;.
 *</pre>
 *
 * These prefixes are tried in the order as indicated above -
 * causing the properties with earlier prefixes being overwritten
 * by those having newer ones. For example, having the following
 * properties in the configuration (<tt>classic.helloworld</tt> is
 * a name of a service):
 *
 *<pre>
 * classic.helloworld.grid.env.One = ein
 * classic.helloworld.grid.env.Two = zwei
 * grid.env.One = un
 * grid.env.Two = deux
 * grid.env.Three = trois
 *</pre>
 *
 * and calling:
 *
 *<pre>
 * getMatchingProperties ("grid.env", "classic.helloworld", null)
 *</pre>
 *
 * will return properties:
 *
 *<pre>
 * One = ein
 * Two = zwei
 * Three = troi
 *</pre>
 *
 * @param prefix is a part of property name (may or may not end
 * with a dot)
 *
 * @param serviceName is a possibly 'prefix' for the 'prefix' (can
 * be null)
 *
 * @param owner is another possible 'prefix' for the 'prefix': if
 * 'owner' is of type Class its name is used, otherwise, the name
 * of the class of the instance 'owner' is used
 *
 * @return properties matching the prefix - the prefix is not part
 * of the returned property names; if nothing matches, an empty
 * list is returned (not null)
 **************************************************************************/
public static Properties getMatchingProperties(String prefix, String serviceName, Object owner) {
    Properties props = new Properties();

    if (prefix.endsWith("."))
        prefix = prefix.substring(0, prefix.length() - 1);

    // try with a pure prefix
    int prefixLen = prefix.length() + 1; // 1 is for ending dor
    CompositeConfiguration cfg = get();
    synchronized (cfg) {
        for (Iterator it = cfg.getKeys(prefix); it.hasNext();) {
            String key = (String) it.next();
            props.put(key.substring(prefixLen), cfg.getString(key));
        }
    }

    String extendedPrefix = null;

    // try with an owner class and prefix
    if (owner != null) {
        extendedPrefix = (owner instanceof Class ? ((Class) owner).getName() : owner.getClass().getName()) + "."
                + prefix;
        prefixLen = extendedPrefix.length() + 1;
        synchronized (cfg) {
            for (Iterator it = cfg.getKeys(extendedPrefix); it.hasNext();) {
                String key = (String) it.next();
                props.put(key.substring(prefixLen), cfg.getString(key));
            }
        }
    }

    // try with a service name and prefix
    if (serviceName != null) {
        extendedPrefix = serviceName + "." + prefix;
        prefixLen = extendedPrefix.length() + 1;
        synchronized (cfg) {
            for (Iterator it = cfg.getKeys(extendedPrefix); it.hasNext();) {
                String key = (String) it.next();
                props.put(key.substring(prefixLen), cfg.getString(key));
            }
        }
    }

    return props;
}

From source file:uk.co.gidley.jmxmonitor.monitoring.MonitoringGroup.java

/**
 * Initialise the monitor. If possible we start the JMX connection now. If not we create a placeholder.
 *
 * @param monitorUrlKey/*  w w w  . ja va  2 s.  c om*/
 * @param monitorsConfiguration
 * @throws MalformedObjectNameException
 * @throws MalformedURLException
 */
private void initialiseMonitorUrl(String monitorUrlKey, CompositeConfiguration monitorsConfiguration)
        throws MalformedObjectNameException, MalformedURLException {
    logger.debug("Initialising Monitor Connection {}", monitorUrlKey);

    String url = monitorsConfiguration.getString(ThreadManager.PROPERTY_PREFIX + monitorUrlKey + URL);
    try {
        // Create JMX connection
        JMXServiceURL serviceUrl = new JMXServiceURL(url);
        JMXConnector jmxc = JMXConnectorFactory.connect(serviceUrl, null);
        logger.debug("JMX connection made {}", jmxc);
        MonitoringGroup.MonitorUrlHolder monitorUrlHolder = monitorUrlHolders.get(monitorUrlKey);
        monitorUrlHolder.setmBeanServerConnection(jmxc.getMBeanServerConnection());
        monitorUrlHolder.getMonitors().clear();

        // Parse monitors inside this
        List<String> loadedMonitors = new ArrayList<String>();
        Iterator<String> monitorKeys = monitorsConfiguration
                .getKeys(ThreadManager.PROPERTY_PREFIX + monitorUrlKey);
        while (monitorKeys.hasNext()) {
            String key = monitorKeys.next();
            if (!key.endsWith(URL)) {
                String monitorName = key.substring(
                        ThreadManager.PROPERTY_PREFIX.length() + monitorUrlKey.length() + 1,
                        key.lastIndexOf("."));
                // Only load each on once (there will be n keys)
                if (!loadedMonitors.contains(monitorName)) {
                    constructMonitor(monitorUrlKey, monitorsConfiguration, monitorUrlHolder, monitorName);
                    loadedMonitors.add(monitorName);
                }
            }
        }
    } catch (IOException e) {
        if (e instanceof MalformedURLException) {
            throw (MalformedURLException) e;
        }
        logger.warn("Unable to connect to {}, {}", monitorUrlKey, e);
    }
}