View Javadoc

1   /*
2    * Copyright 2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Defines the factories information of a configuration. This configuration adds
42   * information about factories to be created as the Service API of Java in
43   * <code>META-INF/services</code> does.
44   * <p>
45   * Example XML fragment of a factories element within a configuration:
46   * </p>
47   *
48   * <pre>
49   * {@markupExample "Factories Element"
50   * &lt;factories&gt;
51   *   &lt;factory type=&quot;de.smartics.properties.api.config.app.ConfigurationPropertiesFactory&quot;&gt;
52   *     de.smartics.properties.impl.config.jndi.JndiConfigurationPropertiesFactory
53   *   &lt;/factory&gt;
54   * &lt;/factories&gt;}
55   * </pre>
56   */
57  public class FactoriesComponent extends AbstractComponent
58  {
59    // ********************************* Fields *********************************
60  
61    // --- constants ------------------------------------------------------------
62  
63    /**
64     * The name of the configuration XML element that holds the factories
65     * information.
66     * <p>
67     * The value of this constant is {@value}.
68     * </p>
69     */
70    public static final String ROOT_ELEMENT_NAME = "factories";
71  
72    /**
73     * The attribute that holds the factories. The interface type maps to the
74     * implementation type.
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    // --- members --------------------------------------------------------------
85  
86    // ****************************** Initializer *******************************
87  
88    // ****************************** Constructors ******************************
89  
90    /**
91     * Default constructor.
92     */
93    public FactoriesComponent(final OperationStepHandler writeHandler)
94    {
95      super(writeHandler);
96    }
97  
98    // ****************************** Inner Classes *****************************
99  
100   // ********************************* Methods ********************************
101 
102   // --- init -----------------------------------------------------------------
103 
104   // --- get&set --------------------------------------------------------------
105 
106   // --- business -------------------------------------------------------------
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   // --- object basics --------------------------------------------------------
235 
236 }