Example usage for org.apache.commons.configuration Configuration getClass

List of usage examples for org.apache.commons.configuration Configuration getClass

Introduction

In this page you can find the example usage for org.apache.commons.configuration Configuration getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:org.soaplab.admin.ExploreConfig.java

/*************************************************************************
 *
 *  An entry point//from   w  w  w . ja  v a2 s  .c o  m
 *
 *************************************************************************/
@SuppressWarnings("unchecked")
public static void main(String[] args) {

    try {
        BaseCmdLine cmd = getCmdLine(args, ExploreConfig.class);
        String param = null;

        // remember System properties before we initialize
        // Soaplab's Config
        Properties sysProps = System.getProperties();

        // for testing: adding property files
        if ((param = cmd.getParam("-f")) != null) {
            title("Adding config property files:");
            String[] files = param.split(",");
            for (String filename : files) {
                if (Config.addConfigPropertyFile(filename))
                    msgln("Added:  " + filename);
                else
                    msgln("Failed: " + filename);
            }
            msgln("");
        }
        if ((param = cmd.getParam("-xml")) != null) {
            title("Adding config XML files:");
            String[] files = param.split(",");
            for (String filename : files) {
                if (Config.addConfigXMLFile(filename))
                    msgln("Added:  " + filename);
                else
                    msgln("Failed: " + filename);
            }
            msgln("");
        }

        // list of configurations
        if (cmd.hasOption("-lf")) {
            title("Using property resources (in this order):");
            int i = 0;
            while (true) {
                Configuration cfg = null;
                int count = 0;
                try {
                    cfg = Config.get().getConfiguration(i++);
                    for (Iterator<String> it = cfg.getKeys(); it.hasNext(); count++)
                        it.next();
                } catch (IndexOutOfBoundsException e) {
                    break;
                }
                if (count == 0)
                    msg(i + ": (empty) ");
                else
                    msg(i + ": (" + String.format("%1$5d", count) + ") ");
                if (cfg instanceof FileConfiguration) {
                    msgln(((FileConfiguration) cfg).getFile().getAbsolutePath());
                } else if (cfg instanceof SystemConfiguration) {
                    msgln("Java System Properties");
                } else if (cfg instanceof BaseConfiguration) {
                    msgln("Directly added properties (no config file)");
                } else {
                    msgln(cfg.getClass().getName());
                }
            }
            msgln("");
        }

        // list of properties
        boolean listProps = cmd.hasOption("-l");
        boolean listAllProps = cmd.hasOption("-la");
        if (listProps || listAllProps) {
            title("Available properties" + (listProps ? " (except System properties):" : ":"));
            SortedMap<String, String> allProps = new TreeMap<String, String>();
            int maxLen = 0;
            for (Iterator<String> it = Config.get().getKeys(); it.hasNext();) {
                String key = it.next();
                if (listAllProps || !sysProps.containsKey(key)) {
                    if (key.length() > maxLen)
                        maxLen = key.length();
                    String[] values = Config.get().getStringArray(key);
                    if (values.length != 0)
                        allProps.put(key, StringUtils.join(values, ","));
                }
            }
            for (Iterator<Map.Entry<String, String>> it = allProps.entrySet().iterator(); it.hasNext();) {
                Map.Entry<String, String> entry = it.next();
                msgln(String.format("%1$-" + maxLen + "s", entry.getKey()) + " => " + entry.getValue());
            }
            msgln("");
        }

        // get properties by prefix
        if ((param = cmd.getParam("-prefix")) != null) {
            String serviceName = cmd.getParam("-service");
            if (serviceName == null)
                title("Properties matching prefix:");
            else
                title("Properties matching service name and prefix:");
            SortedSet<String> selectedKeys = new TreeSet<String>();
            int maxLen = 0;
            Properties props = Config.getMatchingProperties(param, serviceName, null);
            for (Enumeration en = props.propertyNames(); en.hasMoreElements();) {
                String key = (String) en.nextElement();
                if (key.length() > maxLen)
                    maxLen = key.length();
                selectedKeys.add(key);
            }
            for (Iterator<String> it = selectedKeys.iterator(); it.hasNext();) {
                String key = it.next();
                msgln(String.format("%1$-" + maxLen + "s", key) + " => " + props.getProperty(key));
            }
            msgln("");
        }

        // show individual properties value
        if (cmd.params.length > 0) {
            int maxLen = 0;
            for (int i = 0; i < cmd.params.length; i++) {
                int len = cmd.params[i].length();
                if (len > maxLen)
                    maxLen = len;
            }
            title("Selected properties:");
            for (int i = 0; i < cmd.params.length; i++) {
                String value = Config.get().getString(cmd.params[i]);
                msgln(String.format("%1$-" + maxLen + "s", cmd.params[i]) + " => "
                        + (value == null ? "<null>" : value));
            }
        }

    } catch (Throwable e) {
        processErrorAndExit(e);
    }
}