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

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

Introduction

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

Prototype

public void setFile(File file) 

Source Link

Usage

From source file:m.c.m.proxyma.ProxymaServlet.java

/**
 * Initialize the servlet and the proxyma environment.
 */// w  w w. jav a  2 s . c  o m
@Override
public void init() {
    try {
        //Obtain configuration parameters..
        ServletConfig config = this.getServletConfig();
        String proxymaConfigFile = this.getInitParameter("ProxymaConfigurationFile");
        String proxymaContextName = this.getInitParameter("ProxymaContextName");
        String proxymaLogsDirectory = this.getInitParameter("ProxymaLogsDir");

        //if the config file init-parameter is notspecified use the default configuration
        if (proxymaConfigFile == null)
            proxymaConfigFile = config.getServletContext().getRealPath("/WEB-INF/proxyma-config.xml");

        //Hack to get the servlet path reading it directly from the deployment descriptor.
        //Valid until apache will put a getServletMappings() method into the ServletConfig class.
        XMLConfiguration deploymentDescriptor = null;
        try {
            deploymentDescriptor = new XMLConfiguration();
            deploymentDescriptor.setFile(new File(config.getServletContext().getRealPath("/WEB-INF/web.xml")));
            deploymentDescriptor.setValidating(false);
            deploymentDescriptor.load();
        } catch (ConfigurationException ex) {
            Logger.getLogger("").log(Level.SEVERE, "Unable to load web.xml", ex);
        }
        deploymentDescriptor.setExpressionEngine(new XPathExpressionEngine());
        String servletPath = deploymentDescriptor
                .getString("servlet-mapping[servlet-name='" + config.getServletName() + "']/url-pattern");
        String proxymaServletContext = config.getServletContext().getContextPath()
                + servletPath.replaceFirst("/\\*$", GlobalConstants.EMPTY_STRING);

        //Check if the logs directory init-parameter ends with "/"
        if (!proxymaLogsDirectory.endsWith("/")) {
            proxymaLogsDirectory = proxymaLogsDirectory + "/";
        }

        //Create a new proxyma facade
        this.proxyma = new ProxymaFacade();

        //Create a new proxyma context
        this.proxymaContext = proxyma.createNewContext(proxymaContextName, proxymaServletContext,
                proxymaConfigFile, proxymaLogsDirectory);

        //Create a reverse proxy engine for this servlet thread
        this.proxymaEngine = proxyma.createNewProxyEngine(proxymaContext);
    } catch (IllegalAccessException ex) {
        ex.printStackTrace();
    }
}

From source file:ch.admin.suis.msghandler.config.SedexCertConfigFactory.java

/**
 * Factory constructor.//from   w  w w.ja  v  a  2s  .  co m
 *
 * @param configFile File object referencing the file certificateConfiguration.xml rom the sedex configuratioon.
 * @throws ConfigurationException if the file doen't exist or if doesn't conform to the required XML Schema.
 */
public SedexCertConfigFactory(File configFile) throws ConfigurationException {
    FileUtils.isFile(configFile, ".certificateConfigFile[@filePath]");

    XMLConfiguration xmlConfig = new XMLConfiguration();
    // disable the schema validation by XMLConfiguration, since the config file does not contain a schema location
    xmlConfig.setSchemaValidation(false);
    xmlConfig.setFile(configFile);
    try {
        XMLValidator.validateSedexCertificateConfig(configFile);
        xmlConfig.load();
    } catch (ConfigurationException ex) {
        LOG.error("XML File: " + configFile.getAbsolutePath()
                + " could not be loaded. It seems the XML file is not valid");
        throw ex;
    }
    parseFile(xmlConfig, configFile.getAbsolutePath());
}

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

/**
 * Tests setting text of the root element.
 *//*from   w  w  w .j a va  2 s. c  o  m*/
@Test
public void testSetTextRootElement() throws ConfigurationException {
    conf.setProperty("", "Root text");
    conf.save(testSaveConf);
    XMLConfiguration copy = new XMLConfiguration();
    copy.setFile(testSaveConf);
    checkSavedConfig(copy);
}

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

/**
 * Tests saving to a URL./*from  w ww.j av  a  2  s  . c  o  m*/
 */
@Test
public void testSaveToURL() throws Exception {
    conf.save(testSaveConf.toURI().toURL());
    XMLConfiguration checkConfig = new XMLConfiguration();
    checkConfig.setFile(testSaveConf);
    checkSavedConfig(checkConfig);
}

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

/**
 * Tests removing the text of the root element.
 *///  w w w.j  av  a 2  s  .c o m
@Test
public void testClearTextRootElement() throws ConfigurationException {
    final String xml = "<e a=\"v\">text</e>";
    conf.clear();
    StringReader in = new StringReader(xml);
    conf.load(in);
    assertEquals("Wrong text of root", "text", conf.getString(""));

    conf.clearProperty("");
    conf.save(testSaveConf);
    XMLConfiguration copy = new XMLConfiguration();
    copy.setFile(testSaveConf);
    checkSavedConfig(copy);
}

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

/**
 * Tests using multiple attribute values, which are partly escaped when
 * delimiter parsing is not disabled.//from   w w w . jav  a  2s.c  om
 */
@Test
public void testMultipleAttrValuesEscaped() throws ConfigurationException {
    conf.addProperty("test.dir[@name]", "C:\\Temp\\");
    conf.addProperty("test.dir[@name]", "C:\\Data\\");
    conf.save(testSaveConf);
    XMLConfiguration checkConf = new XMLConfiguration();
    checkConf.setFile(testSaveConf);
    checkSavedConfig(checkConf);
}

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

/**
 * Tests adding nodes from another configuration.
 *///  w  w  w . j  av a  2  s  . c  om
@Test
public void testAddNodesCopy() throws ConfigurationException {
    XMLConfiguration c2 = new XMLConfiguration(testProperties2);
    conf.addNodes("copiedProperties", c2.getRootNode().getChildren());
    conf.save(testSaveConf);
    XMLConfiguration checkConf = new XMLConfiguration();
    checkConf.setFile(testSaveConf);
    checkSavedConfig(checkConf);
}

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

/**
 * Tests saving a configuration that was created from a hierarchical
 * configuration. This test exposes bug CONFIGURATION-301.
 *///www  .jav a  2  s .com
@Test
public void testSaveAfterCreateWithCopyConstructor() throws ConfigurationException {
    HierarchicalConfiguration hc = conf.configurationAt("element2");
    conf = new XMLConfiguration(hc);
    conf.save(testSaveConf);
    XMLConfiguration checkConfig = new XMLConfiguration();
    checkConfig.setFile(testSaveConf);
    checkSavedConfig(checkConfig);
    assertEquals("Wrong name of root element", "element2", checkConfig.getRootElementName());
}

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

/**
 * Tests registering the publicId of a DTD.
 *///from w w w  . j a v  a2s .  c om
@Test
public void testRegisterEntityId() throws Exception {
    URL dtdURL = getClass().getResource("/properties.dtd");
    final String publicId = "http://commons.apache.org/test/properties.dtd";
    conf = new XMLConfiguration("testDtd.xml");
    conf.setPublicID(publicId);
    conf.save(testSaveConf);
    XMLConfiguration checkConfig = new XMLConfiguration();
    checkConfig.setFile(testSaveConf);
    checkConfig.registerEntityId(publicId, dtdURL);
    checkConfig.setValidating(true);
    checkSavedConfig(checkConfig);
}

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

/**
 * Tests setting an attribute on the root element.
 *///from  w ww . j a va2  s  .c o m
@Test
public void testSetRootAttribute() throws ConfigurationException {
    conf.setProperty("[@test]", "true");
    assertEquals("Root attribute not set", "true", conf.getString("[@test]"));
    conf.save(testSaveConf);
    XMLConfiguration checkConf = new XMLConfiguration();
    checkConf.setFile(testSaveConf);
    checkSavedConfig(checkConf);
    assertTrue("Attribute not found after save", checkConf.containsKey("[@test]"));
    checkConf.setProperty("[@test]", "newValue");
    checkConf.save();
    conf = checkConf;
    checkConf = new XMLConfiguration();
    checkConf.setFile(testSaveConf);
    checkSavedConfig(checkConf);
    assertEquals("Attribute not modified after save", "newValue", checkConf.getString("[@test]"));
}