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.config.transfer.templatestream;
17  
18  import org.apache.commons.lang.StringEscapeUtils;
19  import org.apache.commons.lang.StringUtils;
20  
21  /**
22   * Creates instances of {@link StreamPropertySink} by providing a template to
23   * generate an XML document for adding properties to an JNDI context.
24   */
25  public class JBoss7NamingFactory extends Factory
26  {
27    // ********************************* Fields *********************************
28  
29    // --- constants ------------------------------------------------------------
30  
31    /**
32     * The default JNDI prefix to store properties.
33     * <p>
34     * The value of this constant is {@value}.
35     * </p>
36     */
37    public static final String DEFAULT_JNDI_PREFIX =
38        "java:/smartics-properties/config";
39  
40    // --- members --------------------------------------------------------------
41  
42    /**
43     * The prefix to the JNDI context to store properties.
44     */
45    private String jndiPrefix = DEFAULT_JNDI_PREFIX;
46  
47    /**
48     * Escaper of config keys, property names and property values.
49     */
50    private final ValueEscaper escaper = new JndiValueEscaper();
51  
52    // ****************************** Initializer *******************************
53  
54    // ****************************** Constructors ******************************
55  
56    /**
57     * Default constructor.
58     */
59    public JBoss7NamingFactory()
60    {
61      setLineIntro("               <!-- source: ${location} -->${newline}");
62      setEscaper(escaper);
63  
64      internalSetTemplate(jndiPrefix);
65    }
66  
67    // ****************************** Inner Classes *****************************
68  
69    /**
70     * Escapes JNDI values.
71     */
72    private final class JndiValueEscaper implements ValueEscaper
73    {
74      @Override
75      public String escapeConfigKey(final String value)
76      {
77        final String normValue = StringUtils.replace(value, "/", "|");
78        return escapeValue(normValue);
79      }
80  
81      @Override
82      public String escapeName(final String value)
83      {
84        final String normValue = StringUtils.replace(value, ".", "/");
85        return escapeValue(normValue);
86      }
87  
88      @Override
89      public String escapeValue(final String value)
90      {
91        return StringEscapeUtils.escapeXml(value);
92      }
93    }
94  
95    // ********************************* Methods ********************************
96  
97    // --- init -----------------------------------------------------------------
98  
99    private void internalSetTemplate(final String jndiPrefix)
100   {
101     final String template =
102         "               <simple name=\""
103             + jndiPrefix
104             + "/${configKey}/${name}\" value=\"${value}\" type=\"java.lang.String\"/>${newline}";
105     setTemplate(template);
106   }
107 
108   // --- get&set --------------------------------------------------------------
109 
110   /**
111    * Returns the prefix to the JNDI context to store properties.
112    *
113    * @return the prefix to the JNDI context to store properties.
114    */
115   public String getJndiPrefix()
116   {
117     return jndiPrefix;
118   }
119 
120   /**
121    * Sets the prefix to the JNDI context to store properties.
122    *
123    * @param jndiPrefix the prefix to the JNDI context to store properties.
124    */
125   public void setJndiPrefix(final String jndiPrefix)
126   {
127     this.jndiPrefix = jndiPrefix;
128     internalSetTemplate(jndiPrefix);
129   }
130 
131   // --- business -------------------------------------------------------------
132 
133   /**
134    * {@inheritDoc}
135    *
136    * @throws IllegalStateException on any problem opening the stream.
137    */
138   @Override
139   public StreamPropertySink create() throws IllegalStateException
140   {
141     setDocumentIntro("<!-- Add this XML fragment inside the ${newline}"
142                      + "  <subsystem xmlns=\"urn:jboss:domain:naming:1.1\">${newline}"
143                      + "    <bindings>${newline}" + "-->${newline}${newline}");
144     setDocumentExtro("<!--${newline}    </bindings>${newline}    </subsystem>${newline}-->${newline}");
145 
146     return super.create();
147   }
148 
149   // --- object basics --------------------------------------------------------
150 
151 }