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.hsql;
17  
18  import java.io.IOException;
19  import java.io.ObjectInputStream;
20  
21  import javax.sql.DataSource;
22  
23  import org.hsqldb.jdbc.JDBCDataSource;
24  
25  /**
26   * Data source proxy to create HSQL data sources.
27   * <p>
28   * Please note that the password passed to this instance is stored. This is
29   * necessary in the case of deserialisation where the wrapped data source
30   * instance must be recreated. If this provides a security problem, please
31   * consider using the
32   * {@link de.smartics.properties.spi.config.ds.JndiDataSourceProxy}.
33   * </p>
34   */
35  public final class HSqlDataSourceProxy extends AbstractHSqlDataSourceProxy
36  {
37    // ********************************* Fields *********************************
38  
39    // --- constants ------------------------------------------------------------
40  
41    /**
42     * The class version identifier.
43     */
44    private static final long serialVersionUID = 1L;
45  
46    // --- members --------------------------------------------------------------
47  
48    /**
49     * The JDBC URL to connect to.
50     *
51     * @serial
52     */
53    private final String connectionUrl;
54  
55    /**
56     * The name of the user to access the data source.
57     *
58     * @serial
59     */
60    private final String userName;
61  
62    /**
63     * The password of the user to access the data source. The password is stored
64     * within this instance.
65     * <p>
66     * Please note that the password passed to this instance is stored. This is
67     * necessary in the case of deserialisation where the wrapped data source
68     * instance must be recreated. If this provides a security problem, please
69     * consider using the
70     * {@link de.smartics.properties.spi.config.ds.JndiDataSourceProxy}.
71     * </p>
72     *
73     * @serial
74     */
75    private final String password;
76  
77    /**
78     * The reference to the data source.
79     */
80    private transient DataSource dataSource;
81  
82    // ****************************** Initializer *******************************
83  
84    // ****************************** Constructors ******************************
85  
86    /**
87     * Default constructor.
88     *
89     * @param connectionUrl the JDBC URL to connect to.
90     * @param userName the name of the user to access the data source.
91     * @param password the password of the user to access the data source.
92     */
93    private HSqlDataSourceProxy(final String connectionUrl,
94        final String userName, final String password)
95    {
96      this.connectionUrl = connectionUrl;
97      this.userName = userName;
98      this.password = password;
99      this.dataSource = createDataSource();
100   }
101 
102   // ****************************** Inner Classes *****************************
103 
104   // ********************************* Methods ********************************
105 
106   // --- init -----------------------------------------------------------------
107 
108   private DataSource createDataSource()
109   {
110     final JDBCDataSource dataSource = new JDBCDataSource();
111     dataSource.setDatabase(connectionUrl);
112     dataSource.setUser(userName);
113     dataSource.setPassword(password);
114 
115     return dataSource;
116   }
117 
118   // --- factory --------------------------------------------------------------
119 
120   /**
121    * Factory method to create with URL <code>jdbc:hsqldb:mem:configmemdb</code>.
122    *
123    * @return the proxy instance.
124    */
125   public static HSqlDataSourceProxy create()
126   {
127     return create("jdbc:hsqldb:mem:configmemdb");
128   }
129 
130   /**
131    * Factory method to create with the given connection URL using user
132    * <code>SA</code> and no password.
133    *
134    * @param connectionUrl the JDBC URL to connect to te database. E.g.
135    *          <code>jdbc:hsqldb:file:target/testdb;shutdown=true</code>.
136    * @return the proxy instance.
137    */
138   public static HSqlDataSourceProxy create(final String connectionUrl)
139   {
140     return create(connectionUrl, "SA", "");
141   }
142 
143   /**
144    * Factory method to create with the given connection URL using the given
145    * credentials.
146    *
147    * @param connectionUrl the JDBC URL to connect to te database. E.g.
148    *          <code>jdbc:hsqldb:file:target/testdb;shutdown=true</code>.
149    * @param userName the name of the database user to connect to the database.
150    * @param password the password to the user.
151    * @return the proxy instance.
152    */
153   public static HSqlDataSourceProxy create(final String connectionUrl,
154       final String userName, final String password)
155   {
156     return new HSqlDataSourceProxy(connectionUrl, userName, password);
157   }
158 
159   // --- get&set --------------------------------------------------------------
160 
161   @Override
162   public String getDataSourceId()
163   {
164     return connectionUrl;
165   }
166 
167   /**
168    * Returns the reference to the data source.
169    *
170    * @return the reference to the data source.
171    */
172   @Override
173   public DataSource getDataSource()
174   {
175     return dataSource;
176   }
177 
178   // --- business -------------------------------------------------------------
179 
180   // --- object basics --------------------------------------------------------
181 
182   /**
183    * Reads the object from the given stream.
184    *
185    * @param in the stream to read from.
186    * @throws IOException on read problems.
187    * @throws ClassNotFoundException if a class cannot be found.
188    */
189   private void readObject(final ObjectInputStream in) throws IOException,
190     ClassNotFoundException
191   {
192     in.defaultReadObject();
193 
194     this.dataSource = createDataSource();
195   }
196 
197   @Override
198   public String toString()
199   {
200     return "(HSQL) " + userName + " @ " + connectionUrl;
201   }
202 }