1 6 7 package org.jfox.jdbc.xa; 8 9 import java.io.PrintWriter ; 10 import java.sql.SQLException ; 11 import java.util.Properties ; 12 import javax.sql.XAConnection ; 13 import javax.sql.XADataSource ; 14 15 18 19 public abstract class AbstractXADataSource implements XADataSource { 20 protected XADataSource xads = null; 21 22 private String url; 23 24 public AbstractXADataSource(String url) { 25 this.url = url; 26 xads = init(url); 27 } 28 29 35 protected abstract XADataSource init(String url); 36 37 public String getUrl() { 38 return url; 39 } 40 41 public int getLoginTimeout() throws SQLException { 42 return xads.getLoginTimeout(); 43 } 44 45 public void setLoginTimeout(int seconds) throws SQLException { 46 xads.setLoginTimeout(seconds); 47 } 48 49 public PrintWriter getLogWriter() throws SQLException { 50 return xads.getLogWriter(); 51 } 52 53 public void setLogWriter(PrintWriter out) throws SQLException { 54 xads.setLogWriter(out); 55 } 56 57 public XAConnection getXAConnection() throws SQLException { 58 return xads.getXAConnection(); 59 } 60 61 public XAConnection getXAConnection(String user, String password) throws SQLException { 62 return xads.getXAConnection(user, password); 63 } 64 65 66 public void setUrl(XADataSource xads, String url) throws Exception { 67 xads.getClass().getMethod("setURL", new Class []{String .class}) 68 .invoke(xads, new Object []{url}); 69 } 70 71 protected static URLProperties parseSqlURL(String url) { 72 int biasIndex = url.indexOf("://"); 74 int semicolonIndex = url.indexOf(";"); 75 76 String serverNameAndPortNumber = url.substring(biasIndex + 3, semicolonIndex); 77 String 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 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 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 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 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 prop = new Properties (); 118 119 public String getProperty(String key) { 120 return prop.getProperty(key.toUpperCase()); 121 } 122 123 public synchronized Object setProperty(String key, String value) { 124 Object old = prop.getProperty(key.toUpperCase()); 125 prop.setProperty(key.toUpperCase(), value); 126 return old; 127 } 128 129 public synchronized boolean containsKey(Object key) { 130 return prop.containsKey(((String ) key).toUpperCase()); 131 } 132 } 133 134 public static void main(String [] args) { 135 136 } 137 } 138 139 | Popular Tags |