Search in sources :

Example 1 with ValidationService

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

the class ServiceLocatorImpl method removeConfigurationInternal.

@SuppressWarnings("unchecked")
private void removeConfigurationInternal(List<SystemDescriptor<?>> unbinds) {
    for (SystemDescriptor<?> unbind : unbinds) {
        if ((BIND_TRACING_PATTERN != null) && doTrace(unbind)) {
            Logger.getLogger().debug("HK2 Bind Tracing: Removing Descriptor " + unbind);
            if (BIND_TRACING_STACKS) {
                Logger.getLogger().debug("ServiceLocatorImpl", "removeConfigurationInternal", new Throwable());
            }
        }
        allDescriptors.removeDescriptor(unbind);
        for (String advertisedContract : getAllContracts(unbind)) {
            IndexedListData ild = descriptorsByAdvertisedContract.get(advertisedContract);
            if (ild == null)
                continue;
            ild.removeDescriptor(unbind);
            if (ild.isEmpty())
                descriptorsByAdvertisedContract.remove(advertisedContract);
        }
        String unbindName = unbind.getName();
        if (unbindName != null) {
            IndexedListData ild = descriptorsByName.get(unbindName);
            if (ild != null) {
                ild.removeDescriptor(unbind);
                if (ild.isEmpty()) {
                    descriptorsByName.remove(unbindName);
                }
            }
        }
        if (unbind.getAdvertisedContracts().contains(ValidationService.class.getName())) {
            ServiceHandle<ValidationService> handle = (ServiceHandle<ValidationService>) getServiceHandle(unbind);
            ValidationService vs = handle.getService();
            allValidators.remove(vs);
        }
        if (unbind.isReified()) {
            for (Injectee injectee : unbind.getInjectees()) {
                if (injectee instanceof SystemInjecteeImpl) {
                    injecteeToResolverCache.remove((SystemInjecteeImpl) injectee);
                }
            }
            classReflectionHelper.clean(unbind.getImplementationClass());
        }
    }
    boolean hasOneUnbind = false;
    for (SystemDescriptor<?> unbind : unbinds) {
        hasOneUnbind = true;
        // Do this after all the other work has been done
        // to ensure we can possibly still use things such
        // as the validation service while we are unbinding
        unbind.close();
    }
    if (hasOneUnbind) {
        perLocatorUtilities.releaseCaches();
    }
}
Also used : Injectee(org.glassfish.hk2.api.Injectee) ServiceHandle(org.glassfish.hk2.api.ServiceHandle) ValidationService(org.glassfish.hk2.api.ValidationService)

Example 2 with ValidationService

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

the class NegativeConfigTest method testPerLookupValidationService.

/**
 * A validation service must be in Singleton scope
 */
@Test
public void testPerLookupValidationService() {
    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(BadValidationService.class).to(ValidationService.class).in(PerLookup.class.getName()).build());
    try {
        dc.commit();
        Assert.fail("Commit should have failed with PerLookup ValidationService");
    } 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) DynamicConfigurationService(org.glassfish.hk2.api.DynamicConfigurationService) DynamicConfiguration(org.glassfish.hk2.api.DynamicConfiguration) PerLookup(org.glassfish.hk2.api.PerLookup) ValidationService(org.glassfish.hk2.api.ValidationService) MultiException(org.glassfish.hk2.api.MultiException) Test(org.junit.Test)

Example 3 with ValidationService

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

the class ServiceLocatorImpl method addConfigurationInternal.

@SuppressWarnings("unchecked")
private List<SystemDescriptor<?>> addConfigurationInternal(DynamicConfigurationImpl dci) {
    List<SystemDescriptor<?>> thingsAdded = new LinkedList<SystemDescriptor<?>>();
    for (SystemDescriptor<?> sd : dci.getAllDescriptors()) {
        if ((BIND_TRACING_PATTERN != null) && doTrace(sd)) {
            Logger.getLogger().debug("HK2 Bind Tracing: Adding Descriptor " + sd);
            if (BIND_TRACING_STACKS) {
                Logger.getLogger().debug("ServiceLocatorImpl", "addConfigurationInternal", new Throwable());
            }
        }
        thingsAdded.add(sd);
        allDescriptors.addDescriptor(sd);
        List<String> allContracts = getAllContracts(sd);
        for (String advertisedContract : allContracts) {
            IndexedListData ild = descriptorsByAdvertisedContract.get(advertisedContract);
            if (ild == null) {
                ild = new IndexedListData();
                descriptorsByAdvertisedContract.put(advertisedContract, ild);
            }
            ild.addDescriptor(sd);
        }
        if (sd.getName() != null) {
            String name = sd.getName();
            IndexedListData ild = descriptorsByName.get(name);
            if (ild == null) {
                ild = new IndexedListData();
                descriptorsByName.put(name, ild);
            }
            ild.addDescriptor(sd);
        }
        if (sd.getAdvertisedContracts().contains(ValidationService.class.getName())) {
            ServiceHandle<ValidationService> handle = getServiceHandle((ActiveDescriptor<ValidationService>) sd);
            ValidationService vs = handle.getService();
            allValidators.add(vs);
        }
    }
    return thingsAdded;
}
Also used : ValidationService(org.glassfish.hk2.api.ValidationService) LinkedList(java.util.LinkedList)

Example 4 with ValidationService

use of org.glassfish.hk2.api.ValidationService 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

ValidationService (org.glassfish.hk2.api.ValidationService)4 LinkedList (java.util.LinkedList)2 MultiException (org.glassfish.hk2.api.MultiException)2 PerLookup (org.glassfish.hk2.api.PerLookup)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 DynamicConfiguration (org.glassfish.hk2.api.DynamicConfiguration)1 DynamicConfigurationListener (org.glassfish.hk2.api.DynamicConfigurationListener)1 DynamicConfigurationService (org.glassfish.hk2.api.DynamicConfigurationService)1 ErrorService (org.glassfish.hk2.api.ErrorService)1 Filter (org.glassfish.hk2.api.Filter)1 IndexedFilter (org.glassfish.hk2.api.IndexedFilter)1 Injectee (org.glassfish.hk2.api.Injectee)1 InjectionResolver (org.glassfish.hk2.api.InjectionResolver)1 InstanceLifecycleListener (org.glassfish.hk2.api.InstanceLifecycleListener)1 InterceptionService (org.glassfish.hk2.api.InterceptionService)1