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();
}
};
}
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());
}
}
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();
}
};
}
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]);
}
}
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());
}
}
Aggregations