Search in sources :

Example 1 with TwoPhaseResource

use of org.glassfish.hk2.api.TwoPhaseResource 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 2 with TwoPhaseResource

use of org.glassfish.hk2.api.TwoPhaseResource in project glassfish-hk2 by eclipse-ee4j.

the class ServiceLocatorImpl method checkConfiguration.

/**
 * Checks the configuration operation before anything happens to the internal data structures.
 *
 * @param dci The configuration that contains the proposed modifications
 * @return A set of descriptors that is being removed fromthe configuration
 */
private CheckConfigurationData checkConfiguration(DynamicConfigurationImpl dci) {
    List<SystemDescriptor<?>> retVal = new LinkedList<SystemDescriptor<?>>();
    boolean addOrRemoveOfInstanceListener = false;
    boolean addOrRemoveOfInjectionResolver = false;
    boolean addOrRemoveOfErrorHandler = false;
    boolean addOrRemoveOfClazzAnalyzer = false;
    boolean addOrRemoveOfConfigListener = false;
    boolean addOrRemoveOfInterceptionService = false;
    HashSet<String> affectedContracts = new HashSet<String>();
    TwoPhaseTransactionDataImpl transactionData = new TwoPhaseTransactionDataImpl();
    for (Filter unbindFilter : dci.getUnbindFilters()) {
        List<SystemDescriptor<?>> results = getDescriptors(unbindFilter, null, false, false, true);
        for (SystemDescriptor<?> candidate : results) {
            affectedContracts.addAll(getAllContracts(candidate));
            if (retVal.contains(candidate))
                continue;
            for (ValidationService vs : getAllValidators()) {
                if (!callValidate(vs, new ValidationInformationImpl(Operation.UNBIND, candidate))) {
                    throw new MultiException(new IllegalArgumentException("Descriptor " + candidate + " did not pass the UNBIND validation"));
                }
            }
            if (candidate.getAdvertisedContracts().contains(InstanceLifecycleListener.class.getName())) {
                addOrRemoveOfInstanceListener = true;
            }
            if (candidate.getAdvertisedContracts().contains(InjectionResolver.class.getName())) {
                addOrRemoveOfInjectionResolver = true;
            }
            if (candidate.getAdvertisedContracts().contains(ErrorService.class.getName())) {
                addOrRemoveOfErrorHandler = true;
            }
            if (candidate.getAdvertisedContracts().contains(ClassAnalyzer.class.getName())) {
                addOrRemoveOfClazzAnalyzer = true;
            }
            if (candidate.getAdvertisedContracts().contains(DynamicConfigurationListener.class.getName())) {
                addOrRemoveOfConfigListener = true;
            }
            if (candidate.getAdvertisedContracts().contains(InterceptionService.class.getName())) {
                addOrRemoveOfInterceptionService = true;
            }
            retVal.add(candidate);
            transactionData.toRemove(candidate);
        }
    }
    for (SystemDescriptor<?> sd : dci.getAllDescriptors()) {
        transactionData.toAdd(sd);
        affectedContracts.addAll(getAllContracts(sd));
        boolean checkScope = false;
        if (sd.getAdvertisedContracts().contains(ValidationService.class.getName()) || sd.getAdvertisedContracts().contains(ErrorService.class.getName()) || sd.getAdvertisedContracts().contains(InterceptionService.class.getName()) || sd.getAdvertisedContracts().contains(InstanceLifecycleListener.class.getName())) {
            // These get reified right away
            reifyDescriptor(sd);
            checkScope = true;
            if (sd.getAdvertisedContracts().contains(ErrorService.class.getName())) {
                addOrRemoveOfErrorHandler = true;
            }
            if (sd.getAdvertisedContracts().contains(InstanceLifecycleListener.class.getName())) {
                addOrRemoveOfInstanceListener = true;
            }
            if (sd.getAdvertisedContracts().contains(InterceptionService.class.getName())) {
                addOrRemoveOfInterceptionService = true;
            }
        }
        if (sd.getAdvertisedContracts().contains(InjectionResolver.class.getName())) {
            // This gets reified right away
            reifyDescriptor(sd);
            checkScope = true;
            if (Utilities.getInjectionResolverType(sd) == null) {
                throw new MultiException(new IllegalArgumentException("An implementation of InjectionResolver must be a parameterized type and the actual type" + " must be an annotation"));
            }
            addOrRemoveOfInjectionResolver = true;
        }
        if (sd.getAdvertisedContracts().contains(DynamicConfigurationListener.class.getName())) {
            // This gets reified right away
            reifyDescriptor(sd);
            checkScope = true;
            addOrRemoveOfConfigListener = true;
        }
        if (sd.getAdvertisedContracts().contains(Context.class.getName())) {
            // This one need not be reified, it will get checked later
            checkScope = true;
        }
        if (sd.getAdvertisedContracts().contains(ClassAnalyzer.class.getName())) {
            addOrRemoveOfClazzAnalyzer = true;
        }
        if (checkScope) {
            String scope = (sd.getScope() == null) ? PerLookup.class.getName() : sd.getScope();
            if (!scope.equals(Singleton.class.getName())) {
                throw new MultiException(new IllegalArgumentException("The implementation class " + sd.getImplementation() + " must be in the Singleton scope"));
            }
        }
        for (ValidationService vs : getAllValidators()) {
            Validator validator = vs.getValidator();
            if (validator == null) {
                throw new MultiException(new IllegalArgumentException("Validator was null from validation service" + vs));
            }
            if (!callValidate(vs, new ValidationInformationImpl(Operation.BIND, sd))) {
                throw new MultiException(new IllegalArgumentException("Descriptor " + sd + " did not pass the BIND validation"));
            }
        }
    }
    List<Filter> idempotentFilters = dci.getIdempotentFilters();
    if (!idempotentFilters.isEmpty()) {
        List<ActiveDescriptor<?>> allValidatedDescriptors = getDescriptors(BuilderHelper.allFilter());
        List<Throwable> idempotentFailures = new LinkedList<>();
        for (ActiveDescriptor<?> aValidatedDescriptor : allValidatedDescriptors) {
            for (Filter idempotentFilter : idempotentFilters) {
                if (BuilderHelper.filterMatches(aValidatedDescriptor, idempotentFilter)) {
                    idempotentFailures.add(new DuplicateServiceException(aValidatedDescriptor, locatorName));
                }
            }
        }
        if (!idempotentFailures.isEmpty()) {
            throw new MultiException(idempotentFailures);
        }
    }
    LinkedList<TwoPhaseResource> resources = dci.getResources();
    List<TwoPhaseResource> completedPrepares = new LinkedList<TwoPhaseResource>();
    for (TwoPhaseResource resource : resources) {
        try {
            resource.prepareDynamicConfiguration(transactionData);
            completedPrepares.add(resource);
        } catch (Throwable th) {
            for (TwoPhaseResource rollMe : completedPrepares) {
                try {
                    rollMe.rollbackDynamicConfiguration(transactionData);
                } catch (Throwable ignore) {
                    Logger.getLogger().debug("Rollback of TwoPhaseResource " + resource + " failed with exception", ignore);
                }
            }
            if (th instanceof RuntimeException) {
                throw (RuntimeException) th;
            }
            throw new RuntimeException(th);
        }
    }
    return new CheckConfigurationData(retVal, addOrRemoveOfInstanceListener, addOrRemoveOfInjectionResolver, addOrRemoveOfErrorHandler, addOrRemoveOfClazzAnalyzer, addOrRemoveOfConfigListener, affectedContracts, addOrRemoveOfInterceptionService, transactionData);
}
Also used : ActiveDescriptor(org.glassfish.hk2.api.ActiveDescriptor) InstanceLifecycleListener(org.glassfish.hk2.api.InstanceLifecycleListener) DynamicConfigurationListener(org.glassfish.hk2.api.DynamicConfigurationListener) DuplicateServiceException(org.glassfish.hk2.api.DuplicateServiceException) TwoPhaseResource(org.glassfish.hk2.api.TwoPhaseResource) PerLookup(org.glassfish.hk2.api.PerLookup) ValidationService(org.glassfish.hk2.api.ValidationService) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Context(org.glassfish.hk2.api.Context) InjectionResolver(org.glassfish.hk2.api.InjectionResolver) JustInTimeInjectionResolver(org.glassfish.hk2.api.JustInTimeInjectionResolver) LinkedList(java.util.LinkedList) ErrorService(org.glassfish.hk2.api.ErrorService) RethrowErrorService(org.glassfish.hk2.utilities.RethrowErrorService) ClassAnalyzer(org.glassfish.hk2.api.ClassAnalyzer) Filter(org.glassfish.hk2.api.Filter) CacheKeyFilter(org.glassfish.hk2.utilities.cache.CacheKeyFilter) IndexedFilter(org.glassfish.hk2.api.IndexedFilter) InterceptionService(org.glassfish.hk2.api.InterceptionService) MultiException(org.glassfish.hk2.api.MultiException) Validator(org.glassfish.hk2.api.Validator)

Aggregations

LinkedList (java.util.LinkedList)2 ErrorService (org.glassfish.hk2.api.ErrorService)2 MultiException (org.glassfish.hk2.api.MultiException)2 TwoPhaseResource (org.glassfish.hk2.api.TwoPhaseResource)2 RethrowErrorService (org.glassfish.hk2.utilities.RethrowErrorService)2 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 ActiveDescriptor (org.glassfish.hk2.api.ActiveDescriptor)1 ClassAnalyzer (org.glassfish.hk2.api.ClassAnalyzer)1 Context (org.glassfish.hk2.api.Context)1 DuplicateServiceException (org.glassfish.hk2.api.DuplicateServiceException)1 DynamicConfigurationListener (org.glassfish.hk2.api.DynamicConfigurationListener)1 Filter (org.glassfish.hk2.api.Filter)1 IndexedFilter (org.glassfish.hk2.api.IndexedFilter)1 InjectionResolver (org.glassfish.hk2.api.InjectionResolver)1 InstanceLifecycleListener (org.glassfish.hk2.api.InstanceLifecycleListener)1 InterceptionService (org.glassfish.hk2.api.InterceptionService)1 JustInTimeInjectionResolver (org.glassfish.hk2.api.JustInTimeInjectionResolver)1 PerLookup (org.glassfish.hk2.api.PerLookup)1 ServiceHandle (org.glassfish.hk2.api.ServiceHandle)1