Example usage for org.apache.commons.configuration ConfigurationUtils locate

List of usage examples for org.apache.commons.configuration ConfigurationUtils locate

Introduction

In this page you can find the example usage for org.apache.commons.configuration ConfigurationUtils locate.

Prototype

public static URL locate(String base, String name) 

Source Link

Document

Return the location of the specified resource by searching the user home directory, the current classpath and the system classpath.

Usage

From source file:com.qperior.GSAOneBoxProvider.implementations.jive.rest.QPJiveJsonObjectTest.java

private String getJSONString(String fileName) {

    String jsonString = "";

    try {//  w  ww  . j  av a  2 s  .com

        URL url = ConfigurationUtils.locate(null, fileName);
        File file = FileUtils.toFile(url);
        jsonString = FileUtils.readFileToString(file);

    } catch (IOException exc) {

        //nothing
    }

    return jsonString;
}

From source file:com.germinus.easyconf.ConfigurationLearningTest.java

public void testGetFromClasspath() throws ConfigurationException {
    URL result = ConfigurationUtils.locate(null, "test_module.properties");
    assertNotNull(result);// ww  w.j a  v a 2s. c o  m
    PropertiesConfiguration conf = new PropertiesConfiguration();
    conf.load("test_module.properties");
    assertEquals("Error reading properties file from classpath", "test_module",
            conf.getString("property-only-in-test-module"));
}

From source file:com.germinus.easyconf.ClasspathUtil.java

/**
 * Return the location of the specified resource by searching the user home
 * directory, the current classpath and the system classpath.
 *
 * @param base the base path of the resource
 * @param name the name of the resource// www.j  a  va 2 s .co m
 *
 * @return the location of the resource or <code>null</code> if it has not
 * been found
 */
public static URL locateResource(String base, String name) {
    return ConfigurationUtils.locate(base, name);
}

From source file:com.vmware.aurora.global.Configuration.java

/**
 * /* w w  w . ja  v a  2 s  .c o m*/
 * @return a memory view of all properties inside serengeti.properties and vc.properties
 */
private static PropertiesConfiguration init() {
    PropertiesConfiguration config = null;

    String homeDir = System.getProperties().getProperty("serengeti.home.dir");
    String ngcConfigFile = NGC_PROP_FILE;
    if (homeDir != null && homeDir.length() > 0) {
        StringBuilder builder = new StringBuilder();
        builder.append(homeDir).append(File.separator).append("conf").append(File.separator);
        configFileName = builder.toString() + "serengeti.properties";
        ngcConfigFile = builder.toString() + NGC_PROP_FILE;
    } else {
        configFileName = "serengeti.properties";
    }

    try {
        URL url = ConfigurationUtils.locate(null, configFileName);
        logger.info("Reading properties file serengeti.properties from " + url.getPath());
        serengetiCfg = new PropertiesConfiguration();
        serengetiCfg.setEncoding("UTF-8");
        serengetiCfg.setFileName(configFileName);
        serengetiCfg.load();
        config = (PropertiesConfiguration) serengetiCfg.clone();
    } catch (ConfigurationException ex) {
        String message = "Failed to load serengeti.properties file.";
        logger.fatal(message, ex);
        throw AuroraException.APP_INIT_ERROR(ex, message);
    }

    String propertyFilePrefix = System.getProperty("PROPERTY_FILE_PREFIX", "vc");
    String propertyFileName = propertyFilePrefix + ".properties";

    try {
        logger.info("Reading properties file " + propertyFileName);
        vcCfg = new PropertiesConfiguration(propertyFileName);
        Iterator<?> keys = vcCfg.getKeys();
        while (keys.hasNext()) {
            String key = (String) keys.next();
            config.setProperty(key, vcCfg.getProperty(key));
        }
    } catch (ConfigurationException ex) {
        // error out if the configuration file is not there
        String message = "Failed to load vc.properties file.";
        logger.fatal(message, ex);
        throw AuroraException.APP_INIT_ERROR(ex, message);
    }
    // load ngc_registrar.properties
    try {
        logger.info("Reading properties file " + ngcConfigFile);
        ngcCfg = new PropertiesConfiguration(ngcConfigFile);
        ngcCfg.setEncoding("UTF-8");
        Iterator<?> keys = ngcCfg.getKeys();
        while (keys.hasNext()) {
            String key = (String) keys.next();
            config.setProperty(key, ngcCfg.getProperty(key));
        }
    } catch (ConfigurationException ex) {
        // error out if the configuration file is not there
        String message = "Failed to load file " + NGC_PROP_FILE;
        logger.fatal(message, ex);
        throw AuroraException.APP_INIT_ERROR(ex, message);
    }

    logConfig(config);
    return config;
}

From source file:com.germinus.easyconf.ClasspathUtil.java

/**
 * Return the location of the specified resource by searching the user home
 * directory, the current classpath and the system classpath.
 *
 * @param name the name of the resource/* w  ww.  ja  v  a  2  s .c  o  m*/
 *
 * @return the location of the resource or <code>null</code> if it has not
 * been found
 */
public static URL locateResource(String name) {
    return ConfigurationUtils.locate(null, name);
}

From source file:com.qperior.gsa.oneboxprovider.implementations.jive.QPJiveProvider.java

/**
 * Bad style to give back a String result with JSON or error, empty..
 * This is done to fully encapsulate the Jive call (HttpGet) in this method.
 * //from   ww w  .  ja v  a2s.  co m
 * @return JSON String: error, empty, security, timeout
 */
private String callJiveRestApi(QPJiveRESTSearchContent content) {

    if (QPJiveProperties.isTestmode()) {

        try {
            this.log.info("Calling Jive REST API: Testmode (no real call, reading out a JSON file).");
            URL url = ConfigurationUtils.locate(null, "data/json_test2.txt");
            File file = FileUtils.toFile(url);
            return FileUtils.readFileToString(file);
        } catch (IOException exc) {

            this.log.error("IOException: ", exc);
            return null;
        }

    } else {
        String uri = QPJiveProperties.getJiveURL() + content.getURLString() + content.getURLParameterString();
        this.log.info("Calling Jive REST API: URL '" + uri + "'.");
        String result = "";

        try {
            HttpGet httpget = new HttpGet(uri);
            DefaultHttpClient httpclient = new DefaultHttpClient();
            httpget.addHeader("accept", "application/json");

            /* Basic authorization with user provided by the GSA. */
            // cast the security provider to use the method
            //QPJiveSecurityProvider secprovider = (QPJiveSecurityProvider) this.getSecurityProvider();
            //httpget.addHeader("Authorization", "Basic " + secprovider.getBasicAccessToken());
            // To test some features directly write down base64 encoded access token
            httpget.addHeader("Authorization", "Basic " + "MTYsdfdsgs2343zcr");

            HttpResponse response = httpclient.execute(httpget);
            // use status
            // 200: OK
            // 401: security
            // 500: internal server error
            int status = response.getStatusLine().getStatusCode();

            if (status == 200) {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    //long len = entity.getContentLength();
                    result = EntityUtils.toString(entity);
                    /*if (len != -1 && len < 2048) {
                        result = EntityUtils.toString(entity);
                    } else {
                        // Stream content out
                       this.log.error("HttpEntity longer than 2048!");
                    }*/

                    /* Response from Jive always starts with "throw 'allowIllegalResourceCall is false.';", 
                     * with this it is no valid JSON, a JSONObject text must begin with '{'
                     */
                    result = StringUtils.replace(result, QPJiveProperties.getJsonPrefix(), "");

                    if (result != null && !result.equals("")) {
                        this.log.info("Calling Jive REST API: result string '" + result + "'.");
                    } else {
                        result = RESULT_EMPTY;
                        this.log.info("Calling Jive REST API: result string is empty.");
                    }
                } else {
                    result = RESULT_EMPTY;
                    this.log.error("Calling Jive REST API: HttpEntity is null.");
                }
            } else if (status == 401) {
                result = RESULT_SECURITY;
            } else {
                result = RESULT_ERROR;
            }
        } catch (HttpHostConnectException hexc) {
            // Timeout
            result = RESULT_TIMEOUT;
            this.log.error("Timeout in contacting Jive REST API.");
        } catch (Exception exc) {

            result = RESULT_ERROR;
            this.log.error("Exception: ", exc);
        }

        return result;
    }
}