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.spi.config.config.AdminResourceConfiguration;
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
55 public class AdminResourceComponent extends AbstractComponent
56 {
57
58
59
60
61
62
63
64
65
66
67
68 public static final String ROOT_ELEMENT_NAME = "adminResource";
69
70
71
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
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
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
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
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
122
123
124
125
126
127
128
129
130 public AdminResourceComponent(final OperationStepHandler writeHandler)
131 {
132 super(writeHandler);
133 }
134
135
136
137
138
139
140
141
142
143
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
249
250 }