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.cache.CacheConfiguration;
36 import de.smartics.properties.spi.config.config.PropertiesConfiguration;
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 public final class CacheComponent extends AbstractComponent
52 {
53
54
55
56
57
58
59
60
61
62
63 public static final String CACHE_ELEMENT_NAME = "cache";
64
65
66
67
68 public static final SimpleAttributeDefinition JNDI_NAME =
69 new SimpleAttributeDefinitionBuilder("cache.jndiName", ModelType.STRING,
70 true).setXmlName("jndiName").setAllowExpression(true)
71 .setFlags(AttributeAccess.Flag.RESTART_ALL_SERVICES).build();
72
73
74
75
76 public static final SimpleAttributeDefinition CACHE_NAME =
77 new SimpleAttributeDefinitionBuilder("cache.cacheName", ModelType.STRING,
78 true).setXmlName("cacheName").setAllowExpression(true)
79 .setFlags(AttributeAccess.Flag.RESTART_ALL_SERVICES).build();
80
81 private static final AttributeDefinition[] DEFINITIONS = { JNDI_NAME,
82 CACHE_NAME };
83
84
85
86
87
88
89
90
91
92
93 public CacheComponent(final OperationStepHandler writeHandler)
94 {
95 super(writeHandler);
96 }
97
98
99
100
101
102
103
104
105
106
107
108 public void registerAttributes(
109 final ManagementResourceRegistration resourceRegistration)
110 {
111 resourceRegistration.registerReadWriteAttribute(JNDI_NAME, null,
112 writeHandler);
113 resourceRegistration.registerReadWriteAttribute(CACHE_NAME, null,
114 writeHandler);
115 }
116
117 public void populateModel(final ModelNode operation, final ModelNode model)
118 throws OperationFailedException
119 {
120 JNDI_NAME.validateAndSet(operation, model);
121 CACHE_NAME.validateAndSet(operation, model);
122 }
123
124 public void read(final XMLExtendedStreamReader reader,
125 final ModelNode addOperation) throws XMLStreamException
126 {
127 ParseUtils.requireNoAttributes(reader);
128
129 while (reader.hasNext() && reader.nextTag() != END_ELEMENT)
130 {
131 if (reader.isStartElement())
132 {
133 final String localName = reader.getLocalName();
134
135 if (localName.equals(JNDI_NAME.getXmlName()))
136 {
137 readAttribute(reader, addOperation, JNDI_NAME);
138 }
139 else if (localName.equals(CACHE_NAME.getXmlName()))
140 {
141 readAttribute(reader, addOperation, CACHE_NAME);
142 }
143 else
144 {
145 throw ParseUtils.unexpectedElement(reader);
146 }
147 }
148 }
149 }
150
151 public void write(final XMLExtendedStreamWriter writer, final ModelNode config)
152 throws XMLStreamException
153 {
154 if (isAtLeastOneDefined(config, DEFINITIONS))
155 {
156 writer.writeStartElement(CACHE_ELEMENT_NAME);
157
158 for (final AttributeDefinition definition : DEFINITIONS)
159 {
160 writeAttribute(writer, config, definition);
161 }
162
163 writer.writeEndElement();
164 }
165 }
166
167 public void apply(final String identifier,
168 final PropertiesConfiguration propertiesConfig, final ModelNode model)
169 {
170 final CacheConfiguration cacheConfig =
171 propertiesConfig.getCacheConfiguration();
172 cacheConfig.setCacheId(identifier);
173 final String jndiName = getAttribute(model, JNDI_NAME);
174 cacheConfig.setJndiName(jndiName);
175 final String cacheName = getAttribute(model, CACHE_NAME);
176 cacheConfig.setCacheName(cacheName);
177 }
178
179
180
181 }