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