Skip to content

Commit

Permalink
Consolidate resource registration logic.
Browse files Browse the repository at this point in the history
Allow add step handler to register attributes with appropriate write handler.
  • Loading branch information
pferraro committed Aug 13, 2015
1 parent 04475f8 commit 2713da2
Show file tree
Hide file tree
Showing 79 changed files with 790 additions and 863 deletions.
@@ -0,0 +1,64 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2015, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package org.jboss.as.clustering.controller;

import org.jboss.as.controller.PathElement;
import org.jboss.as.controller.SimpleResourceDefinition;
import org.jboss.as.controller.descriptions.ResourceDescriptionResolver;
import org.jboss.as.controller.registry.ManagementResourceRegistration;

/**
* Resource definition for resources that performs all registration via {@link Registration#register(Object)}.
* All other registerXXX(...) methods have been made final - to prevent misuse.
* @author Paul Ferraro
*/
public abstract class AbstractResourceDefinition<R> extends SimpleResourceDefinition implements Registration<R> {

protected AbstractResourceDefinition(PathElement path, ResourceDescriptionResolver resolver) {
super(new Parameters(path, resolver));
}

protected AbstractResourceDefinition(Parameters parameters) {
super(parameters);
}

@Override
public final void registerOperations(ManagementResourceRegistration resourceRegistration) {
}

@Override
public final void registerAttributes(ManagementResourceRegistration resourceRegistration) {
}

@Override
public final void registerNotifications(ManagementResourceRegistration resourceRegistration) {
}

@Override
public final void registerChildren(ManagementResourceRegistration resourceRegistration) {
}

@Override
public final void registerCapabilities(ManagementResourceRegistration resourceRegistration) {
}
}
Expand Up @@ -26,7 +26,9 @@
import org.jboss.as.controller.AttributeDefinition;
import org.jboss.as.controller.OperationContext;
import org.jboss.as.controller.OperationFailedException;
import org.jboss.as.controller.OperationStepHandler;
import org.jboss.as.controller.PathAddress;
import org.jboss.as.controller.ReloadRequiredWriteAttributeHandler;
import org.jboss.as.controller.SimpleOperationDefinitionBuilder;
import org.jboss.as.controller.descriptions.ModelDescriptionConstants;
import org.jboss.as.controller.registry.ManagementResourceRegistration;
Expand All @@ -38,19 +40,25 @@
* Generic add operation step handler that delegates service installation/rollback to a {@link ResourceServiceHandler}.
* @author Paul Ferraro
*/
public class AddStepHandler extends AbstractAddStepHandler implements Registration {
public class AddStepHandler extends AbstractAddStepHandler implements Registration<ManagementResourceRegistration> {

private final AddStepHandlerDescriptor descriptor;
private final ResourceServiceHandler handler;
private final OperationStepHandler writeAttributeHandler;

public AddStepHandler(AddStepHandlerDescriptor descriptor) {
this(descriptor, null);
}

public AddStepHandler(AddStepHandlerDescriptor descriptor, ResourceServiceHandler handler) {
this(descriptor, handler, new ReloadRequiredWriteAttributeHandler(descriptor.getAttributes()));
}

AddStepHandler(AddStepHandlerDescriptor descriptor, ResourceServiceHandler handler, OperationStepHandler writeAttributeHandler) {
super(descriptor.getAttributes());
this.descriptor = descriptor;
this.handler = handler;
this.writeAttributeHandler = writeAttributeHandler;
}

@Override
Expand Down Expand Up @@ -92,14 +100,12 @@ protected void recordCapabilitiesAndRequirements(OperationContext context, Model
@Override
public void register(ManagementResourceRegistration registration) {
SimpleOperationDefinitionBuilder builder = new SimpleOperationDefinitionBuilder(ModelDescriptionConstants.ADD, this.descriptor.getDescriptionResolver()).withFlag(OperationEntry.Flag.RESTART_NONE);
for (AttributeDefinition attribute : this.descriptor.getAttributes()) {
builder.addParameter(attribute);
}
for (AttributeDefinition parameter : this.descriptor.getExtraParameters()) {
builder.addParameter(parameter);
}
this.descriptor.getAttributes().forEach(attribute -> builder.addParameter(attribute));
this.descriptor.getExtraParameters().forEach(attribute -> builder.addParameter(attribute));
registration.registerOperationHandler(builder.build(), this);

this.descriptor.getAttributes().forEach(attribute -> registration.registerReadWriteAttribute(attribute, null, this.writeAttributeHandler));

new CapabilityRegistration(this.descriptor.getCapabilities()).register(registration);
}
}
Expand Up @@ -30,12 +30,7 @@
* Describes the common properties of a remove operation handler.
* @author Paul Ferraro
*/
public interface AddStepHandlerDescriptor extends RemoveStepHandlerDescriptor {
/**
* Attributes of the add operation.
* @return a collection of attributes
*/
Collection<AttributeDefinition> getAttributes();
public interface AddStepHandlerDescriptor extends WriteAttributeStepHandlerDescriptor, RemoveStepHandlerDescriptor {

/**
* Extra parameters (not specified by {@link #getAttributes()}) for the add operation.
Expand Down
Expand Up @@ -26,7 +26,9 @@
import org.jboss.as.controller.AttributeDefinition;
import org.jboss.as.controller.OperationContext;
import org.jboss.as.controller.OperationFailedException;
import org.jboss.as.controller.OperationStepHandler;
import org.jboss.as.controller.PathAddress;
import org.jboss.as.controller.ReloadRequiredWriteAttributeHandler;
import org.jboss.as.controller.SimpleOperationDefinitionBuilder;
import org.jboss.as.controller.descriptions.ModelDescriptionConstants;
import org.jboss.as.controller.registry.ManagementResourceRegistration;
Expand All @@ -38,7 +40,7 @@
* Generic boot-time add step handler that delegates service installation/rollback to a {@link ResourceServiceHandler}.
* @author Paul Ferraro
*/
public class BoottimeAddStepHandler extends AbstractBoottimeAddStepHandler implements Registration {
public class BoottimeAddStepHandler extends AbstractBoottimeAddStepHandler implements Registration<ManagementResourceRegistration> {

private final AddStepHandlerDescriptor descriptor;
private final ResourceServiceHandler handler;
Expand Down Expand Up @@ -92,6 +94,9 @@ public void register(ManagementResourceRegistration registration) {
}
registration.registerOperationHandler(builder.build(), this);

OperationStepHandler writeAttributeHandler = new ReloadRequiredWriteAttributeHandler(this.descriptor.getAttributes());
this.descriptor.getAttributes().forEach(attribute -> registration.registerReadWriteAttribute(attribute, null, writeAttributeHandler));

new CapabilityRegistration(this.descriptor.getCapabilities()).register(registration);
}
}
Expand Up @@ -32,7 +32,7 @@
* Registration facility for capabilities.
* @author Paul Ferraro
*/
public class CapabilityRegistration implements Registration {
public class CapabilityRegistration implements Registration<ManagementResourceRegistration> {

private final Collection<? extends Capability> capabilities;

Expand All @@ -48,13 +48,8 @@ public CapabilityRegistration(Collection<? extends Capability> capabilities) {
this.capabilities = capabilities;
}

/**
* {@inheritDoc}
*/
@Override
public void register(ManagementResourceRegistration registration) {
for (Capability capability : this.capabilities) {
registration.registerCapability(capability.getDefinition());
}
this.capabilities.forEach(capability -> registration.registerCapability(capability.getDefinition()));
}
}
@@ -0,0 +1,42 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2015, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package org.jboss.as.clustering.controller;

import org.jboss.as.controller.PathElement;
import org.jboss.as.controller.descriptions.ResourceDescriptionResolver;
import org.jboss.as.controller.registry.ManagementResourceRegistration;

/**
* Resource definition for child resources that performs all registration via {@link #register(ManagementResourceRegistration)}.
* @author Paul Ferraro
*/
public abstract class ChildResourceDefinition extends AbstractResourceDefinition<ManagementResourceRegistration> {

protected ChildResourceDefinition(PathElement path, ResourceDescriptionResolver resolver) {
super(new Parameters(path, resolver));
}

protected ChildResourceDefinition(Parameters parameters) {
super(parameters);
}
}
Expand Up @@ -23,6 +23,7 @@
package org.jboss.as.clustering.controller;

import java.util.Arrays;
import java.util.Collection;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -37,7 +38,7 @@
* Generic {@link org.jboss.as.controller.OperationStepHandler} for runtime metrics.
* @author Paul Ferraro
*/
public class MetricHandler<C> extends AbstractRuntimeOnlyHandler implements Registration {
public class MetricHandler<C> extends AbstractRuntimeOnlyHandler implements Registration<ManagementResourceRegistration> {

private final Map<String, Metric<C>> metrics = new HashMap<>();
private final MetricExecutor<C> executor;
Expand All @@ -50,18 +51,14 @@ public MetricHandler(MetricExecutor<C> executor, Metric<C>[] metrics) {
this(executor, Arrays.asList(metrics));
}

public MetricHandler(MetricExecutor<C> executor, Iterable<? extends Metric<C>> metrics) {
public MetricHandler(MetricExecutor<C> executor, Collection<? extends Metric<C>> metrics) {
this.executor = executor;
for (Metric<C> metric : metrics) {
this.metrics.put(metric.getDefinition().getName(), metric);
}
metrics.forEach(metric -> this.metrics.put(metric.getDefinition().getName(), metric));
}

@Override
public void register(ManagementResourceRegistration registration) {
for (Metric<C> metric : this.metrics.values()) {
registration.registerReadOnlyAttribute(metric.getDefinition(), this);
}
this.metrics.values().forEach(metric -> registration.registerReadOnlyAttribute(metric.getDefinition(), this));
}

@Override
Expand Down
Expand Up @@ -23,6 +23,7 @@
package org.jboss.as.clustering.controller;

import java.util.Arrays;
import java.util.Collection;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -37,7 +38,7 @@
* Generic {@link org.jboss.as.controller.OperationStepHandler} for runtime operations.
* @author Paul Ferraro
*/
public class OperationHandler<C> extends AbstractRuntimeOnlyHandler implements Registration {
public class OperationHandler<C> extends AbstractRuntimeOnlyHandler implements Registration<ManagementResourceRegistration> {

private final Map<String, Operation<C>> operations = new HashMap<>();
private final OperationExecutor<C> executor;
Expand All @@ -50,18 +51,14 @@ public OperationHandler(OperationExecutor<C> executor, Operation<C>[] operations
this(executor, Arrays.asList(operations));
}

public OperationHandler(OperationExecutor<C> executor, Iterable<? extends Operation<C>> operations) {
public OperationHandler(OperationExecutor<C> executor, Collection<? extends Operation<C>> operations) {
this.executor = executor;
for (Operation<C> operation : operations) {
this.operations.put(operation.getDefinition().getName(), operation);
}
operations.forEach(operation -> this.operations.put(operation.getDefinition().getName(), operation));
}

@Override
public void register(ManagementResourceRegistration registration) {
for (Operation<C> operation : this.operations.values()) {
registration.registerOperationHandler(operation.getDefinition(), this);
}
this.operations.values().forEach(operation -> registration.registerOperationHandler(operation.getDefinition(), this));
}

@Override
Expand Down
Expand Up @@ -22,17 +22,15 @@

package org.jboss.as.clustering.controller;

import org.jboss.as.controller.registry.ManagementResourceRegistration;

/**
* Implemented by a resource artifact that can register itself.
* This allows a resource to encapsulates specific registration details (e.g. resource aliases) from the parent resource.
* Implemented by a management artifact that can register itself.
* This allows a management object to encapsulates specific registration details (e.g. resource aliases) from the parent resource.
* @author Paul Ferraro
*/
public interface Registration {
public interface Registration<R> {
/**
* Registers this object with a resource.
* @param registration a registration for a management resource
*/
void register(ManagementResourceRegistration registration);
void register(R registration);
}
Expand Up @@ -22,46 +22,23 @@

package org.jboss.as.clustering.controller;

import java.util.Arrays;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;

import org.jboss.as.controller.AttributeDefinition;
import org.jboss.as.controller.registry.ManagementResourceRegistration;

/**
* Convenience extension of {@link org.jboss.as.controller.ReloadRequiredWriteAttributeHandler} that can be initialized with an {@link Attribute} set.
* @author Paul Ferraro
*/
public class ReloadRequiredWriteAttributeHandler extends org.jboss.as.controller.ReloadRequiredWriteAttributeHandler implements Registration {

private final Map<String, AttributeDefinition> attributes = new HashMap<>();

public <E extends Enum<E> & Attribute> ReloadRequiredWriteAttributeHandler(Class<E> enumClass) {
this(EnumSet.allOf(enumClass));
}
public class ReloadRequiredWriteAttributeHandler extends org.jboss.as.controller.ReloadRequiredWriteAttributeHandler implements Registration<ManagementResourceRegistration> {

public ReloadRequiredWriteAttributeHandler(Attribute... attributes) {
this(Arrays.asList(attributes));
}

public ReloadRequiredWriteAttributeHandler(Iterable<? extends Attribute> attributes) {
for (Attribute attribute : attributes) {
AttributeDefinition definition = attribute.getDefinition();
this.attributes.put(definition.getName(), definition);
}
}
private final WriteAttributeStepHandlerDescriptor descriptor;

@Override
protected AttributeDefinition getAttributeDefinition(String name) {
return this.attributes.get(name);
public ReloadRequiredWriteAttributeHandler(WriteAttributeStepHandlerDescriptor descriptor) {
super(descriptor.getAttributes());
this.descriptor = descriptor;
}

@Override
public void register(ManagementResourceRegistration registration) {
for (AttributeDefinition attribute : this.attributes.values()) {
registration.registerReadWriteAttribute(attribute, null, this);
}
this.descriptor.getAttributes().forEach(attribute -> registration.registerReadWriteAttribute(attribute, null, this));
}
}
Expand Up @@ -37,7 +37,7 @@
* Generic remove operation step handler that delegates service removal/recovery to a dedicated {@link ResourceServiceHandler}.
* @author Paul Ferraro
*/
public class RemoveStepHandler extends AbstractRemoveStepHandler implements Registration {
public class RemoveStepHandler extends AbstractRemoveStepHandler implements Registration<ManagementResourceRegistration> {

private final RemoveStepHandlerDescriptor descriptor;
private final ResourceServiceHandler handler;
Expand Down

0 comments on commit 2713da2

Please sign in to comment.