KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > modeler > pref > DBConnectionInfo


1 /*
2  * ==================================================================== The ObjectStyle
3  * Group Software License, version 1.1 ObjectStyle Group - http://objectstyle.org/
4  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors of the
5  * software. All rights reserved. Redistribution and use in source and binary forms, with
6  * or without modification, are permitted provided that the following conditions are met:
7  * 1. Redistributions of source code must retain the above copyright notice, this list of
8  * conditions and the following disclaimer. 2. Redistributions in binary form must
9  * reproduce the above copyright notice, this list of conditions and the following
10  * disclaimer in the documentation and/or other materials provided with the distribution.
11  * 3. The end-user documentation included with the redistribution, if any, must include
12  * the following acknowlegement: "This product includes software developed by independent
13  * contributors and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
14  * Alternately, this acknowlegement may appear in the software itself, if and wherever
15  * such third-party acknowlegements normally appear. 4. The names "ObjectStyle Group" and
16  * "Cayenne" must not be used to endorse or promote products derived from this software
17  * without prior written permission. For written permission, email "andrus at objectstyle
18  * dot org". 5. Products derived from this software may not be called "ObjectStyle" or
19  * "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their names without prior
20  * written permission. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
21  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE
23  * GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  * ==================================================================== This software
30  * consists of voluntary contributions made by many individuals and hosted on ObjectStyle
31  * Group web site. For more information on the ObjectStyle Group, please see
32  * <http://objectstyle.org/>.
33  */

34 package org.objectstyle.cayenne.modeler.pref;
35
36 import java.sql.Driver JavaDoc;
37 import java.sql.SQLException JavaDoc;
38
39 import javax.sql.DataSource JavaDoc;
40
41 import org.objectstyle.cayenne.conn.DataSourceInfo;
42 import org.objectstyle.cayenne.conn.DriverDataSource;
43 import org.objectstyle.cayenne.dba.DbAdapter;
44 import org.objectstyle.cayenne.modeler.ClassLoadingService;
45 import org.objectstyle.cayenne.util.Util;
46
47 public class DBConnectionInfo extends _DBConnectionInfo {
48
49     /**
50      * Creates a DbAdapter based on configured values.
51      */

52     public DbAdapter makeAdapter(ClassLoadingService classLoader) throws Exception JavaDoc {
53         if (getDbAdapter() == null) {
54             throw new Exception JavaDoc("No DbAdapter set.");
55         }
56
57         try {
58             return (DbAdapter) classLoader.loadClass(getDbAdapter()).newInstance();
59         }
60         catch (Throwable JavaDoc th) {
61             th = Util.unwindException(th);
62             throw new Exception JavaDoc("DbAdapter load error: " + th.getLocalizedMessage());
63         }
64     }
65
66     /**
67      * Returns a DataSource that uses connection information from this object. Returned
68      * DataSource is not pooling its connections. It can be wrapped in PoolManager if
69      * pooling is needed.
70      */

71     public DataSource JavaDoc makeDataSource(ClassLoadingService classLoader) throws SQLException JavaDoc {
72
73         // validate...
74
if (getJdbcDriver() == null) {
75             throw new SQLException JavaDoc("No JDBC driver set.");
76         }
77
78         if (getUrl() == null) {
79             throw new SQLException JavaDoc("No DB URL set.");
80         }
81
82         // load driver...
83
Driver JavaDoc driver;
84
85         try {
86             driver = (Driver JavaDoc) classLoader.loadClass(getJdbcDriver()).newInstance();
87         }
88         catch (Throwable JavaDoc th) {
89             th = Util.unwindException(th);
90             throw new SQLException JavaDoc("Driver load error: " + th.getLocalizedMessage());
91         }
92
93         return new DriverDataSource(driver, getUrl(), getUserName(), getPassword());
94     }
95
96     /**
97      * Updates another DBConnectionInfo with this object's values.
98      */

99     public boolean copyTo(DBConnectionInfo dataSourceInfo) {
100         boolean updated = false;
101
102         if (!Util.nullSafeEquals(dataSourceInfo.getUrl(), getUrl())) {
103             dataSourceInfo.setUrl(getUrl());
104             updated = true;
105         }
106
107         if (!Util.nullSafeEquals(dataSourceInfo.getUserName(), getUserName())) {
108             dataSourceInfo.setUserName(getUserName());
109             updated = true;
110         }
111
112         if (!Util.nullSafeEquals(dataSourceInfo.getPassword(), getPassword())) {
113             dataSourceInfo.setPassword(getPassword());
114             updated = true;
115         }
116
117         if (!Util.nullSafeEquals(dataSourceInfo.getJdbcDriver(), getJdbcDriver())) {
118             dataSourceInfo.setJdbcDriver(getJdbcDriver());
119             updated = true;
120         }
121
122         if (!Util.nullSafeEquals(dataSourceInfo.getDbAdapter(), getDbAdapter())) {
123             dataSourceInfo.setDbAdapter(getDbAdapter());
124             updated = true;
125         }
126
127         return updated;
128     }
129
130     /**
131      * Updates DataSourceInfo with this object's values.
132      * <p>
133      * <i>Currently doesn't set the adapter property. Need to change the UI to handle
134      * adapter via DataSourceInfo first, and then it should be safe to do an adapter
135      * update here. </i>
136      * </p>
137      */

138     public boolean copyTo(DataSourceInfo dataSourceInfo) {
139         boolean updated = false;
140
141         if (!Util.nullSafeEquals(dataSourceInfo.getDataSourceUrl(), getUrl())) {
142             dataSourceInfo.setDataSourceUrl(getUrl());
143             updated = true;
144         }
145
146         if (!Util.nullSafeEquals(dataSourceInfo.getUserName(), getUserName())) {
147             dataSourceInfo.setUserName(getUserName());
148             updated = true;
149         }
150
151         if (!Util.nullSafeEquals(dataSourceInfo.getPassword(), getPassword())) {
152             dataSourceInfo.setPassword(getPassword());
153             updated = true;
154         }
155
156         if (!Util.nullSafeEquals(dataSourceInfo.getJdbcDriver(), getJdbcDriver())) {
157             dataSourceInfo.setJdbcDriver(getJdbcDriver());
158             updated = true;
159         }
160
161         return updated;
162     }
163
164     public boolean copyFrom(DataSourceInfo dataSourceInfo) {
165         boolean updated = false;
166
167         if (!Util.nullSafeEquals(dataSourceInfo.getDataSourceUrl(), getUrl())) {
168             setUrl(dataSourceInfo.getDataSourceUrl());
169             updated = true;
170         }
171
172         if (!Util.nullSafeEquals(dataSourceInfo.getUserName(), getUserName())) {
173             setUserName(dataSourceInfo.getUserName());
174             updated = true;
175         }
176
177         if (!Util.nullSafeEquals(dataSourceInfo.getPassword(), getPassword())) {
178             setPassword(dataSourceInfo.getPassword());
179             updated = true;
180         }
181
182         if (!Util.nullSafeEquals(dataSourceInfo.getJdbcDriver(), getJdbcDriver())) {
183             setJdbcDriver(dataSourceInfo.getJdbcDriver());
184             updated = true;
185         }
186
187         return updated;
188     }
189 }
190
191
Popular Tags