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.api.config.ds.DataSourceConfiguration;
36  import de.smartics.properties.spi.config.config.PropertiesConfiguration;
37  
38  /**
39   * Defines the data source information of a configuration. This information
40   * allows to access a data source containing property definitions.
41   * <p>
42   * Example XML fragment of a data source element within a configuration:
43   * </p>
44   *
45   * <pre>
46   * {@markupExample "Data Source Element"
47   * &lt;datasource&gt;
48   *   &lt;jndiName&gt;java:jboss/datasources/propertiesDS&lt;/jndiName&gt;
49   *   &lt;dropTable&gt;false&lt;/dropTable&gt;
50   *   &lt;createTable&gt;true&lt;/createTable&gt;
51   * &lt;/datasource&gt;}
52   * </pre>
53   */
54  public final class DataSourceComponent extends AbstractComponent
55  {
56    // ********************************* Fields *********************************
57  
58    // --- constants ------------------------------------------------------------
59  
60    /**
61     * The name of the configuration XML element that holds the data source
62     * information.
63     * <p>
64     * The value of this constant is {@value}.
65     * </p>
66     */
67    public static final String DS_ELEMENT_NAME = "datasource";
68  
69    /**
70     * The attribute that holds the JNDI name of the data source.
71     */
72    public static final SimpleAttributeDefinition JNDI_NAME =
73        new SimpleAttributeDefinitionBuilder("datasource.jndiName",
74            ModelType.STRING, true).setXmlName("jndiName")
75            .setAllowExpression(true)
76            .setFlags(AttributeAccess.Flag.RESTART_ALL_SERVICES).build();
77  
78    /**
79     * The attribute that holds the <code>drop table</code> flag of the data
80     * source.
81     */
82    public static final SimpleAttributeDefinition DROP_TABLE =
83        new SimpleAttributeDefinitionBuilder("datasource.dropTable",
84            ModelType.BOOLEAN, true).setXmlName("dropTable")
85            .setAllowExpression(true)
86            .setFlags(AttributeAccess.Flag.RESTART_ALL_SERVICES).build();
87  
88    /**
89     * The attribute that holds the <code>create table</code> flag of the data
90     * source.
91     */
92    public static final SimpleAttributeDefinition CREATE_TABLE =
93        new SimpleAttributeDefinitionBuilder("datasource.createTable",
94            ModelType.BOOLEAN, true).setXmlName("createTable")
95            .setAllowExpression(true)
96            .setFlags(AttributeAccess.Flag.RESTART_ALL_SERVICES).build();
97  
98    private static final AttributeDefinition[] DEFINITIONS = { JNDI_NAME,
99                                                              DROP_TABLE,
100                                                             CREATE_TABLE };
101 
102   // --- members --------------------------------------------------------------
103 
104   // ****************************** Initializer *******************************
105 
106   // ****************************** Constructors ******************************
107 
108   /**
109    * Default constructor.
110    */
111   public DataSourceComponent(final OperationStepHandler writeHandler)
112   {
113     super(writeHandler);
114   }
115 
116   // ****************************** Inner Classes *****************************
117 
118   // ********************************* Methods ********************************
119 
120   // --- init -----------------------------------------------------------------
121 
122   // --- get&set --------------------------------------------------------------
123 
124   // --- business -------------------------------------------------------------
125 
126   public void registerAttributes(
127       final ManagementResourceRegistration resourceRegistration)
128   {
129     resourceRegistration.registerReadWriteAttribute(JNDI_NAME, null,
130         writeHandler);
131     resourceRegistration.registerReadWriteAttribute(DROP_TABLE, null,
132         writeHandler);
133     resourceRegistration.registerReadWriteAttribute(CREATE_TABLE, null,
134         writeHandler);
135   }
136 
137   public void populateModel(final ModelNode operation, final ModelNode model)
138     throws OperationFailedException
139   {
140     JNDI_NAME.validateAndSet(operation, model);
141     DROP_TABLE.validateAndSet(operation, model);
142     CREATE_TABLE.validateAndSet(operation, model);
143   }
144 
145   public void read(final XMLExtendedStreamReader reader,
146       final ModelNode addOperation) throws XMLStreamException
147   {
148     ParseUtils.requireNoAttributes(reader);
149 
150     while (reader.hasNext() && reader.nextTag() != END_ELEMENT)
151     {
152       if (reader.isStartElement())
153       {
154         final String localName = reader.getLocalName();
155 
156         if (DataSourceComponent.JNDI_NAME.getXmlName().equals(localName))
157         {
158           readAttribute(reader, addOperation, DataSourceComponent.JNDI_NAME);
159         }
160         else if (DataSourceComponent.DROP_TABLE.getXmlName().equals(localName))
161         {
162           readAttribute(reader, addOperation, DataSourceComponent.DROP_TABLE);
163         }
164         else if (DataSourceComponent.CREATE_TABLE.getXmlName()
165             .equals(localName))
166         {
167           readAttribute(reader, addOperation, DataSourceComponent.CREATE_TABLE);
168         }
169         else
170         {
171           throw ParseUtils.unexpectedElement(reader);
172         }
173       }
174     }
175   }
176 
177   public void write(final XMLExtendedStreamWriter writer, final ModelNode config)
178     throws XMLStreamException
179   {
180     if (isAtLeastOneDefined(config, DEFINITIONS))
181     {
182       writer.writeStartElement(DataSourceComponent.DS_ELEMENT_NAME);
183 
184       for (final AttributeDefinition definition : DEFINITIONS)
185       {
186         writeAttribute(writer, config, definition);
187       }
188 
189       writer.writeEndElement();
190     }
191   }
192 
193   public void apply(final PropertiesConfiguration propertiesConfig,
194       final ModelNode model)
195   {
196     final DataSourceConfiguration dataSourceConfig =
197         propertiesConfig.getDataSourceConfiguration();
198 
199     final String jndiName = getAttribute(model, JNDI_NAME);
200     dataSourceConfig.setJndiName(jndiName);
201 
202     final String createTableAtt = getAttribute(model, CREATE_TABLE);
203     if (createTableAtt != null)
204     {
205       final boolean createTable = Boolean.parseBoolean(createTableAtt);
206       dataSourceConfig.setCreateTable(createTable);
207     }
208 
209     final String dropTableAtt = getAttribute(model, DROP_TABLE);
210     if (dropTableAtt != null)
211     {
212       final boolean dropTable = Boolean.parseBoolean(dropTableAtt);
213       dataSourceConfig.setDropTable(dropTable);
214     }
215   }
216 
217   // --- object basics --------------------------------------------------------
218 
219 }