KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > jdbc > xa > AbstractXADataSource


1 /* JFox, the OpenSource J2EE Application Server
2  *
3  * Distributable under GNU LGPL license by gun.org
4  * more details please visit http://www.huihoo.org/jfox
5  */

6
7 package org.jfox.jdbc.xa;
8
9 import java.io.PrintWriter JavaDoc;
10 import java.sql.SQLException JavaDoc;
11 import java.util.Properties JavaDoc;
12 import javax.sql.XAConnection JavaDoc;
13 import javax.sql.XADataSource JavaDoc;
14
15 /**
16  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
17  */

18
19 public abstract class AbstractXADataSource implements XADataSource JavaDoc {
20     protected XADataSource JavaDoc xads = null;
21
22     private String JavaDoc url;
23
24     public AbstractXADataSource(String JavaDoc url) {
25         this.url = url;
26         xads = init(url);
27     }
28
29     /**
30      * build the really xa datasource
31      *
32      * @param url
33      * @return
34      */

35     protected abstract XADataSource JavaDoc init(String JavaDoc url);
36
37     public String JavaDoc getUrl() {
38         return url;
39     }
40
41     public int getLoginTimeout() throws SQLException JavaDoc {
42         return xads.getLoginTimeout();
43     }
44
45     public void setLoginTimeout(int seconds) throws SQLException JavaDoc {
46         xads.setLoginTimeout(seconds);
47     }
48
49     public PrintWriter JavaDoc getLogWriter() throws SQLException JavaDoc {
50         return xads.getLogWriter();
51     }
52
53     public void setLogWriter(PrintWriter JavaDoc out) throws SQLException JavaDoc {
54         xads.setLogWriter(out);
55     }
56
57     public XAConnection JavaDoc getXAConnection() throws SQLException JavaDoc {
58         return xads.getXAConnection();
59     }
60
61     public XAConnection JavaDoc getXAConnection(String JavaDoc user, String JavaDoc password) throws SQLException JavaDoc {
62         return xads.getXAConnection(user, password);
63     }
64
65
66     public void setUrl(XADataSource JavaDoc xads, String JavaDoc url) throws Exception JavaDoc {
67         xads.getClass().getMethod("setURL", new Class JavaDoc[]{String JavaDoc.class})
68                 .invoke(xads, new Object JavaDoc[]{url});
69     }
70
71     protected static URLProperties parseSqlURL(String JavaDoc url) {
72         // url example: jdbc:microsoft:sqlserver://localhost:1433;user=x;password=y;databaseName=db;
73
int biasIndex = url.indexOf("://");
74         int semicolonIndex = url.indexOf(";");
75
76         String JavaDoc serverNameAndPortNumber = url.substring(biasIndex + 3, semicolonIndex);
77         String JavaDoc urlProperties = url.substring(semicolonIndex + 1);
78
79         URLProperties urlProp = new URLProperties();
80         parseSqlServerNameAndPort(serverNameAndPortNumber, urlProp);
81         parseSqlProperties(urlProperties, urlProp);
82         return urlProp;
83     }
84
85     protected static void parseSqlProperties(String JavaDoc urlProperties, URLProperties urlProp) {
86         int pre_semicolonIndex = -1;
87         int post_semicolonIndex = pre_semicolonIndex;
88         while((post_semicolonIndex = urlProperties.indexOf(";", pre_semicolonIndex + 1)) > 0) {
89             String JavaDoc kv = urlProperties.substring(pre_semicolonIndex + 1, post_semicolonIndex);
90             int equalsIndex = kv.indexOf("=");
91             if(equalsIndex > 0) {
92                 urlProp.setProperty(kv.substring(0, equalsIndex).trim(), kv.substring(equalsIndex + 1).trim());
93             }
94             pre_semicolonIndex = post_semicolonIndex;
95         }
96         if(pre_semicolonIndex + 1 != urlProperties.length()) {
97             String JavaDoc kv = urlProperties.substring(pre_semicolonIndex + 1);
98             int equalsIndex = kv.indexOf("=");
99             if(equalsIndex > 0) {
100                 urlProp.setProperty(kv.substring(0, equalsIndex).trim(), kv.substring(equalsIndex + 1).trim());
101             }
102         }
103     }
104
105     protected static void parseSqlServerNameAndPort(String JavaDoc serverNameAndPortNumber, URLProperties urlProp) {
106         int colonIndex = serverNameAndPortNumber.indexOf(":");
107         if(colonIndex > 0) {
108             urlProp.setProperty("serverName", serverNameAndPortNumber.substring(0, colonIndex));
109             urlProp.setProperty("portNumber", serverNameAndPortNumber.substring(colonIndex + 1));
110         }
111         else {
112             urlProp.setProperty("serverName", serverNameAndPortNumber);
113         }
114     }
115
116     static protected class URLProperties {
117         private Properties JavaDoc prop = new Properties JavaDoc();
118
119         public String JavaDoc getProperty(String JavaDoc key) {
120             return prop.getProperty(key.toUpperCase());
121         }
122
123         public synchronized Object JavaDoc setProperty(String JavaDoc key, String JavaDoc value) {
124             Object JavaDoc old = prop.getProperty(key.toUpperCase());
125             prop.setProperty(key.toUpperCase(), value);
126             return old;
127         }
128
129         public synchronized boolean containsKey(Object JavaDoc key) {
130             return prop.containsKey(((String JavaDoc) key).toUpperCase());
131         }
132     }
133
134     public static void main(String JavaDoc[] args) {
135
136     }
137 }
138
139
Popular Tags