KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > xml > xdbc > XMLDriverManager


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.xml.xdbc;
24
25
26 /**
27  * This class is used for managing a set of drivers and/or connections.<BR><BR>
28  */

29 public class XMLDriverManager {
30
31
32
33     private static java.util.Set JavaDoc drivers = java.util.Collections.synchronizedSet(new java.util.HashSet JavaDoc());
34     private static java.io.PrintWriter JavaDoc logWriter = null;
35     private static int loginTimeout = 0;
36   
37     private XMLDriverManager() {}
38   
39   /**
40    * Attempts to locate a driver that understands the given URI.
41    * @param uri an URI corresponding to an existing driver.
42    * @return a driver that understands the given URI.
43    * @throws XMLDBCException if a data source access error occurs.
44    */

45   public static XMLDriver getDriver(String JavaDoc uri)
46   throws XMLDBCException {
47     java.util.Iterator JavaDoc it = getDrivers();
48     while (it.hasNext()) {
49       XMLDriver driver = (XMLDriver)it.next();
50       if (driver.acceptsURI(uri))
51         return driver;
52     }
53     return null;
54   }
55   
56   /**
57    * Attempts to establish a connection to the given data source URI.
58    * @param uri an URI corresponding to an existing driver.
59    * @return a connection to the data source.
60    * @throws XMLDBCException if a data source access error occurs.
61    */

62   public static XMLConnection getConnection(String JavaDoc uri)
63   throws XMLDBCException {
64     return getDataSource(uri).getConnection();
65   }
66   
67   /**
68    * Attempts to establish a connection to the given data source URI with a login/password.
69    * @param uri an URI corresponding to an existing driver.
70    * @param user a login to access data source account.
71    * @param password a password to access data source account.
72    * @return a connection to the data source.
73    * @throws XMLDBCException if a data source access error occurs.
74    */

75   public static XMLConnection getConnection(String JavaDoc uri, String JavaDoc user, String JavaDoc password)
76   throws XMLDBCException {
77     XMLDriver driver = getDriver(uri);
78     if (driver == null) throw new XMLDBCException("No driver found for uri: "+uri);
79     // Try first with all parameters
80
XMLDataSource dataSource = driver.getDataSource(uri, user, password);
81     if (dataSource != null) {
82         dataSource.setLogWriter(logWriter);
83         dataSource.setLoginTimeout(loginTimeout);
84         return dataSource.getConnection();
85     }
86     // Try with uri alone
87
dataSource = driver.getDataSource(uri);
88     if (dataSource != null) {
89         dataSource.setLogWriter(logWriter);
90         dataSource.setLoginTimeout(loginTimeout);
91         return dataSource.getConnection(user, password);
92     } else {
93         throw new XMLDBCException("No data source found for uri: "+uri);
94     }
95   }
96   
97   /**
98    * Returns a handle to a data source with the specified URI.
99    * @param uri an URI corresponding to a data source.
100    * @return an instance of the specified data source,
101    * or null if the driver does not know this type of data source
102    * @throws XMLDBCException if a data source access error occurs.
103    */

104   public static XMLDataSource getDataSource(String JavaDoc uri)
105   throws XMLDBCException {
106     XMLDriver driver = getDriver(uri);
107     if (driver == null) throw new XMLDBCException("No driver found for uri: "+uri);
108     XMLDataSource dataSource = driver.getDataSource(uri);
109     if (dataSource == null) throw new XMLDBCException("No data source found for uri: "+uri);
110     dataSource.setLogWriter(logWriter);
111     dataSource.setLoginTimeout(loginTimeout);
112     return dataSource;
113   }
114   
115   /**
116    * Returns a handle to a data source with the specified URI.
117    * @param uri a URI corresponding to a data source.
118    * @param user a user name
119    * @param password a user password
120    * @return an instance of the specified data source,
121    * or null if the driver does not know this type of data source
122    * @throws XMLDBCException if a data source access error occurs.
123    */

124   public static XMLDataSource getDataSource(String JavaDoc uri, String JavaDoc user, String JavaDoc password)
125   throws XMLDBCException {
126     XMLDriver driver = getDriver(uri);
127     if (driver == null) throw new XMLDBCException("No driver found for uri: "+uri);
128     XMLDataSource dataSource = driver.getDataSource(uri, user, password);
129     if (dataSource == null) throw new XMLDBCException("No data source found for uri: "+uri);
130     dataSource.setLogWriter(logWriter);
131     dataSource.setLoginTimeout(loginTimeout);
132     return dataSource;
133   }
134   
135   /**
136    * Sets the default logging/tracing java.io.PrintWriter object that is used
137    * by all data source objects
138    * obtained from the manager.
139    * @param writer the new logging/tracing java.io.PrintWriter object, or null to disable logging and tracing
140    */

141   public static void setLogWriter(java.io.PrintWriter JavaDoc writer) {
142     logWriter = writer;
143   }
144   
145   /**
146    * Retrieves the default log writer currently used by all data source objects
147    * obtained from the manager.
148    * @return the java.io.PrintWriter object used to log data source messages, or null if none is defined
149    */

150   public java.io.PrintWriter JavaDoc getLogWriter() {
151     return logWriter;
152   }
153   
154   /**
155    * Sets the maximum time in seconds that all data source objects obtained
156    * from the manager will wait when attempting to log in to a data source.
157    * Setting the timeout to 0 will disable it.
158    * @param seconds The login time limit in seconds
159    */

160   public void setLoginTimeout(int seconds) {
161     loginTimeout = seconds;
162   }
163   
164   /**
165    * Gets the maximum time in seconds that all data source objects obtained
166    * from the manager can wait when attempting to log in to a data source
167    * @return the data source login timeout in seconds, 0 if no timeout is defined
168    */

169   public int getLoginTimeout() {
170     return loginTimeout;
171   }
172   
173   /**
174    * Adds the given driver in the driver manager list.
175    * @param driver the new XMLDBC driver that must be registered with the
176    * driver manager.
177    */

178   public static void registerDriver(XMLDriver driver) {
179     drivers.add(driver);
180   }
181   
182   /**
183    * Removes the specified driver from the driver manager list.
184    * @param driver the XMLDBC driver that must be dropped from the driver manager.
185    */

186   public static void deregisterDriver(XMLDriver driver) {
187     drivers.remove(driver);
188   }
189   
190   /**
191    * Returns an iterator on registered XMLDBC drivers.
192    * @return an enumeration of XMLDBC drivers.
193    */

194   public static java.util.Iterator JavaDoc getDrivers() {
195     return drivers.iterator();
196   }
197 }
198
Popular Tags