Skip to content

Commit

Permalink
Clean up IronJacamarActivationResourceService
Browse files Browse the repository at this point in the history
1) Drop null checks where other code proves the ref can't be null.
2) Drop redundant if tests
3) Avoid repeated calculation of various fixed path elements and addresses
4) Avoid repeated calls to non-trivial getters; store and reuse
5) Make fields only used in the class private.
6) Clarify some submodel lookups by using a wildcard PathAddress since the expected returned MRR is the wildcard one
7) Save code space of multi OverrideDescriptionProvider impls when they are all the same.
  • Loading branch information
bstansberry committed Dec 11, 2017
1 parent 5344e95 commit ed25abc
Showing 1 changed file with 88 additions and 122 deletions.
Expand Up @@ -52,6 +52,7 @@
import org.jboss.jca.core.api.management.AdminObject;
import org.jboss.jca.core.api.management.ConnectionFactory;
import org.jboss.jca.core.api.management.Connector;
import org.jboss.jca.core.api.management.ManagedConnectionFactory;
import org.jboss.jca.core.connectionmanager.ConnectionManager;
import org.jboss.jca.core.spi.statistics.StatisticsPlugin;
import org.jboss.jca.deployers.common.CommonDeployment;
Expand All @@ -62,26 +63,31 @@
import org.jboss.msc.service.StopContext;
import org.jboss.msc.value.InjectedValue;

public class IronJacamarActivationResourceService implements Service<ManagementResourceRegistration> {
/**
* Registers the {@link ManagementResourceRegistration} and {@link Resource} for displaying various
* statistics reported by a {@link ResourceAdapterDeployment}. The available statistics depend on
* the deployment implementation, so they cannot be known/registered before the deployment is processed.
*/
public final class IronJacamarActivationResourceService implements Service<ManagementResourceRegistration> {

private static PathElement SUBSYSTEM_PATH_ELEMENT = PathElement.pathElement(ModelDescriptionConstants.SUBSYSTEM, ResourceAdaptersExtension.SUBSYSTEM_NAME);
private static PathAddress RA_ADDRESS = PathAddress.pathAddress(SUBSYSTEM_PATH_ELEMENT,
PathElement.pathElement(Constants.IRONJACAMAR_NAME, Constants.IRONJACAMAR_NAME),
PathElement.pathElement(Constants.RESOURCEADAPTER_NAME));

private final ManagementResourceRegistration registration;
private final Resource deploymentResource;
private final boolean statsEnabled;

protected final InjectedValue<ResourceAdapterDeployment> deployment = new InjectedValue<>();
private final InjectedValue<ResourceAdapterDeployment> deployment = new InjectedValue<>();

protected final InjectedValue<AS7MetadataRepository> mdr = new InjectedValue<AS7MetadataRepository>();
private final InjectedValue<AS7MetadataRepository> mdr = new InjectedValue<>();

/**
* create an instance *
*/
public IronJacamarActivationResourceService(final ManagementResourceRegistration registration, final Resource deploymentResource,
final boolean statsEnabled) {
super();
this.registration = registration;
this.deploymentResource = deploymentResource;
this.statsEnabled = statsEnabled;
Expand All @@ -91,148 +97,110 @@ public IronJacamarActivationResourceService(final ManagementResourceRegistration
@Override
public void start(StartContext context) throws StartException {
final CommonDeployment deploymentMD = deployment.getValue().getDeployment();
ROOT_LOGGER.debugf("Starting IronJacamarActivationResourceService %s", deploymentMD.getDeploymentName());

final String deploymentName = deploymentMD.getDeploymentName();
ROOT_LOGGER.debugf("Starting IronJacamarActivationResourceService %s", deploymentName);

try {
if (deploymentResource != null) {
Connector connector = deploymentMD.getConnector();
if (connector != null && connector.getResourceAdapter() != null) {

PathElement peLocalStats = PathElement.pathElement(Constants.STATISTICS_NAME, "extended");
final OverrideDescriptionProvider OD_PROVIDER = new OverrideDescriptionProvider() {
@Override
public Map<String, ModelNode> getAttributeOverrideDescriptions(Locale locale) {
return Collections.emptyMap();
}

if (deploymentMD.getConnector() != null && deploymentMD.getConnector().getResourceAdapter() != null) {
ManagementResourceRegistration raRegistration = registration.
getSubModel(RA_ADDRESS)
.registerOverrideModel(deploymentMD.getDeploymentName(), new OverrideDescriptionProvider() {
@Override
public Map<String, ModelNode> getAttributeOverrideDescriptions(Locale locale) {
return Collections.emptyMap();
}
@Override
public Map<String, ModelNode> getChildTypeOverrideDescriptions(Locale locale) {
return Collections.emptyMap();
}

@Override
public Map<String, ModelNode> getChildTypeOverrideDescriptions(Locale locale) {
return Collections.emptyMap();
}
};

});
final PathElement EXTENDED_STATS = PathElement.pathElement(Constants.STATISTICS_NAME, "extended");
final PathAddress EXTENDED_STATS_ADDR = PathAddress.pathAddress(EXTENDED_STATS);
final PathAddress CON_DEF_ADDR = PathAddress.EMPTY_ADDRESS.append(Constants.CONNECTIONDEFINITIONS_NAME);

ResourceBuilder resourceBuilder = ResourceBuilder.Factory.create(peLocalStats,
new StandardResourceDescriptionResolver(Constants.STATISTICS_NAME + "." + Constants.WORKMANAGER_NAME, CommonAttributes.RESOURCE_NAME, CommonAttributes.class.getClassLoader()));
ManagementResourceRegistration raRegistration =
registration.getSubModel(RA_ADDRESS).registerOverrideModel(deploymentName, OD_PROVIDER);

ManagementResourceRegistration raStatsSubRegistration = raRegistration.registerSubModel(resourceBuilder.build());
ResourceBuilder resourceBuilder = ResourceBuilder.Factory.create(EXTENDED_STATS,
new StandardResourceDescriptionResolver(Constants.STATISTICS_NAME + "." + Constants.WORKMANAGER_NAME, CommonAttributes.RESOURCE_NAME, CommonAttributes.class.getClassLoader()));

ManagementResourceRegistration raStatsSubRegistration = raRegistration.registerSubModel(resourceBuilder.build());

if (deploymentMD.getConnector().getResourceAdapter().getStatistics() != null) {
StatisticsPlugin raStats = deploymentMD.getConnector().getResourceAdapter().getStatistics();
raStats.setEnabled(statsEnabled);
PoolMetrics.ParametrizedPoolMetricsHandler handler = new PoolMetrics.ParametrizedPoolMetricsHandler(raStats);
for (AttributeDefinition attribute : StatisticsResourceDefinition.getAttributesFromPlugin(raStats)) {
raStatsSubRegistration.registerMetric(attribute, handler);
}
raStatsSubRegistration.registerOperationHandler(ClearStatisticsHandler.DEFINITION, new ClearStatisticsHandler(raStats));

StatisticsPlugin raStats = connector.getResourceAdapter().getStatistics();
if (raStats != null) {
raStats.setEnabled(statsEnabled);
PoolMetrics.ParametrizedPoolMetricsHandler handler = new PoolMetrics.ParametrizedPoolMetricsHandler(raStats);
for (AttributeDefinition attribute : StatisticsResourceDefinition.getAttributesFromPlugin(raStats)) {
raStatsSubRegistration.registerMetric(attribute, handler);
}
if (deploymentMD.getConnector() != null && deploymentMD.getConnector().getConnectionFactories() != null) {
for (ConnectionFactory cf : deploymentMD.getConnector().getConnectionFactories()) {
if (cf.getManagedConnectionFactory() != null && cf.getManagedConnectionFactory().getStatistics() != null) {
PathElement peCD = PathElement.pathElement(Constants.CONNECTIONDEFINITIONS_NAME, cf.getJndiName());
PathElement peCdStats = PathElement.pathElement(Constants.STATISTICS_NAME, "extended");
StatisticsPlugin extendStats = cf.getManagedConnectionFactory().getStatistics();
extendStats.setEnabled(statsEnabled);
if (extendStats.getNames().size() != 0) {


if (extendStats.getNames().size() != 0) {
ManagementResourceRegistration cdRegistration = raRegistration.getSubModel(PathAddress.pathAddress(peCD));
ManagementResourceRegistration overrideCdRegistration = cdRegistration.registerOverrideModel(cf.getJndiName(), new OverrideDescriptionProvider() {
@Override
public Map<String, ModelNode> getAttributeOverrideDescriptions(Locale locale) {
return Collections.emptyMap();
}

@Override
public Map<String, ModelNode> getChildTypeOverrideDescriptions(Locale locale) {
return Collections.emptyMap();
}

});
if (extendStats.getNames().size() != 0 && overrideCdRegistration.getSubModel(PathAddress.pathAddress(peCdStats)) == null) {
overrideCdRegistration.registerSubModel(new StatisticsResourceDefinition(peCdStats, CommonAttributes.RESOURCE_NAME, extendStats));
}


}
raStatsSubRegistration.registerOperationHandler(ClearStatisticsHandler.DEFINITION, new ClearStatisticsHandler(raStats));
}

List<ConnectionFactory> connectionFactories = connector.getConnectionFactories();
if (connectionFactories != null) {
for (ConnectionFactory cf : connectionFactories) {
ManagedConnectionFactory mcf = cf.getManagedConnectionFactory();
StatisticsPlugin extendStats = mcf == null ? null : mcf.getStatistics();
if (extendStats != null) {
extendStats.setEnabled(statsEnabled);
if (extendStats.getNames().size() != 0) {

ManagementResourceRegistration cdRegistration = raRegistration.getSubModel(CON_DEF_ADDR);
ManagementResourceRegistration overrideCdRegistration =
cdRegistration.registerOverrideModel(cf.getJndiName(), OD_PROVIDER);
if (overrideCdRegistration.getSubModel(EXTENDED_STATS_ADDR) == null) {
overrideCdRegistration.registerSubModel(new StatisticsResourceDefinition(EXTENDED_STATS, CommonAttributes.RESOURCE_NAME, extendStats));
}
}
}
}
}

if (deploymentMD.getConnectionManagers() != null) {
for (ConnectionManager cm : deploymentMD.getConnectionManagers()) {
if (cm.getPool() != null) {
PathElement peCD = PathElement.pathElement(Constants.CONNECTIONDEFINITIONS_NAME, cm.getJndiName());
PathElement peCdStats = PathElement.pathElement(Constants.STATISTICS_NAME, "pool");
StatisticsPlugin poolStats = cm.getPool().getStatistics();
poolStats.setEnabled(statsEnabled);

if (poolStats.getNames().size() != 0) {
ManagementResourceRegistration cdRegistration = raRegistration.getSubModel(PathAddress.pathAddress(peCD));
ManagementResourceRegistration overrideCdRegistration = cdRegistration.registerOverrideModel(cm.getJndiName(), new OverrideDescriptionProvider() {
@Override
public Map<String, ModelNode> getAttributeOverrideDescriptions(Locale locale) {
return Collections.emptyMap();
}

@Override
public Map<String, ModelNode> getChildTypeOverrideDescriptions(Locale locale) {
return Collections.emptyMap();
}

});
if (poolStats.getNames().size() != 0 && overrideCdRegistration.getSubModel(PathAddress.pathAddress(peCdStats)) == null) {
overrideCdRegistration.registerSubModel(new StatisticsResourceDefinition(peCdStats, CommonAttributes.RESOURCE_NAME, poolStats));
}


ConnectionManager[] connectionManagers = deploymentMD.getConnectionManagers();
if (connectionManagers != null) {
PathElement POOL_STATS = PathElement.pathElement(Constants.STATISTICS_NAME, "pool");
PathAddress POOL_STATS_ADDR = PathAddress.pathAddress(POOL_STATS);
for (ConnectionManager cm : connectionManagers) {
if (cm.getPool() != null) {
StatisticsPlugin poolStats = cm.getPool().getStatistics();
poolStats.setEnabled(statsEnabled);

if (poolStats.getNames().size() != 0) {
ManagementResourceRegistration cdRegistration = raRegistration.getSubModel(CON_DEF_ADDR);
ManagementResourceRegistration overrideCdRegistration =
cdRegistration.registerOverrideModel(cm.getJndiName(), OD_PROVIDER);
if (overrideCdRegistration.getSubModel(POOL_STATS_ADDR) == null) {
overrideCdRegistration.registerSubModel(new StatisticsResourceDefinition(POOL_STATS, CommonAttributes.RESOURCE_NAME, poolStats));
}
}
}
}
}

if (deploymentMD.getConnector() != null && deploymentMD.getConnector().getAdminObjects() != null) {
for (AdminObject ao : deploymentMD.getConnector().getAdminObjects()) {
if (ao.getStatistics() != null) {
PathElement peCD = PathElement.pathElement(Constants.ADMIN_OBJECTS_NAME, ao.getJndiName());
PathElement peCdStats = PathElement.pathElement(Constants.STATISTICS_NAME, "extended");
StatisticsPlugin extendStats = ao.getStatistics();
extendStats.setEnabled(statsEnabled);
if (extendStats.getNames().size() != 0) {


if (extendStats.getNames().size() != 0) {
ManagementResourceRegistration cdRegistration = raRegistration.getSubModel(PathAddress.pathAddress(peCD));
ManagementResourceRegistration overrideCdRegistration = cdRegistration.registerOverrideModel(ao.getJndiName(), new OverrideDescriptionProvider() {
@Override
public Map<String, ModelNode> getAttributeOverrideDescriptions(Locale locale) {
return Collections.emptyMap();
}

@Override
public Map<String, ModelNode> getChildTypeOverrideDescriptions(Locale locale) {
return Collections.emptyMap();
}

});
if (extendStats.getNames().size() != 0 && overrideCdRegistration.getSubModel(PathAddress.pathAddress(peCdStats)) == null) {
overrideCdRegistration.registerSubModel(new StatisticsResourceDefinition(peCdStats, CommonAttributes.RESOURCE_NAME, extendStats));
}


}
List<AdminObject> adminObjects = connector.getAdminObjects();
if (adminObjects != null) {
PathAddress AO_ADDR = PathAddress.EMPTY_ADDRESS.append(Constants.ADMIN_OBJECTS_NAME);
for (AdminObject ao : adminObjects) {
StatisticsPlugin extendStats = ao.getStatistics();
if (extendStats != null) {
extendStats.setEnabled(statsEnabled);
if (extendStats.getNames().size() != 0) {

ManagementResourceRegistration cdRegistration = raRegistration.getSubModel(AO_ADDR);
ManagementResourceRegistration overrideCdRegistration =
cdRegistration.registerOverrideModel(ao.getJndiName(), OD_PROVIDER);
if (overrideCdRegistration.getSubModel(EXTENDED_STATS_ADDR) == null) {
overrideCdRegistration.registerSubModel(new StatisticsResourceDefinition(EXTENDED_STATS, CommonAttributes.RESOURCE_NAME, extendStats));
}
}
}
}

}

}
} catch (IllegalArgumentException e) {
//ignore it, already restered
Expand All @@ -246,10 +214,8 @@ public Map<String, ModelNode> getChildTypeOverrideDescriptions(Locale locale) {
subsystemResource = deploymentResource.getChild(SUBSYSTEM_PATH_ELEMENT);
}


IronJacamarResourceCreator.INSTANCE.execute(subsystemResource, mdr.getValue(), deployment.getValue().getRaName());


}

@Override
Expand Down

0 comments on commit ed25abc

Please sign in to comment.