KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > in > co > daffodil > db > rmi > RmiDaffodilDBDriver


1 package in.co.daffodil.db.rmi;
2
3 import java.rmi.Naming JavaDoc;
4 import java.sql.*;
5 import java.sql.Connection JavaDoc;
6 import java.util.*;
7
8 import com.daffodilwoods.daffodildb.server.serversystem._Connection;
9 import com.daffodilwoods.daffodildb.server.serversystem._Server;
10 import com.daffodilwoods.database.resource.DException;
11 import com.daffodilwoods.rmi.RmiServer;
12 import com.daffodilwoods.rmi.interfaces._RmiServer;
13 import in.co.daffodil.db.jdbc.AbstractDaffodilDBDriver;
14 import in.co.daffodil.db.jdbc.DaffodilDBConnection;
15
16 public class RmiDaffodilDBDriver extends AbstractDaffodilDBDriver{
17
18    private TreeMap serverMap;
19
20    static {
21      try {
22          driver = new RmiDaffodilDBDriver();
23          DriverManager.registerDriver(driver);
24      }
25      catch (Exception JavaDoc ex) {
26      }
27    }
28
29    public RmiDaffodilDBDriver(){
30       super();
31       serverMap = new TreeMap(String.CASE_INSENSITIVE_ORDER);
32    }
33
34    protected Connection JavaDoc getConnection(String JavaDoc url, Properties info) throws SQLException{
35       if(url == null){
36          url = addUrl(info);
37       }
38       String JavaDoc rmiStr=info.getProperty("rmiStr");
39       if(rmiStr==null || rmiStr.equals("")){
40          throw new DException("DSE538",null).getSqlException(null);
41       }
42       String JavaDoc rmiUrl = "rmi:" + rmiStr+"/DaffodilDB";
43       String JavaDoc dbName=(String JavaDoc)info.get(DATABASE_NAME_PROPERTY);
44       try{
45          _Connection conn = getServer(rmiUrl).getConnection(dbName,info);
46          return isStatementCached(info.get(STATEMENT_CACHE)) ? new DaffodilDBConnection(true,rmiUrl,conn) : new DaffodilDBConnection(rmiUrl,conn);
47       }catch(DException E) {
48          throw E.getSqlException(null);
49       }
50    }
51
52    protected String JavaDoc addUrl(java.util.Properties JavaDoc info){
53       StringBuffer JavaDoc strBuffer = new StringBuffer JavaDoc("jdbc:daffodilDB:");
54       int max = info.size() - 1;
55       Iterator it = info.entrySet().iterator();
56       for (int i = 0; i <= max; i++) {
57           Map.Entry e = (Map.Entry) (it.next());
58           Object JavaDoc key = e.getKey();
59           if(key.equals("rmiStr"))
60              continue;
61           Object JavaDoc value = e.getValue();
62           strBuffer.append((key == this ? "(this Map)" : key) + "=" +
63                           (value == this ? "(this Map)" : value));
64           if (i < max)
65              strBuffer.append(";");
66       }
67       return strBuffer.toString();
68    }
69
70    protected java.util.Properties JavaDoc breakURL(String JavaDoc url,java.util.Properties JavaDoc info) throws SQLException{
71       if(info==null)
72          info=new java.util.Properties JavaDoc();
73       int len="jdbc:daffodilDB:".length();
74       String JavaDoc str=url.substring(len);
75       String JavaDoc tkn,key=null;
76       StringTokenizer tkns=new StringTokenizer(str,";");
77       while(tkns.hasMoreTokens()){
78          tkn=tkns.nextToken();
79          int index=tkn.indexOf('=');
80          if(index==-1){
81             int idx=tkn.lastIndexOf('/');
82             if(idx==-1)
83                throw new SQLException("Invalid URL:"+url+"; Should contain a / in ("+tkn+")");
84             String JavaDoc temp = tkn.substring(0,idx);
85             if(temp.indexOf(':') == -1)
86                temp = temp + ":3456";
87             info.put("rmiStr" , temp);
88             if(idx<tkn.length()-1)
89                info.put(DATABASE_NAME_PROPERTY,tkn.substring(idx+1));
90          }
91          else{
92             key=tkn.substring(0,index);
93             tkn= index==tkn.length()-1? "" : tkn.substring(index+1);
94             info.put(key,tkn);
95          }
96       }
97       return info;
98    }
99
100    public boolean acceptsURL(String JavaDoc url) throws SQLException {
101       if(url == null)
102          return false;
103       return url.startsWith("jdbc:daffodilDB:");
104    }
105
106    public DriverPropertyInfo[] getPropertyInfo(String JavaDoc url, java.util.Properties JavaDoc info)throws SQLException {
107       try{
108          breakURL(url,info);
109          ArrayList drpiList=new ArrayList(4);
110          DriverPropertyInfo drpi;
111          if(info.get("user")==null){
112             drpi = new DriverPropertyInfo("user",null);
113             drpi.required = true;
114             drpiList.add(drpi);
115          }
116          if(info.get("password")==null){
117             drpi = new DriverPropertyInfo("password",null);
118             drpiList.add(drpi);
119          }
120          if(info.get("rmiStr")==null){
121             drpi = new DriverPropertyInfo("rmiStr",null);
122             drpiList.add(drpi);
123             drpi.required = true;
124          }
125          if(info.get("dbName")!=null){
126             if(info.get("create")==null){
127                drpi = new DriverPropertyInfo("create","false");
128                drpi.choices = new String JavaDoc[]{"true","false"};
129                drpiList.add(drpi);
130             }
131          }
132          return (DriverPropertyInfo[])drpiList.toArray(new DriverPropertyInfo[0]);
133       }catch(Exception JavaDoc e){
134       }
135       return null;
136    }
137
138    protected _Server getServer(String JavaDoc rmiUrl) throws SQLException {
139       _Server server1 = (_Server)serverMap.get(rmiUrl);
140       if(server1 == null){
141          try {
142             _RmiServer rmiServerInterface = (_RmiServer)Naming.lookup(rmiUrl);
143             server1 = new RmiServer(rmiServerInterface);
144          }catch(Exception JavaDoc ex) {
145             throw new SQLException(""+ex);
146          }
147          serverMap.put(rmiUrl,server1);
148       }
149       server = server1;//required or Not ???
150
return server1;
151    }
152 }
153
Popular Tags