Example usage for org.springframework.util SystemPropertyUtils resolvePlaceholders

List of usage examples for org.springframework.util SystemPropertyUtils resolvePlaceholders

Introduction

In this page you can find the example usage for org.springframework.util SystemPropertyUtils resolvePlaceholders.

Prototype

public static String resolvePlaceholders(String text) 

Source Link

Document

Resolve ${...}} placeholders in the given text, replacing them with corresponding system property values.

Usage

From source file:com.kixeye.chassis.bootstrap.TestUtils.java

public static OutputStream createFile(String path) {
    try {//www.  ja v  a 2 s  . c  o  m
        Path p = Paths.get(SystemPropertyUtils.resolvePlaceholders(path));
        if (Files.exists(p)) {
            Files.delete(p);
        }
        File file = new File(path);
        if (!file.getParentFile().exists()) {
            if (!file.getParentFile().mkdirs()) {
                throw new RuntimeException("Unable to create parent file(s) " + file.getParent());
            }
        }
        return Files.newOutputStream(p);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.shengpay.commons.bp.logback.LogbackConfigurer.java

/**
 * Initialize logback from the given file location, with no config file refreshing. Assumes an XML file in case of a ".xml" file extension, and a properties file otherwise.
 * /*from w ww .j a v  a  2  s  . co  m*/
 * @param location
 *            the location of the config file: either a "classpath:" location (e.g. "classpath:mylogback.properties"), an absolute file URL (e.g. "file:C:/logback.properties), or a plain absolute path in the file system (e.g. "C:/logback.properties")
 * @throws FileNotFoundException
 *             if the location specifies an invalid file path
 */
public static void initLogging(String location) throws FileNotFoundException {
    String resolvedLocation = SystemPropertyUtils.resolvePlaceholders(location);
    URL url = ResourceUtils.getURL(resolvedLocation);
    if (resolvedLocation.toLowerCase().endsWith(XML_FILE_EXTENSION)) {
        //          DOMConfigurator.configure(url);
        configurator.setContext(lc);
        lc.reset();
        try {
            configurator.doConfigure(url);
        } catch (JoranException ex) {
            throw new FileNotFoundException(url.getPath());
        }
        lc.start();
    }
    //      else {
    //          PropertyConfigurator.configure(url);
    //      }
}

From source file:com.trigonic.utils.spring.beans.ImportHelper.java

public static Set<Resource> importResource(XmlBeanDefinitionReader reader, Resource sourceResource,
        String location) {/*from w  w  w .  j av a  2s. c o  m*/
    location = SystemPropertyUtils.resolvePlaceholders(location); // resolve system properties: e.g. "${user.dir}"
    Set<Resource> actualResources = new LinkedHashSet<Resource>(4);

    if (isAbsoluteLocation(location)) {
        importAbsoluteResource(reader, location, actualResources);
    } else {
        importRelativeResource(reader, sourceResource, location, actualResources);
    }

    return actualResources;
}

From source file:org.intalio.tempo.web.SysPropWebApplicationContext.java

@Override
public Resource getResource(String location) {
    location = SystemPropertyUtils.resolvePlaceholders(location);
    return super.getResource(location);
}

From source file:com.shengpay.commons.bp.logback.LogbackWebConfigurer.java

/**
 * Initialize logback, including setting the web app root system property.
 * // ww w  . j a v  a2  s . c o  m
 * @param servletContext
 *            the current ServletContext
 * @see WebUtils#setWebAppRootSystemProperty
 */
public static void initLogging(ServletContext servletContext) {
    // Expose the web app root system property.
    if (exposeWebAppRoot(servletContext)) {
        WebUtils.setWebAppRootSystemProperty(servletContext);
    }

    // Only perform custom logback initialization in case of a config file.
    String location = servletContext.getInitParameter(CONFIG_LOCATION_PARAM);
    if (location != null) {
        // Perform actual logback initialization; else rely on logback's default initialization.
        try {
            // Return a URL (e.g. "classpath:" or "file:") as-is;
            // consider a plain file path as relative to the web application root directory.
            if (!ResourceUtils.isUrl(location)) {
                // Resolve system property placeholders before resolving real path.
                location = SystemPropertyUtils.resolvePlaceholders(location);
                location = WebUtils.getRealPath(servletContext, location);
            }

            // Write log message to server log.
            servletContext.log("Initializing logback from [" + location + "]");

            // Initialize without refresh check, i.e. without logback's watchdog thread.
            LogbackConfigurer.initLogging(location);

        } catch (FileNotFoundException ex) {
            throw new IllegalArgumentException("Invalid 'logbackConfigLocation' parameter: " + ex.getMessage());
        }
    }
}

From source file:com.kixeye.chassis.bootstrap.TestUtils.java

@SuppressWarnings("unchecked")
public static void writePropertiesToFile(String path, Set<Path> filesCreated,
        SimpleEntry<String, String>... entries) {
    Properties properties = new Properties();
    for (SimpleEntry<String, String> entry : entries) {
        properties.put(entry.getKey(), entry.getValue());
    }/*from   w w  w .ja  v a2 s. c o m*/
    Path p = Paths.get(SystemPropertyUtils.resolvePlaceholders(path));
    try (OutputStream os = createFile(p.toString())) {
        properties.store(os, "test properties");
        filesCreated.add(p);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.amalto.core.storage.datasource.DataSourceFactory.java

private static synchronized InputStream readDataSourcesConfiguration() {
    Properties configuration = MDMConfiguration.getConfiguration();
    String dataSourcesLocation = (String) configuration.get(DB_DATASOURCES);
    if (dataSourcesLocation == null) { // DB_DATASOURCES property is mandatory to continue.
        throw new IllegalStateException(DB_DATASOURCES + " is not defined in MDM configuration.");
    }//from  w w w. ja v  a  2 s .  c  o  m
    String dataSourcesFileName = SystemPropertyUtils.resolvePlaceholders(dataSourcesLocation);
    InputStream configurationAsStream = null;
    // 1- Try from file (direct lookup)
    File file = new File(dataSourcesFileName);
    if (file.exists()) {
        LOGGER.info("Reading from datasource file at '" + file.getAbsolutePath() + "'."); //$NON-NLS-1$ //$NON-NLS-2$
        try {
            configurationAsStream = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            throw new IllegalStateException("Unexpected state (file exists but can't create a stream from it).",
                    e);
        }
    }
    // 2- From class path
    if (configurationAsStream == null) {
        List<String> filePaths = Arrays.asList(dataSourcesFileName);
        Iterator<String> iterator = filePaths.iterator();

        String currentFilePath = StringUtils.EMPTY;
        while (configurationAsStream == null && iterator.hasNext()) {
            currentFilePath = iterator.next();
            configurationAsStream = Thread.currentThread().getContextClassLoader()
                    .getResourceAsStream(currentFilePath);
        }
        if (configurationAsStream != null) {
            LOGGER.info("Reading from datasource file at '" + currentFilePath + "'."); //$NON-NLS-1$ //$NON-NLS-2$
        }
    }
    // 3- error: configuration was not found
    if (configurationAsStream == null) {
        throw new IllegalStateException(
                "Could not find datasources configuration file '" + dataSourcesFileName + "'.");
    }
    return configurationAsStream;
}

From source file:ch.qos.logback.ext.spring.LogbackConfigurer.java

/**
 * Initialize logback from the given file.
 *
 * @param location the location of the config file: either a "classpath:" location
 *                 (e.g. "classpath:logback.xml"), an absolute file URL
 *                 (e.g. "file:C:/logback.xml), or a plain absolute path in the file system
 *                 (e.g. "C:/logback.xml")
 * @throws java.io.FileNotFoundException if the location specifies an invalid file path
 * @throws ch.qos.logback.core.joran.spi.JoranException
 *                                       Thrown
 *///from   w  ww . ja v a 2 s  .  c  o m
public static void initLogging(String location) throws FileNotFoundException, JoranException {
    String resolvedLocation = SystemPropertyUtils.resolvePlaceholders(location);
    URL url = ResourceUtils.getURL(resolvedLocation);
    LoggerContext loggerContext = (LoggerContext) StaticLoggerBinder.getSingleton().getLoggerFactory();

    // in the current version logback automatically configures at startup the context, so we have to reset it
    loggerContext.reset();

    // reinitialize the logger context.  calling this method allows configuration through groovy or xml
    new ContextInitializer(loggerContext).configureByResource(url);
}

From source file:com.yxw.framework.common.spring.ext.LogbackConfigurer.java

/**
 * Initialize logback from the given file.
 * /*w  w  w . j a  va  2  s.c o m*/
 * @param location
 *            the location of the config file: either a "classpath:" location (e.g. "classpath:logback.xml"), an
 *            absolute file URL (e.g. "file:C:/logback.xml), or a plain absolute path in the file system (e.g.
 *            "C:/logback.xml")
 * @throws java.io.FileNotFoundException
 *             if the location specifies an invalid file path
 * @throws ch.qos.logback.core.joran.spi.JoranException
 *             Thrown
 */
public static void initLogging(String location) throws FileNotFoundException, JoranException {
    String resolvedLocation = SystemPropertyUtils.resolvePlaceholders(location);
    URL url = ResourceUtils.getURL(resolvedLocation);
    LoggerContext loggerContext = (LoggerContext) StaticLoggerBinder.getSingleton().getLoggerFactory();

    // in the current version logback automatically configures at startup
    // the context, so we have to reset it
    loggerContext.reset();

    // reinitialize the logger context. calling this method allows
    // configuration through groovy or xml
    new ContextInitializer(loggerContext).configureByResource(url);
}

From source file:org.intalio.tempo.web.SysPropApplicationContextLoader.java

public void loadAppContext(String appContextFile, boolean loadDefinitionOnStartup) {
    if (appContextFile == null) {
        throw new IllegalArgumentException("Argument 'contextFile' is null");
    }/*w w  w .  j  av  a 2s  . co m*/
    _appContextFile = SystemPropertyUtils.resolvePlaceholders(appContextFile);
    if (loadDefinitionOnStartup) {
        _beanFactory = new ClassPathXmlApplicationContext(_appContextFile);
    } else {
        if (_appContextFile.startsWith(FILE_PREFIX)) {
            _appContextFile = _appContextFile.substring(FILE_PREFIX.length());
        }
        Resource configResource = new FileSystemResource(_appContextFile);
        _beanFactory = new XmlBeanFactory(configResource);
    }
}