1 package com.bonevich.erj.db;
2
3 import com.bonevich.erj.app.Application;
4 import com.bonevich.util.Functor;
5 import com.bonevich.util.JarFileClassLoader;
6
7 import org.apache.commons.logging.Log;
8 import org.apache.commons.logging.LogFactory;
9
10 import java.sql.Connection;
11 import java.sql.DriverManager;
12 import java.util.*;
13
14 import javax.sql.DataSource;
15
16 public final class DatabaseLogin
17 {
18 private static final Log _logger = LogFactory.getLog(DatabaseLogin.class);
19
20 //////////////////////////////////////////////////////////
21 // Attributes
22 private SupportedDatabase _database;
23 private String _databaseName;
24 private String _driver;
25 private String _jdbcUrl;
26 private String _jarPath;
27
28 private transient JarFileClassLoader _loader;
29 private transient Vector _driverNames;
30
31 private Properties _loginProperties = new Properties();
32 private List _loginFields = new ArrayList(10);
33
34 private Connection _connection;
35
36 //////////////////////////////////////////////////////////
37 // Constructors
38 DatabaseLogin(SupportedDatabase database)
39 {
40 _database = database;
41 }
42
43 public static DatabaseLogin getDefaultInstance()
44 {
45 return new DatabaseLogin(Application.getDefaultDatabase());
46 }
47
48 //////////////////////////////////////////////////////////
49 // Operations
50
51 /***
52 * Returns the database.
53 * @return SupportedDatabase
54 */
55 public SupportedDatabase getSupportedDatabase()
56 {
57 return _database;
58 }
59
60 /***
61 * Sets the database.
62 * @param database The database to set
63 */
64 public void setSupportedDatabase(SupportedDatabase database)
65 {
66 _database = database;
67 }
68
69 /***
70 * Returns the display name of the targeted database (i.e. not the
71 * vendor or RDMS name, but the name of the specific database instance).
72 * @return String
73 */
74 public String getDatabaseName()
75 {
76 return _databaseName;
77 }
78 public void setDatabaseName(String databaseName)
79 {
80 _databaseName = databaseName;
81 }
82
83 /***
84 * Returns the driver.
85 * @return String
86 */
87 public String getDriver()
88 {
89 return _driver;
90 }
91
92 /***
93 * Sets the driver.
94 * @param driver The driver to set
95 */
96 public void setDriver(String driver)
97 {
98 _driver = driver;
99 }
100
101 /***
102 * Returns the jdbcUrl.
103 * @return String
104 */
105 public String getJdbcUrl()
106 {
107 return _jdbcUrl;
108 }
109
110 /***
111 * Sets the jdbcUrl.
112 * @param jdbcUrl The jdbcUrl to set
113 */
114 public void setJdbcUrl(String jdbcUrl)
115 {
116 _jdbcUrl = jdbcUrl;
117 }
118
119 /***
120 * Returns the jarPath.
121 * @return String
122 */
123 public String getJarPath()
124 {
125 return _jarPath;
126 }
127
128 /***
129 * Sets the jdbcJarPath.
130 * @param jarPath The jarPath to set
131 */
132 public void setJarPath(String jarPath)
133 {
134 if (jarPath != null && !jarPath.equals(""))
135 {
136 _jarPath = jarPath;
137 try
138 {
139 _loader = new JarFileClassLoader(_jarPath, this);
140 _driverNames = _loader.loadClasses(
141 java.sql.Driver.class,
142 new Functor()
143 {
144 public Object function(Object arg)
145 {
146 Class clazz = (Class) arg;
147 return clazz.getName();
148 }
149 }
150 );
151 }
152 catch (Exception e)
153 {
154 _driverNames = new Vector();
155 _driverNames.add("[no drivers found]");
156 }
157 }
158 else
159 {
160 _jarPath = null;
161 _driverNames = new Vector();
162 _driverNames.add("[no drivers found]");
163 }
164 }
165
166 /***
167 * Returns the loginProperties.
168 * @return Properties
169 */
170 public Properties getLoginProperties()
171 {
172 return _loginProperties;
173 }
174
175 /***
176 * Sets the loginProperties.
177 * @param loginProperties The loginProperties to set
178 */
179 public void setLoginProperty(String key, String value)
180 {
181 if (_logger.isDebugEnabled())
182 {
183 _logger.debug("setting login prop: " + key + " = " + value);
184 }
185 if (key == null) return;
186 _loginProperties.setProperty(key, value);
187 _loginFields.add(new LoginFieldInfo(key, value));
188 }
189
190 /***
191 * Returns the loginFields.
192 * @return List
193 */
194 public List getLoginFields()
195 {
196 return _loginFields;
197 }
198
199 public static class LoginFieldInfo
200 {
201 public String _key, _value;
202 public LoginFieldInfo(String key, String value)
203 {
204 _key = key;
205 _value = value;
206 }
207 public String getKey() { return _key; }
208 public String getValue() { return _value; }
209 }
210
211 public Connection login() //throws LoginException
212 {
213 if (_connection == null)
214 {
215 synchronized(this)
216 {
217 if (_connection == null)
218 {
219 try
220 {
221 Class.forName(_driver);
222 _connection = DriverManager.getConnection(_jdbcUrl, _loginProperties);
223 }
224 catch (Exception e)
225 {
226 e.printStackTrace();
227 }
228 }
229 }
230 }
231 return _connection;
232 }
233
234 /***
235 * Returns the driverNames.
236 * @return Vector
237 */
238 public Vector getDriverNames()
239 {
240 return _driverNames;
241 }
242
243 /***
244 * Sets the driverNames.
245 * @param driverNames The driverNames to set
246 */
247 public void setDriverNames(Vector driverNames)
248 {
249 _driverNames = driverNames;
250 }
251
252 /***
253 * Method getDriverClass.
254 * @return Class
255 */
256 public Class getDriverClass()
257 {
258 try
259 {
260 return _loader.loadClass(_driver);
261 }
262 catch (Exception e)
263 {
264 return null;
265 }
266 }
267
268 } /* end class DatabaseLogin */
This page was automatically generated by Maven