use of org.apache.openejb.resource.XAResourceWrapper in project tomee by apache.
the class DataSourceFactory method create.
public static CommonDataSource create(final String name, final boolean configuredManaged, final Class impl, final String definition, final Duration maxWaitTime, final Duration timeBetweenEvictionRuns, final Duration minEvictableIdleTime, final boolean useAlternativeDriver) throws IllegalAccessException, InstantiationException, IOException {
final Properties properties = asProperties(definition);
final Set<String> originalKeys = properties.stringPropertyNames();
final String handler = SystemInstance.get().getOptions().get(GLOBAL_HANDLER_PROPERTY, (String) properties.remove(HANDLER_PROPERTY));
boolean flushable = SystemInstance.get().getOptions().get(GLOBAL_FLUSH_PROPERTY, "true".equalsIgnoreCase((String) properties.remove(FLUSHABLE_PROPERTY)));
final String forceDifferent = SystemInstance.get().getOptions().get(XA_GLOBAL_FORCE_DIFFERENT, String.class.cast(properties.remove(XA_FORCE_DIFFERENT)));
convert(properties, maxWaitTime, "maxWaitTime", "maxWait");
convert(properties, timeBetweenEvictionRuns, "timeBetweenEvictionRuns", "timeBetweenEvictionRunsMillis");
convert(properties, minEvictableIdleTime, "minEvictableIdleTime", "minEvictableIdleTimeMillis");
// these can be added and are managed by OpenEJB and not the DataSource itself
properties.remove("Definition");
properties.remove("JtaManaged");
properties.remove("ServiceId");
boolean managed = configuredManaged;
if (properties.containsKey("transactional")) {
managed = Boolean.parseBoolean((String) properties.remove("transactional")) || managed;
}
normalizeJdbcUrl(properties);
final String jdbcUrl = properties.getProperty("JdbcUrl");
final AlternativeDriver driver;
if (Driver.class.isAssignableFrom(impl) && jdbcUrl != null && useAlternativeDriver) {
try {
driver = new AlternativeDriver((Driver) impl.newInstance(), jdbcUrl);
driver.register();
} catch (final SQLException e) {
throw new IllegalStateException(e);
}
} else {
driver = null;
}
final boolean logSql = SystemInstance.get().getOptions().get(GLOBAL_LOG_SQL_PROPERTY, "true".equalsIgnoreCase((String) properties.remove(LOG_SQL_PROPERTY)));
final String logPackages = SystemInstance.get().getProperty(GLOBAL_LOG_SQL_PACKAGE_PROPERTY, (String) properties.remove(LOG_SQL_PACKAGE_PROPERTY));
final DataSourceCreator creator = creator(properties.remove(DATA_SOURCE_CREATOR_PROP), logSql);
final String resetOnError = (String) properties.remove(RESET_PROPERTY);
// before setProperties()
final String resetMethods = (String) properties.remove(RESET_METHODS_PROPERTY);
boolean useContainerLoader = "true".equalsIgnoreCase(SystemInstance.get().getProperty("openejb.resources.use-container-loader", "true")) && impl.getClassLoader() == DataSourceFactory.class.getClassLoader();
final ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
if (useContainerLoader) {
final ClassLoader containerLoader = DataSourceFactory.class.getClassLoader();
Thread.currentThread().setContextClassLoader(containerLoader);
try {
useContainerLoader = basicChecksThatDataSourceCanBeCreatedFromContainerLoader(properties, containerLoader);
} finally {
Thread.currentThread().setContextClassLoader(oldLoader);
}
if (useContainerLoader) {
Thread.currentThread().setContextClassLoader(containerLoader);
} else {
LOGGER.info("Can't use container loader to create datasource " + name + " so using application one");
}
}
try {
CommonDataSource ds;
if (createDataSourceFromClass(impl)) {
// opposed to "by driver"
trimNotSupportedDataSourceProperties(properties);
final ObjectRecipe recipe = new ObjectRecipe(impl);
recipe.allow(Option.CASE_INSENSITIVE_PROPERTIES);
recipe.allow(Option.IGNORE_MISSING_PROPERTIES);
recipe.allow(Option.NAMED_PARAMETERS);
recipe.allow(Option.PRIVATE_PROPERTIES);
recipe.setAllProperties(properties);
if (!properties.containsKey("url") && properties.containsKey("JdbcUrl")) {
// depend on the datasource class so add all well known keys
recipe.setProperty("url", properties.getProperty("JdbcUrl"));
}
CommonDataSource dataSource = (CommonDataSource) recipe.create();
final boolean isDs = DataSource.class.isInstance(dataSource);
if (!isDs && XADataSource.class.isInstance(dataSource) && forceDifferent != null) {
try {
dataSource = CommonDataSource.class.cast(Thread.currentThread().getContextClassLoader().loadClass("true".equals(forceDifferent) ? "org.apache.openejb.resource.jdbc.xa.IsDifferentXaDataSourceWrapper" : forceDifferent).getConstructor(XADataSource.class).newInstance(dataSource));
} catch (InvocationTargetException | ClassNotFoundException | NoSuchMethodException e) {
throw new IllegalArgumentException(e);
}
}
if (managed) {
if (isDs && usePool(properties)) {
ds = creator.poolManaged(name, DataSource.class.cast(dataSource), properties);
} else {
ds = creator.managed(name, dataSource);
}
} else {
if (isDs && usePool(properties)) {
ds = creator.pool(name, DataSource.class.cast(dataSource), properties);
} else {
ds = dataSource;
}
}
} else {
// by driver
if (managed) {
final XAResourceWrapper xaResourceWrapper = SystemInstance.get().getComponent(XAResourceWrapper.class);
if (xaResourceWrapper != null) {
ds = creator.poolManagedWithRecovery(name, xaResourceWrapper, impl.getName(), properties);
} else {
ds = creator.poolManaged(name, impl.getName(), properties);
}
} else {
ds = creator.pool(name, impl.getName(), properties);
}
}
// ds and creator are associated here, not after the proxying of the next if if active
setCreatedWith(creator, ds);
if (driver != null) {
driverByDataSource.put(ds, driver);
}
final boolean doResetOnError = resetOnError != null && !"false".equals(resetOnError);
if (doResetOnError || logSql || flushable) {
// will get proxied
ObjectRecipe objectRecipe = null;
ResettableDataSourceHandler existingResettableHandler = null;
FlushableDataSourceHandler flushableDataSourceHandler = null;
if (ExecutionContext.isContextSet()) {
final ExecutionContext context = ExecutionContext.getContext();
final List<Recipe> stack = context.getStack();
if (stack.size() > 0) {
objectRecipe = ObjectRecipe.class.cast(stack.get(0));
existingResettableHandler = ResettableDataSourceHandler.class.cast(objectRecipe.getProperty("resettableHandler"));
flushableDataSourceHandler = FlushableDataSourceHandler.class.cast(objectRecipe.getProperty("flushableHandler"));
final Map<String, Object> props = objectRecipe.getProperties();
for (final String key : originalKeys) {
props.remove(key);
}
// meta properties, not needed here so gain few cycles removing them
props.remove("properties");
props.remove("Definition");
props.remove("ServiceId");
props.remove("resettableHandler");
props.remove("flushableHandler");
//we create a proxy so we cant get txmgr etc in another manner or we cant extend (= break) this method
new ObjectRecipe(ds.getClass()) {
{
allow(Option.CASE_INSENSITIVE_PROPERTIES);
allow(Option.IGNORE_MISSING_PROPERTIES);
allow(Option.NAMED_PARAMETERS);
allow(Option.PRIVATE_PROPERTIES);
setAllProperties(props);
}
}.setProperties(ds);
}
}
ds = wrapIfNeeded(handler, ds);
if (logSql) {
ds = makeItLogging(ds, logPackages);
}
final ResettableDataSourceHandler resettableDataSourceHandler;
if (doResetOnError) {
// needs to be done after flushable
// ensure we reuse the same handle instance otherwise we loose state
resettableDataSourceHandler = existingResettableHandler != null ? existingResettableHandler : new ResettableDataSourceHandler(ds, resetOnError, resetMethods);
} else {
resettableDataSourceHandler = null;
}
if (flushable || doResetOnError) {
if (flushableDataSourceHandler == null) {
final FlushableDataSourceHandler.FlushConfig flushConfig;
// don't let it wrap the delegate again
properties.remove("flushable");
final Map<String, Object> recipeProps = new HashMap<>(objectRecipe == null ? new HashMap<String, Object>() : objectRecipe.getProperties());
recipeProps.remove("properties");
recipeProps.put("OpenEJBResourceClasspath", String.valueOf(useAlternativeDriver));
flushConfig = new FlushableDataSourceHandler.FlushConfig(recipeProps);
flushableDataSourceHandler = new FlushableDataSourceHandler(ds, flushConfig, resettableDataSourceHandler);
} else {
flushableDataSourceHandler.updateDataSource(ds);
}
ds = makeSerializableFlushableDataSourceProxy(ds, flushableDataSourceHandler);
}
if (doResetOnError) {
// needs to be done after flushable
// ensure we reuse the same handle instance otherwise we loose state
resettableDataSourceHandler.updateDelegate(ds);
ds = makeSerializableFlushableDataSourceProxy(ds, resettableDataSourceHandler);
}
} else {
ds = wrapIfNeeded(handler, ds);
}
return ds;
} finally {
if (useContainerLoader) {
Thread.currentThread().setContextClassLoader(oldLoader);
}
}
}
Aggregations