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

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

Introduction

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

Prototype

private void load(InputSource source) throws ConfigurationException 

Source Link

Document

Loads a configuration file from the specified input source.

Usage

From source file:org.restcomm.connect.dao.mybatis.MybatisExtensionsConfigurationDao.java

@Override
public boolean validate(ExtensionConfiguration extensionConfiguration) {
    ExtensionConfiguration.configurationType configurationType = extensionConfiguration.getConfigurationType();
    if (configurationType.equals(ExtensionConfiguration.configurationType.JSON)) {
        Gson gson = new Gson();
        try {/* w  w  w. j a  va 2  s .c  om*/
            Object o = gson.fromJson((String) extensionConfiguration.getConfigurationData(), Object.class);
            String json = new GsonBuilder().setPrettyPrinting().create().toJson(o);
            return (json != null || !json.isEmpty());
        } catch (Exception e) {
            if (logger.isDebugEnabled()) {
                logger.debug("invalid json format, exception: " + e);
            }
        } finally {
            gson = null;
        }
    } else if (configurationType.equals(ExtensionConfiguration.configurationType.XML)) {
        Configuration xml = null;
        try {
            XMLConfiguration xmlConfiguration = new XMLConfiguration();
            xmlConfiguration.setDelimiterParsingDisabled(true);
            xmlConfiguration.setAttributeSplittingDisabled(true);
            InputStream is = IOUtils.toInputStream(extensionConfiguration.getConfigurationData().toString());
            xmlConfiguration.load(is);
            xml = xmlConfiguration;
            return (xml != null || !xml.isEmpty());
        } catch (Exception e) {
            if (logger.isDebugEnabled()) {
                logger.debug("invalid xml document, exception: " + e);
            }
        } finally {
            xml = null;
        }
    }
    return false;
}

From source file:org.restcomm.connect.dao.mybatis.NotificationsDaoTest.java

@Before
public void before() {
    final InputStream data = getClass().getResourceAsStream("/mybatis.xml");
    final SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
    final SqlSessionFactory factory = builder.build(data);
    manager = new MybatisDaoManager();
    manager.start(factory);// ww w  .  j a va 2s  .  co  m

    XMLConfiguration xmlConfiguration = new XMLConfiguration();
    xmlConfiguration.setDelimiterParsingDisabled(true);
    xmlConfiguration.setAttributeSplittingDisabled(true);
    try {
        xmlConfiguration.load("restcomm.xml");
        RestcommConfiguration.createOnce(xmlConfiguration);
        RestcommConfiguration.getInstance().getMain().setInstanceId(instanceId.toString());
    } catch (ConfigurationException e) {
        e.printStackTrace();
    }
}

From source file:org.restcomm.connect.http.cors.CorsFilter.java

private void initLazily(ServletRequest request) {
    if (lazyServletContext == null) {
        ServletContext context = request.getServletContext();
        String rootPath = context.getRealPath("/");
        rootPath = StringUtils.stripEnd(rootPath, "/"); // remove trailing "/" character
        String restcommXmlPath = rootPath + "/WEB-INF/conf/restcomm.xml";

        // ok, found restcomm.xml. Now let's get rcmlserver/base-url configuration setting
        File restcommXmlFile = new File(restcommXmlPath);
        // Create apache configuration
        XMLConfiguration apacheConf = new XMLConfiguration();
        apacheConf.setDelimiterParsingDisabled(true);
        apacheConf.setAttributeSplittingDisabled(true);
        try {// w w  w .  j  a v a  2s.  c o  m
            apacheConf.load(restcommXmlPath);
        } catch (ConfigurationException e) {
            e.printStackTrace();
        }
        // Create high-level configuration
        ConfigurationSource source = new ApacheConfigurationSource(apacheConf);
        RcmlserverConfigurationSet rcmlserverConfig = new RcmlserverConfigurationSetImpl(source);
        // initialize allowedOrigin
        String baseUrl = rcmlserverConfig.getBaseUrl();
        if (baseUrl != null && (!baseUrl.trim().equals(""))) {
            // baseUrl is set. We need to return CORS allow headers
            allowedOrigin = baseUrl;
        }

        lazyServletContext = context;

        logger.info("Initialized (lazily) CORS servlet response filter. allowedOrigin: " + allowedOrigin);
    }
}

From source file:org.restcomm.connect.interpreter.GatherSpeechTest.java

private Configuration getConfiguration(String path) throws ConfigurationException {
    XMLConfiguration xmlConfiguration = new XMLConfiguration();
    xmlConfiguration.setDelimiterParsingDisabled(true);
    xmlConfiguration.setAttributeSplittingDisabled(true);
    xmlConfiguration.load(path);

    return xmlConfiguration;
}

From source file:org.restcomm.connect.interpreter.mediagroup.StopMediaGroupTest.java

private Configuration getConfiguration(String path) throws ConfigurationException {
    XMLConfiguration xmlConfiguration = new XMLConfiguration();
    xmlConfiguration.setDelimiterParsingDisabled(true);
    xmlConfiguration.setAttributeSplittingDisabled(true);
    xmlConfiguration.load(path);
    return xmlConfiguration;
}

From source file:org.restcomm.connect.telephony.api.util.DigestAuthenticationTest.java

private Configuration prepareConfiguration() {
    Configuration xml = null;//from  w w w  . ja va  2s  .  c o  m
    try {
        URL restcommXml = this.getClass().getClassLoader().getResource("restcomm.xml");
        XMLConfiguration xmlConfiguration = new XMLConfiguration();
        xmlConfiguration.setDelimiterParsingDisabled(true);
        xmlConfiguration.setAttributeSplittingDisabled(true);
        xmlConfiguration.load(restcommXml);
        xml = xmlConfiguration;
    } catch (Exception e) {
        logger.error("Exception: " + e);
    }
    return xml;
}

From source file:org.sindice.analytics.servlet.ServletConfigurationContextListener.java

@Override
public void contextInitialized(ServletContextEvent sce) {
    final ServletContext context = sce.getServletContext();
    String contextPath = context.getContextPath().replaceFirst("^/", "");
    if (contextPath.equals("")) {
        // application deployed in ROOT context
        String applicationName = context.getInitParameter("applicationName");
        if (applicationName != null && !applicationName.equals("")) {
            contextPath = "ROOT_" + applicationName;
        } else {//www  . j a v a  2 s  .  c  om
            contextPath = "ROOT";
        }
        logger.warn("Application deployed in ROOT context.");
    } else {
        logger.info("Application deployed in [" + contextPath + "] context.");
    }
    logger.info("Will use [" + contextPath + "] as a part of a path for reading/storing configuration");

    String servletContextName = context.getServletContextName();
    addEnvToContext(context);

    String sindiceHome = null;
    // FOR DEBIAN PACKAGE LOD2 PROJECT INTEGRATION
    // check that following directory exists
    // if yes please use it
    if (new File("/etc/" + contextPath + "/config.xml").exists()) {
        sindiceHome = "/etc";
        logger.info("Found config.xml at [/etc/" + contextPath + "/config.xml]. Setting sindiceHome to : ["
                + sindiceHome + "]");
    } else {
        logger.info(
                "File /etc/" + contextPath + "/config.xml does not exists will try to determine sindiceHome");
    }
    // END DEBIAN PACKAGE LOD2 PROJECT INTEGRATION
    if (sindiceHome == null && context.getAttribute("sindice.home") != null) {
        sindiceHome = (String) context.getAttribute("sindice.home");
        logger.info("Setting sindiceHome from sindice.home env variable to [" + sindiceHome + "]");
    }
    if (sindiceHome == null && context.getAttribute("SINDICE_HOME") != null) {
        sindiceHome = (String) context.getAttribute("SINDICE_HOME");
        logger.info("Setting sindiceHome from SINDICE_HOME env variable to [" + sindiceHome + "]");
    }
    if (sindiceHome == null || "".equals(sindiceHome.trim())) {
        String userHome = (String) context.getAttribute("user.home");
        sindiceHome = (userHome == null ? "" : userHome) + File.separatorChar + "sindice";
        logger.warn("Neither sindice.home nor SINDICE_HOME are not defined, assuming {}", sindiceHome);
    }

    logger.info("Looking for configuration in [" + sindiceHome + File.separatorChar + contextPath + "]");

    // important to set these two as they are used later in logback.xml
    context.setAttribute("sindice.home", sindiceHome);
    context.setAttribute("app.name", contextPath);

    configFolder = new File(sindiceHome + File.separatorChar + contextPath);
    if (!(configFolder.exists() && configFolder.isDirectory())) {
        logger.warn("Missing configuration folder {}", configFolder);
        if (configFolder.mkdirs()) {
            logger.warn("Creating default configuration at " + configFolder);

        } else {
            // set logging level to INFO
            configureLogging(context, configFolder);
            return;
        }
    }
    // does a specific folder exist for this servlet context?
    if (servletContextName == null) {
        logger.error("specify display-name element in web.xml !!! ");
    } else {
        File specificFolder = new File(configFolder, servletContextName);
        if (specificFolder.exists() && specificFolder.isDirectory()) {
            configFolder = specificFolder;
        }
    }
    logger.info("loading configuration from folder {}", configFolder);
    configureLogging(context, configFolder);
    final XMLConfiguration config = createXMLConfiguration(context);
    File applicationConfigFile = new File(configFolder, "config.xml");
    if (!applicationConfigFile.exists()) {
        logger.warn("missing application config file {}", applicationConfigFile);
        loadDefaultConfiguration(config, applicationConfigFile);
    } else {
        try {
            config.load(applicationConfigFile);
            logger.info("parsed {}", applicationConfigFile);
        } catch (ConfigurationException e) {
            logger.error("Could not load configuration from {}", applicationConfigFile, e);
            loadDefaultConfiguration(config, null);
        }
    }
    context.setAttribute("config", config);
    logger.info("config now availabe via the following line of code\n"
            + "    XMLConfiguration appConfig = (XMLConfiguration) servletContext.getAttribute(\"config\");");

    logger.info("Starting up {}", servletContextName);
}

From source file:org.sindice.analytics.servlet.ServletConfigurationContextListener.java

private void loadDefaultConfiguration(final XMLConfiguration config, File saveAs) {
    logger.info("loading default configuration");
    InputStream in = ServletConfigurationContextListener.class.getClassLoader()
            .getResourceAsStream("default-config.xml");
    if (in == null) {
        logger.error("application is missing default-config.xml from classpath");
    } else {/*from  w  w w  .  j  av a 2 s.  c om*/
        try {
            config.load(in);
            if (saveAs != null) {
                try {
                    saveAs.getParentFile().mkdirs();
                    config.save(saveAs);
                    logger.info("wrote default configuration to {}", saveAs);
                } catch (ConfigurationException e) {
                    logger.warn("Could not write configuration to {}", saveAs, e);
                }
            }
        } catch (ConfigurationException e) {
            logger.error("could not load default-config.xml", e);
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                logger.warn("problem closing stream", e);
            }
        }
    }
}

From source file:org.sindice.core.analytics.commons.webapps.SparqledContextListener.java

@Override
public void contextInitialized(ServletContextEvent sce) {
    final ServletContext context = sce.getServletContext();

    addEnvToContext(context);//from   w w  w. j  a v a  2 s .c om
    final String home = (String) context.getInitParameter(SPARQLED_HOME);
    final File sparqledHome = home == null ? null : new File(home);

    if (sparqledHome == null || !sparqledHome.exists()) {
        throw new RuntimeException("Missing sparqled home");
    }
    logger.info("Looking for configuration in [{}]", sparqledHome);

    configureLogging(context, sparqledHome);
    final XMLConfiguration config = createXMLConfiguration(context);
    final File applicationConfigFile = new File(sparqledHome, CONFIG_XML);

    try {
        config.load(applicationConfigFile);
    } catch (ConfigurationException e) {
        throw new RuntimeException("Invalid configuration file: " + applicationConfigFile, e);
    }
    // important to set this as it is used later in logback.xml
    context.setAttribute("sparqled.home", home);

    context.setAttribute("config", config);
    logger.info("config now availabe via the following line of code\n"
            + "    XMLConfiguration appConfig = (XMLConfiguration) servletContext.getAttribute(\"config\");");
}

From source file:org.sindice.servlet.sparqlqueryservlet.ServletConfigurationContextListener.java

@Override
public void contextInitialized(ServletContextEvent sce) {
    final ServletContext context = sce.getServletContext();
    String contextPath = context.getContextPath().replaceFirst("^/", "");
    if (contextPath.equals("")) {
        // application deployed in ROOT context
        String applicationName = context.getInitParameter("applicationName");
        if (applicationName != null && !applicationName.equals("")) {
            contextPath = "ROOT_" + applicationName;
        } else {//from ww  w  .  j av a  2  s. co m
            contextPath = "ROOT";
        }
        logger.warn("Application deployed in ROOT context.");
    } else {
        logger.info("Application deployed in [" + contextPath + "] context.");
    }
    logger.info("Will use [" + contextPath + "] as a part of a path for reading/storing configuration");

    String servletContextName = context.getServletContextName();
    addEnvToContext(context);

    String sindiceHome = null;
    // FOR DEBIAN PACKAGE LOD2 PROJECT INTEGRATION
    // check that following directory exists
    // if yes please use it
    if (new File("/etc/" + contextPath + "/config.xml").exists()) {
        sindiceHome = "/etc";
        logger.info("Found config.xml at [/etc/" + contextPath + "/config.xml]. Setting sindiceHome to : ["
                + sindiceHome + "]");
    } else {
        logger.info(
                "File /etc/" + contextPath + "/config.xml does not exists will try to determine sindiceHome");
    }
    // END DEBIAN PACKAGE LOD2 PROJECT INTEGRATION
    if (sindiceHome == null && context.getAttribute("sindice.home") != null) {
        sindiceHome = (String) context.getAttribute("sindice.home");
        logger.info("Setting sindiceHome from sindice.home env variable to [" + sindiceHome + "]");
    }
    if (sindiceHome == null && context.getAttribute("SINDICE_HOME") != null) {
        sindiceHome = (String) context.getAttribute("SINDICE_HOME");
        logger.info("Setting sindiceHome from SINDICE_HOME env variable to [" + sindiceHome + "]");
    }
    if (sindiceHome == null || "".equals(sindiceHome.trim())) {
        String userHome = (String) context.getAttribute("user.home");
        sindiceHome = (userHome == null ? "" : userHome) + File.separatorChar + "sindice";
        logger.warn("Neither sindice.home nor SINDICE_HOME are not defined, assuming {}", sindiceHome);
    }

    logger.info("Looking for configuration in [" + sindiceHome + File.separatorChar + contextPath + "]");

    // important to set these two as they are used later in logback.xml
    context.setAttribute("sindice.home", sindiceHome);
    context.setAttribute("app.name", contextPath);

    File configFolder = new File(sindiceHome + File.separatorChar + contextPath);
    if (!(configFolder.exists() && configFolder.isDirectory())) {
        logger.warn("Missing configuration folder {}", configFolder);
        if (configFolder.mkdirs()) {
            logger.warn("Creating default configuration at " + configFolder);

        } else {
            // set logging level to INFO
            configureLogging(context, configFolder);
            return;
        }
    }
    // does a specific folder exist for this servlet context?
    if (servletContextName == null) {
        logger.error("specify display-name element in web.xml !!! ");
    } else {
        File specificFolder = new File(configFolder, servletContextName);
        if (specificFolder.exists() && specificFolder.isDirectory()) {
            configFolder = specificFolder;
        }
    }
    logger.info("loading configuration from folder {}", configFolder);
    configureLogging(context, configFolder);
    final XMLConfiguration config = createXMLConfiguration(context);
    File applicationConfigFile = new File(configFolder, "config.xml");
    if (!applicationConfigFile.exists()) {
        logger.warn("missing application config file {}", applicationConfigFile);
        loadDefaultConfiguration(config, applicationConfigFile);
    } else {
        try {
            config.load(applicationConfigFile);
            logger.info("parsed {}", applicationConfigFile);
        } catch (ConfigurationException e) {
            logger.error("Could not load configuration from {}", applicationConfigFile, e);
            loadDefaultConfiguration(config, null);
        }
    }
    context.setAttribute("config", config);
    logger.info("config now availabe via the following line of code\n"
            + "    XMLConfiguration appConfig = (XMLConfiguration) servletContext.getAttribute(\"config\");");

    logger.info("Starting up {}", servletContextName);
}