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.parser;
17  
18  import static de.smartics.properties.jboss.extension.resources.PropertiesConfigurationResourceDefinition.AN_NAME;
19  import static de.smartics.properties.jboss.extension.resources.PropertiesConfigurationResourceDefinition.CONFIGS_ELEMENT_NAME;
20  import static de.smartics.properties.jboss.extension.resources.PropertiesConfigurationResourceDefinition.CONFIG_ELEMENT_NAME;
21  import static de.smartics.properties.jboss.extension.resources.components.CacheComponent.CACHE_ELEMENT_NAME;
22  import static javax.xml.stream.XMLStreamConstants.END_ELEMENT;
23  import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.ADD;
24  import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OP;
25  import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OP_ADDR;
26  
27  import java.util.Collections;
28  import java.util.List;
29  
30  import javax.xml.stream.XMLStreamException;
31  
32  import org.jboss.as.controller.PathAddress;
33  import org.jboss.as.controller.PathElement;
34  import org.jboss.as.controller.descriptions.ModelDescriptionConstants;
35  import org.jboss.as.controller.parsing.ParseUtils;
36  import org.jboss.dmr.ModelNode;
37  import org.jboss.logging.Logger;
38  import org.jboss.staxmapper.XMLElementReader;
39  import org.jboss.staxmapper.XMLExtendedStreamReader;
40  
41  import de.smartics.properties.jboss.extension.resources.PropertiesConfigurationResourceDefinition;
42  import de.smartics.properties.jboss.extension.resources.SubsystemDefinition;
43  import de.smartics.properties.jboss.extension.resources.components.DataSourceComponent;
44  import de.smartics.properties.jboss.extension.resources.components.AdminResourceComponent;
45  import de.smartics.properties.jboss.extension.resources.components.FactoriesComponent;
46  import de.smartics.properties.jboss.extension.resources.components.SecurityComponent;
47  
48  /**
49   * Reader for extension configurations.
50   */
51  class ConfigurationReader implements XMLElementReader<List<ModelNode>>
52  {
53    // ********************************* Fields *********************************
54  
55    // --- constants ------------------------------------------------------------
56  
57    /**
58     * Reference to the logger for this class.
59     */
60    private static final Logger LOG = Logger.getLogger(ConfigurationReader.class);
61  
62    // --- members --------------------------------------------------------------
63  
64    // ****************************** Initializer *******************************
65  
66    // ****************************** Constructors ******************************
67  
68    /**
69     * Default constructor.
70     */
71    public ConfigurationReader()
72    {
73    }
74  
75    // ****************************** Inner Classes *****************************
76  
77    // ********************************* Methods ********************************
78  
79    // --- init -----------------------------------------------------------------
80  
81    // --- get&set --------------------------------------------------------------
82  
83    // --- business -------------------------------------------------------------
84  
85    @Override
86    public void readElement(final XMLExtendedStreamReader reader,
87        final List<ModelNode> operations) throws XMLStreamException
88    {
89      ParseUtils.requireNoAttributes(reader);
90  
91      final PathAddress subsystemAddress =
92          PathAddress.pathAddress(SubsystemDefinition.SUBSYSTEM_PATH);
93      final ModelNode addSubsystem = createAddOperation(subsystemAddress);
94      operations.add(addSubsystem);
95      readConfigurations(reader, operations, subsystemAddress);
96    }
97  
98    private static ModelNode createAddOperation(final PathAddress address)
99    {
100     final ModelNode operation = new ModelNode();
101     operation.get(OP).set(ADD);
102     operation.get(OP_ADDR).set(address.toModelNode());
103     return operation;
104   }
105 
106   private void readConfigurations(final XMLExtendedStreamReader reader,
107       final List<ModelNode> operations, final PathAddress subsystemAddress)
108     throws XMLStreamException
109   {
110     while (reader.hasNext() && reader.nextTag() != END_ELEMENT)
111     {
112       if (!reader.getLocalName().equals(CONFIGS_ELEMENT_NAME))
113       {
114         throw ParseUtils.unexpectedElement(reader);
115       }
116 
117       while (reader.hasNext() && reader.nextTag() != END_ELEMENT)
118       {
119         if (reader.isStartElement())
120         {
121           readConfiguration(reader, operations, subsystemAddress);
122         }
123       }
124     }
125   }
126 
127   private void readConfiguration(final XMLExtendedStreamReader reader,
128       final List<ModelNode> operations, final PathAddress configsAddress)
129     throws XMLStreamException
130   {
131     final String rootLocalName = reader.getLocalName();
132     LOG.debug("XML:" + rootLocalName);
133 
134     if (!rootLocalName.equals(CONFIG_ELEMENT_NAME))
135     {
136       throw ParseUtils.unexpectedElement(reader);
137     }
138 
139     final ModelNode addOperation = new ModelNode();
140     addOperation.get(OP).set(ModelDescriptionConstants.ADD);
141 
142     String name = null;
143     for (int i = 0; i < reader.getAttributeCount(); i++)
144     {
145       final String attr = reader.getAttributeLocalName(i);
146       final String value = reader.getAttributeValue(i);
147       if (attr.equals(AN_NAME))
148       {
149         PropertiesConfigurationResourceDefinition.NAME.parseAndSetParameter(
150             value, addOperation, reader);
151         name = value;
152       }
153       else
154       {
155         throw ParseUtils.unexpectedAttribute(reader, i);
156       }
157     }
158 
159     if (name == null)
160     {
161       throw ParseUtils.missingRequiredElement(reader,
162           Collections.singleton(AN_NAME));
163     }
164 
165     while (reader.hasNext() && reader.nextTag() != END_ELEMENT)
166     {
167       final String localName = reader.getLocalName();
168       LOG.debug(">XML:" + localName);
169 
170       if (localName.equals(AdminResourceComponent.ROOT_ELEMENT_NAME))
171       {
172         PropertiesConfigurationResourceDefinition.INSTANCE
173             .getDefinitionsComponent().read(reader, addOperation);
174       }
175       else if (localName.equals(CACHE_ELEMENT_NAME))
176       {
177         PropertiesConfigurationResourceDefinition.INSTANCE.getCacheComponent()
178             .read(reader, addOperation);
179       }
180       else if (localName.equals(DataSourceComponent.DS_ELEMENT_NAME))
181       {
182         PropertiesConfigurationResourceDefinition.INSTANCE
183             .getDataSourceComponent().read(reader, addOperation);
184       }
185       else if (localName.equals(SecurityComponent.ROOT_ELEMENT_NAME))
186       {
187         PropertiesConfigurationResourceDefinition.INSTANCE
188             .getSecurityComponent().read(reader, addOperation);
189       }
190       else if (localName.equals(FactoriesComponent.ROOT_ELEMENT_NAME))
191       {
192         PropertiesConfigurationResourceDefinition.INSTANCE
193             .getFactoriesComponent().read(reader, addOperation);
194       }
195       else
196       {
197         throw ParseUtils.unexpectedElement(reader);
198       }
199     }
200     final PathAddress address =
201         configsAddress.append(PathElement.pathElement(CONFIGS_ELEMENT_NAME,
202             name));
203     addOperation.get(OP_ADDR).set(address.toModelNode());
204     operations.add(addOperation);
205   }
206 
207   // --- object basics --------------------------------------------------------
208 
209 }