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.cache;
17  
18  import java.util.Properties;
19  
20  import org.apache.commons.lang.StringUtils;
21  
22  /**
23   * The cache configuration information.
24   */
25  public final class CacheConfiguration
26  {
27    // ********************************* Fields *********************************
28  
29    // --- constants ------------------------------------------------------------
30  
31    /**
32     * The identifier of the cache configuration that provides the properties.
33     * <p>
34     * The value of this constant is {@value}.
35     * </p>
36     */
37    public static final String CACHE_ID = "de.smartics.properties.cache.id";
38  
39    /**
40     * The key to the JNDI name of the cache container. The key is used to lookup
41     * values in the JNDI or in properties configuration files.
42     * <p>
43     * The value of this constant is {@value}.
44     * </p>
45     */
46    public static final String JNDI_NAME =
47        "de.smartics.properties.cache.jndiName";
48  
49    /**
50     * The key to the cache name. The key is used to lookup values in the JNDI or
51     * in properties configuration files.
52     * <p>
53     * The value of this constant is {@value}.
54     * </p>
55     */
56    public static final String CACHE_NAME =
57        "de.smartics.properties.cache.cacheName";
58  
59    // --- members --------------------------------------------------------------
60  
61    /**
62     * The unique identifier of the cache.
63     */
64    private String cacheId;
65  
66    /**
67     * The name to lookup the cache container instance in the JNDI.
68     */
69    private String jndiName;
70  
71    /**
72     * The name of the cache to use within the cache container stored in the JNDI.
73     */
74    private String cacheName;
75  
76    // ****************************** Initializer *******************************
77  
78    // ****************************** Constructors ******************************
79  
80    /**
81     * Default constructor.
82     */
83    public CacheConfiguration()
84    {
85    }
86  
87    /**
88     * Convenience constructor passing in a properties instance with properties to
89     * set by their keys. Look at the keys defined as constants of this class for
90     * possible values.
91     *
92     * @param properties the properties to set.
93     */
94    public CacheConfiguration(final Properties properties)
95    {
96      this.setCacheId(properties.getProperty(CacheConfiguration.CACHE_ID));
97      this.jndiName = properties.getProperty(CacheConfiguration.JNDI_NAME);
98      this.cacheName = properties.getProperty(CacheConfiguration.CACHE_NAME);
99    }
100 
101   // ****************************** Inner Classes *****************************
102 
103   // ********************************* Methods ********************************
104 
105   // --- init -----------------------------------------------------------------
106 
107   // --- get&set --------------------------------------------------------------
108 
109   /**
110    * Returns the unique identifier of the cache.
111    *
112    * @return the unique identifier of the cache.
113    */
114   public String getCacheId()
115   {
116     return cacheId;
117   }
118 
119   /**
120    * Sets the unique identifier of the cache.
121    *
122    * @param cacheId the unique identifier of the cache.
123    */
124   public void setCacheId(final String cacheId)
125   {
126     this.cacheId = cacheId;
127   }
128 
129   /**
130    * Returns the name to lookup the cache container instance in the JNDI.
131    *
132    * @return the name to lookup the cache container instance in the JNDI.
133    */
134   public String getJndiName()
135   {
136     return jndiName;
137   }
138 
139   /**
140    * Sets the name to lookup the cache container instance in the JNDI.
141    *
142    * @param jndiName the name to lookup container the cache instance in the
143    *          JNDI.
144    */
145   public void setJndiName(final String jndiName)
146   {
147     this.jndiName = jndiName;
148   }
149 
150   /**
151    * Returns the name of the cache to use within the cache container stored in
152    * the JNDI.
153    *
154    * @return the name of the cache to use within the cache container stored in
155    *         the JNDI.
156    */
157   public String getCacheName()
158   {
159     return cacheName;
160   }
161 
162   /**
163    * Sets the name of the cache to use within the cache container stored in the
164    * JNDI.
165    *
166    * @param cacheName the name of the cache to use within the cache container
167    *          stored in the JNDI.
168    */
169   public void setCacheName(final String cacheName)
170   {
171     this.cacheName = cacheName;
172   }
173 
174   // --- business -------------------------------------------------------------
175 
176   /**
177    * Checks whether or not the configuration has value.
178    *
179    * @return <code>true</code> if the configuration is unset, <code>false</code>
180    *         otherwise.
181    */
182   public boolean isUnset()
183   {
184     return StringUtils.isBlank(jndiName);
185   }
186 
187   // --- object basics --------------------------------------------------------
188 
189   @Override
190   public String toString()
191   {
192     return "cache: " + cacheId + '/' + jndiName + ':' + cacheName;
193   }
194 }