KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > conn > DataSourceInfo


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.conn;
21
22 import java.io.Serializable JavaDoc;
23
24 import org.apache.cayenne.conf.PasswordEncoding;
25 import org.apache.cayenne.conf.PlainTextPasswordEncoder;
26 import org.apache.cayenne.util.Util;
27
28 /**
29  * Helper JavaBean class that holds DataSource login information.
30  * <p>
31  * <i>For more information see <a HREF="../../../../../../userguide/index.html"
32  * target="_top">Cayenne User Guide.</a></i>
33  * </p>
34  *
35  * @author Andrus Adamchik
36  */

37 public class DataSourceInfo implements Cloneable JavaDoc, Serializable JavaDoc {
38
39     protected String JavaDoc userName;
40     protected String JavaDoc password;
41     protected String JavaDoc jdbcDriver;
42     protected String JavaDoc dataSourceUrl;
43     protected String JavaDoc adapterClassName;
44     protected int minConnections = 1;
45     protected int maxConnections = 1;
46
47     // Constants for passwordLocation
48
public static final String JavaDoc PASSWORD_LOCATION_CLASSPATH = "classpath";
49     public static final String JavaDoc PASSWORD_LOCATION_EXECUTABLE = "executable";
50     public static final String JavaDoc PASSWORD_LOCATION_MODEL = "model";
51     public static final String JavaDoc PASSWORD_LOCATION_URL = "url";
52
53     // Extended parameters
54
protected String JavaDoc passwordEncoderClass = PasswordEncoding.standardEncoders[0];
55     protected String JavaDoc passwordEncoderSalt = "";
56     protected String JavaDoc passwordSourceExecutable = "";
57     protected String JavaDoc passwordSourceFilename = "";
58     protected String JavaDoc passwordSourceModel = "Not Applicable";
59     protected String JavaDoc passwordSourceUrl = "";
60     protected String JavaDoc passwordLocation = PASSWORD_LOCATION_MODEL;
61
62     public boolean equals(Object JavaDoc obj) {
63         if (obj == this)
64             return true;
65
66         if (obj == null)
67             return false;
68
69         if (obj.getClass() != this.getClass())
70             return false;
71
72         DataSourceInfo dsi = (DataSourceInfo) obj;
73
74         if (!Util.nullSafeEquals(this.userName, dsi.userName))
75             return false;
76         if (!Util.nullSafeEquals(this.password, dsi.password))
77             return false;
78         if (!Util.nullSafeEquals(this.jdbcDriver, dsi.jdbcDriver))
79             return false;
80         if (!Util.nullSafeEquals(this.dataSourceUrl, dsi.dataSourceUrl))
81             return false;
82         if (!Util.nullSafeEquals(this.adapterClassName, dsi.adapterClassName))
83             return false;
84         if (this.minConnections != dsi.minConnections)
85             return false;
86         if (this.maxConnections != dsi.maxConnections)
87             return false;
88         if (!Util.nullSafeEquals(this.passwordEncoderClass, dsi.passwordEncoderClass))
89             return false;
90         if (!Util.nullSafeEquals(this.passwordEncoderSalt, dsi.passwordEncoderSalt))
91             return false;
92         if (!Util.nullSafeEquals(this.passwordSourceFilename, dsi.passwordSourceFilename))
93             return false;
94         if (!Util.nullSafeEquals(this.passwordSourceModel, dsi.passwordSourceModel))
95             return false;
96         if (!Util.nullSafeEquals(this.passwordSourceUrl, dsi.passwordSourceUrl))
97             return false;
98         if (!Util.nullSafeEquals(this.passwordLocation, dsi.passwordLocation))
99             return false;
100
101         return true;
102     }
103
104     public DataSourceInfo cloneInfo() {
105         try {
106             return (DataSourceInfo) super.clone();
107         }
108         catch (CloneNotSupportedException JavaDoc ex) {
109             throw new RuntimeException JavaDoc("Cloning error", ex);
110         }
111     }
112
113     public String JavaDoc toString() {
114         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
115         buf.append("[").append(this.getClass().getName()).append(":").append(
116                 "\n user name: ").append(userName).append("\n password: ");
117
118         buf.append("**********");
119         buf
120                 .append("\n driver: ")
121                 .append(jdbcDriver)
122                 .append("\n db adapter class: ")
123                 .append(adapterClassName)
124                 .append("\n url: ")
125                 .append(dataSourceUrl)
126                 .append("\n min. connections: ")
127                 .append(minConnections)
128                 .append("\n max. connections: ")
129                 .append(maxConnections);
130
131         if (!PlainTextPasswordEncoder.class.getName().equals(passwordEncoderClass)) {
132             buf.append("\n encoder class: ").append(passwordEncoderClass).append(
133                     "\n encoder salt: ").append(passwordEncoderSalt);
134         }
135
136         if (!PASSWORD_LOCATION_MODEL.equals(passwordLocation)) {
137             buf.append("\n password location: ").append(passwordLocation).append(
138                     "\n password source: ").append(getPasswordSource());
139         }
140
141         buf.append("\n]");
142         return buf.toString();
143     }
144
145     public String JavaDoc getAdapterClassName() {
146         return adapterClassName;
147     }
148
149     public void setAdapterClassName(String JavaDoc adapterClassName) {
150         this.adapterClassName = adapterClassName;
151     }
152
153     public void setMinConnections(int minConnections) {
154         this.minConnections = minConnections;
155     }
156
157     public int getMinConnections() {
158         return minConnections;
159     }
160
161     public void setMaxConnections(int maxConnections) {
162         this.maxConnections = maxConnections;
163     }
164
165     public int getMaxConnections() {
166         return maxConnections;
167     }
168
169     public void setUserName(String JavaDoc userName) {
170         this.userName = userName;
171     }
172
173     public String JavaDoc getUserName() {
174         return userName;
175     }
176
177     public void setPassword(String JavaDoc password) {
178         this.password = password;
179     }
180
181     public String JavaDoc getPassword() {
182         return password;
183     }
184
185     public void setJdbcDriver(String JavaDoc jdbcDriver) {
186         this.jdbcDriver = jdbcDriver;
187     }
188
189     public String JavaDoc getJdbcDriver() {
190         return jdbcDriver;
191     }
192
193     public void setDataSourceUrl(String JavaDoc dataSourceUrl) {
194         this.dataSourceUrl = dataSourceUrl;
195     }
196
197     public String JavaDoc getDataSourceUrl() {
198         return dataSourceUrl;
199     }
200
201     public PasswordEncoding getPasswordEncoder() {
202         PasswordEncoding encoder = null;
203
204         try {
205             encoder = (PasswordEncoding) Thread
206                     .currentThread()
207                     .getContextClassLoader()
208                     .loadClass(getPasswordEncoderClass())
209                     .newInstance();
210             // encoder = (PasswordEncoding)
211
// Class.forName(getPasswordEncoderClass()).newInstance();
212
}
213         catch (InstantiationException JavaDoc exception) {
214             // TODO Auto-generated catch block
215
exception.printStackTrace();
216         }
217         catch (IllegalAccessException JavaDoc exception) {
218             // TODO Auto-generated catch block
219
exception.printStackTrace();
220         }
221         catch (ClassNotFoundException JavaDoc exception) {
222             // TODO Auto-generated catch block
223
exception.printStackTrace();
224         }
225
226         return encoder;
227     }
228
229     /**
230      * @return the passwordEncoderClass
231      */

232     public String JavaDoc getPasswordEncoderClass() {
233         return passwordEncoderClass;
234     }
235
236     /**
237      * @param passwordEncoderClass the passwordEncoderClass to set
238      */

239     public void setPasswordEncoderClass(String JavaDoc passwordEncoderClass) {
240         if (passwordEncoderClass == null)
241             this.passwordEncoderClass = PasswordEncoding.standardEncoders[0];
242         else
243             this.passwordEncoderClass = passwordEncoderClass;
244     }
245
246     /**
247      * @return the passwordEncoderSalt
248      */

249     public String JavaDoc getPasswordEncoderSalt() {
250         return passwordEncoderSalt;
251     }
252
253     /**
254      * @param passwordEncoderSalt the passwordEncoderSalt to set
255      */

256     public void setPasswordEncoderSalt(String JavaDoc passwordEncoderSalt) {
257         this.passwordEncoderSalt = passwordEncoderSalt;
258     }
259
260     /**
261      * @return the passwordLocationFilename
262      */

263     public String JavaDoc getPasswordSourceFilename() {
264         return passwordSourceFilename;
265     }
266
267     /**
268      * @param passwordSourceFilename the passwordSourceFilename to set
269      */

270     public void setPasswordSourceFilename(String JavaDoc passwordSourceFilename) {
271         this.passwordSourceFilename = passwordSourceFilename;
272     }
273
274     /**
275      * @return the passwordLocationModel
276      */

277     public String JavaDoc getPasswordSourceModel() {
278         return passwordSourceModel;
279     }
280
281     /**
282      * @return the passwordLocationUrl
283      */

284     public String JavaDoc getPasswordSourceUrl() {
285         return passwordSourceUrl;
286     }
287
288     /**
289      * @param passwordSourceUrl the passwordSourceUrl to set
290      */

291     public void setPasswordSourceUrl(String JavaDoc passwordSourceUrl) {
292         this.passwordSourceUrl = passwordSourceUrl;
293     }
294
295     /**
296      * @return the passwordLocationExecutable
297      */

298     public String JavaDoc getPasswordSourceExecutable() {
299         return passwordSourceExecutable;
300     }
301
302     /**
303      * @param passwordSourceExecutable the passwordSourceExecutable to set
304      */

305     public void setPasswordSourceExecutable(String JavaDoc passwordSourceExecutable) {
306         this.passwordSourceExecutable = passwordSourceExecutable;
307     }
308
309     public String JavaDoc getPasswordSource() {
310         if (getPasswordLocation().equals(PASSWORD_LOCATION_CLASSPATH))
311             return getPasswordSourceFilename();
312         else if (getPasswordLocation().equals(PASSWORD_LOCATION_EXECUTABLE))
313             return getPasswordSourceExecutable();
314         else if (getPasswordLocation().equals(PASSWORD_LOCATION_MODEL))
315             return getPasswordSourceModel();
316         else if (getPasswordLocation().equals(PASSWORD_LOCATION_URL))
317             return getPasswordSourceUrl();
318
319         throw new RuntimeException JavaDoc("Invalid password source detected");
320     }
321
322     public void setPasswordSource(String JavaDoc passwordSource) {
323         // The location for the model is omitted since it cannot change
324
if (getPasswordLocation().equals(PASSWORD_LOCATION_CLASSPATH))
325             setPasswordSourceFilename(passwordSource);
326         else if (getPasswordLocation().equals(PASSWORD_LOCATION_EXECUTABLE))
327             setPasswordSourceExecutable(passwordSource);
328         else if (getPasswordLocation().equals(PASSWORD_LOCATION_URL))
329             setPasswordSourceUrl(passwordSource);
330     }
331
332     /**
333      * @return the passwordLocation
334      */

335     public String JavaDoc getPasswordLocation() {
336         return passwordLocation;
337     }
338
339     /**
340      * @param passwordLocation the passwordLocation to set
341      */

342     public void setPasswordLocation(String JavaDoc passwordLocation) {
343         if (passwordLocation == null)
344             this.passwordLocation = DataSourceInfo.PASSWORD_LOCATION_MODEL;
345         else
346             this.passwordLocation = passwordLocation;
347     }
348 }
349
Popular Tags