use of org.glassfish.hk2.api.ErrorService in project glassfish-hk2 by eclipse-ee4j.
the class ServiceLocatorImpl method addConfiguration.
/* package */
void addConfiguration(DynamicConfigurationImpl dci) {
CheckConfigurationData checkData;
List<ServiceHandle<?>> allConfigurationListeners = null;
MultiException configurationError = null;
wLock.lock();
try {
// Does as much preliminary checking as possible
checkData = checkConfiguration(dci);
removeConfigurationInternal(checkData.getUnbinds());
List<SystemDescriptor<?>> thingsAdded = addConfigurationInternal(dci);
reup(thingsAdded, checkData.getInstanceLifecycleModificationsMade(), checkData.getInjectionResolverModificationMade(), checkData.getErrorHandlerModificationMade(), checkData.getClassAnalyzerModificationMade(), checkData.getDynamicConfigurationListenerModificationMade(), checkData.getAffectedContracts(), checkData.getInterceptionServiceModificationMade());
allConfigurationListeners = new LinkedList<ServiceHandle<?>>(configListeners);
} catch (MultiException me) {
configurationError = me;
throw me;
} finally {
List<ErrorService> errorServices = null;
if (configurationError != null) {
errorServices = new LinkedList<ErrorService>(errorHandlers);
}
wLock.unlock();
if (errorServices != null && !errorServices.isEmpty()) {
for (ErrorService errorService : errorServices) {
try {
errorService.onFailure(new ErrorInformationImpl(ErrorType.DYNAMIC_CONFIGURATION_FAILURE, null, null, configurationError));
} catch (Throwable th) {
// Ignore
}
}
}
}
LinkedList<ServiceLocatorImpl> allMyChildren = new LinkedList<ServiceLocatorImpl>();
getAllChildren(allMyChildren);
for (ServiceLocatorImpl sli : allMyChildren) {
sli.reupCache(checkData.getAffectedContracts());
}
callAllConfigurationListeners(allConfigurationListeners);
LinkedList<TwoPhaseResource> resources = dci.getResources();
for (TwoPhaseResource resource : resources) {
try {
resource.activateDynamicConfiguration(checkData.getTransactionData());
} catch (Throwable ignore) {
Logger.getLogger().debug("Activate of TwoPhaseResource " + resource + " failed with exception", ignore);
}
}
}
use of org.glassfish.hk2.api.ErrorService in project glassfish-hk2 by eclipse-ee4j.
the class ServiceLocatorImpl method internalGetDescriptor.
@SuppressWarnings("unchecked")
private <T> ActiveDescriptor<T> internalGetDescriptor(Injectee onBehalfOf, Type contractOrImpl, String name, Unqualified unqualified, boolean isIterable, boolean calledFromSecondChanceResolveMethod, Annotation... qualifiers) throws MultiException {
if (contractOrImpl == null)
throw new IllegalArgumentException();
Class<?> rawClass = ReflectionHelper.getRawClass(contractOrImpl);
// Can't be a TypeVariable or Wildcard
if (rawClass == null)
return null;
Utilities.checkLookupType(rawClass);
rawClass = Utilities.translatePrimitiveType(rawClass);
name = getName(name, qualifiers);
NarrowResults results = null;
LinkedList<ErrorService> currentErrorHandlers = null;
ImmediateResults immediate = null;
unqualified = getEffectiveUnqualified(unqualified, isIterable, qualifiers);
final CacheKey cacheKey = new CacheKey(contractOrImpl, name, unqualified, qualifiers);
final Filter filter = new UnqualifiedIndexedFilter(rawClass.getName(), name, unqualified);
final IgdCacheKey igdCacheKey = new IgdCacheKey(cacheKey, name, onBehalfOf, contractOrImpl, rawClass, qualifiers, filter);
rLock.lock();
try {
final IgdValue value = igdCache.compute(igdCacheKey);
final boolean freshOne = value.freshnessKeeper.compareAndSet(1, 2);
if (!freshOne) {
immediate = narrow(// locator
this, // candidates
null, // requiredType
contractOrImpl, // name
name, // onBehalfOf
onBehalfOf, // onlyOne
true, // doValidation
true, // cachedResults
value.results, // filter
filter, // qualifiers
qualifiers);
results = immediate.getTimelessResults();
} else {
results = value.results;
immediate = value.immediate;
}
if (!results.getErrors().isEmpty()) {
currentErrorHandlers = new LinkedList<ErrorService>(errorHandlers);
}
} finally {
rLock.unlock();
}
if (currentErrorHandlers != null) {
// Do this next call OUTSIDE of the lock
Utilities.handleErrors(results, currentErrorHandlers);
}
// Must do validation here in order to allow for caching
ActiveDescriptor<T> postValidateResult = immediate.getImmediateResults().isEmpty() ? null : (ActiveDescriptor<T>) immediate.getImmediateResults().get(0);
// See https://java.net/jira/browse/HK2-170
if (!calledFromSecondChanceResolveMethod && postValidateResult == null) {
final Injectee injectee;
if (onBehalfOf == null) {
final HashSet<Annotation> requiredQualifiers = new HashSet<Annotation>();
if (qualifiers != null && qualifiers.length > 0) {
for (final Annotation qualifier : qualifiers) {
if (qualifier != null) {
requiredQualifiers.add(qualifier);
}
}
}
final InjecteeImpl injecteeImpl = new InjecteeImpl(contractOrImpl);
injecteeImpl.setRequiredQualifiers(requiredQualifiers);
injecteeImpl.setUnqualified(unqualified);
injectee = injecteeImpl;
} else {
injectee = onBehalfOf;
}
postValidateResult = (ActiveDescriptor<T>) secondChanceResolve(injectee);
}
return postValidateResult;
}
use of org.glassfish.hk2.api.ErrorService in project glassfish-hk2 by eclipse-ee4j.
the class ServiceLocatorImpl method internalGetAllServiceHandles.
private List<?> internalGetAllServiceHandles(Type contractOrImpl, Unqualified unqualified, boolean getHandles, boolean isIterable, Annotation... qualifiers) throws MultiException {
if (contractOrImpl == null)
throw new IllegalArgumentException();
checkState();
final Class<?> rawClass = ReflectionHelper.getRawClass(contractOrImpl);
if (rawClass == null) {
throw new MultiException(new IllegalArgumentException("Type must be a class or parameterized type, it was " + contractOrImpl));
}
final String name = rawClass.getName();
NarrowResults results = null;
LinkedList<ErrorService> currentErrorHandlers = null;
ImmediateResults immediate = null;
unqualified = getEffectiveUnqualified(unqualified, isIterable, qualifiers);
final CacheKey cacheKey = new CacheKey(contractOrImpl, null, unqualified, qualifiers);
final Filter filter = new UnqualifiedIndexedFilter(name, null, unqualified);
final IgdCacheKey igdCacheKey = new IgdCacheKey(cacheKey, name, null, contractOrImpl, rawClass, qualifiers, filter);
rLock.lock();
try {
final IgdValue value = igashCache.compute(igdCacheKey);
final boolean freshOne = value.freshnessKeeper.compareAndSet(1, 2);
if (!freshOne) {
immediate = narrow(this, null, contractOrImpl, null, null, false, true, value.results, filter, qualifiers);
results = immediate.getTimelessResults();
} else {
results = value.results;
immediate = value.immediate;
}
if (!results.getErrors().isEmpty()) {
currentErrorHandlers = new LinkedList<ErrorService>(errorHandlers);
}
} finally {
rLock.unlock();
}
if (currentErrorHandlers != null) {
// Do this next call OUTSIDE of the lock
Utilities.handleErrors(results, currentErrorHandlers);
}
LinkedList<Object> retVal = new LinkedList<Object>();
for (ActiveDescriptor<?> candidate : immediate.getImmediateResults()) {
if (getHandles) {
retVal.add(internalGetServiceHandle(candidate, contractOrImpl, null));
} else {
Object service = Utilities.createService(candidate, null, this, null, rawClass);
retVal.add(service);
}
}
return retVal;
}
use of org.glassfish.hk2.api.ErrorService in project glassfish-hk2 by eclipse-ee4j.
the class NegativeConfigTest method testPerLookupContext.
/**
* A context must be in Singleton scope
*/
@Test
public void testPerLookupContext() {
ServiceLocator locator = LocatorHelper.create();
ServiceLocatorUtilities.addClasses(locator, DynamicConfigErrorService.class);
DynamicConfigErrorService errorService = locator.getService(DynamicConfigErrorService.class);
Assert.assertNull(errorService.getConfigException());
DynamicConfigurationService dcs = locator.getService(DynamicConfigurationService.class);
DynamicConfiguration dc = dcs.createDynamicConfiguration();
dc.bind(BuilderHelper.link(BadContext.class).to(Context.class).in(PerLookup.class.getName()).build());
try {
dc.commit();
Assert.fail("Commit should have failed with PerLookup Context");
} catch (MultiException me) {
Assert.assertTrue(me.getMessage(), me.getMessage().contains(" must be in the Singleton scope"));
Assert.assertEquals(errorService.getConfigException(), me);
}
}
Aggregations