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 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   * Base class for components.
32   */
33  public abstract class AbstractComponent
34  {
35    // ********************************* Fields *********************************
36  
37    // --- constants ------------------------------------------------------------
38  
39    // --- members --------------------------------------------------------------
40  
41    /**
42     * The write handler for all attributes of this component.
43     */
44    protected final OperationStepHandler writeHandler;
45  
46    // ****************************** Initializer *******************************
47  
48    // ****************************** Constructors ******************************
49  
50    /**
51     * Default constructor.
52     */
53    protected AbstractComponent(final OperationStepHandler writeHandler)
54    {
55      this.writeHandler = writeHandler;
56    }
57  
58    // ****************************** Inner Classes *****************************
59  
60    // ********************************* Methods ********************************
61  
62    // --- init -----------------------------------------------------------------
63  
64    // --- get&set --------------------------------------------------------------
65  
66    // --- business -------------------------------------------------------------
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   // --- object basics --------------------------------------------------------
134 
135 }