|
| 1 | +/* |
| 2 | + * Copyright The WildFly Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.jboss.as.controller.transform; |
| 7 | + |
| 8 | +import java.util.EnumSet; |
| 9 | +import java.util.function.BiConsumer; |
| 10 | + |
| 11 | +import org.jboss.as.controller.ModelVersion; |
| 12 | +import org.jboss.as.controller.SubsystemModel; |
| 13 | +import org.jboss.as.controller.transform.description.ResourceTransformationDescriptionBuilder; |
| 14 | +import org.jboss.as.controller.transform.description.TransformationDescription; |
| 15 | + |
| 16 | +/** |
| 17 | + * A transformer registration of a single subsystem. |
| 18 | + */ |
| 19 | +public class SubsystemModelTransformerRegistration<E extends Enum<E> & SubsystemModel> implements ExtensionTransformerRegistration { |
| 20 | + |
| 21 | + private final String subsystemName; |
| 22 | + private final E currentSubsystemModel; |
| 23 | + private final BiConsumer<ResourceTransformationDescriptionBuilder, ModelVersion> transformation; |
| 24 | + |
| 25 | + /** |
| 26 | + * Creates a transformer registration for a subsystem. |
| 27 | + * @param subsystemName the subsystem name |
| 28 | + * @param currentSubsystemModel the current subsystem model |
| 29 | + * @param transformation a consumer that builds a transformer description for a given target model version |
| 30 | + */ |
| 31 | + public SubsystemModelTransformerRegistration(String subsystemName, E currentSubsystemModel, BiConsumer<ResourceTransformationDescriptionBuilder, ModelVersion> transformation) { |
| 32 | + this.subsystemName = subsystemName; |
| 33 | + this.currentSubsystemModel = currentSubsystemModel; |
| 34 | + this.transformation = transformation; |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public String getSubsystemName() { |
| 39 | + return this.subsystemName; |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public void registerTransformers(SubsystemTransformerRegistration registration) { |
| 44 | + // Build and register transformation descriptions for all but the current subsystem model version |
| 45 | + for (E model : EnumSet.complementOf(EnumSet.of(this.currentSubsystemModel))) { |
| 46 | + ModelVersion version = model.getVersion(); |
| 47 | + ResourceTransformationDescriptionBuilder builder = registration.createResourceTransformationDescriptionBuilder(); |
| 48 | + this.transformation.accept(builder, version); |
| 49 | + TransformationDescription.Tools.register(builder.build(), registration, version); |
| 50 | + } |
| 51 | + } |
| 52 | +} |
0 commit comments