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.mysql;
17  
18  import java.io.IOException;
19  import java.io.ObjectInputStream;
20  
21  import javax.sql.DataSource;
22  
23  import com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource;
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 MySqlDataSourceProxy extends AbstractMySqlDataSourceProxy
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 MySqlDataSourceProxy(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 MysqlConnectionPoolDataSource dataSource =
111         new MysqlConnectionPoolDataSource();
112 
113     dataSource.setURL(connectionUrl);
114     dataSource.setUser(userName);
115     dataSource.setPassword(password);
116 
117     return dataSource;
118   }
119 
120   // --- factory --------------------------------------------------------------
121 
122   /**
123    * Factory method to create with URL
124    * <code>jdbc:mysql://localhost:3306/properties</code>.
125    *
126    * @return the proxy instance.
127    */
128   public static MySqlDataSourceProxy create()
129   {
130     return create("jdbc:mysql://localhost:3306/properties");
131   }
132 
133   /**
134    * Factory method to create with the given connection URL using user
135    * <code>properties</code> and password <code>properties</code>.
136    *
137    * @param connectionUrl the JDBC URL to connect to te database. E.g.
138    *          <code>jdbc:mysql://localhost:3306/properties</code>.
139    * @return the proxy instance.
140    */
141   public static MySqlDataSourceProxy create(final String connectionUrl)
142   {
143     return create(connectionUrl, "properties", "properties");
144   }
145 
146   /**
147    * Factory method to create with the given connection URL using the given
148    * credentials.
149    *
150    * @param connectionUrl the JDBC URL to connect to te database. E.g.
151    *          <code>jdbc:mysql://localhost:3306/properties</code>.
152    * @param userName the name of the database user to connect to the database.
153    * @param password the password to the user.
154    * @return the proxy instance.
155    */
156   public static MySqlDataSourceProxy create(final String connectionUrl,
157       final String userName, final String password)
158   {
159     return new MySqlDataSourceProxy(connectionUrl, userName, password);
160   }
161 
162   // --- get&set --------------------------------------------------------------
163 
164   @Override
165   public String getDataSourceId()
166   {
167     return connectionUrl;
168   }
169 
170   @Override
171   public DataSource getDataSource()
172   {
173     return dataSource;
174   }
175 
176   // --- business -------------------------------------------------------------
177 
178   // --- object basics --------------------------------------------------------
179 
180   /**
181    * Reads the object from the given stream.
182    *
183    * @param in the stream to read from.
184    * @throws IOException on read problems.
185    * @throws ClassNotFoundException if a class cannot be found.
186    */
187   private void readObject(final ObjectInputStream in) throws IOException,
188     ClassNotFoundException
189   {
190     in.defaultReadObject();
191 
192     this.dataSource = createDataSource();
193   }
194 
195   @Override
196   public String toString()
197   {
198     return "(MySQL) " + userName + " @ " + connectionUrl;
199   }
200 }