1 package in.co.daffodil.db.jdbc; 2 3 import java.io.*; 4 import java.sql.*; 5 import java.util.*; 6 import com.daffodilwoods.daffodildb.server.serversystem._Server; 7 import com.daffodilwoods.daffodildb.server.serversystem._Connection; 8 import com.daffodilwoods.database.resource.*; 9 import java.lang.reflect.Constructor ; 10 import com.daffodilwoods.database.utility.P; 11 12 public class DaffodilDBDriver extends AbstractDaffodilDBDriver implements Serializable { 13 static { 14 try { 15 driver = new DaffodilDBDriver(); 16 DriverManager.registerDriver(driver); 17 } catch (SQLException ex) { 18 } 19 } 20 private int urlLen = 0; 21 public DaffodilDBDriver() { 22 urlLen = "jdbc:daffodilDB_embedded:".length(); 23 } 24 25 protected Connection getConnection(String url, Properties info) throws SQLException { 26 if (url == null) { 27 url = addUrl(info); 28 } 29 String pth = info.getProperty(DATABASE_HOME_PROPERTY, ""); 30 if (pth.trim().length() != 0) { 31 System.setProperty(_Server.DAFFODILDB_HOME, pth); 32 } 33 String dbName = (String ) info.get(DATABASE_NAME_PROPERTY); 34 35 try { 36 _Connection session = getServer(url).getConnection(dbName, info); 37 return isStatementCached(info.get(STATEMENT_CACHE)) ? new DaffodilDBConnection(true,url, session): new DaffodilDBConnection(url, session); 38 39 } catch (DException E) { 40 throw E.getSqlException(null); 41 } 42 } 43 44 public boolean acceptsURL(String url) throws SQLException { 45 if (url == null) 46 return false; 47 return url.startsWith("jdbc:daffodilDB_embedded:"); 48 } 49 50 public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException { 51 ArrayList drpiList = new ArrayList(4); 52 DriverPropertyInfo drpi; 53 54 if (info.get("user") == null) { 55 drpi = new DriverPropertyInfo("user", null); 56 drpi.description = "User Name for the connection"; 57 drpi.required = true; 58 drpiList.add(drpi); 59 } 60 61 if (info.get("password") == null) { 62 drpi = new DriverPropertyInfo("password", null); 63 drpi.description = "Password for the connection"; 64 drpiList.add(drpi); 65 } 66 67 if (info.get(DATABASE_NAME_PROPERTY) != null) { 68 if (info.get(DATABASE_CREATE_PROPERTY) == null) { 69 drpi = new DriverPropertyInfo(DATABASE_CREATE_PROPERTY, "false"); 70 drpi.description = "Create property to specify whether to create new database if not present"; 71 drpi.choices = new String [] {"true", "false"}; 72 drpiList.add(drpi); 73 } 74 } 75 return (DriverPropertyInfo[]) drpiList.toArray(new DriverPropertyInfo[0]); 76 } 77 78 83 public void setServer(_Server server) { 84 this.server = server; 85 } 86 87 88 protected _Server getServer(String url) throws SQLException { 89 Properties prop = null; 90 prop = breakURL(url, prop); 91 try { 92 if (server == null) { 93 prop = P.convertDaffodilDBKeyssToUpperCase(prop); 94 String readOnly = prop.getProperty(_Server.READONLY); 95 if (readOnly != null) { 96 if (prop.getProperty(_Server.READONLY).equalsIgnoreCase("true")) { 97 Class cl = Class.forName("com.daffodilwoods.daffodildb.server.serversystem.ServerSystem"); 98 Constructor construct = cl.getConstructor(new Class [] {boolean.class}); 99 server = (_Server) construct.newInstance(new Boolean [] {Utilities.getBooleanValue(true)}); 100 return server; 101 } 102 } 103 Class cl = Class.forName("com.daffodilwoods.daffodildb.server.serversystem.ServerSystem"); 104 server = (_Server) cl.newInstance(); 105 } 106 } catch (Exception ex) { 107 throw new SQLException("" + ex); 108 } 109 return server; 110 } 111 112 protected String addUrl(java.util.Properties info) { 113 StringBuffer strBuffer = new StringBuffer ("jdbc:daffodilDB_embedded:"); 114 int max = info.size() - 1; 115 Iterator it = info.entrySet().iterator(); 116 for (int i = 0; i <= max; i++) { 117 Map.Entry e = (Map.Entry) (it.next()); 118 Object key = e.getKey(); 119 Object value = e.getValue(); 120 strBuffer.append( (key == this ? "(this Map)" : key) + "=" + 121 (value == this ? "(this Map)" : value)); 122 if (i < max) 123 strBuffer.append(";"); 124 } 125 return strBuffer.toString(); 126 } 127 128 protected java.util.Properties breakURL(String url, Properties info) throws SQLException { 129 if (info == null) 130 info = new java.util.Properties (); 131 String str = url.substring(urlLen); 132 String tkn, key = null; 133 StringTokenizer tkns = new StringTokenizer(str, ";"); 134 while (tkns.hasMoreTokens()) { 135 tkn = tkns.nextToken(); 136 int index = tkn.indexOf('='); 137 if (index == -1) 138 info.put(DATABASE_NAME_PROPERTY, tkn); 139 else { 140 key = tkn.substring(0, index); 141 tkn = index == tkn.length() - 1 ? "" : tkn.substring(index + 1); 142 info.put(key, tkn); 143 } 144 } 145 return info; 146 } 147 } 148 | Popular Tags |