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.CHARACTERS;
19 import static javax.xml.stream.XMLStreamConstants.END_ELEMENT;
20
21 import javax.xml.stream.XMLStreamException;
22
23 import org.jboss.as.controller.AttributeDefinition;
24 import org.jboss.as.controller.OperationStepHandler;
25 import org.jboss.as.controller.SimpleAttributeDefinition;
26 import org.jboss.dmr.ModelNode;
27 import org.jboss.staxmapper.XMLExtendedStreamReader;
28 import org.jboss.staxmapper.XMLExtendedStreamWriter;
29
30
31
32
33 public abstract class AbstractComponent
34 {
35
36
37
38
39
40
41
42
43
44 protected final OperationStepHandler writeHandler;
45
46
47
48
49
50
51
52
53 protected AbstractComponent(final OperationStepHandler writeHandler)
54 {
55 this.writeHandler = writeHandler;
56 }
57
58
59
60
61
62
63
64
65
66
67
68 protected static final void readAttribute(
69 final XMLExtendedStreamReader reader, final ModelNode addOperation,
70 final SimpleAttributeDefinition definition) throws XMLStreamException
71 {
72 final StringBuilder buffer = new StringBuilder();
73 while (reader.hasNext() && reader.next() != END_ELEMENT)
74 {
75 if (reader.getEventType() == CHARACTERS)
76 {
77 final String rawText = reader.getText();
78 final String text = normalize(rawText);
79 buffer.append(text);
80 }
81 }
82 if (buffer.length() > 0)
83 {
84 final String text = buffer.toString();
85 definition.parseAndSetParameter(text, addOperation, reader);
86 }
87 }
88
89 protected static final String normalize(final String rawText)
90 {
91 final String text;
92 if (rawText != null)
93 {
94 text = rawText.trim();
95 }
96 else
97 {
98 text = null;
99 }
100 return text;
101 }
102
103 protected static final void writeAttribute(
104 final XMLExtendedStreamWriter writer, final ModelNode resource,
105 final AttributeDefinition definition) throws XMLStreamException
106 {
107 definition.marshallAsElement(resource, writer);
108 }
109
110 protected static final String getAttribute(final ModelNode resource,
111 final AttributeDefinition definition)
112 {
113 final String attributeId = definition.getName();
114 final ModelNode attribute = resource.get(attributeId);
115 final String rawText = attribute.isDefined() ? attribute.asString() : null;
116 return normalize(rawText);
117 }
118
119 protected static final boolean isAtLeastOneDefined(final ModelNode config,
120 final AttributeDefinition[] definitions)
121 {
122 for (final AttributeDefinition definition : definitions)
123 {
124 if (config.get(definition.getName()).isDefined())
125 {
126 return true;
127 }
128 }
129
130 return false;
131 }
132
133
134
135 }