Example usage for org.apache.commons.configuration XMLConfiguration getDocument

List of usage examples for org.apache.commons.configuration XMLConfiguration getDocument

Introduction

In this page you can find the example usage for org.apache.commons.configuration XMLConfiguration getDocument.

Prototype

public Document getDocument() 

Source Link

Document

Returns the XML document this configuration was loaded from.

Usage

From source file:io.haze.core.ServiceFactory.java

/**
 * Create all {@link Service} instances for this {@link ServiceFactory}.
 *
 * @param application The application.//from  w  w  w .  j a  va2s . c  om
 */
protected void createServices(Application application) {
    XMLConfiguration configuration = application.getConfiguration();
    int count = 0;
    String element = getConfigurationElement();
    String subElement = getConfigurationSubElement();

    try {
        count = ((Element) configuration.getDocument().getElementsByTagName(element).item(0))
                .getElementsByTagName(subElement).getLength();
    } catch (Exception e) {
        logger.error(String.format("You must specify at least one %s", subElement));

        return;
    }

    for (int i = 0; i < count; i++) {
        String namespace = String.format("%s.%s(%d)", element, subElement, i);
        String name = configuration.getString(String.format("%s[@name]", namespace), "").trim();

        if (name.length() == 0) {
            logger.error(String.format("<%s> is missing name attribute", subElement));

            continue;
        }

        try {
            ServiceFactoryContext context = new ServiceFactoryContext(application, namespace, name);
            Service service = newInstance(context);

            service.setServiceId(getNextServiceId());
            application.registerService(service);
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
    }

}

From source file:com.sqewd.open.dal.core.persistence.DataManager.java

private void init(final XMLConfiguration config) throws Exception {
    try {/*  w  w  w .  java 2 s  .c  o  m*/
        state = EnumInstanceState.Running;

        if (Env.get().getEntityLoader() == null) {
            Env.get().setEntityLoader(new EntityClassLoader(this.getClass().getClassLoader()));
        }

        String rootpath = Env._CONFIG_XPATH_ROOT_ + _CONFIG_XPATH_;
        NodeList nl = XMLUtils.search(rootpath, config.getDocument().getDocumentElement());
        if (nl == null || nl.getLength() <= 0)
            throw new Exception(
                    "Invalid Configuration : DataManager configuration node [" + rootpath + "] not found.");
        Element dmroot = (Element) nl.item(0);

        NodeList packnl = XMLUtils.search(_CONFIG_ENTITY_PACKAGES_, dmroot);
        if (packnl != null && packnl.getLength() > 0) {
            for (int ii = 0; ii < packnl.getLength(); ii++) {
                Element jelm = (Element) packnl.item(ii);

                String jar = jelm.getAttribute("name");
                if (jar != null) {
                    String pack = jelm.getAttribute("package");
                    if (pack != null && !pack.isEmpty()) {
                        List<String> packs = null;
                        if (scanjars.containsKey(jar)) {
                            packs = scanjars.get(jar);
                        } else {
                            packs = new ArrayList<String>();
                            scanjars.put(jar, packs);
                        }

                        packs.add(pack);
                    }
                }
            }
            scanEntities();
        }

        NodeList pernl = XMLUtils.search(_CONFIG_PERSIST_XPATH_, dmroot);
        if (pernl != null && pernl.getLength() > 0) {
            initPersisters((Element) pernl.item(0));
        }

        log.debug("DataManager initialzied...");
    } catch (Exception e) {
        log.error(e.getLocalizedMessage());
        state = EnumInstanceState.Exception;
        LogUtils.stacktrace(log, e);
    }
}

From source file:com.sqewd.open.dal.server.ServerConfig.java

/**
 * Initialize the Server Configuration./*  w  ww.  j  a v  a2 s .c  o m*/
 * 
 * @param config
 *            - XML Configuration.
 * 
 * @throws Exception
 */
public void init(XMLConfiguration config) throws Exception {
    String value = config.getString(_CONFIG_SERVER_PORT_);
    if (value != null && !value.isEmpty())
        port = Integer.parseInt(value);

    value = config.getString(_CONFIG_SERVER_NTHREADS_);
    if (value != null && !value.isEmpty())
        numThreads = Integer.parseInt(value);

    value = config.getString(_CONFIG_SERVER_WEBROOT_);
    if (value != null && !value.isEmpty())
        webRoot = value;
    else
        webRoot = Env.get().getWorkdir();

    value = config.getString(_CONFIG_JETTY_HOME_);
    if (value != null && !value.isEmpty())
        jettyHome = value;

    value = config.getString(_CONFIG_SERVER_STOPPORT_);
    if (value != null && !value.isEmpty())
        monitorPort = Integer.parseInt(value);

    value = config.getString(_CONFIG_SERVICES_PACKAGE_);
    if (value != null && !value.isEmpty()) {
        servicesPackage = value;
    }

    loadWebApps(config.getDocument());

    Env.get().register(_SERVER_CONFIG_KEY_, this, true);
}

From source file:com.sqewd.open.dal.core.persistence.db.H2DbPersister.java

private void checkSetup() throws Exception {
    XMLConfiguration config = new XMLConfiguration(dbconfig);
    String version = config.getString(_CONFIG_SETUP_VERSION_);
    if (version == null || version.isEmpty())
        throw new Exception(
                "Invalid DB Setup Configuration : Missing parameter [" + _CONFIG_SETUP_VERSION_ + "]");

    if (!checkSchema()) {
        SimpleDbQuery dbq = new SimpleDbQuery();
        List<String> createsql = dbq.getCreateTableDDL(DBVersion.class);
        Connection conn = getConnection(true);
        Statement stmnt = conn.createStatement();
        try {// w w  w.  j a v  a  2  s.  com
            for (String sql : createsql) {
                log.debug("TABLE SQL [" + sql + "]");
                stmnt.execute(sql);
            }

            DBVersion dbv = (DBVersion) DataManager.newInstance(DBVersion.class);
            dbv.setVersion(version);
            save(dbv, false);

            NodeList nl = XMLUtils.search(_CONFIG_SETUP_ENTITIES_, config.getDocument().getDocumentElement());
            if (nl != null && nl.getLength() > 0) {
                for (int ii = 0; ii < nl.getLength(); ii++) {
                    Element elm = (Element) nl.item(ii);
                    String eclass = elm.getAttribute("class");
                    if (eclass != null && !eclass.isEmpty()) {
                        Class<?> cls = Class.forName(eclass);
                        createsql = dbq.getCreateTableDDL(cls);
                        for (String sql : createsql) {
                            log.debug("TABLE SQL [" + sql + "]");
                            stmnt.execute(sql);
                        }
                        createIndex(elm, cls, dbq, stmnt);
                    }
                }
            }

        } finally {
            if (stmnt != null && !stmnt.isClosed())
                stmnt.close();

        }
    } else {
        List<AbstractEntity> versions = read("", DBVersion.class, 1);
        if (versions == null || versions.isEmpty()) {
            throw new Exception("Error retrieving Schema Version. Database might be corrupted.");
        }
        for (AbstractEntity ver : versions) {
            if (ver instanceof DBVersion) {
                DBVersion dbv = (DBVersion) ver;
                if (dbv.getVersion().compareTo(version) != 0) {
                    throw new Exception("Database Version missmatch, Expection version [" + version
                            + "], current DB version [" + dbv.getVersion() + "]");
                }
            }
        }
    }
}

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

/**
 * Tests the clone() method./*w ww . j a  v  a2s.c  o  m*/
 */
@Test
public void testClone() {
    Configuration c = (Configuration) conf.clone();
    assertTrue(c instanceof XMLConfiguration);
    XMLConfiguration copy = (XMLConfiguration) c;
    assertNotNull(conf.getDocument());
    assertNull(copy.getDocument());
    assertNotNull(conf.getFileName());
    assertNull(copy.getFileName());

    copy.setProperty("element3", "clonedValue");
    assertEquals("value", conf.getString("element3"));
    conf.setProperty("element3[@name]", "originalFoo");
    assertEquals("foo", copy.getString("element3[@name]"));
}

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

/**
 * Tests the copy constructor.//from  w  w  w.  j a v a 2s.c  o  m
 */
@Test
public void testInitCopy() throws ConfigurationException {
    XMLConfiguration copy = new XMLConfiguration(conf);
    assertEquals("value", copy.getProperty("element"));
    assertNull("Document was copied, too", copy.getDocument());
    ConfigurationNode root = copy.getRootNode();
    for (ConfigurationNode node : root.getChildren()) {
        assertNull("Reference was not cleared", node.getReference());
    }

    removeTestFile();
    copy.setFile(testSaveConf);
    copy.save();
    copy.clear();
    checkSavedConfig(copy);
}

From source file:uk.ac.soton.itinnovation.easyjena.core.impl.JenaOntologyManager.java

/**
 * Load an XML file containing alternative import locations for ontologies, e.g.:
 *
 *   <?xml version="1.0" encoding="UTF-8"?>
 *   <configs>//from   ww  w  .j ava2  s. c  om
 *
 *         <file>
 *            <uri>http://www.w3.org/2001/XMLSchema#</uri>
 *            <path>ontologies/XMLSchema.xsd</path>
 *            <location>[disk|web|classpath]</location>
 *         </file>
 *
 *   </configs>
 *
 * @param xmlpath the location of the XML config file
 */
public void loadImportLocationConfigFile(String xmlpath) {

    try {
        logger.debug("Loading import location config file {}", xmlpath);
        XMLConfiguration config = new XMLConfiguration(xmlpath);

        // get all mappings
        NodeList files = config.getDocument().getElementsByTagName("file");
        for (int i = 0; i < files.getLength(); i++) {
            Node file = files.item(i);
            NodeList tags = file.getChildNodes();

            String uri = null;
            String location = null;
            //use "all" as default loading type
            LoadingLocation type = LoadingLocation.ALL;

            for (int j = 0; j < tags.getLength(); j++) {
                Node tag = tags.item(j);

                switch (tag.getNodeName()) {
                case "uri":
                    uri = tag.getTextContent();
                    break;

                case "path":
                    location = tag.getTextContent();
                    break;

                case "location":
                    switch (tag.getTextContent()) {
                    case "disk":
                        type = LoadingLocation.DIRECTORY;
                        break;
                    case "web":
                        type = LoadingLocation.WEB;
                        break;
                    case "classpath":
                        type = LoadingLocation.CLASSPATH;
                        break;
                    default:
                        logger.warn("Invalid loading location provided: {}. "
                                + "Needs to be one of [disk, web, classpath]", tag.getTextContent());
                    }
                    break;

                default:
                    //do nothing
                }
            }

            if (uri != null && location != null) {
                addImportLocationMapping(uri, location, type);
            } else {
                logger.warn("Incomplete entry in XML file, skipping <{}> ({})", uri, location);
            }
        }
    } catch (ConfigurationException e) {
        logger.error("Error loading import locations XML file {}", xmlpath, e);
        //no throwing necessary, will continue without any mapped loactions
    }
}