/*
- * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
+ * Copyright (c) 2013, 2017 Cisco Systems, Inc. and others. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* by creating proxy that would throw exception if those methods are called
* during validation. Tracks dependencies for ordering purposes.
*/
-final class DependencyResolverImpl implements DependencyResolver,
- Comparable<DependencyResolverImpl> {
+public final class DependencyResolverImpl implements DependencyResolver, Comparable<DependencyResolverImpl> {
private static final Logger LOG = LoggerFactory.getLogger(DependencyResolverImpl.class);
private final ModulesHolder modulesHolder;
private final ServiceReferenceReadableRegistry readableRegistry;
private final BindingContextProvider bindingContextProvider;
private final String transactionName;
- private final MBeanServer mBeanServer;
+ private final MBeanServer beanServer;
private Integer maxDependencyDepth;
- DependencyResolverImpl(final ModuleIdentifier currentModule,
- final TransactionStatus transactionStatus, final ModulesHolder modulesHolder,
- final ServiceReferenceReadableRegistry readableRegistry, final BindingContextProvider bindingContextProvider,
- final String transactionName, final MBeanServer mBeanServer) {
+ public DependencyResolverImpl(final ModuleIdentifier currentModule, final TransactionStatus transactionStatus,
+ final ModulesHolder modulesHolder, final ServiceReferenceReadableRegistry readableRegistry,
+ final BindingContextProvider bindingContextProvider, final String transactionName,
+ final MBeanServer beanServer) {
this.bindingContextProvider = bindingContextProvider;
this.name = currentModule;
this.transactionStatus = transactionStatus;
this.modulesHolder = modulesHolder;
this.readableRegistry = readableRegistry;
this.transactionName = transactionName;
- this.mBeanServer = mBeanServer;
+ this.beanServer = beanServer;
}
/**
* {@inheritDoc}
*/
- //TODO: check for cycles
+ // TODO: check for cycles
@Override
- public void validateDependency(
- final Class<? extends AbstractServiceInterface> expectedServiceInterface,
+ public void validateDependency(final Class<? extends AbstractServiceInterface> expectedServiceInterface,
final ObjectName dependentReadOnlyON, final JmxAttribute jmxAttribute) {
this.transactionStatus.checkNotCommitted();
if (expectedServiceInterface == null) {
- throw new NullPointerException(
- "Parameter 'expectedServiceInterface' is null");
+ throw new NullPointerException("Parameter 'expectedServiceInterface' is null");
}
if (jmxAttribute == null) {
throw new NullPointerException("Parameter 'jmxAttribute' is null");
}
JmxAttributeValidationException.checkNotNull(dependentReadOnlyON,
- "is null, expected dependency implementing "
- + expectedServiceInterface, jmxAttribute
- );
-
+ "is null, expected dependency implementing " + expectedServiceInterface, jmxAttribute);
// check that objectName belongs to this transaction - this should be
// stripped
// in DynamicWritableWrapper
- final boolean hasTransaction = ObjectNameUtil
- .getTransactionName(dependentReadOnlyON) != null;
- JmxAttributeValidationException.checkCondition(
- !hasTransaction,
- String.format("ObjectName should not contain "
- + "transaction name. %s set to %s. ", jmxAttribute,
- dependentReadOnlyON
- ), jmxAttribute
- );
+ final boolean hasTransaction = ObjectNameUtil.getTransactionName(dependentReadOnlyON) != null;
+ JmxAttributeValidationException.checkCondition(!hasTransaction,
+ String.format("ObjectName should not contain " + "transaction name. %s set to %s. ", jmxAttribute,
+ dependentReadOnlyON),
+ jmxAttribute);
final ObjectName newDependentReadOnlyON = translateServiceRefIfPossible(dependentReadOnlyON);
- final ModuleIdentifier moduleIdentifier = ObjectNameUtil.fromON(newDependentReadOnlyON, ObjectNameUtil
- .TYPE_MODULE);
+ final ModuleIdentifier moduleIdentifier = ObjectNameUtil.fromON(newDependentReadOnlyON,
+ ObjectNameUtil.TYPE_MODULE);
final ModuleFactory foundFactory = this.modulesHolder.findModuleFactory(moduleIdentifier, jmxAttribute);
- final boolean implementsSI = foundFactory
- .isModuleImplementingServiceInterface(expectedServiceInterface);
+ final boolean implementsSI = foundFactory.isModuleImplementingServiceInterface(expectedServiceInterface);
if (!implementsSI) {
final String message = String.format(
"Found module factory does not expose expected service interface. "
+ "Module name is %s : %s, expected service interface %s, dependent module ON %s , "
+ "attribute %s",
- foundFactory.getImplementationName(), foundFactory,
- expectedServiceInterface, newDependentReadOnlyON,
- jmxAttribute
- );
+ foundFactory.getImplementationName(), foundFactory, expectedServiceInterface,
+ newDependentReadOnlyON, jmxAttribute);
throw new JmxAttributeValidationException(message, jmxAttribute);
}
synchronized (this) {
if (ObjectNameUtil.isServiceReference(translatedDependentReadOnlyON)) {
final String serviceQName = ObjectNameUtil.getServiceQName(translatedDependentReadOnlyON);
final String refName = ObjectNameUtil.getReferenceName(translatedDependentReadOnlyON);
- translatedDependentReadOnlyON = ObjectNameUtil.withoutTransactionName( // strip again of transaction name
+ // strip again of transaction name
+ translatedDependentReadOnlyON = ObjectNameUtil.withoutTransactionName(
this.readableRegistry.lookupConfigBeanByServiceInterfaceName(serviceQName, refName));
}
return translatedDependentReadOnlyON;
}
- //TODO: check for cycles
+ // TODO: check for cycles
@Override
public <T> T resolveInstance(final Class<T> expectedType, final ObjectName dependentReadOnlyON,
- final JmxAttribute jmxAttribute) {
+ final JmxAttribute jmxAttribute) {
final Module module = resolveModuleInstance(dependentReadOnlyON, jmxAttribute);
synchronized (this) {
final AutoCloseable instance = module.getInstance();
if (instance == null) {
final String message = String.format(
- "Error while %s resolving instance %s. getInstance() returned null. Expected type %s, attribute %s",
- this.name, module.getIdentifier(), expectedType, jmxAttribute);
+ "Error while %s resolving instance %s. getInstance() returned null. Expected type %s, attribute %s",
+ this.name, module.getIdentifier(), expectedType, jmxAttribute);
throw new JmxAttributeValidationException(message, jmxAttribute);
}
try {
return expectedType.cast(instance);
} catch (final ClassCastException e) {
final String message = String.format(
- "Instance cannot be cast to expected type. Instance class is %s, expected type %s , attribute %s",
- instance.getClass(), expectedType, jmxAttribute);
+ "Instance cannot be cast to expected type. Instance class is %s, expected type %s , attribute %s",
+ instance.getClass(), expectedType, jmxAttribute);
throw new JmxAttributeValidationException(message, e, jmxAttribute);
}
}
- private Module resolveModuleInstance(final ObjectName dependentReadOnlyON,
- final JmxAttribute jmxAttribute) {
- Preconditions.checkArgument(dependentReadOnlyON != null ,"dependentReadOnlyON");
+ private Module resolveModuleInstance(final ObjectName dependentReadOnlyON, final JmxAttribute jmxAttribute) {
+ Preconditions.checkArgument(dependentReadOnlyON != null, "dependentReadOnlyON");
Preconditions.checkArgument(jmxAttribute != null, "jmxAttribute");
final ObjectName translatedDependentReadOnlyON = translateServiceRefIfPossible(dependentReadOnlyON);
this.transactionStatus.checkCommitStarted();
this.transactionStatus.checkNotCommitted();
- final ModuleIdentifier dependentModuleIdentifier = ObjectNameUtil.fromON(
- translatedDependentReadOnlyON, ObjectNameUtil.TYPE_MODULE);
+ final ModuleIdentifier dependentModuleIdentifier = ObjectNameUtil.fromON(translatedDependentReadOnlyON,
+ ObjectNameUtil.TYPE_MODULE);
return Preconditions.checkNotNull(this.modulesHolder.findModule(dependentModuleIdentifier, jmxAttribute));
}
.findModuleInternalTransactionalInfo(identifier);
if (moduleInternalTransactionalInfo.hasOldModule()) {
- final Module oldModule = moduleInternalTransactionalInfo.getOldInternalInfo().getReadableModule().getModule();
+ final Module oldModule = moduleInternalTransactionalInfo.getOldInternalInfo().getReadableModule()
+ .getModule();
return currentModule.canReuse(oldModule);
}
return false;
public <T extends BaseIdentity> Class<? extends T> resolveIdentity(final IdentityAttributeRef identityRef,
final Class<T> expectedBaseClass) {
final QName qName = QName.create(identityRef.getqNameOfIdentity());
- final Class<?> deserialized = this.bindingContextProvider.getBindingContext().getIdentityClass(qName);
+ final Class<?> deserialized = this.bindingContextProvider.getBindingContext().getIdentityClass(qName);
if (deserialized == null) {
throw new IllegalStateException("Unable to retrieve identity class for " + qName + ", null response from "
+ this.bindingContextProvider.getBindingContext());
return (Class<T>) deserialized;
}
LOG.error("Cannot resolve class of identity {} : deserialized class {} is not a subclass of {}.", identityRef,
- deserialized, expectedBaseClass);
- throw new IllegalArgumentException("Deserialized identity " + deserialized + " cannot be cast to " + expectedBaseClass);
+ deserialized, expectedBaseClass);
+ throw new IllegalArgumentException(
+ "Deserialized identity " + deserialized + " cannot be cast to " + expectedBaseClass);
}
@Override
public <T extends BaseIdentity> void validateIdentity(final IdentityAttributeRef identityRef,
final Class<T> expectedBaseClass, final JmxAttribute jmxAttribute) {
- try {
- resolveIdentity(identityRef, expectedBaseClass);
- } catch (final Exception e) {
- throw JmxAttributeValidationException.wrap(e, jmxAttribute);
- }
+ resolveIdentity(identityRef, expectedBaseClass);
}
@Override
- public int compareTo(final DependencyResolverImpl o) {
+ public int compareTo(final DependencyResolverImpl dependencyResolverImpl) {
this.transactionStatus.checkCommitStarted();
- return Integer.compare(getMaxDependencyDepth(),
- o.getMaxDependencyDepth());
+ return Integer.compare(getMaxDependencyDepth(), dependencyResolverImpl.getMaxDependencyDepth());
}
int getMaxDependencyDepth() {
void countMaxDependencyDepth(final DependencyResolverManager manager) {
// We can calculate the dependency after second phase commit was started
- // Second phase commit starts after validation and validation adds the dependencies into the dependency resolver, which are necessary for the calculation
- // FIXME generated code for abstract module declares validate method as non-final
- // Overriding the validate would cause recreate every time instead of reuse + also possibly wrong close order if there is another module depending
+ // Second phase commit starts after validation and validation adds the
+ // dependencies into the dependency resolver, which are necessary for the
+ // calculation
+ // FIXME generated code for abstract module declares validate method as
+ // non-final
+ // Overriding the validate would cause recreate every time instead of reuse +
+ // also possibly wrong close order if there is another module depending
this.transactionStatus.checkCommitStarted();
if (this.maxDependencyDepth == null) {
- this.maxDependencyDepth = getMaxDepth(this, manager,
- new LinkedHashSet<>());
+ this.maxDependencyDepth = getMaxDepth(this, manager, new LinkedHashSet<>());
}
}
- private static int getMaxDepth(final DependencyResolverImpl impl,
- final DependencyResolverManager manager,
- final LinkedHashSet<ModuleIdentifier> chainForDetectingCycles) {
+ private static int getMaxDepth(final DependencyResolverImpl impl, final DependencyResolverManager manager,
+ final LinkedHashSet<ModuleIdentifier> chainForDetectingCycles) {
int maxDepth = 0;
- final LinkedHashSet<ModuleIdentifier> chainForDetectingCycles2 = new LinkedHashSet<>(
- chainForDetectingCycles);
+ final LinkedHashSet<ModuleIdentifier> chainForDetectingCycles2 = new LinkedHashSet<>(chainForDetectingCycles);
chainForDetectingCycles2.add(impl.getIdentifier());
for (final ModuleIdentifier dependencyName : impl.dependencies) {
- final DependencyResolverImpl dependentDRI = manager
- .getOrCreate(dependencyName);
+ final DependencyResolverImpl dependentDRI = manager.getOrCreate(dependencyName);
if (chainForDetectingCycles2.contains(dependencyName)) {
- throw new IllegalStateException(String.format(
- "Cycle detected, %s contains %s",
- chainForDetectingCycles2, dependencyName));
+ throw new IllegalStateException(
+ String.format("Cycle detected, %s contains %s", chainForDetectingCycles2, dependencyName));
}
int subDepth;
if (dependentDRI.maxDependencyDepth != null) {
subDepth = dependentDRI.maxDependencyDepth;
} else {
- subDepth = getMaxDepth(dependentDRI, manager,
- chainForDetectingCycles2);
+ subDepth = getMaxDepth(dependentDRI, manager, chainForDetectingCycles2);
dependentDRI.maxDependencyDepth = subDepth;
}
if (subDepth + 1 > maxDepth) {
ObjectName newName = translateServiceRefIfPossible(name);
// add transaction name
newName = ObjectNameUtil.withTransactionName(newName, this.transactionName);
- return this.mBeanServer.getAttribute(newName, attribute);
+ return this.beanServer.getAttribute(newName, attribute);
}
@Override
ObjectName newName = translateServiceRefIfPossible(name);
// add transaction name
newName = ObjectNameUtil.withTransactionName(newName, this.transactionName);
- return JMX.newMXBeanProxy(this.mBeanServer, newName, interfaceClass);
+ return JMX.newMXBeanProxy(this.beanServer, newName, interfaceClass);
}
}