use of org.exist.LifeCycle in project exist by eXist-db.
the class Configurator method create.
/**
* @return The Configurable or null
*/
private static Configurable create(final Configuration conf, final Configurable instance, final Class<?> clazz) {
boolean interrupted = false;
try {
final MethodHandles.Lookup lookup = MethodHandles.lookup();
Configurable obj = null;
try {
final MethodHandle methodHandle = lookup.findConstructor(clazz, methodType(void.class, instance.getClass(), Configuration.class));
final BiFunction<Configurable, Configuration, Configurable> constructor = (BiFunction<Configurable, Configuration, Configurable>) LambdaMetafactory.metafactory(lookup, "apply", methodType(BiFunction.class), methodHandle.type().erase(), methodHandle, methodHandle.type()).getTarget().invokeExact();
obj = constructor.apply(instance, conf);
} catch (final Throwable e) {
if (e instanceof InterruptedException) {
interrupted = true;
}
if (LOG.isDebugEnabled()) {
LOG.debug("Unable to invoke Constructor on Configurable instance '{}', so creating new Constructor...", e.getMessage());
}
try {
final MethodHandle methodHandle = lookup.findConstructor(clazz, methodType(void.class, Configuration.class));
final Function<Configuration, Configurable> constructor = (Function<Configuration, Configurable>) LambdaMetafactory.metafactory(lookup, "apply", methodType(Function.class), methodHandle.type().erase(), methodHandle, methodHandle.type()).getTarget().invokeExact();
obj = constructor.apply(conf);
} catch (final Throwable ee) {
if (ee instanceof InterruptedException) {
interrupted = true;
}
LOG.warn("Instantiation exception on {} creation '{}', skipping instance creation.", clazz, ee.getMessage());
LOG.debug(e.getMessage(), ee);
}
}
if (obj == null) {
return null;
}
if (obj instanceof LifeCycle) {
BrokerPool db = null;
try {
db = BrokerPool.getInstance();
} catch (final EXistException e) {
// ignore if database is starting-up
LOG.warn("Unable to start lifecycle object: {}", obj.getClass().getName());
// TODO: add to BrokerPool static list to activate when ready
}
if (db != null) {
try (final DBBroker broker = db.getBroker();
final Txn transaction = broker.continueOrBeginTransaction()) {
((LifeCycle) obj).start(broker, transaction);
transaction.commit();
}
}
}
return obj;
} catch (final EXistException ee) {
LOG.warn("Database exception on {} startup '{}', skipping instance creation.", clazz, ee.getMessage());
LOG.debug(ee.getMessage(), ee);
return null;
} finally {
if (interrupted) {
// NOTE: must set interrupted flag
Thread.currentThread().interrupt();
}
}
}
Aggregations