Search in sources :

Example 6 with DataSourcePlugin

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

the class BasicManagedDataSource method createDataSource.

protected DataSource createDataSource() throws SQLException {
    final ReentrantLock l = lock;
    l.lock();
    try {
        final Object dataSource = Reflections.get(this, "dataSource");
        if (dataSource != null) {
            return DataSource.class.cast(dataSource);
        }
        // check password codec if available
        if (null != 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);
            }
        }
        wrapTransactionManager();
        // create the data source
        if (helper == null || !helper.enableUserDirHack()) {
            try {
                return super.createDataSource();
            } catch (final Throwable e) {
                throw BasicDataSource.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 {
                    return super.createDataSource();
                } catch (final Throwable e) {
                    throw BasicDataSource.toSQLException(e);
                }
            } finally {
                systemProperties.setProperty("user.dir", userDir);
            }
        }
    } finally {
        l.unlock();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) DataSourcePlugin(org.apache.openejb.resource.jdbc.plugin.DataSourcePlugin) PasswordCipher(org.apache.openejb.cipher.PasswordCipher) Properties(java.util.Properties) File(java.io.File)

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