View Javadoc

1   /*
2    * Copyright 2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.properties.jboss.extension.resources.components;
17  
18  import static javax.xml.stream.XMLStreamConstants.END_ELEMENT;
19  
20  import javax.xml.stream.XMLStreamException;
21  
22  import org.jboss.as.controller.AttributeDefinition;
23  import org.jboss.as.controller.OperationFailedException;
24  import org.jboss.as.controller.OperationStepHandler;
25  import org.jboss.as.controller.SimpleAttributeDefinition;
26  import org.jboss.as.controller.SimpleAttributeDefinitionBuilder;
27  import org.jboss.as.controller.parsing.ParseUtils;
28  import org.jboss.as.controller.registry.AttributeAccess;
29  import org.jboss.as.controller.registry.ManagementResourceRegistration;
30  import org.jboss.dmr.ModelNode;
31  import org.jboss.dmr.ModelType;
32  import org.jboss.staxmapper.XMLExtendedStreamReader;
33  import org.jboss.staxmapper.XMLExtendedStreamWriter;
34  
35  import de.smartics.properties.spi.config.config.AdminResourceConfiguration;
36  import de.smartics.properties.spi.config.config.PropertiesConfiguration;
37  
38  /**
39   * Defines the admin resource information of a configuration.
40   * <p>
41   * Example XML fragment of a admin resource within a configuration:
42   * </p>
43   *
44   * <pre>
45   * {@markupExample "Admin Resource Element"
46   * &lt;adminResource&gt;
47   *   &lt;groupId&gt;de.smartics.sandbox&lt;/groupId&gt;
48   *   &lt;artifactId&gt;test-application-context-ds&lt;/artifactId&gt;
49   *   &lt;version&gt;0.1.0&lt;/version&gt;
50   *   &lt;archiveType&gt;pom&lt;/archiveType&gt;
51   *   &lt;!-- classifier / --&gt;
52   * &lt;/definitions&gt;
53   * </pre>
54   */
55  public class AdminResourceComponent extends AbstractComponent
56  {
57    // ********************************* Fields *********************************
58  
59    // --- constants ------------------------------------------------------------
60  
61    /**
62     * The name of the configuration XML element that holds the admin resource
63     * information.
64     * <p>
65     * The value of this constant is {@value}.
66     * </p>
67     */
68    public static final String ROOT_ELEMENT_NAME = "adminResource";
69  
70    /**
71     * The attribute that holds the group ID.
72     */
73    public static final SimpleAttributeDefinition GROUP_ID =
74        new SimpleAttributeDefinitionBuilder("adminResource.groupId",
75            ModelType.STRING, true).setXmlName("groupId")
76            .setAllowExpression(true)
77            .setFlags(AttributeAccess.Flag.RESTART_ALL_SERVICES).build();
78  
79    /**
80     * The attribute that holds the artifact ID.
81     */
82    public static final SimpleAttributeDefinition ARTIFACT_ID =
83        new SimpleAttributeDefinitionBuilder("adminResource.artifactId",
84            ModelType.STRING, true).setXmlName("artifactId")
85            .setAllowExpression(true)
86            .setFlags(AttributeAccess.Flag.RESTART_ALL_SERVICES).build();
87  
88    /**
89     * The attribute that holds the version.
90     */
91    public static final SimpleAttributeDefinition VERSION =
92        new SimpleAttributeDefinitionBuilder("adminResource.version",
93            ModelType.STRING, true).setXmlName("version")
94            .setAllowExpression(true)
95            .setFlags(AttributeAccess.Flag.RESTART_ALL_SERVICES).build();
96  
97    /**
98     * The attribute that holds the archive type.
99     */
100   public static final SimpleAttributeDefinition ARCHIVE_TYPE =
101       new SimpleAttributeDefinitionBuilder("adminResource.archiveType",
102           ModelType.STRING, true).setXmlName("archiveType")
103           .setAllowExpression(true)
104           .setFlags(AttributeAccess.Flag.RESTART_ALL_SERVICES).build();
105 
106   /**
107    * The attribute that holds the classifier.
108    */
109   public static final SimpleAttributeDefinition CLASSIFIER =
110       new SimpleAttributeDefinitionBuilder("adminResource.classifier",
111           ModelType.STRING, true).setXmlName("classifier")
112           .setAllowExpression(true)
113           .setFlags(AttributeAccess.Flag.RESTART_ALL_SERVICES).build();
114 
115   private static final AttributeDefinition[] DEFINITIONS = { GROUP_ID,
116                                                             ARTIFACT_ID,
117                                                             VERSION,
118                                                             ARCHIVE_TYPE,
119                                                             CLASSIFIER };
120 
121   // --- members --------------------------------------------------------------
122 
123   // ****************************** Initializer *******************************
124 
125   // ****************************** Constructors ******************************
126 
127   /**
128    * Default constructor.
129    */
130   public AdminResourceComponent(final OperationStepHandler writeHandler)
131   {
132     super(writeHandler);
133   }
134 
135   // ****************************** Inner Classes *****************************
136 
137   // ********************************* Methods ********************************
138 
139   // --- init -----------------------------------------------------------------
140 
141   // --- get&set --------------------------------------------------------------
142 
143   // --- business -------------------------------------------------------------
144 
145   public void registerAttributes(
146       final ManagementResourceRegistration resourceRegistration)
147   {
148     resourceRegistration.registerReadWriteAttribute(GROUP_ID, null,
149         writeHandler);
150     resourceRegistration.registerReadWriteAttribute(ARTIFACT_ID, null,
151         writeHandler);
152     resourceRegistration
153         .registerReadWriteAttribute(VERSION, null, writeHandler);
154     resourceRegistration.registerReadWriteAttribute(ARCHIVE_TYPE, null,
155         writeHandler);
156     resourceRegistration.registerReadWriteAttribute(CLASSIFIER, null,
157         writeHandler);
158   }
159 
160   public void populateModel(final ModelNode operation, final ModelNode model)
161     throws OperationFailedException
162   {
163     GROUP_ID.validateAndSet(operation, model);
164     ARTIFACT_ID.validateAndSet(operation, model);
165     VERSION.validateAndSet(operation, model);
166     ARCHIVE_TYPE.validateAndSet(operation, model);
167     CLASSIFIER.validateAndSet(operation, model);
168   }
169 
170   public void read(final XMLExtendedStreamReader reader,
171       final ModelNode addOperation) throws XMLStreamException
172   {
173     ParseUtils.requireNoAttributes(reader);
174 
175     while (reader.hasNext() && reader.nextTag() != END_ELEMENT)
176     {
177       if (reader.isStartElement())
178       {
179         final String localName = reader.getLocalName();
180         if (AdminResourceComponent.GROUP_ID.getXmlName().equals(localName))
181         {
182           readAttribute(reader, addOperation, AdminResourceComponent.GROUP_ID);
183         }
184         else if (AdminResourceComponent.ARTIFACT_ID.getXmlName()
185             .equals(localName))
186         {
187           readAttribute(reader, addOperation, AdminResourceComponent.ARTIFACT_ID);
188         }
189         else if (AdminResourceComponent.VERSION.getXmlName().equals(localName))
190         {
191           readAttribute(reader, addOperation, AdminResourceComponent.VERSION);
192         }
193         else if (AdminResourceComponent.ARCHIVE_TYPE.getXmlName().equals(
194             localName))
195         {
196           readAttribute(reader, addOperation, AdminResourceComponent.ARCHIVE_TYPE);
197         }
198         else if (AdminResourceComponent.CLASSIFIER.getXmlName().equals(localName))
199         {
200           readAttribute(reader, addOperation, AdminResourceComponent.CLASSIFIER);
201         }
202         else
203         {
204           throw ParseUtils.unexpectedElement(reader);
205         }
206       }
207     }
208   }
209 
210   public void write(final XMLExtendedStreamWriter writer, final ModelNode config)
211     throws XMLStreamException
212   {
213     if (isAtLeastOneDefined(config, DEFINITIONS))
214     {
215       writer.writeStartElement(AdminResourceComponent.ROOT_ELEMENT_NAME);
216 
217       for (final AttributeDefinition definition : DEFINITIONS)
218       {
219         writeAttribute(writer, config, definition);
220       }
221 
222       writer.writeEndElement();
223     }
224   }
225 
226   public void apply(final PropertiesConfiguration propertiesConfig,
227       final ModelNode model)
228   {
229     final AdminResourceConfiguration adminResourceConfig =
230         propertiesConfig.getAdminResourceConfiguration();
231 
232     final String groupId = getAttribute(model, GROUP_ID);
233     adminResourceConfig.setGroupId(groupId);
234 
235     final String artifactId = getAttribute(model, ARTIFACT_ID);
236     adminResourceConfig.setArtifactId(artifactId);
237 
238     final String version = getAttribute(model, VERSION);
239     adminResourceConfig.setVersion(version);
240 
241     final String archiveType = getAttribute(model, ARCHIVE_TYPE);
242     adminResourceConfig.setArchiveType(archiveType);
243 
244     final String classifier = getAttribute(model, CLASSIFIER);
245     adminResourceConfig.setClassifier(classifier);
246   }
247 
248   // --- object basics --------------------------------------------------------
249 
250 }