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  
22  import javax.xml.stream.XMLStreamException;
23  
24  import org.jboss.as.controller.persistence.SubsystemMarshallingContext;
25  import org.jboss.dmr.ModelNode;
26  import org.jboss.staxmapper.XMLElementWriter;
27  import org.jboss.staxmapper.XMLExtendedStreamWriter;
28  
29  import de.smartics.properties.jboss.extension.SubsystemExtension;
30  import de.smartics.properties.jboss.extension.resources.PropertiesConfigurationResourceDefinition;
31  
32  /**
33   * Writer for extension configurations.
34   */
35  class ConfigurationWriter implements
36      XMLElementWriter<SubsystemMarshallingContext>
37  {
38    // ********************************* Fields *********************************
39  
40    // --- constants ------------------------------------------------------------
41  
42    // --- members --------------------------------------------------------------
43  
44    // ****************************** Initializer *******************************
45  
46    // ****************************** Constructors ******************************
47  
48    /**
49     * Default constructor.
50     */
51    ConfigurationWriter()
52    {
53    }
54  
55    // ****************************** Inner Classes *****************************
56  
57    // ********************************* Methods ********************************
58  
59    // --- init -----------------------------------------------------------------
60  
61    // --- get&set --------------------------------------------------------------
62  
63    // --- business -------------------------------------------------------------
64  
65    @Override
66    public void writeContent(final XMLExtendedStreamWriter writer,
67        final SubsystemMarshallingContext context) throws XMLStreamException
68    {
69      context.startSubsystemElement(SubsystemExtension.NAMESPACE, false);
70      final ModelNode node = context.getModelNode();
71      final ModelNode configs = node.get(CONFIGS_ELEMENT_NAME);
72      if (configs.isDefined())
73      {
74        writer.writeStartElement(CONFIGS_ELEMENT_NAME);
75        for (final String configName : configs.keys())
76        {
77          final ModelNode config = configs.get(configName);
78          writeConfiguration(writer, config);
79        }
80        writer.writeEndElement();
81      }
82      writer.writeEndElement();
83    }
84  
85    private void writeConfiguration(final XMLExtendedStreamWriter writer,
86        final ModelNode config) throws XMLStreamException
87    {
88      writer.writeStartElement(CONFIG_ELEMENT_NAME);
89  
90      final String name = config.get("name").asString();
91      writer.writeAttribute(AN_NAME, name);
92      PropertiesConfigurationResourceDefinition.INSTANCE
93          .getDefinitionsComponent().write(writer, config);
94      PropertiesConfigurationResourceDefinition.INSTANCE.getCacheComponent()
95          .write(writer, config);
96      PropertiesConfigurationResourceDefinition.INSTANCE.getDataSourceComponent()
97          .write(writer, config);
98      PropertiesConfigurationResourceDefinition.INSTANCE.getSecurityComponent()
99          .write(writer, config);
100     PropertiesConfigurationResourceDefinition.INSTANCE.getFactoriesComponent()
101         .write(writer, config);
102 
103     writer.writeEndElement();
104   }
105 
106   // --- object basics --------------------------------------------------------
107 
108 }