Search in sources :

Example 6 with ErrorService

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);
        }
    }
}
Also used : LinkedList(java.util.LinkedList) ErrorService(org.glassfish.hk2.api.ErrorService) RethrowErrorService(org.glassfish.hk2.utilities.RethrowErrorService) TwoPhaseResource(org.glassfish.hk2.api.TwoPhaseResource) ServiceHandle(org.glassfish.hk2.api.ServiceHandle) MultiException(org.glassfish.hk2.api.MultiException)

Example 7 with ErrorService

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;
}
Also used : Annotation(java.lang.annotation.Annotation) ErrorService(org.glassfish.hk2.api.ErrorService) RethrowErrorService(org.glassfish.hk2.utilities.RethrowErrorService) Injectee(org.glassfish.hk2.api.Injectee) Filter(org.glassfish.hk2.api.Filter) CacheKeyFilter(org.glassfish.hk2.utilities.cache.CacheKeyFilter) IndexedFilter(org.glassfish.hk2.api.IndexedFilter) InjecteeImpl(org.glassfish.hk2.utilities.InjecteeImpl) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 8 with ErrorService

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;
}
Also used : LinkedList(java.util.LinkedList) ErrorService(org.glassfish.hk2.api.ErrorService) RethrowErrorService(org.glassfish.hk2.utilities.RethrowErrorService) Filter(org.glassfish.hk2.api.Filter) CacheKeyFilter(org.glassfish.hk2.utilities.cache.CacheKeyFilter) IndexedFilter(org.glassfish.hk2.api.IndexedFilter) MultiException(org.glassfish.hk2.api.MultiException)

Example 9 with ErrorService

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);
    }
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator) Context(org.glassfish.hk2.api.Context) DynamicConfigurationService(org.glassfish.hk2.api.DynamicConfigurationService) DynamicConfiguration(org.glassfish.hk2.api.DynamicConfiguration) PerLookup(org.glassfish.hk2.api.PerLookup) MultiException(org.glassfish.hk2.api.MultiException) Test(org.junit.Test)

Aggregations

MultiException (org.glassfish.hk2.api.MultiException)8 DynamicConfiguration (org.glassfish.hk2.api.DynamicConfiguration)5 DynamicConfigurationService (org.glassfish.hk2.api.DynamicConfigurationService)5 ServiceLocator (org.glassfish.hk2.api.ServiceLocator)5 Test (org.junit.Test)5 ErrorService (org.glassfish.hk2.api.ErrorService)4 RethrowErrorService (org.glassfish.hk2.utilities.RethrowErrorService)3 LinkedList (java.util.LinkedList)2 Filter (org.glassfish.hk2.api.Filter)2 IndexedFilter (org.glassfish.hk2.api.IndexedFilter)2 PerLookup (org.glassfish.hk2.api.PerLookup)2 CacheKeyFilter (org.glassfish.hk2.utilities.cache.CacheKeyFilter)2 Annotation (java.lang.annotation.Annotation)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 Context (org.glassfish.hk2.api.Context)1 Injectee (org.glassfish.hk2.api.Injectee)1 ServiceHandle (org.glassfish.hk2.api.ServiceHandle)1 TwoPhaseResource (org.glassfish.hk2.api.TwoPhaseResource)1 ValidationService (org.glassfish.hk2.api.ValidationService)1