Search in sources :

Example 1 with StrLookup

use of org.apache.commons.lang.text.StrLookup in project cuba by cuba-platform.

the class TestContainer method initAppProperties.

protected void initAppProperties() {
    final Properties properties = new Properties();
    List<String> locations = getAppPropertiesFiles();
    DefaultResourceLoader resourceLoader = new DefaultResourceLoader();
    for (String location : locations) {
        Resource resource = resourceLoader.getResource(location);
        if (resource.exists()) {
            InputStream stream = null;
            try {
                stream = resource.getInputStream();
                properties.load(stream);
            } catch (IOException e) {
                throw new RuntimeException(e);
            } finally {
                IOUtils.closeQuietly(stream);
            }
        } else {
            log.warn("Resource " + location + " not found, ignore it");
        }
    }
    StrSubstitutor substitutor = new StrSubstitutor(new StrLookup() {

        @Override
        public String lookup(String key) {
            String subst = properties.getProperty(key);
            return subst != null ? subst : System.getProperty(key);
        }
    });
    for (Object key : properties.keySet()) {
        String value = substitutor.replace(properties.getProperty((String) key));
        appProperties.put((String) key, value);
    }
    File dir;
    dir = new File(appProperties.get("cuba.confDir"));
    dir.mkdirs();
    dir = new File(appProperties.get("cuba.logDir"));
    dir.mkdirs();
    dir = new File(appProperties.get("cuba.tempDir"));
    dir.mkdirs();
    dir = new File(appProperties.get("cuba.dataDir"));
    dir.mkdirs();
}
Also used : InputStream(java.io.InputStream) Resource(org.springframework.core.io.Resource) ExternalResource(org.junit.rules.ExternalResource) IOException(java.io.IOException) StrSubstitutor(org.apache.commons.lang.text.StrSubstitutor) StrLookup(org.apache.commons.lang.text.StrLookup) File(java.io.File) DefaultResourceLoader(org.springframework.core.io.DefaultResourceLoader)

Example 2 with StrLookup

use of org.apache.commons.lang.text.StrLookup in project kylo by Teradata.

the class PropertyExpressionResolver method resolveVariables.

/**
 * Resolves the variables in the value of the specified property.
 *
 * @param property the property
 * @param metadata the feed
 * @return the result of the transformation
 */
private ResolveResult resolveVariables(@Nonnull final NifiProperty property, @Nonnull final FeedMetadata metadata) {
    // Filter blank values
    final String value = property.getValue();
    if (StringUtils.isBlank(value)) {
        return new ResolveResult(false, false);
    }
    final boolean[] hasConfig = { false };
    final boolean[] isModified = { false };
    StrLookup resolver = new StrLookup() {

        @Override
        public String lookup(String variable) {
            // Resolve configuration variables
            final String configValue = getConfigurationPropertyValue(property, variable);
            if (configValue != null && property.getValue() != null && !property.getValue().equalsIgnoreCase(configValue)) {
                hasConfig[0] = true;
                isModified[0] = true;
                // if this is the first time we found the config var, set the template value correctly
                if (!property.isContainsConfigurationVariables()) {
                    property.setTemplateValue(property.getValue());
                    property.setContainsConfigurationVariables(true);
                }
                return configValue;
            }
            // Resolve metadata variables
            try {
                final String metadataValue = getMetadataPropertyValue(metadata, variable);
                if (metadataValue != null) {
                    isModified[0] = true;
                    return metadataValue;
                }
            } catch (Exception e) {
                log.error("Unable to resolve variable: " + variable, e);
            }
            return null;
        }
    };
    StrSubstitutor ss = new StrSubstitutor(resolver);
    ss.setEnableSubstitutionInVariables(true);
    // escape
    String val = StringUtils.trim(ss.replace(value));
    // fix
    property.setValue(val);
    return new ResolveResult(hasConfig[0], isModified[0]);
}
Also used : StrSubstitutor(org.apache.commons.lang.text.StrSubstitutor) StrLookup(org.apache.commons.lang.text.StrLookup) ResolveResult(javax.naming.spi.ResolveResult)

Example 3 with StrLookup

use of org.apache.commons.lang.text.StrLookup in project kylo by Teradata.

the class PropertyExpressionResolver method resolveVariables.

/**
 * Resolve the str with values from the supplied {@code properties} This will recursively fill out the str looking back at the properties to get the correct value. NOTE the property values will be
 * overwritten if replacement is found!
 */
public ResolvedVariables resolveVariables(String str, List<NifiProperty> properties) {
    ResolvedVariables variables = new ResolvedVariables(str);
    StrLookup resolver = new StrLookup() {

        @Override
        public String lookup(String key) {
            Optional<NifiProperty> optional = properties.stream().filter(prop -> key.equals(prop.getKey())).findFirst();
            if (optional.isPresent()) {
                NifiProperty property = optional.get();
                String value = StringUtils.isNotBlank(property.getValue()) ? property.getValue().trim() : "";
                variables.getResolvedVariables().put(property.getKey(), value);
                return value;
            } else {
                return null;
            }
        }
    };
    StrSubstitutor ss = new StrSubstitutor(resolver);
    variables.setResolvedString(ss.replace(str));
    Map<String, String> map = variables.getResolvedVariables();
    Map<String, String> vars = map.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, entry -> ss.replace(entry.getValue())));
    variables.getResolvedVariables().putAll(vars);
    return variables;
}
Also used : Logger(org.slf4j.Logger) MetadataFieldAnnotationFieldNameResolver(com.thinkbiganalytics.feedmgr.MetadataFieldAnnotationFieldNameResolver) MetadataFields(com.thinkbiganalytics.feedmgr.MetadataFields) NifiProperty(com.thinkbiganalytics.nifi.rest.model.NifiProperty) BeanUtils(org.apache.commons.beanutils.BeanUtils) SpringEnvironmentProperties(com.thinkbiganalytics.spring.SpringEnvironmentProperties) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) FeedMetadata(com.thinkbiganalytics.feedmgr.rest.model.FeedMetadata) MetadataField(com.thinkbiganalytics.metadata.MetadataField) StringUtils(org.apache.commons.lang3.StringUtils) Collectors(java.util.stream.Collectors) StrLookup(org.apache.commons.lang.text.StrLookup) AnnotatedFieldProperty(com.thinkbiganalytics.annotations.AnnotatedFieldProperty) List(java.util.List) ConfigurationPropertyReplacer(com.thinkbiganalytics.nifi.feedmgr.ConfigurationPropertyReplacer) StrSubstitutor(org.apache.commons.lang.text.StrSubstitutor) Map(java.util.Map) Optional(java.util.Optional) ResolveResult(javax.naming.spi.ResolveResult) Collections(java.util.Collections) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) StrSubstitutor(org.apache.commons.lang.text.StrSubstitutor) StrLookup(org.apache.commons.lang.text.StrLookup) NifiProperty(com.thinkbiganalytics.nifi.rest.model.NifiProperty) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with StrLookup

use of org.apache.commons.lang.text.StrLookup in project kylo by Teradata.

the class PropertyExpressionResolver method replaceAll.

public String replaceAll(String str, String replacement) {
    if (str != null) {
        StrLookup lookup = new StrLookup() {

            @Override
            public String lookup(String key) {
                return replacement;
            }
        };
        StrSubstitutor ss = new StrSubstitutor(lookup);
        return ss.replace(str);
    }
    return null;
}
Also used : StrSubstitutor(org.apache.commons.lang.text.StrSubstitutor) StrLookup(org.apache.commons.lang.text.StrLookup)

Aggregations

StrLookup (org.apache.commons.lang.text.StrLookup)4 StrSubstitutor (org.apache.commons.lang.text.StrSubstitutor)4 ResolveResult (javax.naming.spi.ResolveResult)2 AnnotatedFieldProperty (com.thinkbiganalytics.annotations.AnnotatedFieldProperty)1 MetadataFieldAnnotationFieldNameResolver (com.thinkbiganalytics.feedmgr.MetadataFieldAnnotationFieldNameResolver)1 MetadataFields (com.thinkbiganalytics.feedmgr.MetadataFields)1 FeedMetadata (com.thinkbiganalytics.feedmgr.rest.model.FeedMetadata)1 MetadataField (com.thinkbiganalytics.metadata.MetadataField)1 ConfigurationPropertyReplacer (com.thinkbiganalytics.nifi.feedmgr.ConfigurationPropertyReplacer)1 NifiProperty (com.thinkbiganalytics.nifi.rest.model.NifiProperty)1 SpringEnvironmentProperties (com.thinkbiganalytics.spring.SpringEnvironmentProperties)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1