Example usage for org.springframework.core.io.support PropertiesLoaderUtils loadAllProperties

List of usage examples for org.springframework.core.io.support PropertiesLoaderUtils loadAllProperties

Introduction

In this page you can find the example usage for org.springframework.core.io.support PropertiesLoaderUtils loadAllProperties.

Prototype

public static Properties loadAllProperties(String resourceName) throws IOException 

Source Link

Document

Load all properties from the specified class path resource (in ISO-8859-1 encoding), using the default class loader.

Usage

From source file:org.xinta.eazycode.common.test.selenium.SeleniumTestCase.java

/**
 * ?selenium client.//from  w w w. ja v a 2  s. c om
 * application.test.properties?selenium?,??.
 */
@BeforeClass
public static void setUp() throws Exception {
    Properties p = PropertiesLoaderUtils.loadAllProperties(PROPERTY_FILE);
    String browser = p.getProperty(PROPERTY_BROWSER_NAME, DEFAULT_BROWSER);
    String url = p.getProperty(PROPERTY_URL_NAME, DEFAULT_URL);
    String host = p.getProperty(PROPERTY_SELENIUM_HOST_NAME, DEFAULT_SELENIUM_HOST);
    int port = Integer.valueOf(p.getProperty(PROPERTY_SELENIUM_PORT_NAME, DEFAULT_SELENIUM_PORT));

    selenium = new DefaultSelenium(host, port, browser, url);
    selenium.start();
    selenium.windowFocus();
    selenium.windowMaximize();
}

From source file:de.tudarmstadt.ukp.clarin.webanno.webapp.home.page.SettingsUtil.java

public static Properties getVersionProperties() {
    if (versionInfo == null) {
        try {//from  w w  w .j a  v a 2 s.  c  o m
            versionInfo = PropertiesLoaderUtils.loadAllProperties("/META-INF/version.properties");
        } catch (IOException e) {
            versionInfo = new Properties();
            versionInfo.setProperty(PROP_VERSION, "unknown");
            versionInfo.setProperty(PROP_TIMESTAMP, "unknown");
            versionInfo.setProperty(PROP_BUILD_NUMBER, "unknown");
        }
    }

    return versionInfo;
}

From source file:com.taobao.itest.util.PropertiesUtil.java

/**
 * The properties file key-value information into a Map, if the key contains
 * a underline into the next hump style change
 * //from w  w w  . ja  v  a  2 s  . c o  m
 * @param resourceName
 * @param characterSet
 * @return
 * @throws IOException
 */
public static Map<String, String> loadProperties(String resourceName, String characterSet) {
    Properties properties;
    Map<String, String> paramsMap = Collections.synchronizedMap(new CamelCasingHashMap<String, String>());
    try {
        properties = PropertiesLoaderUtils.loadAllProperties(resourceName);

        for (Enumeration<?> keys = properties.keys(); keys.hasMoreElements();) {
            String key = (String) keys.nextElement();
            paramsMap.put(key, getValue(properties.getProperty(key).trim(), characterSet));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return paramsMap;
}

From source file:org.cloudfoundry.identity.uaa.authentication.login.LoginInfoEndpoint.java

public LoginInfoEndpoint() {
    try {/*from   ww w  . j  a v a2 s .com*/
        gitProperties = PropertiesLoaderUtils.loadAllProperties("git.properties");
    } catch (IOException e) {
        // Ignore
    }
    try {
        buildProperties = PropertiesLoaderUtils.loadAllProperties("build.properties");
    } catch (IOException e) {
        // Ignore
    }
}

From source file:org.cloudfoundry.identity.uaa.login.BuildInfo.java

@Override
public void afterPropertiesSet() {
    try {/*from   w w  w. j a  v a  2  s.  c  o  m*/
        Properties gitProperties = PropertiesLoaderUtils.loadAllProperties("git.properties");
        commitId = gitProperties.getProperty("git.commit.id.abbrev", "UNKNOWN");
        String currentTime = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date());
        timestamp = gitProperties.getProperty("git.commit.time", currentTime);
    } catch (IOException e) {
        logger.debug("Exception loading git.properties", e);
    }
    try {
        Properties buildProperties = PropertiesLoaderUtils.loadAllProperties("build.properties");
        version = buildProperties.getProperty("build.version");
    } catch (IOException e) {
        logger.debug("Exception loading build.properties", e);
    }
    Assert.hasText(uaaUrl);
    Assert.hasText(version);
    Assert.hasText(commitId);
    Assert.hasText(timestamp);
}

From source file:de.hska.ld.core.controller.InfoController.java

@Autowired
public void init(Environment env) {
    try {//ww  w.  jav  a  2 s  .c  o m
        Properties infoProp = PropertiesLoaderUtils.loadAllProperties("info.properties");
        String sandbox = Boolean.parseBoolean(env.getProperty("module.sandbox.enabled")) ? " Sandbox" : "";
        info = new Info(infoProp.getProperty("info.title") + sandbox, infoProp.getProperty("info.version"),
                infoProp.getProperty("info.organization"));
    } catch (IOException exception) {
        //
    }
}

From source file:org.codice.ddf.security.common.PropertiesLoader.java

/**
 * Will attempt to load properties from a file using the given classloader. If that fails,
 * several other methods will be tried until the properties file is located.
 *
 * @param propertiesFile/*from ww  w  .  j  ava  2s  .c  om*/
 * @param classLoader
 * @return Properties
 */
public static Properties loadProperties(String propertiesFile, ClassLoader classLoader) {
    boolean error = false;
    Properties properties = new Properties();
    if (propertiesFile != null) {
        try {
            if (logger.isDebugEnabled()) {
                logger.debug("Attempting to load properties from " + propertiesFile
                        + " with Spring PropertiesLoaderUtils.");
            }
            properties = PropertiesLoaderUtils.loadAllProperties(propertiesFile);
        } catch (IOException e) {
            error = true;
            logger.error("Unable to load properties using default Spring properties loader.", e);
        }
        if (error || properties.isEmpty()) {
            if (classLoader != null) {
                try {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Attempting to load properties from " + propertiesFile
                                + " with Spring PropertiesLoaderUtils with class loader.");
                    }
                    properties = PropertiesLoaderUtils.loadAllProperties(propertiesFile, classLoader);
                    error = false;
                } catch (IOException e) {
                    error = true;
                    logger.error("Unable to load properties using default Spring properties loader.", e);
                }
            } else {
                try {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Attempting to load properties from " + propertiesFile
                                + " with Spring PropertiesLoaderUtils with class loader.");
                    }
                    properties = PropertiesLoaderUtils.loadAllProperties(propertiesFile,
                            PropertiesLoader.class.getClassLoader());
                    error = false;
                } catch (IOException e) {
                    error = true;
                    logger.error("Unable to load properties using default Spring properties loader.", e);
                }
            }
        }

        if (error || properties.isEmpty()) {
            if (logger.isDebugEnabled()) {
                logger.debug("Attempting to load properties from file system: " + propertiesFile);
            }
            File propFile = new File(propertiesFile);
            // If properties file has fully-qualified absolute path (which
            // the blueprint file specifies) then can load it directly.
            if (propFile.isAbsolute()) {
                logger.debug("propertiesFile {} is absolute", propertiesFile);
                propFile = new File(propertiesFile);
            } else {
                // Otherwise need to prepend parent path which is based on
                // the installation directory
                String karafHome = System.getProperty("karaf.home");
                if (karafHome != null && !karafHome.isEmpty()) {
                    propFile = new File(karafHome, propertiesFile);
                } else {
                    karafHome = System.getProperty("ddf.home");
                    if (karafHome != null && !karafHome.isEmpty()) {
                        propFile = new File(karafHome, propertiesFile);
                    } else {
                        propFile = new File(propertiesFile);
                    }
                }
            }
            properties = new Properties();
            try (FileReader reader = new FileReader(propFile)) {
                properties.load(reader);
            } catch (FileNotFoundException e) {
                error = true;
                logger.error("Could not find properties file: " + propFile.getAbsolutePath(), e);
            } catch (IOException e) {
                error = true;
                logger.error("Error reading properties file: " + propFile.getAbsolutePath(), e);
            }
        }
        if (error || properties.isEmpty()) {
            if (logger.isDebugEnabled()) {
                logger.debug("Attempting to load properties as a resource: " + propertiesFile);
            }
            InputStream ins = PropertiesLoader.class.getResourceAsStream(propertiesFile);
            if (ins != null) {
                try {
                    properties.load(ins);
                    ins.close();
                } catch (IOException e) {
                    logger.error("Unable to load properties: " + propertiesFile, e);
                } finally {
                    try {
                        ins.close();
                    } catch (IOException ignore) {
                    }
                }
            }
        }
    } else {
        if (logger.isDebugEnabled()) {
            logger.debug("Properties file must not be null.");
        }
    }

    return properties;
}

From source file:ddf.security.common.util.PropertiesLoader.java

/**
 * Will attempt to load properties from a file using the given classloader. If that fails,
 * several other methods will be tried until the properties file is located.
 * /*from   w  ww .java 2s. c o  m*/
 * @param propertiesFile
 * @param classLoader
 * @return Properties
 */
public static Properties loadProperties(String propertiesFile, ClassLoader classLoader) {
    boolean error = false;
    Properties properties = new Properties();
    if (propertiesFile != null) {
        try {
            if (logger.isDebugEnabled()) {
                logger.debug("Attempting to load properties from " + propertiesFile
                        + " with Spring PropertiesLoaderUtils.");
            }
            properties = PropertiesLoaderUtils.loadAllProperties(propertiesFile);
        } catch (IOException e) {
            error = true;
            logger.error("Unable to load properties using default Spring properties loader.", e);
        }
        if (error || properties.isEmpty()) {
            if (classLoader != null) {
                try {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Attempting to load properties from " + propertiesFile
                                + " with Spring PropertiesLoaderUtils with class loader.");
                    }
                    properties = PropertiesLoaderUtils.loadAllProperties(propertiesFile, classLoader);
                    error = false;
                } catch (IOException e) {
                    error = true;
                    logger.error("Unable to load properties using default Spring properties loader.", e);
                }
            } else {
                try {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Attempting to load properties from " + propertiesFile
                                + " with Spring PropertiesLoaderUtils with class loader.");
                    }
                    properties = PropertiesLoaderUtils.loadAllProperties(propertiesFile,
                            PropertiesLoader.class.getClassLoader());
                    error = false;
                } catch (IOException e) {
                    error = true;
                    logger.error("Unable to load properties using default Spring properties loader.", e);
                }
            }
        }

        if (error || properties.isEmpty()) {
            if (logger.isDebugEnabled()) {
                logger.debug("Attempting to load properties from file system: " + propertiesFile);
            }
            File propFile = null;
            if (propertiesFile.startsWith("/")) {
                propFile = new File(propertiesFile);
            } else {
                String karafHome = System.getProperty("karaf.home");
                if (karafHome != null && !karafHome.isEmpty()) {
                    propFile = new File(karafHome, propertiesFile);
                } else {
                    karafHome = System.getProperty("ddf.home");
                    if (karafHome != null && !karafHome.isEmpty()) {
                        propFile = new File(karafHome, propertiesFile);
                    } else {
                        propFile = new File(propertiesFile);
                    }
                }
            }
            properties = new Properties();
            try {
                properties.load(new FileReader(propFile));
            } catch (FileNotFoundException e) {
                error = true;
                logger.error("Could not find properties file: " + propFile.getAbsolutePath(), e);
            } catch (IOException e) {
                error = true;
                logger.error("Error reading properties file: " + propFile.getAbsolutePath(), e);
            }
        }
        if (error || properties.isEmpty()) {
            if (logger.isDebugEnabled()) {
                logger.debug("Attempting to load properties as a resource: " + propertiesFile);
            }
            InputStream ins = PropertiesLoader.class.getResourceAsStream(propertiesFile);
            if (ins != null) {
                try {
                    properties.load(ins);
                    ins.close();
                } catch (IOException e) {
                    logger.error("Unable to load properties: " + propertiesFile, e);
                } finally {
                    try {
                        ins.close();
                    } catch (IOException ignore) {
                    }
                }
            }
        }
    } else {
        if (logger.isDebugEnabled()) {
            logger.debug("Properties file must not be null.");
        }
    }

    return properties;
}

From source file:ddf.security.PropertiesLoader.java

/**
 * Will attempt to load properties from a file using the given classloader. If that fails,
 * several other methods will be tried until the properties file is located.
 *
 * @param propertiesFile// w  ww.  ja v a2s.c om
 * @param classLoader
 * @return Properties
 */
public static Properties loadProperties(String propertiesFile, ClassLoader classLoader) {
    boolean error = false;
    Properties properties = new Properties();
    if (propertiesFile != null) {
        try {
            LOGGER.debug("Attempting to load properties from {} with Spring PropertiesLoaderUtils.",
                    propertiesFile);
            properties = PropertiesLoaderUtils.loadAllProperties(propertiesFile);
        } catch (IOException e) {
            error = true;
            LOGGER.error("Unable to load properties using default Spring properties loader.", e);
        }
        if (error || properties.isEmpty()) {
            if (classLoader != null) {
                try {
                    LOGGER.debug(
                            "Attempting to load properties from {} with Spring PropertiesLoaderUtils with class loader.",
                            propertiesFile);
                    properties = PropertiesLoaderUtils.loadAllProperties(propertiesFile, classLoader);
                    error = false;
                } catch (IOException e) {
                    error = true;
                    LOGGER.error("Unable to load properties using default Spring properties loader.", e);
                }
            } else {
                try {
                    LOGGER.debug(
                            "Attempting to load properties from {} with Spring PropertiesLoaderUtils with class loader.",
                            propertiesFile);
                    properties = PropertiesLoaderUtils.loadAllProperties(propertiesFile,
                            PropertiesLoader.class.getClassLoader());
                    error = false;
                } catch (IOException e) {
                    error = true;
                    LOGGER.error("Unable to load properties using default Spring properties loader.", e);
                }
            }
        }

        if (error || properties.isEmpty()) {
            LOGGER.debug("Attempting to load properties from file system: {}", propertiesFile);
            File propFile = new File(propertiesFile);
            // If properties file has fully-qualified absolute path (which
            // the blueprint file specifies) then can load it directly.
            if (propFile.isAbsolute()) {
                LOGGER.debug("propertiesFile {} is absolute", propertiesFile);
                propFile = new File(propertiesFile);
            } else {
                String karafHome = System.getProperty("karaf.home");
                if (karafHome != null && !karafHome.isEmpty()) {
                    propFile = new File(karafHome, propertiesFile);
                } else {
                    karafHome = System.getProperty("ddf.home");
                    if (karafHome != null && !karafHome.isEmpty()) {
                        propFile = new File(karafHome, propertiesFile);
                    } else {
                        propFile = new File(propertiesFile);
                    }
                }
            }
            properties = new Properties();

            try (InputStreamReader reader = new InputStreamReader(new FileInputStream(propertiesFile),
                    StandardCharsets.UTF_8)) {
                properties.load(reader);
            } catch (FileNotFoundException e) {
                error = true;
                LOGGER.error("Could not find properties file: {}", propFile.getAbsolutePath(), e);
            } catch (IOException e) {
                error = true;
                LOGGER.error("Error reading properties file: {}", propFile.getAbsolutePath(), e);
            }
        }
        if (error || properties.isEmpty()) {
            LOGGER.debug("Attempting to load properties as a resource: {}", propertiesFile);
            InputStream ins = PropertiesLoader.class.getResourceAsStream(propertiesFile);
            if (ins != null) {
                try {
                    properties.load(ins);
                    ins.close();
                } catch (IOException e) {
                    LOGGER.error("Unable to load properties: {}", propertiesFile, e);
                } finally {
                    IOUtils.closeQuietly(ins);
                }
            }
        }

        //replace any ${prop} with system properties
        Properties filtered = new Properties();
        for (Map.Entry<?, ?> entry : properties.entrySet()) {
            filtered.put(StrSubstitutor.replaceSystemProperties(entry.getKey()),
                    StrSubstitutor.replaceSystemProperties(entry.getValue()));
        }
        properties = filtered;

    } else {
        LOGGER.debug("Properties file must not be null.");
    }

    return properties;
}

From source file:hr.fer.zemris.vhdllab.platform.remoting.HttpClientRequestExecutor.java

public HttpClientRequestExecutor(HttpClient httpClient) throws IOException {
    super(httpClient);
    getHttpClient().getParams().setAuthenticationPreemptive(true);
    Properties properties = PropertiesLoaderUtils.loadAllProperties("server.properties");
    int port = Integer.parseInt(properties.getProperty("port"));
    Protocol easyhttps = new Protocol("https", new EasySSLProtocolSocketFactory(), port);
    Protocol.registerProtocol("https", easyhttps);
}