Source code: com/rohanclan/ashpool/jdbc/Driver.java
1 /*
2 * Ashpool - XML Database
3 * Copyright (C) 2003 Rob Rohan
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 675 Mass Ave, Cambridge, MA 02139, USA.
17 *
18 * Driver.java
19 *
20 * Created on January 30, 2003, 7:09 PM
21 */
22
23 package com.rohanclan.ashpool.jdbc;
24
25 import java.sql.*;
26 import java.util.*;
27
28 /**
29 *
30 * @author rob
31 */
32 public class Driver implements java.sql.Driver {
33
34 /** Creates a new instance of Driver */
35 public Driver() {;}
36
37 public Driver newInstance(){
38 return new Driver();
39 }
40
41 /** Retrieves whether the driver thinks that it can open a connection
42 * to the given URL. Typically drivers will return <code>true</code> if they
43 * understand the subprotocol specified in the URL and <code>false</code> if
44 * they do not.
45 *
46 * @param url the URL of the database
47 * @return <code>true</code> if this driver understands the given URL;
48 * <code>false</code> otherwise
49 * @exception SQLException if a database access error occurs
50 *
51 */
52 public boolean acceptsURL(String url) throws SQLException {
53 System.out.println("Does Ashpool driver accept " + url + "?");
54
55 StringTokenizer stok = new StringTokenizer(url,":");
56
57 if(stok.nextElement().equals("jdbc")
58 && stok.nextElement().equals("ashpool")){
59 //close enough for jazz
60 return true;
61 }else{
62 return false;
63 }
64 }
65
66 /** Attempts to make a database connection to the given URL.
67 * The driver should return "null" if it realizes it is the wrong kind
68 * of driver to connect to the given URL. This will be common, as when
69 * the JDBC driver manager is asked to connect to a given URL it passes
70 * the URL to each loaded driver in turn.
71 *
72 * <P>The driver should throw an <code>SQLException</code> if it is the right
73 * driver to connect to the given URL but has trouble connecting to
74 * the database.
75 *
76 * <P>The <code>java.util.Properties</code> argument can be used to pass
77 * arbitrary string tag/value pairs as connection arguments.
78 * Normally at least "user" and "password" properties should be
79 * included in the <code>Properties</code> object.
80 *
81 * @param url the URL of the database to which to connect
82 * @param info a list of arbitrary string tag/value pairs as
83 * connection arguments. Normally at least a "user" and
84 * "password" property should be included.
85 * @return a <code>Connection</code> object that represents a
86 * connection to the URL
87 * @exception SQLException if a database access error occurs
88 *
89 * jdbc:ashpool://file:///home/rob/projects/Ashpool/datastore/;password=groovy
90 * jdbc:ashpool://file://C:\winnt\fake\xmldir\;password=groovy
91 *
92 */
93 public java.sql.Connection connect(String url, java.util.Properties info) throws SQLException {
94 System.out.println("Trying to connect to " + url);
95
96 StringTokenizer stok = new StringTokenizer(url,":");
97
98 if(stok.nextElement().equals("jdbc")
99 && stok.nextElement().equals("ashpool")){
100
101 //this should be the "file" word
102 stok.nextElement();
103
104 //this should be the path to the directory
105 String storepath = stok.nextElement().toString();
106
107 if(storepath.toString().startsWith("//")){
108 try{
109 System.out.println("Connecting to: " + storepath.substring(2));
110 java.io.File datastore = new java.io.File(storepath.substring(2));
111
112 if(!datastore.exists() || !datastore.isDirectory()){
113 throw new SQLException("Datastore either does not exsit, or is not a directory " + storepath.substring(2));
114 }
115
116 return new Connection(datastore);
117
118 }catch(Exception e){
119 throw new SQLException("Could not open datastore: " + e.toString());
120 }
121
122 }else{
123 System.err.println("Malformed url");
124 return null;
125 }
126 }else{
127 System.err.println("Malformed url");
128 return null;
129 }
130 }
131
132 /** Retrieves the driver's major version number. Initially this should be 1.
133 *
134 * @return this driver's major version number
135 *
136 */
137 public int getMajorVersion() {
138 return 0;
139 }
140
141 /** Gets the driver's minor version number. Initially this should be 0.
142 * @return this driver's minor version number
143 *
144 */
145 public int getMinorVersion() {
146 return 0;
147 }
148
149 /** Gets information about the possible properties for this driver.
150 * <P>
151 * The <code>getPropertyInfo</code> method is intended to allow a generic
152 * GUI tool to discover what properties it should prompt
153 * a human for in order to get
154 * enough information to connect to a database. Note that depending on
155 * the values the human has supplied so far, additional values may become
156 * necessary, so it may be necessary to iterate though several calls
157 * to the <code>getPropertyInfo</code> method.
158 *
159 * @param url the URL of the database to which to connect
160 * @param info a proposed list of tag/value pairs that will be sent on
161 * connect open
162 * @return an array of <code>DriverPropertyInfo</code> objects describing
163 * possible properties. This array may be an empty array if
164 * no properties are required.
165 * @exception SQLException if a database access error occurs
166 *
167 */
168 public DriverPropertyInfo[] getPropertyInfo(String url, java.util.Properties info) throws SQLException {
169 DriverPropertyInfo driverPI[] = new DriverPropertyInfo[0];
170 return driverPI;
171 }
172
173 /** Reports whether this driver is a genuine JDBC
174 * Compliant<sup><font size=-2>TM</font></sup> driver.
175 * A driver may only report <code>true</code> here if it passes the JDBC
176 * compliance tests; otherwise it is required to return <code>false</code>.
177 * <P>
178 * JDBC compliance requires full support for the JDBC API and full support
179 * for SQL 92 Entry Level. It is expected that JDBC compliant drivers will
180 * be available for all the major commercial databases.
181 * <P>
182 * This method is not intended to encourage the development of non-JDBC
183 * compliant drivers, but is a recognition of the fact that some vendors
184 * are interested in using the JDBC API and framework for lightweight
185 * databases that do not support full database functionality, or for
186 * special databases such as document information retrieval where a SQL
187 * implementation may not be feasible.
188 * @return <code>true</code> if this driver is JDBC Compliant; <code>false</code>
189 * otherwise
190 *
191 */
192 public boolean jdbcCompliant() {
193 return false;
194 }
195
196 }