KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > jdbc > DjDriver


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, is permitted provided that the following conditions are met: -
6  * Redistributions of source code must retain the above copyright notice, this
7  * list of conditions and the following disclaimer. - Redistributions in binary
8  * form must reproduce the above copyright notice, this list of conditions and
9  * the following disclaimer in the documentation and/or other materials
10  * provided with the distribution. - All advertising materials mentioning
11  * features or use of this software must display the following acknowledgment:
12  * "This product includes Djeneric." - Products derived from this software may
13  * not be called "Djeneric" nor may "Djeneric" appear in their names without
14  * prior written permission of Genimen BV. - Redistributions of any form
15  * whatsoever must retain the following acknowledgment: "This product includes
16  * Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG, OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */

30
31 package com.genimen.djeneric.jdbc;
32
33 import java.sql.Connection JavaDoc;
34 import java.sql.DriverManager JavaDoc;
35 import java.sql.DriverPropertyInfo JavaDoc;
36 import java.sql.SQLException JavaDoc;
37 import java.util.ArrayList JavaDoc;
38 import java.util.List JavaDoc;
39 import java.util.Properties JavaDoc;
40 import java.util.StringTokenizer JavaDoc;
41
42 import com.genimen.djeneric.repository.DjContext;
43 import com.genimen.djeneric.repository.DjCredentials;
44 import com.genimen.djeneric.repository.DjMessenger;
45 import com.genimen.djeneric.repository.DjRepositoryDescriptor;
46 import com.genimen.djeneric.repository.exceptions.DjenericException;
47 import com.genimen.djeneric.repository.exceptions.LogonException;
48 import com.genimen.djeneric.repository.rdbms.RdbmsPersistenceManager;
49 import com.genimen.djeneric.util.DjLogger;
50
51 class DjDriverMessenger implements DjMessenger
52 {
53   private DjCredentials _cred = new DjCredentials();
54
55   DjDriverMessenger(String JavaDoc uid, String JavaDoc pwd)
56   {
57     _cred.setPassword(pwd);
58     _cred.setUserid(uid);
59   }
60
61   public DjCredentials getCredentials(DjRepositoryDescriptor repository)
62   {
63     return _cred;
64   }
65
66   /*
67    * (non-Javadoc)
68    *
69    * @see com.genimen.djeneric.repository.DjMessenger#selectContext(com.genimen.djeneric.repository.DjContext[])
70    */

71   public DjContext selectContext(DjContext[] ctxts) throws LogonException
72   {
73     return null;
74   }
75
76   /*
77    * (non-Javadoc)
78    *
79    * @see com.genimen.djeneric.repository.DjMessenger#selectRepository(com.genimen.djeneric.repository.DjRepositoryDescriptor[])
80    */

81   public DjRepositoryDescriptor selectRepository(DjRepositoryDescriptor[] descriptors)
82   {
83     return null;
84   }
85
86   /*
87    * (non-Javadoc)
88    *
89    * @see com.genimen.djeneric.repository.DjMessenger#showMessage(java.lang.String,
90    * java.lang.String)
91    */

92   public void showMessage(String JavaDoc title, String JavaDoc msg)
93   {
94     DriverManager.println(title);
95     DriverManager.println(msg);
96   }
97
98   /*
99    * (non-Javadoc)
100    *
101    * @see com.genimen.djeneric.repository.DjMessenger#showWarning(java.lang.String,
102    * java.lang.String)
103    */

104   public void showWarning(String JavaDoc title, String JavaDoc msg)
105   {
106     DriverManager.println(title);
107     DriverManager.println(msg);
108   }
109
110 }
111
112 public class DjDriver implements java.sql.Driver JavaDoc
113 {
114   static final String JavaDoc CLASSNAME = DjDriver.class.getName();
115   static final int MAJOR_VERSION = 1;
116   static final int MINOR_VERSION = 0;
117   private List JavaDoc _allConnections = new ArrayList JavaDoc();
118
119   static
120   {
121     // register this driver during classloading
122
try
123     {
124       // System.err.println("DjDriver BETA version");
125
DriverManager.registerDriver(new DjDriver());
126       // DriverManager.println(CLASSNAME + ": registered");
127
}
128     catch (SQLException JavaDoc sqle)
129     {
130       // DriverManager.println(CLASSNAME + ": SQLException during
131
// registration:");
132
DriverManager.println(sqle.toString());
133     }
134   }
135
136   // There must be a public ctor as per the jdbc-specs.
137
public DjDriver()
138   {
139   }
140
141   String JavaDoc parseUrl(String JavaDoc p_url, String JavaDoc p_key)
142   {
143     // "jdbc:djeneric:schemafile=..."
144
String JavaDoc url = p_url.substring(14).trim();
145     StringTokenizer JavaDoc stok = new StringTokenizer JavaDoc(url, ";");
146     while (stok.hasMoreElements())
147     {
148       String JavaDoc elem = stok.nextToken();
149       if (elem.indexOf('=') == -1) continue;
150       int posEq = elem.indexOf('=');
151       String JavaDoc key = elem.substring(0, posEq).trim();
152       if (key.equalsIgnoreCase(p_key)) return elem.substring(posEq + 1).trim();
153     }
154     return null;
155   }
156
157   public Connection JavaDoc connect(String JavaDoc p_url, Properties JavaDoc p_info) throws SQLException JavaDoc
158   {
159     if (acceptsURL(p_url) == false)
160     {
161       return null;
162     }
163     //throw new SQLException("url is not supported by djeneric driver");
164
String JavaDoc urlName = parseUrl(p_url, "name");
165     String JavaDoc urlLocation = parseUrl(p_url, "location");
166     String JavaDoc urlDriver = parseUrl(p_url, "driver");
167     String JavaDoc urlUrl = parseUrl(p_url, "url");
168     String JavaDoc urlUser = parseUrl(p_url, "user");
169     String JavaDoc urlPass = parseUrl(p_url, "password");
170     try
171     {
172       DjRepositoryDescriptor desc = new DjRepositoryDescriptor(urlName, urlLocation, urlDriver, urlUrl);
173       DjConnection newConnection = new DjConnection(this, new RdbmsPersistenceManager(new DjDriverMessenger(urlUser,
174           urlPass), desc));
175       _allConnections.add(newConnection);
176       return newConnection;
177     }
178     catch (DjenericException ex)
179     {
180       DjLogger.log(ex);
181
182       throw new SQLException JavaDoc(ex.toString());
183     }
184   }
185
186   void disconnect(DjConnection p_con)
187   {
188     DjAssert.isNotNull(p_con);
189     DjAssert.check(_allConnections.contains(p_con));
190     _allConnections.remove(p_con);
191   }
192
193   public boolean acceptsURL(String JavaDoc url) throws SQLException JavaDoc
194   {
195     DjAssert.precondition(url != null);
196     /*
197      * we accept URL's of the following form:
198      * jdbc:djeneric:repository=repository.xml;name=MyRepos;user=us;password=pw
199      */

200     if (!url.startsWith("jdbc:djeneric:"))
201     {
202       return false;
203     }
204     return true;
205   }
206
207   public DriverPropertyInfo JavaDoc[] getPropertyInfo(String JavaDoc url, Properties JavaDoc info) throws SQLException JavaDoc
208   {
209     return new DriverPropertyInfo JavaDoc[0];
210   }
211
212   public int getMajorVersion()
213   {
214     return MAJOR_VERSION;
215   }
216
217   public int getMinorVersion()
218   {
219     return MINOR_VERSION;
220   }
221
222   public boolean jdbcCompliant()
223   {
224     return false;
225   }
226 }
Popular Tags