View Javadoc

1   /*
2    * Copyright 2012-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.api.config.app;
17  
18  import java.io.Serializable;
19  
20  import javax.annotation.concurrent.ThreadSafe;
21  
22  import org.apache.commons.lang.builder.ToStringBuilder;
23  
24  import de.smartics.properties.api.core.domain.ConfigException;
25  import de.smartics.properties.api.core.security.Base64PropertyValueSecurity;
26  import de.smartics.properties.api.core.security.PropertyValueSecurity;
27  import de.smartics.properties.api.core.security.PropertyValueSecurityFactory;
28  
29  /**
30   * Provides properties to configure an instance of
31   * {@link de.smartics.properties.api.config.app.ConfigurationPropertiesFactory}.
32   */
33  @ThreadSafe
34  public final class FactoryConfiguration implements Serializable
35  {
36    // ********************************* Fields *********************************
37  
38    // --- constants ------------------------------------------------------------
39  
40    /**
41     * The class version identifier.
42     */
43    private static final long serialVersionUID = 1L;
44  
45    // --- members --------------------------------------------------------------
46  
47    /**
48     * The flag to tell whether (<code>true</code>) or not (<code>false</code>)
49     * the URLs of the context class loader should be added.
50     *
51     * @serial
52     */
53    private volatile boolean addDefaultRootLocations = true;
54  
55    /**
56     * The flag indicates that loading properties form the class path is to be
57     * skipped.
58     *
59     * @serial
60     */
61    private volatile boolean skipClassPathPropertyLoading;
62  
63    /**
64     * The flag indicates that a cache should be provided, if supported.
65     *
66     * @serial
67     */
68    private volatile boolean useCache = true;
69  
70    /**
71     * The helper to decrypt secured property values.
72     *
73     * @serial
74     */
75    private volatile PropertyValueSecurity decrypter = createDecrypter();
76  
77    // ****************************** Initializer *******************************
78  
79    // ****************************** Constructors ******************************
80  
81    /**
82     * Default constructor.
83     */
84    public FactoryConfiguration()
85    {
86    }
87  
88    // ****************************** Inner Classes *****************************
89  
90    // ********************************* Methods ********************************
91  
92    // --- init -----------------------------------------------------------------
93  
94    // --- get&set --------------------------------------------------------------
95  
96    /**
97     * Returns the flag to tell whether (<code>true</code>) or not (
98     * <code>false</code>) the URLs of the context class loader should be added.
99     *
100    * @return the flag to tell whether (<code>true</code>) or not (
101    *         <code>false</code>) the URLs of the context class loader should be
102    *         added.
103    */
104   public boolean isAddDefaultRootLocations()
105   {
106     return addDefaultRootLocations;
107   }
108 
109   /**
110    * Sets the flag to tell whether (<code>true</code>) or not (
111    * <code>false</code>) the URLs of the context class loader should be added.
112    *
113    * @param addDefaultRootLocations the flag to tell whether (<code>true</code>)
114    *          or not (<code>false</code>) the URLs of the context class loader
115    *          should be added.
116    */
117   public void setAddDefaultRootLocations(final boolean addDefaultRootLocations)
118   {
119     this.addDefaultRootLocations = addDefaultRootLocations;
120   }
121 
122   /**
123    * Returns the flag indicates that loading properties form the class path is
124    * to be skipped.
125    *
126    * @return the flag indicates that loading properties form the class path is
127    *         to be skipped.
128    */
129   public boolean isSkipClassPathPropertyLoading()
130   {
131     return skipClassPathPropertyLoading;
132   }
133 
134   /**
135    * Sets the flag indicates that loading properties from the class path is to
136    * be skipped.
137    *
138    * @param skipClassPathPropertyLoading the flag indicates that loading
139    *          properties from the class path is to be skipped.
140    */
141   public void setSkipClassPathPropertyLoading(
142       final boolean skipClassPathPropertyLoading)
143   {
144     this.skipClassPathPropertyLoading = skipClassPathPropertyLoading;
145   }
146 
147   /**
148    * Returns the flag indicates that a cache should be provided, if supported.
149    *
150    * @return the flag indicates that a cache should be provided, if supported.
151    */
152   public boolean isUseCache()
153   {
154     return useCache;
155   }
156 
157   /**
158    * Sets the flag indicates that a cache should be provided, if supported.
159    *
160    * @param useCache the flag indicates that a cache should be provided, if
161    *          supported.
162    */
163   public void setUseCache(final boolean useCache)
164   {
165     this.useCache = useCache;
166   }
167 
168   /**
169    * Returns the helper to decrypt secured property values.
170    *
171    * @return the helper to decrypt secured property values.
172    */
173   public PropertyValueSecurity getDecrypter()
174   {
175     return decrypter;
176   }
177 
178   /**
179    * Sets the helper to decrypt secured property values.
180    *
181    * @param decrypter the helper to decrypt secured property values.
182    */
183   public void setDecrypter(final PropertyValueSecurity decrypter)
184   {
185     this.decrypter = decrypter;
186   }
187 
188   // --- business -------------------------------------------------------------
189 
190   /**
191    * Creates an instance of {@link PropertyValueSecurity} as registered in
192    * <code>META-INF/services/de.smartics.properties.api.core.security.PropertyValueSecurity</code>
193    * or via JNDI configuration in the <code>factories</code> section.
194    * <p>
195    * Issues a warning if more than one implementation is found. If no
196    * implementation is configured, a non-secure instance is returned.
197    * </p>
198    *
199    * @return the instance of a decrypter. Never <code>null</code>.
200    */
201   private PropertyValueSecurity createDecrypter()
202   {
203     final PropertyValueSecurityFactory factory =
204         new PropertyValueSecurityFactory();
205     final Class<Base64PropertyValueSecurity> defaultType =
206         Base64PropertyValueSecurity.class;
207     try
208     {
209       final PropertyValueSecurity security = factory.create(defaultType);
210       return security;
211     }
212     catch (final ConfigException e)
213     {
214       return factory.createBackup(defaultType);
215     }
216   }
217 
218   // --- object basics --------------------------------------------------------
219 
220   /**
221    * {@inheritDoc}
222    * <p>
223    * Provides the properties via reflection for displaying debug information.
224    * </p>
225    * <p>
226    * Please note that calling this method may not be thread-safe. The flag
227    * printed may not reflect an actual state or a state that ever existed.
228    * </p>
229    *
230    * @see java.lang.Object#toString()
231    */
232   @Override
233   public String toString()
234   {
235     return ToStringBuilder.reflectionToString(this);
236   }
237 }