Example usage for org.apache.commons.lang3.text StrSubstitutor replaceSystemProperties

List of usage examples for org.apache.commons.lang3.text StrSubstitutor replaceSystemProperties

Introduction

In this page you can find the example usage for org.apache.commons.lang3.text StrSubstitutor replaceSystemProperties.

Prototype

public static String replaceSystemProperties(final Object source) 

Source Link

Document

Replaces all the occurrences of variables in the given source object with their matching values from the system properties.

Usage

From source file:com.opensymphony.xwork2.config.providers.EnvsValueSubstitutor.java

@Override
public String substitute(String value) {
    LOG.debug("Substituting value {} with proper System variable or environment variable", value);

    String substituted = StrSubstitutor.replaceSystemProperties(value);
    return strSubstitutor.replace(substituted);
}

From source file:com.redhat.lightblue.util.JsonUtils.java

public static JsonNode json(String s, boolean systemPropertySubstitution) throws IOException {
    String jsonString;/*from w  w w  .  j  a v a 2s .co  m*/
    if (systemPropertySubstitution) {
        // do system property expansion
        jsonString = StrSubstitutor.replaceSystemProperties(s);
    } else {
        jsonString = s;
    }
    return getObjectMapper().readTree(jsonString);
}

From source file:de.betterform.xml.config.DefaultConfig.java

/**
 * Returns the specified configuration section in a hash map.
 * //from  w  ww  . j  av  a 2s .c om
 * @param configContext
 *            the context holding the configuration.
 * @param sectionPath
 *            the context relative path to the section.
 * @param nameAttribute
 *            the name of the attribute to be used as a hash map key.
 * @param valueAttribute
 *            the name of the attribute to be used as a hash map value.
 * @return the specified configuration section in a hash map.
 * @throws Exception
 *             if any error occured during configuration loading.
 */
private HashMap load(NodeInfo configContext, String sectionPath, String nameAttribute, String valueAttribute)
        throws Exception {
    HashMap map = new HashMap();
    List nodeset = XPathCache.getInstance().evaluate(configContext, sectionPath, Collections.EMPTY_MAP, null);

    for (int i = 0; i < nodeset.size(); ++i) {
        Element element = (Element) XPathUtil.getAsNode(nodeset, i + 1);

        String attributeValue = StrSubstitutor.replaceSystemProperties(element.getAttribute(valueAttribute));

        map.put(element.getAttribute(nameAttribute), attributeValue);
    }

    return map;
}

From source file:com.jkoolcloud.jesl.simulator.TNT4JSimulatorParserHandler.java

/**
 * Resolve variable given name to a global variable.
 * Global variables are referenced using: ${var} convention.
 *
 * @param text object to use//w ww  .  j  a v a  2 s .c om
 * @return resolved variable or itself if not a variable
 */
public String expandEnvVars(String text) {
    return StrSubstitutor.replaceSystemProperties(sub.replace(text));
}

From source file:org.apache.tomee.embedded.TomEEEmbeddedApplicationRunner.java

public synchronized void start(final Class<?> marker, final Properties config, final String... args)
        throws Exception {
    if (started) {
        return;/*from   w  w w.j a  va  2s  . com*/
    }

    ensureAppInit(marker);
    started = true;

    final Class<?> appClass = app.getClass();
    final AnnotationFinder finder = new AnnotationFinder(new ClassesArchive(ancestors(appClass)));

    // setup the container config reading class annotation, using a randome http port and deploying the classpath
    final Configuration configuration = new Configuration();
    final ContainerProperties props = appClass.getAnnotation(ContainerProperties.class);
    if (props != null) {
        final Properties runnerProperties = new Properties();
        for (final ContainerProperties.Property p : props.value()) {
            final String name = p.name();
            if (name.startsWith("tomee.embedded.application.runner.")) { // allow to tune the Configuration
                // no need to filter there since it is done in loadFromProperties()
                runnerProperties.setProperty(name.substring("tomee.embedded.application.runner.".length()),
                        p.value());
            } else {
                configuration.property(name, StrSubstitutor.replaceSystemProperties(p.value()));
            }
        }
        if (!runnerProperties.isEmpty()) {
            configuration.loadFromProperties(runnerProperties);
        }
    }
    configuration.loadFromProperties(System.getProperties()); // overrides, note that some config are additive by design

    final List<Method> annotatedMethods = finder
            .findAnnotatedMethods(org.apache.openejb.testing.Configuration.class);
    if (annotatedMethods.size() > 1) {
        throw new IllegalArgumentException("Only one @Configuration is supported: " + annotatedMethods);
    }
    for (final Method m : annotatedMethods) {
        final Object o = m.invoke(app);
        if (Properties.class.isInstance(o)) {
            final Properties properties = Properties.class.cast(o);
            if (configuration.getProperties() == null) {
                configuration.setProperties(new Properties());
            }
            configuration.getProperties().putAll(properties);
        } else {
            throw new IllegalArgumentException("Unsupported " + o + " for @Configuration");
        }
    }

    final Collection<org.apache.tomee.embedded.LifecycleTask> lifecycleTasks = new ArrayList<>();
    final Collection<Closeable> postTasks = new ArrayList<>();
    final LifecycleTasks tasks = appClass.getAnnotation(LifecycleTasks.class);
    if (tasks != null) {
        for (final Class<? extends org.apache.tomee.embedded.LifecycleTask> type : tasks.value()) {
            final org.apache.tomee.embedded.LifecycleTask lifecycleTask = type.newInstance();
            lifecycleTasks.add(lifecycleTask);
            postTasks.add(lifecycleTask.beforeContainerStartup());
        }
    }

    final Map<String, Field> ports = new HashMap<>();
    {
        Class<?> type = appClass;
        while (type != null && type != Object.class) {
            for (final Field f : type.getDeclaredFields()) {
                final RandomPort annotation = f.getAnnotation(RandomPort.class);
                final String value = annotation == null ? null : annotation.value();
                if (value != null && value.startsWith("http")) {
                    f.setAccessible(true);
                    ports.put(value, f);
                }
            }
            type = type.getSuperclass();
        }
    }

    if (ports.containsKey("http")) {
        configuration.randomHttpPort();
    }

    // at least after LifecycleTasks to inherit from potential states (system properties to get a port etc...)
    final Configurers configurers = appClass.getAnnotation(Configurers.class);
    if (configurers != null) {
        for (final Class<? extends Configurer> type : configurers.value()) {
            type.newInstance().configure(configuration);
        }
    }

    final Classes classes = appClass.getAnnotation(Classes.class);
    String context = classes != null ? classes.context() : "";
    context = !context.isEmpty() && context.startsWith("/") ? context.substring(1) : context;

    Archive archive = null;
    if (classes != null && classes.value().length > 0) {
        archive = new ClassesArchive(classes.value());
    }

    final Jars jars = appClass.getAnnotation(Jars.class);
    final List<URL> urls;
    if (jars != null) {
        final Collection<File> files = ApplicationComposers.findFiles(jars);
        urls = new ArrayList<>(files.size());
        for (final File f : files) {
            urls.add(f.toURI().toURL());
        }
    } else {
        urls = null;
    }

    final WebResource resources = appClass.getAnnotation(WebResource.class);
    if (resources != null && resources.value().length > 1) {
        throw new IllegalArgumentException("Only one docBase is supported for now using @WebResource");
    }

    String webResource = null;
    if (resources != null && resources.value().length > 0) {
        webResource = resources.value()[0];
    } else {
        final File webapp = new File("src/main/webapp");
        if (webapp.isDirectory()) {
            webResource = "src/main/webapp";
        }
    }

    if (config != null) { // override other config from annotations
        configuration.loadFromProperties(config);
    }

    final Container container = new Container(configuration);
    SystemInstance.get().setComponent(TomEEEmbeddedArgs.class, new TomEEEmbeddedArgs(args, null));
    SystemInstance.get().setComponent(LifecycleTaskAccessor.class, new LifecycleTaskAccessor(lifecycleTasks));
    container.deploy(new Container.DeploymentRequest(context,
            // call ClasspathSearcher that lazily since container needs to be started to not preload logging
            urls == null
                    ? new DeploymentsResolver.ClasspathSearcher()
                            .loadUrls(Thread.currentThread().getContextClassLoader()).getUrls()
                    : urls,
            webResource != null ? new File(webResource) : null, true, null, archive));

    for (final Map.Entry<String, Field> f : ports.entrySet()) {
        switch (f.getKey()) {
        case "http":
            setPortField(f.getKey(), f.getValue(), configuration, context, app);
            break;
        case "https":
            break;
        default:
            throw new IllegalArgumentException("port " + f.getKey() + " not yet supported");
        }
    }

    SystemInstance.get().addObserver(app);
    composerInject(app);

    final AnnotationFinder appFinder = new AnnotationFinder(new ClassesArchive(appClass));
    for (final Method mtd : appFinder.findAnnotatedMethods(PostConstruct.class)) {
        if (mtd.getParameterTypes().length == 0) {
            if (!mtd.isAccessible()) {
                mtd.setAccessible(true);
            }
            mtd.invoke(app);
        }
    }

    hook = new Thread() {
        @Override
        public void run() { // ensure to log errors but not fail there
            for (final Method mtd : appFinder.findAnnotatedMethods(PreDestroy.class)) {
                if (mtd.getParameterTypes().length == 0) {
                    if (!mtd.isAccessible()) {
                        mtd.setAccessible(true);
                    }
                    try {
                        mtd.invoke(app);
                    } catch (final IllegalAccessException e) {
                        throw new IllegalStateException(e);
                    } catch (final InvocationTargetException e) {
                        throw new IllegalStateException(e.getCause());
                    }
                }
            }

            try {
                container.close();
            } catch (final Exception e) {
                e.printStackTrace();
            }
            for (final Closeable c : postTasks) {
                try {
                    c.close();
                } catch (final IOException e) {
                    e.printStackTrace();
                }
            }
            postTasks.clear();
            app = null;
            try {
                SHUTDOWN_TASKS.remove(this);
            } catch (final Exception e) {
                // no-op: that's ok at that moment if not called manually
            }
        }
    };
    SHUTDOWN_TASKS.put(hook, hook);
}

From source file:org.codice.ddf.condpermadmin.PermissionActivator.java

private String replaceSystemProperties(String string) {
    return StrSubstitutor.replaceSystemProperties(string);
}

From source file:org.wso2.appserver.ApplicationServerConfigurationTest.java

private static AppServerClassLoading prepareClassLoaderEnv() {
    AppServerClassLoading classloadingEnvironments = new AppServerClassLoading();

    AppServerClassLoading.Environment cxf = new AppServerClassLoading.Environment();
    cxf.setName(TestConstants.CXF_ENV_NAME);
    cxf.setClasspath(TestConstants.CXF_ENV_CLASSPATH);
    AppServerClassLoading.Environment jaxrs = new AppServerClassLoading.Environment();
    jaxrs.setName(TestConstants.JAXRS_ENV_NAME);
    jaxrs.setClasspath(TestConstants.JAXRS_ENV_CLASSPATH);

    List<AppServerClassLoading.Environment> envList = new ArrayList<>();
    envList.add(cxf);//  w ww  .  ja v  a 2 s  .  co  m
    envList.add(jaxrs);

    envList.forEach(environment -> environment.setClasspath(string_sub.replace(environment.getClasspath())));
    envList.forEach(environment -> environment
            .setClasspath(StrSubstitutor.replaceSystemProperties(environment.getClasspath())));

    AppServerClassLoading.Environments environments = new AppServerClassLoading.Environments();
    environments.setEnvironments(envList);
    classloadingEnvironments.setEnvironments(environments);

    return classloadingEnvironments;
}

From source file:org.wso2.appserver.ApplicationServerConfigurationTest.java

private static AppServerSecurity prepareSecurityConfigs() {
    AppServerSecurity configuration = new AppServerSecurity();

    AppServerSecurity.Keystore keystore = new AppServerSecurity.Keystore();
    keystore.setLocation(TestConstants.KEYSTORE_PATH);
    keystore.setPassword(TestConstants.KEYSTORE_PASSWORD);
    keystore.setType(TestConstants.TYPE);
    keystore.setKeyAlias(TestConstants.PRIVATE_KEY_ALIAS);
    keystore.setKeyPassword(TestConstants.PRIVATE_KEY_PASSWORD);

    AppServerSecurity.Truststore truststore = new AppServerSecurity.Truststore();
    truststore.setLocation(TestConstants.TRUSTSTORE_PATH);
    truststore.setType(TestConstants.TYPE);
    truststore.setPassword(TestConstants.TRUSTSTORE_PASSWORD);

    configuration.setKeystore(keystore);
    configuration.setTruststore(truststore);

    configuration.getKeystore().setLocation(string_sub.replace(configuration.getKeystore().getLocation()));
    configuration.getTruststore().setLocation(string_sub.replace(configuration.getTruststore().getLocation()));
    configuration.getKeystore()/*  w  ww .j av  a2 s .  c  om*/
            .setLocation(StrSubstitutor.replaceSystemProperties(configuration.getKeystore().getLocation()));
    configuration.getTruststore()
            .setLocation(StrSubstitutor.replaceSystemProperties(configuration.getTruststore().getLocation()));

    return configuration;
}

From source file:org.wso2.appserver.AppServerConfigurationTest.java

private static ClassLoaderEnvironments prepareClassLoaderEnv() {
    ClassLoaderEnvironments classloadingEnvironments = new ClassLoaderEnvironments();

    ClassLoaderEnvironments.Environment cxf = new ClassLoaderEnvironments.Environment();
    cxf.setName(TestConstants.CXF_ENV_NAME);
    cxf.setClasspath(TestConstants.CXF_ENV_CLASSPATH);
    ClassLoaderEnvironments.Environment jaxrs = new ClassLoaderEnvironments.Environment();
    jaxrs.setName(TestConstants.JAXRS_ENV_NAME);
    jaxrs.setClasspath(TestConstants.JAXRS_ENV_CLASSPATH);

    List<ClassLoaderEnvironments.Environment> envList = new ArrayList<>();
    envList.add(cxf);/*w  w  w .  ja v  a 2s  .  c o m*/
    envList.add(jaxrs);

    envList.forEach(environment -> environment.setClasspath(STRING_SUB.replace(environment.getClasspath())));
    envList.forEach(environment -> environment
            .setClasspath(StrSubstitutor.replaceSystemProperties(environment.getClasspath())));

    ClassLoaderEnvironments.Environments environments = new ClassLoaderEnvironments.Environments();
    environments.setEnvironments(envList);
    classloadingEnvironments.setEnvironments(environments);

    return classloadingEnvironments;
}

From source file:org.wso2.appserver.AppServerConfigurationTest.java

private static SecurityConfiguration prepareSecurityConfigs() {
    SecurityConfiguration configuration = new SecurityConfiguration();

    SecurityConfiguration.Keystore keystore = new SecurityConfiguration.Keystore();
    keystore.setLocation(TestConstants.KEYSTORE_PATH);
    keystore.setPassword(TestConstants.KEYSTORE_PASSWORD);
    keystore.setType(TestConstants.TYPE);
    keystore.setKeyAlias(TestConstants.PRIVATE_KEY_ALIAS);
    keystore.setKeyPassword(TestConstants.PRIVATE_KEY_PASSWORD);

    SecurityConfiguration.Truststore truststore = new SecurityConfiguration.Truststore();
    truststore.setLocation(TestConstants.TRUSTSTORE_PATH);
    truststore.setType(TestConstants.TYPE);
    truststore.setPassword(TestConstants.TRUSTSTORE_PASSWORD);

    configuration.setKeystore(keystore);
    configuration.setTruststore(truststore);

    configuration.getKeystore().setLocation(STRING_SUB.replace(configuration.getKeystore().getLocation()));
    configuration.getTruststore().setLocation(STRING_SUB.replace(configuration.getTruststore().getLocation()));
    configuration.getKeystore()//ww w  . ja v a  2s.com
            .setLocation(StrSubstitutor.replaceSystemProperties(configuration.getKeystore().getLocation()));
    configuration.getTruststore()
            .setLocation(StrSubstitutor.replaceSystemProperties(configuration.getTruststore().getLocation()));

    return configuration;
}