Search in sources :

Example 1 with InternalContext

use of org.elasticsearch.common.inject.internal.InternalContext in project elasticsearch by elastic.

the class InjectorImpl method getProviderOrThrow.

<T> Provider<T> getProviderOrThrow(final Key<T> key, Errors errors) throws ErrorsException {
    final InternalFactory<? extends T> factory = getInternalFactory(key, errors);
    // ES: optimize for a common case of read only instance getting from the parent...
    if (factory instanceof InternalFactory.Instance) {
        return new Provider<T>() {

            @Override
            public T get() {
                try {
                    return (T) ((InternalFactory.Instance) factory).get(null, null, null);
                } catch (ErrorsException e) {
                // ignore
                }
                // should never happen...
                assert false;
                return null;
            }
        };
    }
    final Dependency<T> dependency = Dependency.get(key);
    return new Provider<T>() {

        @Override
        public T get() {
            final Errors errors = new Errors(dependency);
            try {
                T t = callInContext(new ContextualCallable<T>() {

                    @Override
                    public T call(InternalContext context) throws ErrorsException {
                        context.setDependency(dependency);
                        try {
                            return factory.get(errors, context, dependency);
                        } finally {
                            context.setDependency(null);
                        }
                    }
                });
                errors.throwIfNewErrors(0);
                return t;
            } catch (ErrorsException e) {
                throw new ProvisionException(errors.merge(e.getErrors()).getMessages());
            }
        }

        @Override
        public String toString() {
            return factory.toString();
        }
    };
}
Also used : Errors(org.elasticsearch.common.inject.internal.Errors) InternalContext(org.elasticsearch.common.inject.internal.InternalContext) InternalFactory(org.elasticsearch.common.inject.internal.InternalFactory) ErrorsException(org.elasticsearch.common.inject.internal.ErrorsException) SourceProvider(org.elasticsearch.common.inject.internal.SourceProvider)

Example 2 with InternalContext

use of org.elasticsearch.common.inject.internal.InternalContext in project elasticsearch by elastic.

the class ProviderToInternalFactoryAdapter method get.

@Override
public T get() {
    final Errors errors = new Errors();
    try {
        T t = injector.callInContext(new ContextualCallable<T>() {

            @Override
            public T call(InternalContext context) throws ErrorsException {
                Dependency dependency = context.getDependency();
                return internalFactory.get(errors, context, dependency);
            }
        });
        errors.throwIfNewErrors(0);
        return t;
    } catch (ErrorsException e) {
        throw new ProvisionException(errors.merge(e.getErrors()).getMessages());
    }
}
Also used : Errors(org.elasticsearch.common.inject.internal.Errors) InternalContext(org.elasticsearch.common.inject.internal.InternalContext) ErrorsException(org.elasticsearch.common.inject.internal.ErrorsException) Dependency(org.elasticsearch.common.inject.spi.Dependency)

Example 3 with InternalContext

use of org.elasticsearch.common.inject.internal.InternalContext in project crate by crate.

the class InjectorImpl method getProviderOrThrow.

<T> Provider<T> getProviderOrThrow(final Key<T> key, Errors errors) throws ErrorsException {
    final InternalFactory<? extends T> factory = getInternalFactory(key, errors);
    // ES: optimize for a common case of read only instance getting from the parent...
    if (factory instanceof InternalFactory.Instance) {
        return new Provider<T>() {

            @Override
            public T get() {
                try {
                    return (T) ((InternalFactory.Instance) factory).get(null, null, null);
                } catch (ErrorsException e) {
                // ignore
                }
                // should never happen...
                assert false;
                return null;
            }
        };
    }
    final Dependency<T> dependency = Dependency.get(key);
    return new Provider<T>() {

        @Override
        public T get() {
            final Errors errors = new Errors(dependency);
            try {
                T t = callInContext(new ContextualCallable<T>() {

                    @Override
                    public T call(InternalContext context) throws ErrorsException {
                        context.setDependency(dependency);
                        try {
                            return factory.get(errors, context, dependency);
                        } finally {
                            context.setDependency(null);
                        }
                    }
                });
                errors.throwIfNewErrors(0);
                return t;
            } catch (ErrorsException e) {
                throw new ProvisionException(errors.merge(e.getErrors()).getMessages());
            }
        }

        @Override
        public String toString() {
            return factory.toString();
        }
    };
}
Also used : Errors(org.elasticsearch.common.inject.internal.Errors) InternalContext(org.elasticsearch.common.inject.internal.InternalContext) InternalFactory(org.elasticsearch.common.inject.internal.InternalFactory) ErrorsException(org.elasticsearch.common.inject.internal.ErrorsException) SourceProvider(org.elasticsearch.common.inject.internal.SourceProvider)

Example 4 with InternalContext

use of org.elasticsearch.common.inject.internal.InternalContext in project crate by crate.

the class InjectorImpl method callInContext.

/**
 * Looks up thread local context. Creates (and removes) a new context if necessary.
 */
<T> T callInContext(ContextualCallable<T> callable) throws ErrorsException {
    Object[] reference = localContext.get();
    if (reference == null) {
        reference = new Object[1];
        localContext.set(reference);
    }
    if (reference[0] == null) {
        reference[0] = new InternalContext();
        try {
            return callable.call((InternalContext) reference[0]);
        } finally {
            // Only clear the context if this call created it.
            reference[0] = null;
        }
    } else {
        // Someone else will clean up this context.
        return callable.call((InternalContext) reference[0]);
    }
}
Also used : InternalContext(org.elasticsearch.common.inject.internal.InternalContext)

Example 5 with InternalContext

use of org.elasticsearch.common.inject.internal.InternalContext in project crate by crate.

the class ProviderToInternalFactoryAdapter method get.

@Override
public T get() {
    final Errors errors = new Errors();
    try {
        T t = injector.callInContext(new ContextualCallable<T>() {

            @Override
            public T call(InternalContext context) throws ErrorsException {
                Dependency dependency = context.getDependency();
                return internalFactory.get(errors, context, dependency);
            }
        });
        errors.throwIfNewErrors(0);
        return t;
    } catch (ErrorsException e) {
        throw new ProvisionException(errors.merge(e.getErrors()).getMessages());
    }
}
Also used : Errors(org.elasticsearch.common.inject.internal.Errors) InternalContext(org.elasticsearch.common.inject.internal.InternalContext) ErrorsException(org.elasticsearch.common.inject.internal.ErrorsException) Dependency(org.elasticsearch.common.inject.spi.Dependency)

Aggregations

InternalContext (org.elasticsearch.common.inject.internal.InternalContext)6 Errors (org.elasticsearch.common.inject.internal.Errors)4 ErrorsException (org.elasticsearch.common.inject.internal.ErrorsException)4 InternalFactory (org.elasticsearch.common.inject.internal.InternalFactory)2 SourceProvider (org.elasticsearch.common.inject.internal.SourceProvider)2 Dependency (org.elasticsearch.common.inject.spi.Dependency)2