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.ds;
17  
18  import java.io.Serializable;
19  import java.util.Properties;
20  
21  import org.apache.commons.lang.StringUtils;
22  
23  import de.smartics.util.lang.Arg;
24  
25  /**
26   * Configuration to access a data source via JNDI.
27   * <p>
28   * The following properties are valid:
29   * </p>
30   * <ol>
31   * <li><code>{@value #JNDI_NAME}</code> - the name of the data source in the
32   * JNDI.</li>
33   * <li><code>{@value #DROP_TABLE}</code> - flag to drop the configuration table
34   * if set to <code>true</code> (usually this should only be used in test
35   * environments).</li>
36   * <li><code>{@value #CREATE_TABLE}</code> - flag to create the configuration
37   * table if set to <code>true</code>.</li>
38   * </ol>
39   * <p>
40   * The property {@link #CONFIG_SOURCE_ID} allows to track the source of the
41   * configuration information.
42   * </p>
43   */
44  public final class DataSourceConfiguration implements Serializable
45  {
46    // ********************************* Fields *********************************
47  
48    // --- constants ------------------------------------------------------------
49  
50    /**
51     * The class version identifier.
52     */
53    private static final long serialVersionUID = 1L;
54  
55    /**
56     * The identifier of the configuration for the data source.
57     */
58    public static final String CONFIG_SOURCE_ID = "de.smartics.properties.ds.id";
59  
60    /**
61     * The key to the JNDI name of the data source.
62     */
63    public static final String JNDI_NAME = "de.smartics.properties.ds.jndiName";
64  
65    /**
66     * The key to flag to drop the configuration table in the data source.
67     */
68    public static final String DROP_TABLE = "de.smartics.properties.ds.dropTable";
69  
70    /**
71     * The key to flag to create the configuration table in the data source.
72     */
73    public static final String CREATE_TABLE =
74        "de.smartics.properties.ds.createTable";
75  
76    // --- members --------------------------------------------------------------
77  
78    /**
79     * The source of the data source configuration.
80     *
81     * @serial
82     */
83    private String configSourceId;
84  
85    /**
86     * The name of the data source to lookup in a JNDI context.
87     *
88     * @serial
89     */
90    private String jndiName;
91  
92    /**
93     * The flag to drop the configuration table if set to <code>true</code>.
94     *
95     * @serial
96     */
97    private boolean dropTable;
98  
99    /**
100    * The flag to create the configuration if set to <code>true</code>.
101    *
102    * @serial
103    */
104   private boolean createTable;
105 
106   // ****************************** Initializer *******************************
107 
108   // ****************************** Constructors ******************************
109 
110   /**
111    * Default constructor.
112    *
113    * @param configSourceId the source of the data source configuration.
114    * @param jndiName the jndi name.
115    * @param dropTable the boolean whether or not existing tables shall be
116    *          dropped.
117    * @param createTable the boolean whether or the tables shall be created, if
118    *          they do not exist already
119    */
120   public DataSourceConfiguration(final String configSourceId,
121       final String jndiName, final boolean dropTable, final boolean createTable)
122   {
123     this.configSourceId = Arg.checkNotBlank("configSourceId", configSourceId);
124     this.jndiName = Arg.checkNotBlank("jndiName", jndiName);
125     this.dropTable = dropTable;
126     this.createTable = createTable;
127   }
128 
129   /**
130    * Default constructor.
131    *
132    * @param properties the properties containing jndiName, dropTable and
133    *          createTable.
134    */
135   public DataSourceConfiguration(final Properties properties)
136   {
137     this(Arg.checkNotBlank(CONFIG_SOURCE_ID,
138         properties.getProperty(CONFIG_SOURCE_ID)), Arg.checkNotBlank(JNDI_NAME,
139         properties.getProperty(JNDI_NAME)), Boolean.parseBoolean(Arg
140         .checkNotBlank(DROP_TABLE, properties.getProperty(DROP_TABLE))),
141         Boolean.parseBoolean(Arg.checkNotBlank(CREATE_TABLE,
142             properties.getProperty(CREATE_TABLE))));
143 
144   }
145 
146   /**
147    * Convenience constructor to construct the instance first and set properties
148    * afterwards. The client is responsible to check that all properties are
149    * valid before its first use.
150    */
151   public DataSourceConfiguration()
152   {
153   }
154 
155   // ****************************** Inner Classes *****************************
156 
157   // ********************************* Methods ********************************
158 
159   // --- init -----------------------------------------------------------------
160 
161   // --- get&set --------------------------------------------------------------
162 
163   /**
164    * Returns the source of the data source configuration.
165    *
166    * @return the source of the data source configuration.
167    */
168   public String getConfigSourceId()
169   {
170     return configSourceId;
171   }
172 
173   /**
174    * Returns the name of the data source to lookup in a JNDI context.
175    *
176    * @return the name of the data source to lookup in a JNDI context.
177    */
178   public String getJndiName()
179   {
180     return jndiName;
181   }
182 
183   /**
184    * Sets the name of the data source to lookup in a JNDI context.
185    *
186    * @param jndiName the name of the data source to lookup in a JNDI context.
187    */
188   public void setJndiName(final String jndiName)
189   {
190     this.jndiName = jndiName;
191   }
192 
193   /**
194    * Returns the flag to drop the configuration table if set to
195    * <code>true</code>.
196    *
197    * @return the flag to drop tables if set to <code>true</code>.
198    */
199   public boolean isDropTable()
200   {
201     return dropTable;
202   }
203 
204   /**
205    * Sets the flag to drop the configuration table if set to <code>true</code>.
206    *
207    * @param dropTable the flag to drop the configuration table if set to
208    *          <code>true</code>.
209    */
210   public void setDropTable(final boolean dropTable)
211   {
212     this.dropTable = dropTable;
213   }
214 
215   /**
216    * Returns the flag to create the configuration table if set to
217    * <code>true</code>.
218    *
219    * @return the flag to create tables if set to <code>true</code>.
220    */
221   public boolean isCreateTable()
222   {
223     return createTable;
224   }
225 
226   /**
227    * Sets the flag to create the configuration if set to <code>true</code>.
228    *
229    * @param createTable the flag to create the configuration if set to
230    *          <code>true</code>.
231    */
232   public void setCreateTable(final boolean createTable)
233   {
234     this.createTable = createTable;
235   }
236 
237   // --- business -------------------------------------------------------------
238 
239   /**
240    * Checks whether or not the configuration has value.
241    *
242    * @return <code>true</code> if the configuration is unset, <code>false</code>
243    *         otherwise.
244    */
245   public boolean isUnset()
246   {
247     return StringUtils.isBlank(jndiName);
248   }
249 
250   // --- object basics --------------------------------------------------------
251 
252   @Override
253   public String toString()
254   {
255     return jndiName;
256   }
257 }