1 30 31 package com.genimen.djeneric.jdbc; 32 33 import java.sql.Connection ; 34 import java.sql.DriverManager ; 35 import java.sql.DriverPropertyInfo ; 36 import java.sql.SQLException ; 37 import java.util.ArrayList ; 38 import java.util.List ; 39 import java.util.Properties ; 40 import java.util.StringTokenizer ; 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 uid, String 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 71 public DjContext selectContext(DjContext[] ctxts) throws LogonException 72 { 73 return null; 74 } 75 76 81 public DjRepositoryDescriptor selectRepository(DjRepositoryDescriptor[] descriptors) 82 { 83 return null; 84 } 85 86 92 public void showMessage(String title, String msg) 93 { 94 DriverManager.println(title); 95 DriverManager.println(msg); 96 } 97 98 104 public void showWarning(String title, String msg) 105 { 106 DriverManager.println(title); 107 DriverManager.println(msg); 108 } 109 110 } 111 112 public class DjDriver implements java.sql.Driver 113 { 114 static final String CLASSNAME = DjDriver.class.getName(); 115 static final int MAJOR_VERSION = 1; 116 static final int MINOR_VERSION = 0; 117 private List _allConnections = new ArrayList (); 118 119 static 120 { 121 try 123 { 124 DriverManager.registerDriver(new DjDriver()); 126 } 128 catch (SQLException sqle) 129 { 130 DriverManager.println(sqle.toString()); 133 } 134 } 135 136 public DjDriver() 138 { 139 } 140 141 String parseUrl(String p_url, String p_key) 142 { 143 String url = p_url.substring(14).trim(); 145 StringTokenizer stok = new StringTokenizer (url, ";"); 146 while (stok.hasMoreElements()) 147 { 148 String elem = stok.nextToken(); 149 if (elem.indexOf('=') == -1) continue; 150 int posEq = elem.indexOf('='); 151 String 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 connect(String p_url, Properties p_info) throws SQLException 158 { 159 if (acceptsURL(p_url) == false) 160 { 161 return null; 162 } 163 String urlName = parseUrl(p_url, "name"); 165 String urlLocation = parseUrl(p_url, "location"); 166 String urlDriver = parseUrl(p_url, "driver"); 167 String urlUrl = parseUrl(p_url, "url"); 168 String urlUser = parseUrl(p_url, "user"); 169 String 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 (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 url) throws SQLException 194 { 195 DjAssert.precondition(url != null); 196 200 if (!url.startsWith("jdbc:djeneric:")) 201 { 202 return false; 203 } 204 return true; 205 } 206 207 public DriverPropertyInfo [] getPropertyInfo(String url, Properties info) throws SQLException 208 { 209 return new DriverPropertyInfo [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 |