Search in sources :

Example 21 with StrSubstitutor

use of org.apache.commons.lang3.text.StrSubstitutor in project jirm by agentgt.

the class SqlWriterStrategy method replaceProperties.

public String replaceProperties(final SqlObjectDefinition<?> definition, final String sql) {
    StrLookup<String> lookup = new StrLookup<String>() {

        @Override
        public String lookup(String key) {
            Optional<String> sqlName = definition.parameterNameToSql(key);
            check.state(sqlName.isPresent(), "Property: {} not found in object.", key);
            return sqlName.get();
        }
    };
    StrSubstitutor s = new StrSubstitutor(lookup, "{{", "}}", '$');
    String result = s.replace(sql);
    return result;
}
Also used : StrSubstitutor(org.apache.commons.lang3.text.StrSubstitutor) StrLookup(org.apache.commons.lang3.text.StrLookup)

Example 22 with StrSubstitutor

use of org.apache.commons.lang3.text.StrSubstitutor in project stanbol by apache.

the class KiWiRepositoryService method activate.

@Activate
protected final void activate(ComponentContext context) throws ConfigurationException, RepositoryException {
    log.info("activate KiWi repository ...");
    if (context == null || context.getProperties() == null) {
        throw new IllegalStateException("No valid" + ComponentContext.class + " parsed in activate!");
    }
    // copy the read-only configuration as we might need to change it before
    // adding it to the registered service
    final Dictionary<String, Object> config = copyConfig(context);
    final BundleContext bc = context.getBundleContext();
    // we want to substitute variables used in the dbURL with configuration,
    // framework and system properties
    StrSubstitutor strSubstitutor = new StrSubstitutor(new StrLookup<Object>() {

        @Override
        public String lookup(String key) {
            Object val = config.get(key);
            if (val == null) {
                val = bc.getProperty(key);
            }
            return val.toString();
        }
    });
    String name = (String) config.get(REPOSITORY_ID);
    if (StringUtils.isBlank(name)) {
        throw new ConfigurationException(REPOSITORY_ID, "The parsed Repository ID MUST NOT be NULL nor blank!");
    } else {
        log.debug(" - name: {}", name);
    }
    KiWiDialect dialect;
    String db_type;
    if (StringUtils.equalsIgnoreCase("postgres", (String) config.get(DB_DIALECT))) {
        dialect = new PostgreSQLDialect();
        db_type = "postgresql";
    } else if (StringUtils.equalsIgnoreCase("mysql", (String) config.get(DB_DIALECT))) {
        dialect = new MySQLDialect();
        db_type = "mysql";
    } else if (StringUtils.equalsIgnoreCase("h2", (String) config.get(DB_DIALECT))) {
        dialect = new H2Dialect();
        db_type = "h2";
    } else {
        throw new ConfigurationException(DB_DIALECT, "No valid database dialect was given");
    }
    log.debug(" - dialect: {}", dialect);
    String db_url = (String) config.get(DB_URL);
    if (StringUtils.isBlank(db_url)) {
        // build the db url from parameters
        String db_host = (String) config.get(DB_HOST);
        if (StringUtils.isBlank(db_host)) {
            db_host = DEFAULT_DB_HOST;
        }
        log.debug(" - db host: {}", db_host);
        String db_name = (String) config.get(DB_NAME);
        if (StringUtils.isBlank(db_name)) {
            db_name = DEFAULT_DB_NAME;
        }
        log.debug(" - db name:  {}", name);
        int db_port;
        Object value = config.get(DB_PORT);
        if (value instanceof Number) {
            db_port = ((Number) value).intValue();
        } else if (value != null && !StringUtils.isBlank(value.toString())) {
            db_port = Integer.parseInt(value.toString());
        } else {
            db_port = DEFAULT_DB_PORT;
        }
        log.debug(" - db port: {}", db_port);
        String db_opts = (String) config.get(DB_OPTS);
        log.debug(" - db options: {}", db_opts);
        StringBuilder dbUrlBuilder = new StringBuilder("jdbc:").append(db_type);
        if (dialect instanceof H2Dialect) {
            // H2 uses a file path and not a host so we do not need the ://
            dbUrlBuilder.append(':').append(db_host);
        } else {
            dbUrlBuilder.append("://").append(db_host);
        }
        if (db_port > 0) {
            dbUrlBuilder.append(':').append(db_port);
        }
        if (!StringUtils.isBlank(db_name)) {
            dbUrlBuilder.append('/').append(db_name);
        }
        if (!StringUtils.isBlank(db_opts)) {
            dbUrlBuilder.append(db_opts);
        }
        dbUrl = strSubstitutor.replace(dbUrlBuilder);
    } else if (!db_url.startsWith("jdbc:")) {
        throw new ConfigurationException(DB_URL, "Database URLs are expected to start with " + "'jdbc:' (parsed: '" + db_url + "')!");
    } else {
        dbUrl = strSubstitutor.replace(db_url);
    }
    String db_user = (String) config.get(DB_USER);
    if (StringUtils.isBlank(db_user)) {
        db_user = DEFAULT_DB_USER;
    } else {
        db_user = strSubstitutor.replace(db_user);
    }
    log.debug(" - db user: {}", db_user);
    String db_pass = (String) config.get(DB_PASS);
    if (StringUtils.isBlank(db_pass)) {
        log.debug(" - db pwd is set to default");
        db_pass = DEFAULT_DB_PASS;
    } else {
        log.debug(" - db pwd is set to parsed value");
    }
    KiWiConfiguration configuration = new KiWiConfiguration("Marmotta KiWi", dbUrl, db_user, db_pass, dialect);
    // parse cluster options
    String cluster = (String) config.get(CLUSTER);
    if (!StringUtils.isBlank(cluster)) {
        log.debug(" - cluster: {}", cluster);
        configuration.setClustered(true);
        configuration.setClusterName(cluster);
        String clusterAddress = (String) config.get(CLUSTER_ADDRESS);
        if (!StringUtils.isBlank(clusterAddress)) {
            configuration.setClusterAddress(strSubstitutor.replace(clusterAddress));
        }
        log.debug(" - cluster address: {}", configuration.getClusterAddress());
        Object clusterPort = config.get(CLUSTER_PORT);
        if (clusterPort instanceof Number) {
            int port = ((Number) clusterPort).intValue();
            if (port > 0) {
                configuration.setClusterPort(port);
            }
        // else use default
        } else if (clusterPort != null) {
            try {
                int port = Integer.parseInt(strSubstitutor.replace(clusterPort));
                if (port > 0) {
                    configuration.setClusterPort(port);
                }
            } catch (NumberFormatException e) {
                throw new ConfigurationException(CLUSTER_PORT, "Unable to parse " + "Cluster Port from configured value '" + clusterPort + "'!", e);
            }
        }
        log.debug(" - cluster port ({})", configuration.getClusterPort());
        String cachingBackend = (String) config.get(CACHING_BACKEND);
        if (StringUtils.isBlank(cachingBackend)) {
            configuration.setCachingBackend(CachingBackends.valueOf(DEFAULT_CACHING_BACKEND));
        } else {
            try {
                configuration.setCachingBackend(CachingBackends.valueOf(strSubstitutor.replace(cachingBackend)));
            } catch (IllegalArgumentException e) {
                throw new ConfigurationException(CACHING_BACKEND, "Unsupported CachingBackend '" + cachingBackend + "' (supported: " + Arrays.toString(CachingBackends.values()) + ")!", e);
            }
        }
        log.debug(" - caching Backend: {}", configuration.getCachingBackend());
        String cacheMode = (String) config.get(CACHE_MODE);
        if (StringUtils.isBlank(cacheMode)) {
            cacheMode = DEFAULT_CACHE_MODE;
        }
        try {
            configuration.setCacheMode(CacheMode.valueOf(strSubstitutor.replace(cacheMode)));
        } catch (IllegalArgumentException e) {
            throw new ConfigurationException(CACHE_MODE, "Unsupported CacheMode '" + cacheMode + "' (supported: " + Arrays.toString(CacheMode.values()) + ")!");
        }
        log.debug(" - cache mode: {}", configuration.getCacheMode());
    } else {
        // not clustered
        log.debug(" - no cluster configured");
        configuration.setClustered(false);
    }
    log.info(" ... initialise KiWi repository: {}", dbUrl);
    KiWiStore store = new KiWiStore(configuration);
    repository = new SailRepository(new KiWiSparqlSail(store));
    repository.initialize();
    // set the repository type property to KiWiStore
    config.put(SAIL_IMPL, KiWiStore.class.getName());
    repoRegistration = context.getBundleContext().registerService(Repository.class.getName(), repository, config);
    log.info("  - successfully registered KiWi Repository {}", name);
}
Also used : KiWiSparqlSail(org.apache.marmotta.kiwi.sparql.sail.KiWiSparqlSail) H2Dialect(org.apache.marmotta.kiwi.persistence.h2.H2Dialect) SailRepository(org.openrdf.repository.sail.SailRepository) KiWiStore(org.apache.marmotta.kiwi.sail.KiWiStore) KiWiDialect(org.apache.marmotta.kiwi.persistence.KiWiDialect) StrSubstitutor(org.apache.commons.lang3.text.StrSubstitutor) MySQLDialect(org.apache.marmotta.kiwi.persistence.mysql.MySQLDialect) PostgreSQLDialect(org.apache.marmotta.kiwi.persistence.pgsql.PostgreSQLDialect) ConfigurationException(org.osgi.service.cm.ConfigurationException) KiWiConfiguration(org.apache.marmotta.kiwi.config.KiWiConfiguration) BundleContext(org.osgi.framework.BundleContext) Activate(org.apache.felix.scr.annotations.Activate)

Example 23 with StrSubstitutor

use of org.apache.commons.lang3.text.StrSubstitutor in project spring-cloud-connectors by spring-cloud.

the class PropertiesFileResolver method findCloudPropertiesFileFromClasspath.

/**
 * Looks for a resource named {@code filename} (usually {@value #BOOTSTRAP_PROPERTIES_FILENAME}) on the classpath. If present,
 * it is loaded and
 * inspected for a property named {@value LocalConfigConnector#PROPERTIES_FILE_PROPERTY}, which is interpolated from the system
 * properties and returned.
 *
 * @return the filename derived from the classpath control file, or {@code null} if one couldn't be found
 */
File findCloudPropertiesFileFromClasspath() {
    // see if we have a spring-cloud.properties at all
    InputStream in = getClass().getClassLoader().getResourceAsStream(classpathPropertiesFilename);
    if (in == null) {
        logger.info("no " + classpathPropertiesFilename + " found on the classpath to direct us to an external properties file");
        return null;
    }
    // load it as a properties file
    Properties properties = new Properties();
    try {
        properties.load(in);
    } catch (IOException e) {
        logger.log(Level.SEVERE, "found " + classpathPropertiesFilename + " on the classpath but couldn't load it as a properties file", e);
        return null;
    }
    // read the spring.cloud.propertiesFile property from it
    String template = properties.getProperty(LocalConfigConnector.PROPERTIES_FILE_PROPERTY);
    if (template == null) {
        logger.log(Level.SEVERE, "found properties file " + classpathPropertiesFilename + " on the classpath, but it didn't contain a property named " + LocalConfigConnector.PROPERTIES_FILE_PROPERTY);
        return null;
    }
    // if there's anything else, the client probably tried to put an app ID or other credentials there
    if (properties.entrySet().size() > 1)
        logger.warning("the properties file " + classpathPropertiesFilename + " contained properties besides " + LocalConfigConnector.PROPERTIES_FILE_PROPERTY + "; ignoring");
    logger.fine("substituting system properties into '" + template + "'");
    File configFile = new File(new StrSubstitutor(systemPropertiesLookup(env)).replace(template));
    logger.info("derived configuration file name: " + configFile);
    return configFile;
}
Also used : StrSubstitutor(org.apache.commons.lang3.text.StrSubstitutor) InputStream(java.io.InputStream) IOException(java.io.IOException) Properties(java.util.Properties) File(java.io.File)

Example 24 with StrSubstitutor

use of org.apache.commons.lang3.text.StrSubstitutor 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 : PropertyUtils(org.apache.commons.beanutils.PropertyUtils) 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) StringUtils(org.apache.commons.lang3.StringUtils) StrLookup(org.apache.commons.lang.text.StrLookup) Lists(com.google.common.collect.Lists) ConfigurationPropertyReplacer(com.thinkbiganalytics.nifi.feedmgr.ConfigurationPropertyReplacer) Map(java.util.Map) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) Logger(org.slf4j.Logger) MetadataFieldAnnotationFieldNameResolver(com.thinkbiganalytics.feedmgr.MetadataFieldAnnotationFieldNameResolver) MetadataFields(com.thinkbiganalytics.feedmgr.MetadataFields) NifiProperty(com.thinkbiganalytics.nifi.rest.model.NifiProperty) Collection(java.util.Collection) MetadataField(com.thinkbiganalytics.metadata.MetadataField) Collectors(java.util.stream.Collectors) AnnotatedFieldProperty(com.thinkbiganalytics.annotations.AnnotatedFieldProperty) List(java.util.List) StrSubstitutor(org.apache.commons.lang.text.StrSubstitutor) Optional(java.util.Optional) Collections(java.util.Collections) 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 25 with StrSubstitutor

use of org.apache.commons.lang3.text.StrSubstitutor in project solr-cmd-utils by tblsoft.

the class SolrCompareFilter method query.

SearchResult query(Document document, SolrUrl solrUrl, SolrClient solrClient) {
    SolrQuery solrQuery = new SolrQuery();
    Map<String, String> valueMap = new HashMap<String, String>();
    String queryString = document.getFieldValue("query");
    valueMap.put("query", queryString);
    StrSubstitutor strSubstitutor = new StrSubstitutor(valueMap);
    for (Map<String, String> map : solrUrl.getQuery()) {
        for (String key : map.keySet()) {
            String value = map.get(key);
            value = strSubstitutor.replace(value);
            solrQuery.add(key, value);
        }
    }
    SearchResult searchResult = new SearchResult();
    try {
        QueryResponse response = solrClient.query(solrQuery);
        searchResult.setResponseTime(response.getQTime());
        searchResult.setNumFound(response.getResults().getNumFound());
        searchResult.setQuery(queryString);
        Iterator<SolrDocument> resultIterator = response.getResults().iterator();
        int position = 0;
        while (resultIterator.hasNext()) {
            SolrDocument solrDocument = resultIterator.next();
            String id = (String) solrDocument.getFieldValue("id");
            Result result = new Result(id, position++);
            searchResult.getResultList().add(result);
        }
    } catch (Exception e) {
        if (failOnError) {
            throw new RuntimeException(e);
        } else {
            searchResult.setValid(false);
            searchResult.setErrorMessage(e.getMessage());
        }
    }
    return searchResult;
}
Also used : HashMap(java.util.HashMap) SolrQuery(org.apache.solr.client.solrj.SolrQuery) StrSubstitutor(org.apache.commons.lang3.text.StrSubstitutor) SolrDocument(org.apache.solr.common.SolrDocument) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse)

Aggregations

StrSubstitutor (org.apache.commons.lang3.text.StrSubstitutor)32 HashMap (java.util.HashMap)16 File (java.io.File)5 Map (java.util.Map)5 IOException (java.io.IOException)4 StrLookup (org.apache.commons.lang3.text.StrLookup)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 UnrecognizedPropertyException (com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException)2 MetricConfigDTO (com.linkedin.thirdeye.datalayer.dto.MetricConfigDTO)2 Field (de.tblsoft.solr.pipeline.bean.Field)2 FileWriter (java.io.FileWriter)2 SQLException (java.sql.SQLException)2 List (java.util.List)2 Properties (java.util.Properties)2 StringUtils (org.apache.commons.lang3.StringUtils)2 Test (org.junit.Test)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 DomainVO (com.cloud.domain.DomainVO)1 AccountVO (com.cloud.user.AccountVO)1 UserVO (com.cloud.user.UserVO)1