Search in sources :

Example 1 with DataSourcePlugin

use of org.apache.openejb.resource.jdbc.plugin.DataSourcePlugin in project tomee by apache.

the class BasicDataSourceUtilTest method assertPluginClass.

private void assertPluginClass(final String jdbcUrl, final Class<? extends DataSourcePlugin> pluginClass) throws SQLException {
    final DataSourcePlugin plugin = BasicDataSourceUtil.getDataSourcePlugin(jdbcUrl);
    assertNotNull(plugin);
    assertSame(pluginClass, plugin.getClass());
}
Also used : DataSourcePlugin(org.apache.openejb.resource.jdbc.plugin.DataSourcePlugin) DerbyDataSourcePlugin(org.apache.openejb.resource.jdbc.plugin.DerbyDataSourcePlugin) HsqldbDataSourcePlugin(org.apache.openejb.resource.jdbc.plugin.HsqldbDataSourcePlugin) InstantdbDataSourcePlugin(org.apache.openejb.resource.jdbc.plugin.InstantdbDataSourcePlugin)

Example 2 with DataSourcePlugin

use of org.apache.openejb.resource.jdbc.plugin.DataSourcePlugin in project tomee by apache.

the class DataSourceFactory method normalizeJdbcUrl.

private static void normalizeJdbcUrl(final Properties properties) {
    final String key = "JdbcUrl";
    final String jdbcUrl = properties.getProperty(key);
    if (jdbcUrl == null) {
        return;
    }
    try {
        // get the plugin
        final DataSourcePlugin helper = BasicDataSourceUtil.getDataSourcePlugin(jdbcUrl);
        // configure this
        if (AbstractDataSourcePlugin.isActive(helper)) {
            final String newUrl = helper.updatedUrl(jdbcUrl);
            properties.setProperty(key, newUrl);
        }
    } catch (final SQLException e) {
        throw new IllegalStateException(e);
    }
}
Also used : AbstractDataSourcePlugin(org.apache.openejb.resource.jdbc.plugin.AbstractDataSourcePlugin) DataSourcePlugin(org.apache.openejb.resource.jdbc.plugin.DataSourcePlugin) SQLException(java.sql.SQLException)

Example 3 with DataSourcePlugin

use of org.apache.openejb.resource.jdbc.plugin.DataSourcePlugin in project tomee by apache.

the class BasicDataSource method createDataSource.

@Override
protected DataSource createDataSource() throws SQLException {
    if (dsRef != null) {
        return dsRef;
    }
    synchronized (this) {
        if (dsRef != null) {
            return dsRef;
        }
        // check password codec if available
        if (null != passwordCipher && !"PlainText".equals(passwordCipher)) {
            final PasswordCipher cipher = PasswordCipherFactory.getPasswordCipher(passwordCipher);
            // always use the initial encrypted value
            final String plainPwd = cipher.decrypt(initialPassword.toCharArray());
            // override previous password value
            super.setPassword(plainPwd);
        }
        // get the plugin
        final DataSourcePlugin helper = BasicDataSourceUtil.getDataSourcePlugin(getUrl());
        // configure this
        if (helper != null) {
            final String currentUrl = getUrl();
            final String newUrl = helper.updatedUrl(currentUrl);
            if (!currentUrl.equals(newUrl)) {
                super.setUrl(newUrl);
            }
        }
        // create the data source
        if (helper == null || !helper.enableUserDirHack()) {
            try {
                super.createDataSource();
            } catch (final Throwable e) {
                throw toSQLException(e);
            }
        } else {
            // wrap super call with code that sets user.dir to openejb.base and then resets it
            final Properties systemProperties = JavaSecurityManagers.getSystemProperties();
            final String userDir = systemProperties.getProperty("user.dir");
            try {
                final File base = SystemInstance.get().getBase().getDirectory();
                systemProperties.setProperty("user.dir", base.getAbsolutePath());
                try {
                    super.createDataSource();
                } catch (final Throwable e) {
                    throw toSQLException(e);
                }
            } finally {
                systemProperties.setProperty("user.dir", userDir);
            }
        }
        return dsRef = DataSource.class.cast(Reflections.get(this, "dataSource"));
    }
}
Also used : DataSourcePlugin(org.apache.openejb.resource.jdbc.plugin.DataSourcePlugin) PasswordCipher(org.apache.openejb.cipher.PasswordCipher) Properties(java.util.Properties) File(java.io.File) XADataSource(javax.sql.XADataSource) CommonDataSource(javax.sql.CommonDataSource) DataSource(javax.sql.DataSource)

Example 4 with DataSourcePlugin

use of org.apache.openejb.resource.jdbc.plugin.DataSourcePlugin in project tomee by apache.

the class BoneCPDataSourceCreator method prefixedProps.

private Properties prefixedProps(final Properties properties) {
    if (properties.containsKey("url")) {
        properties.setProperty("url", properties.getProperty("url"));
    }
    // updating relative url if mandatory (hsqldb for instance)
    final String currentUrl = properties.getProperty("jdbcUrl");
    if (currentUrl != null) {
        try {
            final DataSourcePlugin helper = BasicDataSourceUtil.getDataSourcePlugin(currentUrl);
            if (helper != null) {
                final String newUrl = helper.updatedUrl(currentUrl);
                if (!currentUrl.equals(newUrl)) {
                    properties.setProperty("jdbcUrl", newUrl);
                }
            }
        } catch (SQLException ignored) {
        // no-op
        }
    }
    final String cipher = properties.getProperty("PasswordCipher");
    if (cipher == null || "PlainText".equals(cipher)) {
        // no need to warn
        properties.remove("PasswordCipher");
    }
    // bonecp expects bonecp prefix in properties
    final Properties prefixedProps = new Properties();
    for (Map.Entry<Object, Object> entry : properties.entrySet()) {
        final String suffix = Strings.lcfirst((String) entry.getKey());
        if (!suffix.startsWith("bonecp.")) {
            prefixedProps.put("bonecp." + suffix, entry.getValue());
        } else {
            prefixedProps.put(suffix, entry.getValue());
        }
    }
    return prefixedProps;
}
Also used : DataSourcePlugin(org.apache.openejb.resource.jdbc.plugin.DataSourcePlugin) SQLException(java.sql.SQLException) Properties(java.util.Properties) Map(java.util.Map)

Example 5 with DataSourcePlugin

use of org.apache.openejb.resource.jdbc.plugin.DataSourcePlugin in project tomee by apache.

the class BasicDataSourceUtil method getDataSourcePlugin.

public static DataSourcePlugin getDataSourcePlugin(final String jdbcUrl) throws SQLException {
    // determine the vendor based on the jdbcUrl stirng "jdbc:${Vendor}:properties"
    final String vendor = getJdbcName(jdbcUrl);
    // no vendor so no plugin
    if (vendor == null) {
        return null;
    }
    // find the plugin class
    String pluginClassName = null;
    try {
        final ResourceFinder finder = new ResourceFinder("META-INF");
        final Map<String, String> plugins = finder.mapAvailableStrings(DataSourcePlugin.class.getName());
        pluginClassName = plugins.get(vendor);
    } catch (final IOException ignored) {
    // couldn't determine the plugins, which isn't fatal
    }
    // no plugin found
    if (pluginClassName == null || pluginClassName.length() <= 0) {
        return null;
    }
    // create the plugin
    try {
        final Class pluginClass = Class.forName(pluginClassName);
        return (DataSourcePlugin) pluginClass.newInstance();
    } catch (final ClassNotFoundException e) {
        throw new SQLException("Unable to load data source helper class '" + pluginClassName + "' for database '" + vendor + "'");
    } catch (final Exception e) {
        throw (SQLException) new SQLException("Unable to create data source helper class '" + pluginClassName + "' for database '" + vendor + "'").initCause(e);
    }
}
Also used : ResourceFinder(org.apache.xbean.finder.ResourceFinder) DataSourcePlugin(org.apache.openejb.resource.jdbc.plugin.DataSourcePlugin) SQLException(java.sql.SQLException) IOException(java.io.IOException) SQLException(java.sql.SQLException) IOException(java.io.IOException)

Aggregations

DataSourcePlugin (org.apache.openejb.resource.jdbc.plugin.DataSourcePlugin)6 SQLException (java.sql.SQLException)3 Properties (java.util.Properties)3 File (java.io.File)2 PasswordCipher (org.apache.openejb.cipher.PasswordCipher)2 IOException (java.io.IOException)1 Map (java.util.Map)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 CommonDataSource (javax.sql.CommonDataSource)1 DataSource (javax.sql.DataSource)1 XADataSource (javax.sql.XADataSource)1 AbstractDataSourcePlugin (org.apache.openejb.resource.jdbc.plugin.AbstractDataSourcePlugin)1 DerbyDataSourcePlugin (org.apache.openejb.resource.jdbc.plugin.DerbyDataSourcePlugin)1 HsqldbDataSourcePlugin (org.apache.openejb.resource.jdbc.plugin.HsqldbDataSourcePlugin)1 InstantdbDataSourcePlugin (org.apache.openejb.resource.jdbc.plugin.InstantdbDataSourcePlugin)1 ResourceFinder (org.apache.xbean.finder.ResourceFinder)1