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 java.util.Map;
22
23 import javax.xml.stream.Location;
24 import javax.xml.stream.XMLStreamException;
25
26 import org.jboss.as.controller.AttributeDefinition;
27 import org.jboss.as.controller.OperationFailedException;
28 import org.jboss.as.controller.OperationStepHandler;
29 import org.jboss.as.controller.parsing.ParseUtils;
30 import org.jboss.as.controller.registry.ManagementResourceRegistration;
31 import org.jboss.dmr.ModelNode;
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.jboss.extension.resources.util.SimpleMapAttributeDefinition;
37 import de.smartics.properties.spi.config.config.PropertiesConfiguration;
38 import de.smartics.properties.spi.core.config.FactoriesConfiguration;
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 public class FactoriesComponent extends AbstractComponent
58 {
59
60
61
62
63
64
65
66
67
68
69
70 public static final String ROOT_ELEMENT_NAME = "factories";
71
72
73
74
75
76 public static final SimpleMapAttributeDefinition FACTORIES =
77 new SimpleMapAttributeDefinition.Builder().withName("factories")
78 .withAllowNull(true).withAllowExpression(true)
79 .withWrapperXmlName("factories").withXmlName("factory")
80 .withXmlAttributeName("type").build();
81
82 private static final AttributeDefinition[] DEFINITIONS = { FACTORIES };
83
84
85
86
87
88
89
90
91
92
93 public FactoriesComponent(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(FACTORIES, null,
112 writeHandler);
113 }
114
115 public void populateModel(final ModelNode operation, final ModelNode model)
116 throws OperationFailedException
117 {
118 FACTORIES.validateAndSet(operation, model);
119 }
120
121 public void read(final XMLExtendedStreamReader reader,
122 final ModelNode addOperation) throws XMLStreamException
123 {
124 while (reader.hasNext() && reader.nextTag() != END_ELEMENT)
125 {
126 if (reader.isStartElement())
127 {
128 final String localName = reader.getLocalName();
129
130 if (FactoriesComponent.FACTORIES.getXmlName().equals(localName))
131 {
132 final String factoryType =
133 reader.getAttributeValue(null, FACTORIES.getXmlAttributeName());
134 final Location location = reader.getLocation();
135 final String factoryImpl = getContents(reader);
136 FACTORIES.parseAndAddParameterElement(factoryType, factoryImpl,
137 addOperation, location);
138 }
139 else
140 {
141 throw ParseUtils.unexpectedElement(reader);
142 }
143 }
144 }
145 }
146
147 private String getContents(final XMLExtendedStreamReader reader)
148 throws XMLStreamException
149 {
150 String text = null;
151 if (reader.hasNext() && reader.next() == CHARACTERS)
152 {
153 text = normalize(reader.getText());
154 reader.nextTag();
155 }
156 return text;
157 }
158
159 public void write(final XMLExtendedStreamWriter writer, final ModelNode config)
160 throws XMLStreamException
161 {
162 if (isAtLeastOneDefined(config, DEFINITIONS))
163 {
164 FACTORIES.marshallAsElement(config, writer);
165 }
166 }
167
168 public void apply(final PropertiesConfiguration propertiesConfig,
169 final ModelNode model)
170 {
171 final FactoriesConfiguration factoriesConfig =
172 propertiesConfig.getFactoriesConfiguration();
173
174 final Map<String, String> map = FACTORIES.toStringMap(model);
175 for (final Map.Entry<String, String> entry : map.entrySet())
176 {
177 final String factoryTypeName = entry.getKey();
178 final String factoryImplementationTypeName = entry.getValue();
179 factoriesConfig.add(factoryTypeName, factoryImplementationTypeName);
180 }
181
182 addDefaults(propertiesConfig);
183 }
184
185 private void addDefaults(PropertiesConfiguration propertiesConfig)
186 {
187 final FactoriesConfiguration factoriesConfig =
188 propertiesConfig.getFactoriesConfiguration();
189
190 final String configurationKeyContext =
191 propertiesConfig.getDataSourceConfiguration().isUnset()
192 ? "de.smartics.properties.impl.config.properties.PropertiesConfigurationPropertiesFactory"
193 : "de.smartics.properties.impl.config.ds.AutodetectDataSourceConfigurationPropertiesFactory";
194 factoriesConfig.addDefault(
195 "de.smartics.properties.api.config.app.ConfigurationPropertiesFactory",
196 configurationKeyContext);
197
198 if (!propertiesConfig.getAdminResourceConfiguration().isUnset())
199 {
200 factoriesConfig
201 .addDefault(
202 "de.smartics.properties.resource.repository.ResourceRepository",
203 "de.smartics.properties.resource.maven.repository.SessionedMavenResourceRepository");
204 }
205
206 final String propertyValueSecurity =
207 propertiesConfig.getSecurityConfiguration().isUnset()
208 ? "de.smartics.properties.api.core.security.Base64PropertyValueSecurity"
209 : "de.smartics.properties.api.core.security.PropertiesBasedPropertyValueSecurity";
210 factoriesConfig.addDefault(
211 "de.smartics.properties.api.core.security.PropertyValueSecurity",
212 propertyValueSecurity);
213
214 final CacheConfiguration cacheConfiguration =
215 propertiesConfig.getCacheConfiguration();
216 final String cacheManager =
217 (!cacheConfiguration.isUnset() && cacheConfiguration.getJndiName()
218 .contains("/infinispan"))
219 ? "de.smartics.properties.spi.config.cache.infinispan.JndiInfinispanCompoundKeyCacheManager"
220 : "de.smartics.properties.spi.config.cache.InMemoryCacheManager";
221 factoriesConfig.addDefault(
222 "de.smartics.properties.spi.config.cache.CacheManager", cacheManager);
223
224 factoriesConfig
225 .addDefault(
226 "de.smartics.properties.spi.config.domain.key.ConfigurationKeyContext",
227 "de.smartics.properties.impl.config.domain.key.envapp.EnvAppConfigurationKeyContext");
228
229 factoriesConfig.addDefault(
230 "de.smartics.util.lang.classpath.ClassPathListing",
231 "de.smartics.jboss.vfs.VfsClassPathListing");
232 }
233
234
235
236 }