From a44cf96fd070186f6689e4223c95b21ef4c29570 Mon Sep 17 00:00:00 2001 From: Paul Ferraro Date: Sun, 17 Nov 2024 15:36:39 +0000 Subject: [PATCH] Migrate discovery subsystem to SubsystemResourceXMLSchema. --- ...eDiscoveryProviderResourceDescription.java | 53 ++++++++ ...teDiscoveryProviderResourceRegistrar.java} | 20 +-- .../discovery/DiscoveryExtension.java | 2 +- ...overyExtensionTransformerRegistration.java | 2 +- .../DiscoveryProviderResourceDescription.java | 17 +++ ...> DiscoveryProviderResourceRegistrar.java} | 33 +++-- ...DiscoverySubsystemResourceDescription.java | 20 +++ ... DiscoverySubsystemResourceRegistrar.java} | 14 +-- .../discovery/DiscoverySubsystemSchema.java | 21 ++-- ...cDiscoveryProviderResourceDescription.java | 115 ++++++++++++++++++ ...icDiscoveryProviderResourceRegistrar.java} | 11 +- .../discovery/DiscoverySubsystemTestCase.java | 2 +- 12 files changed, 254 insertions(+), 56 deletions(-) create mode 100644 discovery/src/main/java/org/wildfly/extension/discovery/AggregateDiscoveryProviderResourceDescription.java rename discovery/src/main/java/org/wildfly/extension/discovery/{AggregateDiscoveryProviderRegistrar.java => AggregateDiscoveryProviderResourceRegistrar.java} (50%) create mode 100644 discovery/src/main/java/org/wildfly/extension/discovery/DiscoveryProviderResourceDescription.java rename discovery/src/main/java/org/wildfly/extension/discovery/{DiscoveryProviderRegistrar.java => DiscoveryProviderResourceRegistrar.java} (57%) create mode 100644 discovery/src/main/java/org/wildfly/extension/discovery/DiscoverySubsystemResourceDescription.java rename discovery/src/main/java/org/wildfly/extension/discovery/{DiscoverySubsystemRegistrar.java => DiscoverySubsystemResourceRegistrar.java} (66%) create mode 100644 discovery/src/main/java/org/wildfly/extension/discovery/StaticDiscoveryProviderResourceDescription.java rename discovery/src/main/java/org/wildfly/extension/discovery/{StaticDiscoveryProviderRegistrar.java => StaticDiscoveryProviderResourceRegistrar.java} (91%) diff --git a/discovery/src/main/java/org/wildfly/extension/discovery/AggregateDiscoveryProviderResourceDescription.java b/discovery/src/main/java/org/wildfly/extension/discovery/AggregateDiscoveryProviderResourceDescription.java new file mode 100644 index 00000000000..b0e87f18756 --- /dev/null +++ b/discovery/src/main/java/org/wildfly/extension/discovery/AggregateDiscoveryProviderResourceDescription.java @@ -0,0 +1,53 @@ +/* + * Copyright The WildFly Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.wildfly.extension.discovery; + +import java.util.List; +import java.util.function.Function; +import java.util.stream.Stream; + +import org.jboss.as.controller.AttributeDefinition; +import org.jboss.as.controller.OperationContext; +import org.jboss.as.controller.OperationFailedException; +import org.jboss.as.controller.PathElement; +import org.jboss.dmr.ModelNode; +import org.wildfly.discovery.impl.AggregateDiscoveryProvider; +import org.wildfly.discovery.spi.DiscoveryProvider; +import org.wildfly.subsystem.resource.capability.CapabilityReference; +import org.wildfly.subsystem.resource.capability.CapabilityReferenceListAttributeDefinition; +import org.wildfly.subsystem.service.ServiceDependency; + +/** + * Describes an aggregate discovery provider resource. + * @author Paul Ferraro + */ +public enum AggregateDiscoveryProviderResourceDescription implements DiscoveryProviderResourceDescription { + INSTANCE; + + static final CapabilityReferenceListAttributeDefinition PROVIDER_NAMES = new CapabilityReferenceListAttributeDefinition.Builder<>("providers", CapabilityReference.builder(DiscoveryProviderResourceRegistrar.DISCOVERY_PROVIDER_CAPABILITY, DiscoveryProviderResourceRegistrar.DISCOVERY_PROVIDER_DESCRIPTOR).build()).build(); + + private final PathElement path = PathElement.pathElement("aggregate-provider"); + + @Override + public PathElement getPathElement() { + return this.path; + } + + @Override + public Stream getAttributes() { + return Stream.of(PROVIDER_NAMES); + } + + @Override + public ServiceDependency resolve(OperationContext context, ModelNode model) throws OperationFailedException { + return AggregateDiscoveryProviderResourceDescription.PROVIDER_NAMES.resolve(context, model).map(new Function<>() { + @Override + public DiscoveryProvider apply(List providers) { + return new AggregateDiscoveryProvider(providers.toArray(DiscoveryProvider[]::new)); + } + }); + } +} diff --git a/discovery/src/main/java/org/wildfly/extension/discovery/AggregateDiscoveryProviderRegistrar.java b/discovery/src/main/java/org/wildfly/extension/discovery/AggregateDiscoveryProviderResourceRegistrar.java similarity index 50% rename from discovery/src/main/java/org/wildfly/extension/discovery/AggregateDiscoveryProviderRegistrar.java rename to discovery/src/main/java/org/wildfly/extension/discovery/AggregateDiscoveryProviderResourceRegistrar.java index cb4b953d96b..d4e223bb333 100644 --- a/discovery/src/main/java/org/wildfly/extension/discovery/AggregateDiscoveryProviderRegistrar.java +++ b/discovery/src/main/java/org/wildfly/extension/discovery/AggregateDiscoveryProviderResourceRegistrar.java @@ -4,20 +4,14 @@ */ package org.wildfly.extension.discovery; -import java.util.Collection; import java.util.List; import java.util.function.Function; -import org.jboss.as.controller.AttributeDefinition; import org.jboss.as.controller.OperationContext; import org.jboss.as.controller.OperationFailedException; -import org.jboss.as.controller.PathElement; import org.jboss.dmr.ModelNode; import org.wildfly.discovery.impl.AggregateDiscoveryProvider; import org.wildfly.discovery.spi.DiscoveryProvider; -import org.wildfly.subsystem.resource.ResourceDescriptor; -import org.wildfly.subsystem.resource.capability.CapabilityReference; -import org.wildfly.subsystem.resource.capability.CapabilityReferenceListAttributeDefinition; import org.wildfly.subsystem.service.ResourceServiceInstaller; import org.wildfly.subsystem.service.ServiceDependency; import org.wildfly.subsystem.service.capability.CapabilityServiceInstaller; @@ -26,21 +20,15 @@ * Registers the aggregate discovery provider resource definition. * @author Paul Ferraro */ -public class AggregateDiscoveryProviderRegistrar extends DiscoveryProviderRegistrar { +public class AggregateDiscoveryProviderResourceRegistrar extends DiscoveryProviderResourceRegistrar { - static final PathElement PATH = PathElement.pathElement("aggregate-provider"); - - private static final CapabilityReferenceListAttributeDefinition PROVIDER_NAMES = new CapabilityReferenceListAttributeDefinition.Builder<>("providers", CapabilityReference.builder(DISCOVERY_PROVIDER_CAPABILITY, DISCOVERY_PROVIDER_DESCRIPTOR).build()).build(); - - static final Collection ATTRIBUTES = List.of(PROVIDER_NAMES); - - AggregateDiscoveryProviderRegistrar() { - super(PATH, ResourceDescriptor.builder(DiscoverySubsystemRegistrar.RESOLVER.createChildResolver(PATH)).addAttributes(ATTRIBUTES)); + AggregateDiscoveryProviderResourceRegistrar() { + super(AggregateDiscoveryProviderResourceDescription.INSTANCE); } @Override public ResourceServiceInstaller configure(OperationContext context, ModelNode model) throws OperationFailedException { - ServiceDependency provider = PROVIDER_NAMES.resolve(context, model).map(new Function<>() { + ServiceDependency provider = AggregateDiscoveryProviderResourceDescription.PROVIDER_NAMES.resolve(context, model).map(new Function<>() { @Override public DiscoveryProvider apply(List providers) { return new AggregateDiscoveryProvider(providers.toArray(DiscoveryProvider[]::new)); diff --git a/discovery/src/main/java/org/wildfly/extension/discovery/DiscoveryExtension.java b/discovery/src/main/java/org/wildfly/extension/discovery/DiscoveryExtension.java index 5763fc74bed..209634504f6 100644 --- a/discovery/src/main/java/org/wildfly/extension/discovery/DiscoveryExtension.java +++ b/discovery/src/main/java/org/wildfly/extension/discovery/DiscoveryExtension.java @@ -17,6 +17,6 @@ public class DiscoveryExtension extends SubsystemExtension { public DiscoveryExtension() { - super(SubsystemConfiguration.of(DiscoverySubsystemRegistrar.NAME, DiscoverySubsystemModel.CURRENT, DiscoverySubsystemRegistrar::new), SubsystemPersistence.of(DiscoverySubsystemSchema.CURRENT)); + super(SubsystemConfiguration.of(DiscoverySubsystemResourceDescription.INSTANCE, DiscoverySubsystemModel.CURRENT, DiscoverySubsystemResourceRegistrar::new), SubsystemPersistence.of(DiscoverySubsystemSchema.CURRENT)); } } diff --git a/discovery/src/main/java/org/wildfly/extension/discovery/DiscoveryExtensionTransformerRegistration.java b/discovery/src/main/java/org/wildfly/extension/discovery/DiscoveryExtensionTransformerRegistration.java index 35cd52d521c..0f086e34916 100644 --- a/discovery/src/main/java/org/wildfly/extension/discovery/DiscoveryExtensionTransformerRegistration.java +++ b/discovery/src/main/java/org/wildfly/extension/discovery/DiscoveryExtensionTransformerRegistration.java @@ -14,6 +14,6 @@ public class DiscoveryExtensionTransformerRegistration extends SubsystemExtensionTransformerRegistration { public DiscoveryExtensionTransformerRegistration() { - super(DiscoverySubsystemRegistrar.NAME, DiscoverySubsystemModel.CURRENT, DiscoverySubsystemTransformationDescriptionFactory.INSTANCE); + super(DiscoverySubsystemResourceDescription.INSTANCE, DiscoverySubsystemModel.CURRENT, DiscoverySubsystemTransformationDescriptionFactory.INSTANCE); } } diff --git a/discovery/src/main/java/org/wildfly/extension/discovery/DiscoveryProviderResourceDescription.java b/discovery/src/main/java/org/wildfly/extension/discovery/DiscoveryProviderResourceDescription.java new file mode 100644 index 00000000000..d4ebddd9fe1 --- /dev/null +++ b/discovery/src/main/java/org/wildfly/extension/discovery/DiscoveryProviderResourceDescription.java @@ -0,0 +1,17 @@ +/* + * Copyright The WildFly Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.wildfly.extension.discovery; + +import org.jboss.as.controller.ResourceDescription; +import org.wildfly.discovery.spi.DiscoveryProvider; +import org.wildfly.subsystem.resource.ResourceModelResolver; +import org.wildfly.subsystem.service.ServiceDependency; + +/** + * Describes a discovery provider resource + */ +public interface DiscoveryProviderResourceDescription extends ResourceDescription, ResourceModelResolver> { +} diff --git a/discovery/src/main/java/org/wildfly/extension/discovery/DiscoveryProviderRegistrar.java b/discovery/src/main/java/org/wildfly/extension/discovery/DiscoveryProviderResourceRegistrar.java similarity index 57% rename from discovery/src/main/java/org/wildfly/extension/discovery/DiscoveryProviderRegistrar.java rename to discovery/src/main/java/org/wildfly/extension/discovery/DiscoveryProviderResourceRegistrar.java index 5507d98384a..257ee31d209 100644 --- a/discovery/src/main/java/org/wildfly/extension/discovery/DiscoveryProviderRegistrar.java +++ b/discovery/src/main/java/org/wildfly/extension/discovery/DiscoveryProviderResourceRegistrar.java @@ -4,11 +4,12 @@ */ package org.wildfly.extension.discovery; -import org.jboss.as.controller.PathElement; +import org.jboss.as.controller.OperationContext; +import org.jboss.as.controller.OperationFailedException; import org.jboss.as.controller.ResourceDefinition; -import org.jboss.as.controller.ResourceRegistration; import org.jboss.as.controller.capability.RuntimeCapability; import org.jboss.as.controller.registry.ManagementResourceRegistration; +import org.jboss.dmr.ModelNode; import org.wildfly.discovery.spi.DiscoveryProvider; import org.wildfly.service.descriptor.UnaryServiceDescriptor; import org.wildfly.subsystem.resource.ChildResourceDefinitionRegistrar; @@ -17,34 +18,42 @@ import org.wildfly.subsystem.resource.ResourceDescriptor; import org.wildfly.subsystem.resource.operation.ResourceOperationRuntimeHandler; import org.wildfly.subsystem.service.ResourceServiceConfigurator; +import org.wildfly.subsystem.service.ResourceServiceInstaller; +import org.wildfly.subsystem.service.capability.CapabilityServiceInstaller; /** * Abstract registrar for a discovery provider resource definition. * @author Paul Ferraro */ -public abstract class DiscoveryProviderRegistrar implements ChildResourceDefinitionRegistrar, ResourceServiceConfigurator { +public class DiscoveryProviderResourceRegistrar implements ChildResourceDefinitionRegistrar, ResourceServiceConfigurator { // TODO Move this to an SPI module, when this capability acquires any consumers static final UnaryServiceDescriptor DISCOVERY_PROVIDER_DESCRIPTOR = UnaryServiceDescriptor.of("org.wildfly.discovery.provider", DiscoveryProvider.class); static final RuntimeCapability DISCOVERY_PROVIDER_CAPABILITY = RuntimeCapability.Builder.of(DISCOVERY_PROVIDER_DESCRIPTOR).setAllowMultipleRegistrations(true).build(); - private final ResourceRegistration registration; - private final ResourceDescriptor descriptor; + private final DiscoveryProviderResourceDescription description; - DiscoveryProviderRegistrar(PathElement path, ResourceDescriptor.Builder builder) { - this.registration = ResourceRegistration.of(path); - this.descriptor = builder.addCapability(DISCOVERY_PROVIDER_CAPABILITY) - .withRuntimeHandler(ResourceOperationRuntimeHandler.configureService(this)) - .build(); + DiscoveryProviderResourceRegistrar(DiscoveryProviderResourceDescription description) { + this.description = description; } @Override public ManagementResourceRegistration register(ManagementResourceRegistration parent, ManagementResourceRegistrationContext context) { - ResourceDefinition definition = ResourceDefinition.builder(this.registration, this.descriptor.getResourceDescriptionResolver()).build(); + ResourceDescriptor descriptor = ResourceDescriptor.builder(DiscoverySubsystemResourceRegistrar.RESOLVER.createChildResolver(this.description.getPathElement())) + .addAttributes(this.description.getAttributes().toList()) + .addCapability(DISCOVERY_PROVIDER_CAPABILITY) + .withRuntimeHandler(ResourceOperationRuntimeHandler.configureService(this)) + .build(); + ResourceDefinition definition = ResourceDefinition.builder(this.description, descriptor.getResourceDescriptionResolver()).build(); ManagementResourceRegistration registration = parent.registerSubModel(definition); - ManagementResourceRegistrar.of(this.descriptor).register(registration); + ManagementResourceRegistrar.of(descriptor).register(registration); return registration; } + + @Override + public ResourceServiceInstaller configure(OperationContext context, ModelNode model) throws OperationFailedException { + return CapabilityServiceInstaller.builder(DiscoveryProviderResourceRegistrar.DISCOVERY_PROVIDER_CAPABILITY, this.description.resolve(context, model)).build(); + } } diff --git a/discovery/src/main/java/org/wildfly/extension/discovery/DiscoverySubsystemResourceDescription.java b/discovery/src/main/java/org/wildfly/extension/discovery/DiscoverySubsystemResourceDescription.java new file mode 100644 index 00000000000..9a2967b939a --- /dev/null +++ b/discovery/src/main/java/org/wildfly/extension/discovery/DiscoverySubsystemResourceDescription.java @@ -0,0 +1,20 @@ +/* + * Copyright The WildFly Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.wildfly.extension.discovery; + +import org.jboss.as.controller.SubsystemResourceDescription; + +/** + * Describes the discovery subsystem resource. + */ +public enum DiscoverySubsystemResourceDescription implements SubsystemResourceDescription { + INSTANCE; + + @Override + public String getName() { + return "discovery"; + } +} diff --git a/discovery/src/main/java/org/wildfly/extension/discovery/DiscoverySubsystemRegistrar.java b/discovery/src/main/java/org/wildfly/extension/discovery/DiscoverySubsystemResourceRegistrar.java similarity index 66% rename from discovery/src/main/java/org/wildfly/extension/discovery/DiscoverySubsystemRegistrar.java rename to discovery/src/main/java/org/wildfly/extension/discovery/DiscoverySubsystemResourceRegistrar.java index bd36f3ebab1..bd106ec480c 100644 --- a/discovery/src/main/java/org/wildfly/extension/discovery/DiscoverySubsystemRegistrar.java +++ b/discovery/src/main/java/org/wildfly/extension/discovery/DiscoverySubsystemResourceRegistrar.java @@ -5,9 +5,7 @@ package org.wildfly.extension.discovery; -import org.jboss.as.controller.PathElement; import org.jboss.as.controller.ResourceDefinition; -import org.jboss.as.controller.ResourceRegistration; import org.jboss.as.controller.SubsystemRegistration; import org.jboss.as.controller.descriptions.ParentResourceDescriptionResolver; import org.jboss.as.controller.descriptions.SubsystemResourceDescriptionResolver; @@ -21,23 +19,21 @@ * Registrar for the discovery subsystem. * @author Paul Ferraro */ -class DiscoverySubsystemRegistrar implements SubsystemResourceDefinitionRegistrar { +class DiscoverySubsystemResourceRegistrar implements SubsystemResourceDefinitionRegistrar { - static final String NAME = "discovery"; - static final PathElement PATH = SubsystemResourceDefinitionRegistrar.pathElement(NAME); - static final ParentResourceDescriptionResolver RESOLVER = new SubsystemResourceDescriptionResolver(NAME, DiscoverySubsystemRegistrar.class); + static final ParentResourceDescriptionResolver RESOLVER = new SubsystemResourceDescriptionResolver(DiscoverySubsystemResourceDescription.INSTANCE.getName(), DiscoverySubsystemResourceRegistrar.class); @Override public ManagementResourceRegistration register(SubsystemRegistration parent, ManagementResourceRegistrationContext context) { parent.setHostCapable(); - ManagementResourceRegistration registration = parent.registerSubsystemModel(ResourceDefinition.builder(ResourceRegistration.of(PATH), RESOLVER).build()); + ManagementResourceRegistration registration = parent.registerSubsystemModel(ResourceDefinition.builder(DiscoverySubsystemResourceDescription.INSTANCE, RESOLVER).build()); ResourceDescriptor descriptor = ResourceDescriptor.builder(RESOLVER).build(); ManagementResourceRegistrar.of(descriptor).register(registration); - new AggregateDiscoveryProviderRegistrar().register(registration, context); - new StaticDiscoveryProviderRegistrar().register(registration, context); + new AggregateDiscoveryProviderResourceRegistrar().register(registration, context); + new StaticDiscoveryProviderResourceRegistrar().register(registration, context); return registration; } diff --git a/discovery/src/main/java/org/wildfly/extension/discovery/DiscoverySubsystemSchema.java b/discovery/src/main/java/org/wildfly/extension/discovery/DiscoverySubsystemSchema.java index e6f939ddeaf..c4423c309b3 100644 --- a/discovery/src/main/java/org/wildfly/extension/discovery/DiscoverySubsystemSchema.java +++ b/discovery/src/main/java/org/wildfly/extension/discovery/DiscoverySubsystemSchema.java @@ -5,17 +5,21 @@ package org.wildfly.extension.discovery; -import org.jboss.as.controller.PersistentResourceXMLDescription; -import org.jboss.as.controller.PersistentSubsystemSchema; +import java.util.List; + import org.jboss.as.controller.SubsystemSchema; +import org.jboss.as.controller.persistence.xml.ResourceXMLElement; +import org.jboss.as.controller.persistence.xml.SubsystemResourceXMLSchema; import org.jboss.as.controller.xml.VersionedNamespace; +import org.jboss.as.controller.xml.XMLCardinality; +import org.jboss.as.controller.xml.XMLChoice; import org.jboss.staxmapper.IntVersion; /** * Enumeration of discovery subsystem schema versions. * @author Paul Ferraro */ -enum DiscoverySubsystemSchema implements PersistentSubsystemSchema { +enum DiscoverySubsystemSchema implements SubsystemResourceXMLSchema { VERSION_1_0(1, 0), ; static final DiscoverySubsystemSchema CURRENT = VERSION_1_0; @@ -23,7 +27,7 @@ enum DiscoverySubsystemSchema implements PersistentSubsystemSchema namespace; DiscoverySubsystemSchema(int major, int minor) { - this.namespace = SubsystemSchema.createLegacySubsystemURN(DiscoverySubsystemRegistrar.NAME, new IntVersion(major, minor)); + this.namespace = SubsystemSchema.createLegacySubsystemURN(DiscoverySubsystemResourceDescription.INSTANCE.getName(), new IntVersion(major, minor)); } @Override @@ -32,11 +36,10 @@ public VersionedNamespace getNamespace() { } @Override - public PersistentResourceXMLDescription getXMLDescription() { - PersistentResourceXMLDescription.Factory factory = PersistentResourceXMLDescription.factory(this); - return factory.builder(DiscoverySubsystemRegistrar.PATH) - .addChild(factory.builder(StaticDiscoveryProviderRegistrar.PATH).addAttributes(StaticDiscoveryProviderRegistrar.ATTRIBUTES.stream()).build()) - .addChild(factory.builder(AggregateDiscoveryProviderRegistrar.PATH).addAttributes(AggregateDiscoveryProviderRegistrar.ATTRIBUTES.stream()).build()) + public ResourceXMLElement getSubsystemResourceXMLElement() { + ResourceXMLElement.Builder.Factory factory = ResourceXMLElement.Builder.Factory.newInstance(this); + return factory.createBuilder(DiscoverySubsystemResourceDescription.INSTANCE) + .appendChild(XMLChoice.of(List.of(factory.createBuilder(StaticDiscoveryProviderResourceDescription.INSTANCE).build(), factory.createBuilder(AggregateDiscoveryProviderResourceDescription.INSTANCE).build()), XMLCardinality.Unbounded.OPTIONAL)) .build(); } } diff --git a/discovery/src/main/java/org/wildfly/extension/discovery/StaticDiscoveryProviderResourceDescription.java b/discovery/src/main/java/org/wildfly/extension/discovery/StaticDiscoveryProviderResourceDescription.java new file mode 100644 index 00000000000..975273984f5 --- /dev/null +++ b/discovery/src/main/java/org/wildfly/extension/discovery/StaticDiscoveryProviderResourceDescription.java @@ -0,0 +1,115 @@ +/* + * Copyright The WildFly Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.wildfly.extension.discovery; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Stream; + +import org.jboss.as.controller.AttributeDefinition; +import org.jboss.as.controller.AttributeMarshaller; +import org.jboss.as.controller.AttributeParser; +import org.jboss.as.controller.ObjectListAttributeDefinition; +import org.jboss.as.controller.ObjectTypeAttributeDefinition; +import org.jboss.as.controller.OperationContext; +import org.jboss.as.controller.OperationFailedException; +import org.jboss.as.controller.PathElement; +import org.jboss.as.controller.SimpleAttributeDefinition; +import org.jboss.as.controller.SimpleAttributeDefinitionBuilder; +import org.jboss.as.controller.descriptions.ModelDescriptionConstants; +import org.jboss.as.controller.registry.AttributeAccess.Flag; +import org.jboss.dmr.ModelNode; +import org.jboss.dmr.ModelType; +import org.wildfly.discovery.AttributeValue; +import org.wildfly.discovery.ServiceURL; +import org.wildfly.discovery.impl.StaticDiscoveryProvider; +import org.wildfly.discovery.spi.DiscoveryProvider; +import org.wildfly.subsystem.service.ServiceDependency; + +/** + * Describes an aggregate discovery provider resource. + * @author Paul Ferraro + */ +public enum StaticDiscoveryProviderResourceDescription implements DiscoveryProviderResourceDescription { + INSTANCE; + + private static final SimpleAttributeDefinition ABSTRACT_TYPE = new SimpleAttributeDefinitionBuilder("abstract-type", ModelType.STRING, true).setAllowExpression(true).build(); + private static final SimpleAttributeDefinition ABSTRACT_TYPE_AUTHORITY = new SimpleAttributeDefinitionBuilder("abstract-type-authority", ModelType.STRING, true).setAllowExpression(true).build(); + private static final SimpleAttributeDefinition URI = new SimpleAttributeDefinitionBuilder("uri", ModelType.STRING, false).setValidator(new ServiceURIValidator()).setAllowExpression(true).build(); + private static final SimpleAttributeDefinition URI_SCHEME_AUTHORITY = new SimpleAttributeDefinitionBuilder("uri-scheme-authority", ModelType.STRING, true).setAllowExpression(true).build(); + + private static final SimpleAttributeDefinition NAME = new SimpleAttributeDefinitionBuilder(ModelDescriptionConstants.NAME, ModelType.STRING, false).setAllowExpression(true).build(); + private static final SimpleAttributeDefinition VALUE = new SimpleAttributeDefinitionBuilder(ModelDescriptionConstants.VALUE, ModelType.STRING, true).setAllowExpression(true).build(); + + private static final ObjectTypeAttributeDefinition ATTRIBUTE = new ObjectTypeAttributeDefinition.Builder("attribute", NAME, VALUE).build(); + + private static final ObjectListAttributeDefinition SERVICE_ATTRIBUTES = new ObjectListAttributeDefinition.Builder("attributes", ATTRIBUTE) + .setAttributeMarshaller(AttributeMarshaller.UNWRAPPED_OBJECT_LIST_MARSHALLER) + .setAttributeParser(AttributeParser.UNWRAPPED_OBJECT_LIST_PARSER) + .setRequired(false) + .build(); + + private static final ObjectTypeAttributeDefinition SERVICE = new ObjectTypeAttributeDefinition.Builder("service", + ABSTRACT_TYPE, + ABSTRACT_TYPE_AUTHORITY, + URI, + URI_SCHEME_AUTHORITY, + SERVICE_ATTRIBUTES + ).build(); + + private static final ObjectListAttributeDefinition SERVICES = new ObjectListAttributeDefinition.Builder("services", SERVICE) + .setAttributeMarshaller(AttributeMarshaller.UNWRAPPED_OBJECT_LIST_MARSHALLER) + .setAttributeParser(AttributeParser.UNWRAPPED_OBJECT_LIST_PARSER) + .setFlags(Flag.RESTART_RESOURCE_SERVICES) + .build(); + + private final PathElement path = PathElement.pathElement("static-provider"); + + @Override + public PathElement getPathElement() { + return this.path; + } + + @Override + public Stream getAttributes() { + return Stream.of(SERVICES); + } + + @Override + public ServiceDependency resolve(OperationContext context, ModelNode model) throws OperationFailedException { + List services = SERVICES.resolveModelAttribute(context, model).asListOrEmpty(); + List serviceURLs = new ArrayList<>(services.size()); + for (ModelNode service : services) { + ServiceURL.Builder builder = new ServiceURL.Builder(); + builder.setUri(java.net.URI.create(URI.resolveModelAttribute(context, service).asString())); + String abstractType = ABSTRACT_TYPE.resolveModelAttribute(context, service).asStringOrNull(); + if (abstractType != null) { + builder.setAbstractType(abstractType); + } + String abstractTypeAuthority = ABSTRACT_TYPE_AUTHORITY.resolveModelAttribute(context, service).asStringOrNull(); + if (abstractTypeAuthority != null) { + builder.setAbstractTypeAuthority(abstractTypeAuthority); + } + String uriSchemeAuthority = URI_SCHEME_AUTHORITY.resolveModelAttribute(context, service).asStringOrNull(); + if (uriSchemeAuthority != null) { + builder.setUriSchemeAuthority(uriSchemeAuthority); + } + for (ModelNode attribute : SERVICE_ATTRIBUTES.resolveModelAttribute(context, service).asListOrEmpty()) { + String name = NAME.resolveModelAttribute(context, attribute).asString(); + String value = VALUE.resolveModelAttribute(context, attribute).asStringOrNull(); + if (value != null) { + builder.addAttribute(name, AttributeValue.fromString(value)); + } else { + builder.addAttribute(name); + } + } + ServiceURL serviceURL = builder.create(); + Messages.log.tracef("Adding service URL %s", serviceURL); + serviceURLs.add(serviceURL); + } + return ServiceDependency.of(new StaticDiscoveryProvider(serviceURLs)); + } +} diff --git a/discovery/src/main/java/org/wildfly/extension/discovery/StaticDiscoveryProviderRegistrar.java b/discovery/src/main/java/org/wildfly/extension/discovery/StaticDiscoveryProviderResourceRegistrar.java similarity index 91% rename from discovery/src/main/java/org/wildfly/extension/discovery/StaticDiscoveryProviderRegistrar.java rename to discovery/src/main/java/org/wildfly/extension/discovery/StaticDiscoveryProviderResourceRegistrar.java index f61b795d90a..b3343820e0f 100644 --- a/discovery/src/main/java/org/wildfly/extension/discovery/StaticDiscoveryProviderRegistrar.java +++ b/discovery/src/main/java/org/wildfly/extension/discovery/StaticDiscoveryProviderResourceRegistrar.java @@ -16,7 +16,6 @@ import org.jboss.as.controller.ObjectTypeAttributeDefinition; import org.jboss.as.controller.OperationContext; import org.jboss.as.controller.OperationFailedException; -import org.jboss.as.controller.PathElement; import org.jboss.as.controller.SimpleAttributeDefinition; import org.jboss.as.controller.SimpleAttributeDefinitionBuilder; import org.jboss.as.controller.descriptions.ModelDescriptionConstants; @@ -26,7 +25,6 @@ import org.wildfly.discovery.AttributeValue; import org.wildfly.discovery.ServiceURL; import org.wildfly.discovery.impl.StaticDiscoveryProvider; -import org.wildfly.subsystem.resource.ResourceDescriptor; import org.wildfly.subsystem.service.ResourceServiceInstaller; import org.wildfly.subsystem.service.capability.CapabilityServiceInstaller; @@ -34,8 +32,7 @@ * Registers the static discovery provider resource definition. * @author Paul Ferraro */ -public class StaticDiscoveryProviderRegistrar extends DiscoveryProviderRegistrar { - static final PathElement PATH = PathElement.pathElement("static-provider"); +public class StaticDiscoveryProviderResourceRegistrar extends DiscoveryProviderResourceRegistrar { private static final SimpleAttributeDefinition ABSTRACT_TYPE = new SimpleAttributeDefinitionBuilder("abstract-type", ModelType.STRING, true).setAllowExpression(true).build(); private static final SimpleAttributeDefinition ABSTRACT_TYPE_AUTHORITY = new SimpleAttributeDefinitionBuilder("abstract-type-authority", ModelType.STRING, true).setAllowExpression(true).build(); @@ -69,8 +66,8 @@ public class StaticDiscoveryProviderRegistrar extends DiscoveryProviderRegistrar static final Collection ATTRIBUTES = List.of(SERVICES); - StaticDiscoveryProviderRegistrar() { - super(PATH, ResourceDescriptor.builder(DiscoverySubsystemRegistrar.RESOLVER.createChildResolver(PATH)).addAttributes(ATTRIBUTES)); + StaticDiscoveryProviderResourceRegistrar() { + super(StaticDiscoveryProviderResourceDescription.INSTANCE); } @Override @@ -105,6 +102,6 @@ public ResourceServiceInstaller configure(OperationContext context, ModelNode mo Messages.log.tracef("Adding service URL %s", serviceURL); serviceURLs.add(serviceURL); } - return CapabilityServiceInstaller.builder(DiscoveryProviderRegistrar.DISCOVERY_PROVIDER_CAPABILITY, new StaticDiscoveryProvider(serviceURLs)).build(); + return CapabilityServiceInstaller.builder(DiscoveryProviderResourceRegistrar.DISCOVERY_PROVIDER_CAPABILITY, new StaticDiscoveryProvider(serviceURLs)).build(); } } diff --git a/discovery/src/test/java/org/wildfly/extension/discovery/DiscoverySubsystemTestCase.java b/discovery/src/test/java/org/wildfly/extension/discovery/DiscoverySubsystemTestCase.java index 3a3e9e41cf4..8532185abc9 100644 --- a/discovery/src/test/java/org/wildfly/extension/discovery/DiscoverySubsystemTestCase.java +++ b/discovery/src/test/java/org/wildfly/extension/discovery/DiscoverySubsystemTestCase.java @@ -25,6 +25,6 @@ public static Iterable parameters() { } public DiscoverySubsystemTestCase(DiscoverySubsystemSchema schema) { - super(DiscoverySubsystemRegistrar.NAME, new DiscoveryExtension(), schema, DiscoverySubsystemSchema.CURRENT); + super(DiscoverySubsystemResourceDescription.INSTANCE.getName(), new DiscoveryExtension(), schema, DiscoverySubsystemSchema.CURRENT); } }