1 55 package org.lateralnz.c3d; 56 57 import java.sql.Connection ; 58 import java.sql.Driver ; 59 import java.sql.DriverManager ; 60 import java.sql.DriverPropertyInfo ; 61 import java.sql.SQLException ; 62 import java.util.HashMap ; 63 import java.util.Iterator ; 64 import java.util.Map ; 65 import java.util.Properties ; 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 , Constants { 74 private static final Logger log = Logger.getLogger(DCDriver.class.getName()); 75 76 private static final String C3D = "jdbc:c3d://"; 77 private static final int C3D_LEN = C3D.length(); 78 private static final String DBPASSWORD = "dbpassword"; 79 private static final String DBURL = "dburl"; 80 private static final String DBUSER = "dbuser"; 81 private static final String PASSWORD = "password"; 82 private static final String USER = "user"; 83 84 private static HashMap dbmap = new HashMap (); 85 86 private static final DriverPropertyInfo USER_PI = new DriverPropertyInfo ("user", ""); 87 private static final DriverPropertyInfo PASS_PI = new DriverPropertyInfo ("password", ""); 88 private static final DriverPropertyInfo [] 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 104 public static final void set(String dbname, Properties props) throws SQLException { 105 try { 106 String dbdriver = props.getProperty("dbdriver"); 107 108 Class c = Class.forName(dbdriver); 109 DriverManager.registerDriver((java.sql.Driver )c.newInstance()); 110 111 dbmap.put(dbname, DatabaseEngine.getInstance(dbname, props)); 112 } 113 catch (Exception e) { 114 e.printStackTrace(); 115 throw new SQLException ("invalid or missing database properties"); 116 } 117 } 118 119 public boolean acceptsURL(String url) throws SQLException { 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 connect(String url, Properties info) throws SQLException { 128 url = url.substring(C3D_LEN); 129 130 int pos = url.indexOf('?'); 131 String 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 tmp = StringUtils.toMap(url, AMPERSAND); 142 ResourceUtils.loadProperties(info, tmp); 143 if (!dbmap.containsKey(db)) { 144 throw new SQLException ("no database bridge named " + db); 145 } 146 147 DatabaseEngine dbengine = (DatabaseEngine)dbmap.get(db); 148 149 String user = info.getProperty(USER, EMPTY); 150 String pass = info.getProperty(PASSWORD, EMPTY); 151 152 if (!dbengine.validate(user, pass)) { 153 throw new SQLException ("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 [] getPropertyInfo(String url, Properties info) { 168 DriverPropertyInfo [] dpi = new DriverPropertyInfo [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 179 public boolean jdbcCompliant() { 180 return false; 181 } 182 183 private static final DriverPropertyInfo createCopy(DriverPropertyInfo info) { 184 DriverPropertyInfo dpi = new DriverPropertyInfo (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 |