001: /* ====================================================================
002: * The LateralNZ Software License, Version 1.0
003: *
004: * Copyright (c) 2003 LateralNZ. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution,
019: * if any, must include the following acknowledgment:
020: * "This product includes software developed by
021: * LateralNZ (http://www.lateralnz.org/) and other third parties."
022: * Alternately, this acknowledgment may appear in the software itself,
023: * if and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. The names "LateralNZ" must not be used to endorse or promote
026: * products derived from this software without prior written
027: * permission. For written permission, please
028: * contact oss@lateralnz.org.
029: *
030: * 5. Products derived from this software may not be called "Panther",
031: * or "Lateral" or "LateralNZ", nor may "PANTHER" or "LATERAL" or
032: * "LATERALNZ" appear in their name, without prior written
033: * permission of LateralNZ.
034: *
035: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: *
049: * This software consists of voluntary contributions made by many
050: * individuals on behalf of LateralNZ. For more
051: * information on Lateral, please see http://www.lateralnz.com/ or
052: * http://www.lateralnz.org
053: *
054: */
055: package org.lateralnz.c3d;
056:
057: import java.sql.Connection;
058: import java.sql.Driver;
059: import java.sql.DriverManager;
060: import java.sql.DriverPropertyInfo;
061: import java.sql.SQLException;
062: import java.util.HashMap;
063: import java.util.Iterator;
064: import java.util.Map;
065: import java.util.Properties;
066:
067: import org.apache.log4j.Logger;
068:
069: import org.lateralnz.common.util.Constants;
070: import org.lateralnz.common.util.ResourceUtils;
071: import org.lateralnz.common.util.StringUtils;
072:
073: public class DCDriver implements Driver, Constants {
074: private static final Logger log = Logger.getLogger(DCDriver.class
075: .getName());
076:
077: private static final String C3D = "jdbc:c3d://";
078: private static final int C3D_LEN = C3D.length();
079: private static final String DBPASSWORD = "dbpassword";
080: private static final String DBURL = "dburl";
081: private static final String DBUSER = "dbuser";
082: private static final String PASSWORD = "password";
083: private static final String USER = "user";
084:
085: private static HashMap dbmap = new HashMap();
086:
087: private static final DriverPropertyInfo USER_PI = new DriverPropertyInfo(
088: "user", "");
089: private static final DriverPropertyInfo PASS_PI = new DriverPropertyInfo(
090: "password", "");
091: private static final DriverPropertyInfo[] PROPS = { USER_PI,
092: PASS_PI };
093: static {
094: USER_PI.required = true;
095: USER_PI.description = "user account name";
096: PASS_PI.required = true;
097: PASS_PI.description = "user account password";
098: }
099:
100: public DCDriver() {
101: }
102:
103: /**
104: * set up a database cache with the specified properties
105: * @param dbname the name by which to refer to this db
106: * @para props the properties to use for this database
107: */
108: public static final void set(String dbname, Properties props)
109: throws SQLException {
110: try {
111: String dbdriver = props.getProperty("dbdriver");
112:
113: Class c = Class.forName(dbdriver);
114: DriverManager.registerDriver((java.sql.Driver) c
115: .newInstance());
116:
117: dbmap
118: .put(dbname, DatabaseEngine.getInstance(dbname,
119: props));
120: } catch (Exception e) {
121: e.printStackTrace();
122: throw new SQLException(
123: "invalid or missing database properties");
124: }
125: }
126:
127: public boolean acceptsURL(String url) throws SQLException {
128: boolean accept = (!StringUtils.isEmpty(url) && url
129: .startsWith(C3D));
130: if (!accept && log.isDebugEnabled()) {
131: log.debug(url + " is not accepted by this driver");
132: }
133: return accept;
134: }
135:
136: public Connection connect(String url, Properties info)
137: throws SQLException {
138: url = url.substring(C3D_LEN);
139:
140: int pos = url.indexOf('?');
141: String db;
142: if (pos >= 0) {
143: db = url.substring(0, pos);
144: url = url.substring(pos + 1);
145: } else {
146: db = url;
147: url = EMPTY;
148: }
149:
150: Map tmp = StringUtils.toMap(url, AMPERSAND);
151: ResourceUtils.loadProperties(info, tmp);
152: if (!dbmap.containsKey(db)) {
153: throw new SQLException("no database bridge named " + db);
154: }
155:
156: DatabaseEngine dbengine = (DatabaseEngine) dbmap.get(db);
157:
158: String user = info.getProperty(USER, EMPTY);
159: String pass = info.getProperty(PASSWORD, EMPTY);
160:
161: if (!dbengine.validate(user, pass)) {
162: throw new SQLException("invalid user and/or password");
163: }
164:
165: return new DCConnection(DriverManager.getConnection(dbengine
166: .getProperty(DBURL), dbengine.getProperty(DBUSER),
167: dbengine.getProperty(DBPASSWORD)), dbengine);
168: }
169:
170: public int getMajorVersion() {
171: return 0;
172: }
173:
174: public int getMinorVersion() {
175: return 2;
176: }
177:
178: public DriverPropertyInfo[] getPropertyInfo(String url,
179: Properties info) {
180: DriverPropertyInfo[] dpi = new DriverPropertyInfo[PROPS.length];
181: for (int i = 0; i < PROPS.length; i++) {
182: dpi[i] = createCopy(PROPS[i]);
183: }
184:
185: return dpi;
186: }
187:
188: /**
189: * for the moment at least, not jdbc compliant
190: */
191: public boolean jdbcCompliant() {
192: return false;
193: }
194:
195: private static final DriverPropertyInfo createCopy(
196: DriverPropertyInfo info) {
197: DriverPropertyInfo dpi = new DriverPropertyInfo(info.name,
198: info.value);
199: dpi.required = info.required;
200: dpi.description = info.description;
201: dpi.choices = info.choices;
202: return dpi;
203: }
204:
205: }
|