Search in sources :

Example 1 with LifeCycle

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();
        }
    }
}
Also used : LifeCycle(org.exist.LifeCycle) EXistException(org.exist.EXistException) Txn(org.exist.storage.txn.Txn) MethodHandles(java.lang.invoke.MethodHandles) BiFunction(java.util.function.BiFunction) Function(java.util.function.Function) DBBroker(org.exist.storage.DBBroker) BiFunction(java.util.function.BiFunction) BrokerPool(org.exist.storage.BrokerPool) MethodHandle(java.lang.invoke.MethodHandle)

Aggregations

MethodHandle (java.lang.invoke.MethodHandle)1 MethodHandles (java.lang.invoke.MethodHandles)1 BiFunction (java.util.function.BiFunction)1 Function (java.util.function.Function)1 EXistException (org.exist.EXistException)1 LifeCycle (org.exist.LifeCycle)1 BrokerPool (org.exist.storage.BrokerPool)1 DBBroker (org.exist.storage.DBBroker)1 Txn (org.exist.storage.txn.Txn)1