KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lateralnz > c3d > DCDriver


1 /* ====================================================================
2  * The LateralNZ Software License, Version 1.0
3  *
4  * Copyright (c) 2003 LateralNZ. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by
21  * LateralNZ (http://www.lateralnz.org/) and other third parties."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "LateralNZ" must not be used to endorse or promote
26  * products derived from this software without prior written
27  * permission. For written permission, please
28  * contact oss@lateralnz.org.
29  *
30  * 5. Products derived from this software may not be called "Panther",
31  * or "Lateral" or "LateralNZ", nor may "PANTHER" or "LATERAL" or
32  * "LATERALNZ" appear in their name, without prior written
33  * permission of LateralNZ.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of LateralNZ. For more
51  * information on Lateral, please see http://www.lateralnz.com/ or
52  * http://www.lateralnz.org
53  *
54  */

55 package org.lateralnz.c3d;
56
57 import java.sql.Connection JavaDoc;
58 import java.sql.Driver JavaDoc;
59 import java.sql.DriverManager JavaDoc;
60 import java.sql.DriverPropertyInfo JavaDoc;
61 import java.sql.SQLException JavaDoc;
62 import java.util.HashMap JavaDoc;
63 import java.util.Iterator JavaDoc;
64 import java.util.Map JavaDoc;
65 import java.util.Properties JavaDoc;
66
67 import org.apache.log4j.Logger;
68
69 import org.lateralnz.common.util.Constants;
70 import org.lateralnz.common.util.ResourceUtils;
71 import org.lateralnz.common.util.StringUtils;
72
73 public class DCDriver implements Driver JavaDoc, Constants {
74   private static final Logger log = Logger.getLogger(DCDriver.class.getName());
75   
76   private static final String JavaDoc C3D = "jdbc:c3d://";
77   private static final int C3D_LEN = C3D.length();
78   private static final String JavaDoc DBPASSWORD = "dbpassword";
79   private static final String JavaDoc DBURL = "dburl";
80   private static final String JavaDoc DBUSER = "dbuser";
81   private static final String JavaDoc PASSWORD = "password";
82   private static final String JavaDoc USER = "user";
83   
84   private static HashMap JavaDoc dbmap = new HashMap JavaDoc();
85   
86   private static final DriverPropertyInfo JavaDoc USER_PI = new DriverPropertyInfo JavaDoc("user", "");
87   private static final DriverPropertyInfo JavaDoc PASS_PI = new DriverPropertyInfo JavaDoc("password", "");
88   private static final DriverPropertyInfo JavaDoc[] PROPS = { USER_PI, PASS_PI };
89   static {
90     USER_PI.required = true;
91     USER_PI.description = "user account name";
92     PASS_PI.required = true;
93     PASS_PI.description = "user account password";
94   }
95   
96   public DCDriver() {
97   }
98    
99  /**
100   * set up a database cache with the specified properties
101   * @param dbname the name by which to refer to this db
102   * @para props the properties to use for this database
103   */

104   public static final void set(String JavaDoc dbname, Properties JavaDoc props) throws SQLException JavaDoc {
105     try {
106       String JavaDoc dbdriver = props.getProperty("dbdriver");
107         
108       Class JavaDoc c = Class.forName(dbdriver);
109       DriverManager.registerDriver((java.sql.Driver JavaDoc)c.newInstance());
110         
111       dbmap.put(dbname, DatabaseEngine.getInstance(dbname, props));
112     }
113     catch (Exception JavaDoc e) {
114       e.printStackTrace();
115       throw new SQLException JavaDoc("invalid or missing database properties");
116     }
117   }
118
119   public boolean acceptsURL(String JavaDoc url) throws SQLException JavaDoc {
120     boolean accept = (!StringUtils.isEmpty(url) && url.startsWith(C3D));
121     if (!accept && log.isDebugEnabled()) {
122       log.debug(url + " is not accepted by this driver");
123     }
124     return accept;
125   }
126   
127   public Connection JavaDoc connect(String JavaDoc url, Properties JavaDoc info) throws SQLException JavaDoc {
128     url = url.substring(C3D_LEN);
129     
130     int pos = url.indexOf('?');
131     String JavaDoc db;
132     if (pos >= 0) {
133       db = url.substring(0, pos);
134       url = url.substring(pos+1);
135     }
136     else {
137       db = url;
138       url = EMPTY;
139     }
140     
141     Map JavaDoc tmp = StringUtils.toMap(url, AMPERSAND);
142     ResourceUtils.loadProperties(info, tmp);
143     if (!dbmap.containsKey(db)) {
144       throw new SQLException JavaDoc("no database bridge named " + db);
145     }
146     
147     DatabaseEngine dbengine = (DatabaseEngine)dbmap.get(db);
148     
149     String JavaDoc user = info.getProperty(USER, EMPTY);
150     String JavaDoc pass = info.getProperty(PASSWORD, EMPTY);
151     
152     if (!dbengine.validate(user, pass)) {
153       throw new SQLException JavaDoc("invalid user and/or password");
154     }
155     
156     return new DCConnection(DriverManager.getConnection(dbengine.getProperty(DBURL), dbengine.getProperty(DBUSER), dbengine.getProperty(DBPASSWORD)), dbengine);
157   }
158   
159   public int getMajorVersion() {
160     return 0;
161   }
162   
163   public int getMinorVersion() {
164     return 2;
165   }
166   
167   public DriverPropertyInfo JavaDoc[] getPropertyInfo(String JavaDoc url, Properties JavaDoc info) {
168     DriverPropertyInfo JavaDoc[] dpi = new DriverPropertyInfo JavaDoc[PROPS.length];
169     for (int i = 0; i < PROPS.length; i++) {
170       dpi[i] = createCopy(PROPS[i]);
171     }
172     
173     return dpi;
174   }
175
176  /**
177   * for the moment at least, not jdbc compliant
178   */

179   public boolean jdbcCompliant() {
180     return false;
181   }
182   
183   private static final DriverPropertyInfo JavaDoc createCopy(DriverPropertyInfo JavaDoc info) {
184     DriverPropertyInfo JavaDoc dpi = new DriverPropertyInfo JavaDoc(info.name, info.value);
185     dpi.required = info.required;
186     dpi.description = info.description;
187     dpi.choices = info.choices;
188     return dpi;
189   }
190
191 }
Popular Tags