Search in sources :

Example 1 with DataSourceInformation

use of org.apache.synapse.commons.datasource.DataSourceInformation in project wso2-synapse by wso2.

the class DataSourceInformationFactory method createDataSourceInformation.

/**
 * Factory method to create a DataSourceInformation instance based on given properties
 *
 * @param dsName     DataSource Name
 * @param properties Properties to create and configure DataSource
 * @return DataSourceInformation instance
 */
public static DataSourceInformation createDataSourceInformation(String dsName, Properties properties) {
    if (dsName == null || "".equals(dsName)) {
        if (log.isDebugEnabled()) {
            log.debug("DataSource name is either empty or null, ignoring..");
        }
        return null;
    }
    StringBuffer buffer = new StringBuffer();
    buffer.append(DataSourceConstants.PROP_SYNAPSE_PREFIX_DS);
    buffer.append(DataSourceConstants.DOT_STRING);
    buffer.append(dsName);
    buffer.append(DataSourceConstants.DOT_STRING);
    // Prefix for getting particular data source's properties
    String prefix = buffer.toString();
    String driver = MiscellaneousUtil.getProperty(properties, prefix + DataSourceConstants.PROP_DRIVER_CLS_NAME, null);
    if (driver == null) {
        handleException(prefix + DataSourceConstants.PROP_DRIVER_CLS_NAME + " cannot be found.");
    }
    String url = MiscellaneousUtil.getProperty(properties, prefix + DataSourceConstants.PROP_URL, null);
    if (url == null) {
        handleException(prefix + DataSourceConstants.PROP_URL + " cannot be found.");
    }
    DataSourceInformation datasourceInformation = new DataSourceInformation();
    datasourceInformation.setAlias(dsName);
    datasourceInformation.setDriver(driver);
    datasourceInformation.setUrl(url);
    String dataSourceName = MiscellaneousUtil.getProperty(properties, prefix + DataSourceConstants.PROP_DS_NAME, dsName, String.class);
    datasourceInformation.setDatasourceName(dataSourceName);
    String dsType = MiscellaneousUtil.getProperty(properties, prefix + DataSourceConstants.PROP_TYPE, DataSourceConstants.PROP_BASIC_DATA_SOURCE, String.class);
    datasourceInformation.setType(dsType);
    String repositoryType = MiscellaneousUtil.getProperty(properties, prefix + DataSourceConstants.PROP_REGISTRY, DataSourceConstants.PROP_REGISTRY_MEMORY, String.class);
    datasourceInformation.setRepositoryType(repositoryType);
    Integer maxActive = MiscellaneousUtil.getProperty(properties, prefix + DataSourceConstants.PROP_MAX_ACTIVE, GenericObjectPool.DEFAULT_MAX_ACTIVE, Integer.class);
    datasourceInformation.setMaxActive(maxActive);
    Integer maxIdle = MiscellaneousUtil.getProperty(properties, prefix + DataSourceConstants.PROP_MAX_IDLE, GenericObjectPool.DEFAULT_MAX_IDLE, Integer.class);
    datasourceInformation.setMaxIdle(maxIdle);
    Long maxWait = MiscellaneousUtil.getProperty(properties, prefix + DataSourceConstants.PROP_MAX_WAIT, GenericObjectPool.DEFAULT_MAX_WAIT, Long.class);
    datasourceInformation.setMaxWait(maxWait);
    // Construct DriverAdapterCPDS reference
    String suffix = DataSourceConstants.PROP_CPDS_ADAPTER + DataSourceConstants.DOT_STRING + DataSourceConstants.PROP_CLASS_NAME;
    String className = MiscellaneousUtil.getProperty(properties, prefix + suffix, DataSourceConstants.PROP_CPDS_ADAPTER_DRIVER);
    datasourceInformation.addParameter(suffix, className);
    suffix = DataSourceConstants.PROP_CPDS_ADAPTER + DataSourceConstants.DOT_STRING + DataSourceConstants.PROP_FACTORY;
    String factory = MiscellaneousUtil.getProperty(properties, prefix + suffix, DataSourceConstants.PROP_CPDS_ADAPTER_DRIVER);
    datasourceInformation.addParameter(suffix, factory);
    suffix = DataSourceConstants.PROP_CPDS_ADAPTER + DataSourceConstants.DOT_STRING + DataSourceConstants.PROP_NAME;
    String name = MiscellaneousUtil.getProperty(properties, prefix + suffix, "cpds");
    datasourceInformation.addParameter(suffix, name);
    boolean defaultAutoCommit = MiscellaneousUtil.getProperty(properties, prefix + DataSourceConstants.PROP_DEFAULT_AUTO_COMMIT, true, Boolean.class);
    boolean defaultReadOnly = MiscellaneousUtil.getProperty(properties, prefix + DataSourceConstants.PROP_DEFAULT_READ_ONLY, false, Boolean.class);
    boolean testOnBorrow = MiscellaneousUtil.getProperty(properties, prefix + DataSourceConstants.PROP_TEST_ON_BORROW, true, Boolean.class);
    boolean testOnReturn = MiscellaneousUtil.getProperty(properties, prefix + DataSourceConstants.PROP_TEST_ON_RETURN, false, Boolean.class);
    long timeBetweenEvictionRunsMillis = MiscellaneousUtil.getProperty(properties, prefix + DataSourceConstants.PROP_TIME_BETWEEN_EVICTION_RUNS_MILLIS, GenericObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS, Long.class);
    int numTestsPerEvictionRun = MiscellaneousUtil.getProperty(properties, prefix + DataSourceConstants.PROP_NUM_TESTS_PER_EVICTION_RUN, GenericObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN, Integer.class);
    long minEvictableIdleTimeMillis = MiscellaneousUtil.getProperty(properties, prefix + DataSourceConstants.PROP_MIN_EVICTABLE_IDLE_TIME_MILLIS, GenericObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS, Long.class);
    boolean testWhileIdle = MiscellaneousUtil.getProperty(properties, prefix + DataSourceConstants.PROP_TEST_WHILE_IDLE, false, Boolean.class);
    String validationQuery = MiscellaneousUtil.getProperty(properties, prefix + DataSourceConstants.PROP_VALIDATION_QUERY, null);
    int minIdle = MiscellaneousUtil.getProperty(properties, prefix + DataSourceConstants.PROP_MIN_IDLE, GenericObjectPool.DEFAULT_MIN_IDLE, Integer.class);
    int initialSize = MiscellaneousUtil.getProperty(properties, prefix + DataSourceConstants.PROP_INITIAL_SIZE, 0, Integer.class);
    int defaultTransactionIsolation = MiscellaneousUtil.getProperty(properties, prefix + DataSourceConstants.PROP_DEFAULT_TRANSACTION_ISOLATION, -1, Integer.class);
    String defaultCatalog = MiscellaneousUtil.getProperty(properties, prefix + DataSourceConstants.PROP_DEFAULT_CATALOG, null);
    boolean accessToUnderlyingConnectionAllowed = MiscellaneousUtil.getProperty(properties, prefix + DataSourceConstants.PROP_ACCESS_TO_UNDERLYING_CONNECTION_ALLOWED, false, Boolean.class);
    boolean removeAbandoned = MiscellaneousUtil.getProperty(properties, prefix + DataSourceConstants.PROP_REMOVE_ABANDONED, false, Boolean.class);
    int removeAbandonedTimeout = MiscellaneousUtil.getProperty(properties, prefix + DataSourceConstants.PROP_REMOVE_ABANDONED_TIMEOUT, 300, Integer.class);
    boolean logAbandoned = MiscellaneousUtil.getProperty(properties, prefix + DataSourceConstants.PROP_LOG_ABANDONED, false, Boolean.class);
    boolean poolPreparedStatements = MiscellaneousUtil.getProperty(properties, prefix + DataSourceConstants.PROP_POOL_PREPARED_STATEMENTS, false, Boolean.class);
    int maxOpenPreparedStatements = MiscellaneousUtil.getProperty(properties, prefix + DataSourceConstants.PROP_MAX_OPEN_PREPARED_STATEMENTS, GenericKeyedObjectPool.DEFAULT_MAX_TOTAL, Integer.class);
    datasourceInformation.setDefaultAutoCommit(defaultAutoCommit);
    datasourceInformation.setDefaultReadOnly(defaultReadOnly);
    datasourceInformation.setTestOnBorrow(testOnBorrow);
    datasourceInformation.setTestOnReturn(testOnReturn);
    datasourceInformation.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
    datasourceInformation.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
    datasourceInformation.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
    datasourceInformation.setTestWhileIdle(testWhileIdle);
    datasourceInformation.setMinIdle(minIdle);
    datasourceInformation.setDefaultTransactionIsolation(defaultTransactionIsolation);
    datasourceInformation.setAccessToUnderlyingConnectionAllowed(accessToUnderlyingConnectionAllowed);
    datasourceInformation.setRemoveAbandoned(removeAbandoned);
    datasourceInformation.setRemoveAbandonedTimeout(removeAbandonedTimeout);
    datasourceInformation.setLogAbandoned(logAbandoned);
    datasourceInformation.setPoolPreparedStatements(poolPreparedStatements);
    datasourceInformation.setMaxOpenPreparedStatements(maxOpenPreparedStatements);
    datasourceInformation.setInitialSize(initialSize);
    if (validationQuery != null && !"".equals(validationQuery)) {
        datasourceInformation.setValidationQuery(validationQuery);
    }
    if (defaultCatalog != null && !"".equals(defaultCatalog)) {
        datasourceInformation.setDefaultCatalog(defaultCatalog);
    }
    datasourceInformation.addProperty(prefix + DataSourceConstants.PROP_IC_FACTORY, MiscellaneousUtil.getProperty(properties, prefix + DataSourceConstants.PROP_IC_FACTORY, null));
    // Provider URL
    datasourceInformation.addProperty(prefix + DataSourceConstants.PROP_PROVIDER_URL, MiscellaneousUtil.getProperty(properties, prefix + DataSourceConstants.PROP_PROVIDER_URL, null));
    datasourceInformation.addProperty(prefix + DataSourceConstants.PROP_PROVIDER_PORT, MiscellaneousUtil.getProperty(properties, prefix + DataSourceConstants.PROP_PROVIDER_PORT, null));
    String passwordPrompt = MiscellaneousUtil.getProperty(properties, prefix + SecurityConstants.PROP_PASSWORD_PROMPT, "Password for datasource " + dsName, String.class);
    SecretInformation secretInformation = SecretInformationFactory.createSecretInformation(properties, prefix, passwordPrompt);
    secretInformation.setToken(dsName + "." + SecurityConstants.PROP_PASSWORD);
    datasourceInformation.setSecretInformation(secretInformation);
    return datasourceInformation;
}
Also used : SecretInformation(org.wso2.securevault.secret.SecretInformation) DataSourceInformation(org.apache.synapse.commons.datasource.DataSourceInformation)

Example 2 with DataSourceInformation

use of org.apache.synapse.commons.datasource.DataSourceInformation in project wso2-synapse by wso2.

the class JDBCConfiguration method readCustomDataSourceConfig.

/**
 * Configure for custom datasource
 *
 * @param parameters - parameters given in configuration
 */
private void readCustomDataSourceConfig(Map<String, Object> parameters) {
    DataSourceInformation dataSourceInformation = new DataSourceInformation();
    dataSourceInformation.setDriver((String) parameters.get(JDBCMessageStoreConstants.JDBC_CONNECTION_DRIVER));
    dataSourceInformation.setUrl((String) parameters.get(JDBCMessageStoreConstants.JDBC_CONNECTION_URL));
    SecretInformation secretInformation = new SecretInformation();
    secretInformation.setUser((String) parameters.get(JDBCMessageStoreConstants.JDBC_USERNAME));
    secretInformation.setAliasSecret((String) parameters.get(JDBCMessageStoreConstants.JDBC_PASSWORD));
    dataSourceInformation.setSecretInformation(secretInformation);
    this.setDataSourceInformation(dataSourceInformation);
}
Also used : SecretInformation(org.wso2.securevault.secret.SecretInformation) DataSourceInformation(org.apache.synapse.commons.datasource.DataSourceInformation)

Example 3 with DataSourceInformation

use of org.apache.synapse.commons.datasource.DataSourceInformation in project wso2-synapse by wso2.

the class DataSourceFactoryTest method testCreateDatasource.

/**
 * Test create DataSource from DataSourceInformation object
 */
public void testCreateDatasource() {
    DataSourceInformation dataSourceInformation = createDataSourceInformation();
    DataSource dataSource = DataSourceFactory.createDataSource(dataSourceInformation);
    assertNotNull("Datasource not created", dataSource);
}
Also used : DataSourceInformation(org.apache.synapse.commons.datasource.DataSourceInformation) DataSource(javax.sql.DataSource)

Example 4 with DataSourceInformation

use of org.apache.synapse.commons.datasource.DataSourceInformation in project wso2-synapse by wso2.

the class DataSourceFactoryTest method testCreateDatasourceUrlNull.

/**
 * Test create DataSource from DataSourceInformation object with null url
 */
public void testCreateDatasourceUrlNull() {
    DataSourceInformation dataSourceInformation = createDataSourceInformation();
    dataSourceInformation.setUrl(null);
    try {
        DataSourceFactory.createDataSource(dataSourceInformation);
        fail("SynapseCommonsException expected");
    } catch (SynapseCommonsException e) {
        assertEquals("Invalid exception message", "Database connection URL cannot be found.", e.getMessage());
    }
}
Also used : SynapseCommonsException(org.apache.synapse.commons.SynapseCommonsException) DataSourceInformation(org.apache.synapse.commons.datasource.DataSourceInformation)

Example 5 with DataSourceInformation

use of org.apache.synapse.commons.datasource.DataSourceInformation in project wso2-synapse by wso2.

the class DataSourceInformationListFactoryTest method testCreateDataSourceInformationListNamesNotDefined.

/**
 * Test creating a list of DataSourceInformation objects from properties when "synapse.datasources" is not defined
 */
public void testCreateDataSourceInformationListNamesNotDefined() {
    Properties properties = new Properties();
    properties.put("synapse.datasources.dataSource1.driverClassName", "org.h2.Driver");
    properties.put("synapse.datasources.dataSource1.url", "jdbc:h2:repository/database/test_db1");
    properties.put("synapse.datasources.dataSource1.dsName", "dataSource1");
    properties.put("synapse.datasources.dataSource2.driverClassName", "org.h2.Driver");
    properties.put("synapse.datasources.dataSource2.url", "jdbc:h2:repository/database/test_db2");
    properties.put("synapse.datasources.dataSource2.dsName", "dataSource2");
    properties.put("synapse.datasources.dataSource3.driverClassName", "org.h2.Driver");
    properties.put("synapse.datasources.dataSource3.url", "jdbc:h2:repository/database/test_db3");
    properties.put("synapse.datasources.dataSource3.dsName", "dataSource3");
    List<DataSourceInformation> dataSourceInformations = DataSourceInformationListFactory.createDataSourceInformationList(properties);
    assertEquals("Created no of DataSourceInformation objects should be zero", 0, dataSourceInformations.size());
}
Also used : DataSourceInformation(org.apache.synapse.commons.datasource.DataSourceInformation) Properties(java.util.Properties)

Aggregations

DataSourceInformation (org.apache.synapse.commons.datasource.DataSourceInformation)13 Properties (java.util.Properties)7 SecretInformation (org.wso2.securevault.secret.SecretInformation)4 ArrayList (java.util.ArrayList)2 Iterator (java.util.Iterator)1 DataSource (javax.sql.DataSource)1 OMElement (org.apache.axiom.om.OMElement)1 SynapseCommonsException (org.apache.synapse.commons.SynapseCommonsException)1