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.impl.config.ds;
17  
18  import java.io.Serializable;
19  import java.util.Properties;
20  
21  import de.smartics.util.lang.Arg;
22  
23  /**
24   * Configuration to access a data source via JNDI.
25   * <p>
26   * The following properties are valid:
27   * </p>
28   * <ol>
29   * <li><code>{@value #JNDI_NAME}</code> - the name of the data source in the
30   * JNDI.</li>
31   * <li><code>{@value #DROP_TABLE}</code> - flag to drop the configuration table
32   * if set to <code>true</code> (usually this should only be used in test
33   * environments).</li>
34   * <li><code>{@value #CREATE_TABLE}</code> - flag to create the configuration
35   * table if set to <code>true</code>.</li>
36   * </ol>
37   * <p>
38   * The property {@link #CONFIG_SOURCE_ID} allows to track the source of the
39   * configuration information.
40   * </p>
41   */
42  public final class DataSourceConfiguration implements Serializable
43  {
44    // ********************************* Fields *********************************
45  
46    // --- constants ------------------------------------------------------------
47  
48    /**
49     * The class version identifier.
50     */
51    private static final long serialVersionUID = 1L;
52  
53    /**
54     * The identifier of the configuration for the data source.
55     */
56    public static final String CONFIG_SOURCE_ID = "de.smartics.properties.ds.id";
57  
58    /**
59     * The key to the JNDI name of the data source.
60     */
61    public static final String JNDI_NAME = "de.smartics.properties.ds.jndiName";
62  
63    /**
64     * The key to flag to drop the configuration table in the data source.
65     */
66    public static final String DROP_TABLE = "de.smartics.properties.ds.dropTable";
67  
68    /**
69     * The key to flag to create the configuration table in the data source.
70     */
71    public static final String CREATE_TABLE =
72        "de.smartics.properties.ds.createTable";
73  
74    // --- members --------------------------------------------------------------
75  
76    /**
77     * The source of the data source configuration.
78     *
79     * @serial
80     */
81    private final String configSourceId;
82  
83    /**
84     * The name of the data source to lookup in a JNDI context.
85     *
86     * @serial
87     */
88    private final String jndiName;
89  
90    /**
91     * The flag to drop the configuration table if set to <code>true</code>.
92     *
93     * @serial
94     */
95    private final boolean dropTable;
96  
97    /**
98     * The flag to create the configuration if set to <code>true</code>.
99     *
100    * @serial
101    */
102   private final boolean createTable;
103 
104   // ****************************** Initializer *******************************
105 
106   // ****************************** Constructors ******************************
107 
108   /**
109    * Default constructor.
110    *
111    * @param configSourceId the source of the data source configuration.
112    * @param jndiName the jndi name.
113    * @param dropTable the boolean whether or not existing tables shall be
114    *          dropped.
115    * @param createTable the boolean whether or the tables shall be created, if
116    *          they do not exist already
117    */
118   DataSourceConfiguration(final String configSourceId, final String jndiName,
119       final boolean dropTable, final boolean createTable)
120   {
121     this.configSourceId = Arg.checkNotBlank("configSourceId", configSourceId);
122     this.jndiName = Arg.checkNotBlank("jndiName", jndiName);
123     this.dropTable = dropTable;
124     this.createTable = createTable;
125   }
126 
127   /**
128    * Default constructor.
129    *
130    * @param properties the properties containing jndiName, dropTable and
131    *          createTable.
132    */
133   DataSourceConfiguration(final Properties properties)
134   {
135     this(Arg.checkNotBlank(CONFIG_SOURCE_ID,
136         properties.getProperty(CONFIG_SOURCE_ID)), Arg.checkNotBlank(JNDI_NAME,
137         properties.getProperty(JNDI_NAME)), Boolean.parseBoolean(Arg
138         .checkNotBlank(DROP_TABLE, properties.getProperty(DROP_TABLE))),
139         Boolean.parseBoolean(Arg.checkNotBlank(CREATE_TABLE,
140             properties.getProperty(CREATE_TABLE))));
141 
142   }
143 
144   // ****************************** Inner Classes *****************************
145 
146   // ********************************* Methods ********************************
147 
148   // --- init -----------------------------------------------------------------
149 
150   // --- get&set --------------------------------------------------------------
151 
152   /**
153    * Returns the source of the data source configuration.
154    *
155    * @return the source of the data source configuration.
156    */
157   public String getConfigSourceId()
158   {
159     return configSourceId;
160   }
161 
162   /**
163    * Returns the name of the data source to lookup in a JNDI context.
164    *
165    * @return the name of the data source to lookup in a JNDI context.
166    */
167   public String getJndiName()
168   {
169     return jndiName;
170   }
171 
172   /**
173    * Returns the flag to drop the configuration table if set to
174    * <code>true</code>.
175    *
176    * @return the flag to drop tables if set to <code>true</code>.
177    */
178   public boolean isDropTable()
179   {
180     return dropTable;
181   }
182 
183   /**
184    * Returns the flag to create the configuration table if set to
185    * <code>true</code>.
186    *
187    * @return the flag to create tables if set to <code>true</code>.
188    */
189   public boolean isCreateTable()
190   {
191     return createTable;
192   }
193 
194   // --- business -------------------------------------------------------------
195 
196   // --- object basics --------------------------------------------------------
197 
198   @Override
199   public String toString()
200   {
201     return jndiName;
202   }
203 }