1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 public final class DataSourceComponent extends AbstractComponent
55 {
56
57
58
59
60
61
62
63
64
65
66
67 public static final String DS_ELEMENT_NAME = "datasource";
68
69
70
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
80
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
90
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
103
104
105
106
107
108
109
110
111 public DataSourceComponent(final OperationStepHandler writeHandler)
112 {
113 super(writeHandler);
114 }
115
116
117
118
119
120
121
122
123
124
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
218
219 }