use of javax.sql.CommonDataSource in project tomee by apache.
the class FlushableDataSourceHandler method updateDataSource.
public void updateDataSource(final CommonDataSource ds) {
// order is important, check DataSourceFactory
CommonDataSource current = ds;
while (Proxy.isProxyClass(current.getClass())) {
final InvocationHandler handler = Proxy.getInvocationHandler(current);
if (FlushableDataSourceHandler.class.isInstance(handler) || ResettableDataSourceHandler.class.isInstance(handler)) {
current = DelegatableHandler.class.cast(handler).getDelegate();
} else {
break;
}
}
delegate.set(current);
}
use of javax.sql.CommonDataSource in project tomee by apache.
the class FlushableDataSourceHandler method createANewDelegate.
private void createANewDelegate() {
final CommonDataSource old = delegate.get();
try {
final ObjectRecipe recipe = new ObjectRecipe(DataSourceFactory.class.getName(), "create", FACTORY_ARGS);
recipe.allow(Option.CASE_INSENSITIVE_PROPERTIES);
recipe.allow(Option.IGNORE_MISSING_PROPERTIES);
recipe.allow(Option.NAMED_PARAMETERS);
recipe.allow(Option.PRIVATE_PROPERTIES);
recipe.setAllProperties(config.properties);
recipe.setProperty("resettableHandler", resettableHandler);
recipe.setProperty("flushableHandler", this);
updateDataSource(CommonDataSource.class.cast(recipe.create()));
} catch (final Exception e) {
LOGGER.error("Can't recreate the datasource, keeping old one", e);
return;
}
if (DataSourceFactory.knows(old)) {
try {
DataSourceFactory.destroy(old);
} catch (final Throwable t) {
// Ignore
}
}
}
use of javax.sql.CommonDataSource in project tomee by apache.
the class FlushableDataSourceHandler method invoke.
@Override
public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
final CommonDataSource actualDelegate = delegate.get();
if (Object.class == method.getDeclaringClass()) {
if ("hashCode".equals(method.getName())) {
return hashCode();
}
if ("toString".equals(method.getName())) {
return "Flushable[" + actualDelegate.toString() + "]";
}
}
if (Flushable.class == method.getDeclaringClass()) {
final Lock l = lock.writeLock();
l.lock();
try {
createANewDelegate();
if (Flushable.class.isInstance(actualDelegate)) {
// these sanity could be enhanced
if (!Proxy.isProxyClass(actualDelegate.getClass()) || // reset implies flush so we need to check both
(!FlushableDataSourceHandler.class.isInstance(Proxy.getInvocationHandler(actualDelegate)) && !ResettableDataSourceHandler.class.isInstance(Proxy.getInvocationHandler(actualDelegate)))) {
Flushable.class.cast(actualDelegate).flush();
}
}
} finally {
l.unlock();
}
return null;
}
final Lock l = lock.readLock();
l.lock();
try {
return method.invoke(getDelegate(), args);
} catch (final InvocationTargetException ite) {
throw ite.getCause();
} finally {
l.unlock();
}
}
Aggregations