Search in sources :

Example 1 with SynapseCommonsException

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

the class DataSourceInformationRepository method addDataSourceInformation.

/**
 * Adding a DataSourceInformation instance
 *
 * @param dataSourceInformation <code>DataSourceInformation</code> instance
 */
public void addDataSourceInformation(DataSourceInformation dataSourceInformation) {
    if (dataSourceInformation == null) {
        throw new SynapseCommonsException("DataSource information is null", log);
    }
    // Sets the global secret resolver
    SecretInformation secretInformation = dataSourceInformation.getSecretInformation();
    if (secretInformation != null) {
        secretInformation.setGlobalSecretResolver(secretResolver);
    }
    dataSourceInformationMap.put(dataSourceInformation.getAlias(), dataSourceInformation);
    if (assertListerNotNull()) {
        listener.addDataSourceInformation(dataSourceInformation);
    }
}
Also used : SynapseCommonsException(org.apache.synapse.commons.SynapseCommonsException) SecretInformation(org.wso2.securevault.secret.SecretInformation)

Example 2 with SynapseCommonsException

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

the class JNDIBasedDataSourceRepository method register.

/**
 * Register a DataSource in the JNDI tree
 *
 * @see DataSourceRepository#register(DataSourceInformation)
 */
public void register(DataSourceInformation information) {
    validateInitialized();
    String dataSourceName = information.getDatasourceName();
    validateDSName(dataSourceName);
    Properties properties = information.getProperties();
    InitialContext context = null;
    Properties jndiEvn = null;
    if (properties == null || properties.isEmpty()) {
        if (initialContext != null) {
            context = initialContext;
            if (log.isDebugEnabled()) {
                log.debug("Empty JNDI properties for datasource " + dataSourceName);
                log.debug("Using system-wide jndi properties : " + jndiProperties);
            }
            jndiEvn = jndiProperties;
        }
    }
    if (context == null) {
        jndiEvn = createJNDIEnvironment(properties, information.getAlias());
        context = createInitialContext(jndiEvn);
        if (context == null) {
            validateInitialContext(initialContext);
            context = initialContext;
            if (log.isDebugEnabled()) {
                log.debug("Cannot create a name context with provided jndi properties : " + jndiEvn);
                log.debug("Using system-wide JNDI properties : " + jndiProperties);
            }
            jndiEvn = jndiProperties;
        } else {
            perDataSourceICMap.put(dataSourceName, context);
        }
    }
    String dsType = information.getType();
    String driver = information.getDriver();
    String url = information.getUrl();
    String user = information.getSecretInformation().getUser();
    String password = information.getSecretInformation().getResolvedSecret();
    String maxActive = String.valueOf(information.getMaxActive());
    String maxIdle = String.valueOf(information.getMaxIdle());
    String maxWait = String.valueOf(information.getMaxWait());
    // populates context tree
    populateContextTree(context, dataSourceName);
    if (DataSourceInformation.BASIC_DATA_SOURCE.equals(dsType)) {
        Reference ref = new Reference("javax.sql.DataSource", "org.apache.commons.dbcp.BasicDataSourceFactory", null);
        ref.add(new StringRefAddr(DataSourceConstants.PROP_DRIVER_CLS_NAME, driver));
        ref.add(new StringRefAddr(DataSourceConstants.PROP_URL, url));
        ref.add(new StringRefAddr(SecurityConstants.PROP_USER_NAME, user));
        ref.add(new StringRefAddr(SecurityConstants.PROP_PASSWORD, password));
        ref.add(new StringRefAddr(DataSourceConstants.PROP_MAX_ACTIVE, maxActive));
        ref.add(new StringRefAddr(DataSourceConstants.PROP_MAX_IDLE, maxIdle));
        ref.add(new StringRefAddr(DataSourceConstants.PROP_MAX_WAIT, maxWait));
        // set BasicDataSource specific parameters
        setBasicDataSourceParameters(ref, information);
        // set default jndiProperties for reference
        setCommonParameters(ref, information);
        try {
            if (log.isDebugEnabled()) {
                log.debug("Registering a DataSource with name : " + dataSourceName + " in the JNDI tree with jndiProperties : " + jndiEvn);
            }
            context.rebind(dataSourceName, ref);
        } catch (NamingException e) {
            String msg = " Error binding name ' " + dataSourceName + " ' to " + "the DataSource(BasicDataSource) reference";
            throw new SynapseCommonsException(msg, e, log);
        }
    } else if (DataSourceInformation.PER_USER_POOL_DATA_SOURCE.equals(dsType)) {
        // Construct DriverAdapterCPDS reference
        String className = (String) information.getParameter(DataSourceConstants.PROP_CPDS_ADAPTER + DataSourceConstants.DOT_STRING + DataSourceConstants.PROP_CPDS_CLASS_NAME);
        String factory = (String) information.getParameter(DataSourceConstants.PROP_CPDS_ADAPTER + DataSourceConstants.DOT_STRING + DataSourceConstants.PROP_CPDS_FACTORY);
        String name = (String) information.getParameter(DataSourceConstants.PROP_CPDS_ADAPTER + DataSourceConstants.DOT_STRING + DataSourceConstants.PROP_CPDS_NAME);
        Reference cpdsRef = new Reference(className, factory, null);
        cpdsRef.add(new StringRefAddr(DataSourceConstants.PROP_DRIVER, driver));
        cpdsRef.add(new StringRefAddr(DataSourceConstants.PROP_URL, url));
        cpdsRef.add(new StringRefAddr(DataSourceConstants.PROP_USER, user));
        cpdsRef.add(new StringRefAddr(SecurityConstants.PROP_PASSWORD, password));
        try {
            context.rebind(name, cpdsRef);
        } catch (NamingException e) {
            String msg = "Error binding name '" + name + "' to " + "the DriverAdapterCPDS reference";
            throw new SynapseCommonsException(msg, e, log);
        }
        // Construct PerUserPoolDataSource reference
        Reference ref = new Reference("org.apache.commons.dbcp.datasources.PerUserPoolDataSource", "org.apache.commons.dbcp.datasources.PerUserPoolDataSourceFactory", null);
        ref.add(new BinaryRefAddr(DataSourceConstants.PROP_JNDI_ENV, MiscellaneousUtil.serialize(jndiEvn)));
        ref.add(new StringRefAddr(DataSourceConstants.PROP_DATA_SOURCE_NAME, name));
        ref.add(new StringRefAddr(DataSourceConstants.PROP_DEFAULT_MAX_ACTIVE, maxActive));
        ref.add(new StringRefAddr(DataSourceConstants.PROP_DEFAULT_MAX_IDLE, maxIdle));
        ref.add(new StringRefAddr(DataSourceConstants.PROP_DEFAULT_MAX_WAIT, maxWait));
        // set default jndiProperties for reference
        setCommonParameters(ref, information);
        try {
            if (log.isDebugEnabled()) {
                log.debug("Registering a DataSource with name : " + dataSourceName + " in the JNDI tree with jndiProperties : " + jndiEvn);
            }
            context.rebind(dataSourceName, ref);
        } catch (NamingException e) {
            String msg = "Error binding name ' " + dataSourceName + " ' to " + "the PerUserPoolDataSource reference";
            throw new SynapseCommonsException(msg, e, log);
        }
    } else {
        throw new SynapseCommonsException("Unsupported data source type : " + dsType, log);
    }
    cachedNameList.add(dataSourceName);
}
Also used : SynapseCommonsException(org.apache.synapse.commons.SynapseCommonsException)

Example 3 with SynapseCommonsException

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

the class MiscellaneousUtil method asBytes.

@SuppressWarnings({ "UnusedDeclaration" })
public static byte[] asBytes(InputStream in) {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int len;
    try {
        while ((len = in.read(buffer)) >= 0) out.write(buffer, 0, len);
    } catch (IOException e) {
        throw new SynapseCommonsException("Error during converting a input stream " + "into a byte array", e, log);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException ignored) {
            }
        }
        try {
            out.close();
        } catch (IOException ignored) {
        }
    }
    return out.toByteArray();
}
Also used : SynapseCommonsException(org.apache.synapse.commons.SynapseCommonsException)

Example 4 with SynapseCommonsException

use of org.apache.synapse.commons.SynapseCommonsException 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 SynapseCommonsException

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

the class DataSourceInformationFactoryTest method testCreateDataSourceInformationUrlNull.

/**
 * Test creating DatasourceInformation without defining db url
 */
public void testCreateDataSourceInformationUrlNull() {
    Properties properties = new Properties();
    properties.put("synapse.datasources.dataSource1.driverClassName", "org.h2.Driver");
    properties.put("synapse.datasources.dataSource1.dsName", "dataSource1");
    try {
        DataSourceInformationFactory.createDataSourceInformation("dataSource1", properties);
    } catch (SynapseCommonsException e) {
        assertEquals("Invalid exception message", "synapse.datasources.dataSource1.url cannot be found.", e.getMessage());
    }
}
Also used : SynapseCommonsException(org.apache.synapse.commons.SynapseCommonsException) Properties(java.util.Properties)

Aggregations

SynapseCommonsException (org.apache.synapse.commons.SynapseCommonsException)7 Properties (java.util.Properties)1 DataSource (javax.sql.DataSource)1 DataSourceInformation (org.apache.synapse.commons.datasource.DataSourceInformation)1 SecretInformation (org.wso2.securevault.secret.SecretInformation)1