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());
}
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);
}
}
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"));
}
}
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;
}
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);
}
}
Aggregations