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.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   * Defines the cache information of a configuration.
40   * <p>
41   * Example XML fragment of a cache within a configuration:
42   * </p>
43   *
44   * <pre>
45   * {@markupExample "Cache Element"
46   * &lt;cache&gt;
47   *   &lt;jndiName&gt;java:jboss/infinispan/container/smartics-properties&lt;/jndiName&gt;
48   * &lt;/cache&gt;}
49   * </pre>
50   */
51  public final class CacheComponent extends AbstractComponent
52  {
53    // ********************************* Fields *********************************
54  
55    // --- constants ------------------------------------------------------------
56  
57    /**
58     * The name of the configuration XML element that holds the cache information.
59     * <p>
60     * The value of this constant is {@value}.
61     * </p>
62     */
63    public static final String CACHE_ELEMENT_NAME = "cache";
64  
65    /**
66     * The attribute that holds the JNDI name of the cache container.
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     * The attribute that holds the name of the cache within the container.
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    // --- members --------------------------------------------------------------
85  
86    // ****************************** Initializer *******************************
87  
88    // ****************************** Constructors ******************************
89  
90    /**
91     * Default constructor.
92     */
93    public CacheComponent(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(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   // --- object basics --------------------------------------------------------
180 
181 }