1 22 package org.jboss.resource.adapter.jdbc.local; 23 24 import org.jboss.resource.JBossResourceException; 25 import org.jboss.util.JBossStringBuilder; 26 27 import javax.resource.spi.ManagedConnection ; 28 import javax.resource.spi.ConnectionRequestInfo ; 29 import javax.resource.ResourceException ; 30 import javax.security.auth.Subject ; 31 import java.util.List ; 32 import java.util.ArrayList ; 33 import java.util.Properties ; 34 import java.util.Collections ; 35 import java.sql.Driver ; 36 import java.sql.Connection ; 37 38 42 public class HALocalManagedConnectionFactory 43 extends LocalManagedConnectionFactory 44 { 45 private static final long serialVersionUID = -6506610639011749394L; 46 47 private URLSelector urlSelector; 48 private String urlDelimeter; 49 50 public String getURLDelimeter() 51 { 52 return urlDelimeter; 53 } 54 55 public void setURLDelimeter(String urlDelimeter) 56 { 57 this.urlDelimeter = urlDelimeter; 58 if(getConnectionURL() != null) 59 { 60 initUrlSelector(); 61 } 62 } 63 64 public void setConnectionURL(String connectionURL) 65 { 66 super.setConnectionURL(connectionURL); 67 if(urlDelimeter != null) 68 { 69 initUrlSelector(); 70 } 71 } 72 73 public ManagedConnection createManagedConnection(Subject subject, ConnectionRequestInfo cri) 74 throws ResourceException 75 { 76 boolean trace = log.isTraceEnabled(); 77 Properties props = getConnectionProperties(subject, cri); 78 Properties copy = (Properties )props.clone(); 82 if(trace) 83 { 84 Properties logCopy = copy; 86 if(copy.getProperty("password") != null) 87 { 88 logCopy = (Properties )props.clone(); 89 logCopy.setProperty("password", "--hidden--"); 90 } 91 log.trace("Using properties: " + logCopy); 92 } 93 94 return doCreateManagedConnection(copy, props); 95 } 96 97 private ManagedConnection doCreateManagedConnection(Properties copy, Properties props) 98 throws JBossResourceException 99 { 100 boolean trace = log.isTraceEnabled(); 101 102 if (urlSelector == null) 103 { 104 JBossStringBuilder buffer = new JBossStringBuilder(); 105 buffer.append("Missing configuration for HA local datasource. "); 106 if (getConnectionURL() == null) 107 buffer.append("No connection-url. "); 108 if (urlDelimeter == null) 109 buffer.append("No url-delimiter. "); 110 throw new JBossResourceException(buffer.toString()); 111 } 112 113 for(int i = 0; i < urlSelector.getUrlList().size(); ++i) 115 { 116 String url = urlSelector.getUrl(); 117 118 if(trace) 119 { 120 log.trace("Trying to create a connection to " + url); 121 } 122 123 try 124 { 125 Driver d = getDriver(url); 126 Connection con = d.connect(url, copy); 127 if(con == null) 128 { 129 log.warn("Wrong driver class for this connection URL: " + url); 130 urlSelector.failedUrl(url); 131 } 132 else 133 { 134 return new LocalManagedConnection(this, con, props, transactionIsolation, preparedStatementCacheSize); 135 } 136 } 137 catch(Exception e) 138 { 139 log.warn("Failed to create connection for " + url + ": " + e.getMessage()); 140 urlSelector.failedUrl(url); 141 } 142 } 143 144 throw new JBossResourceException( 146 "Could not create connection using any of the URLs: " + urlSelector.getUrlList() 147 ); 148 } 149 150 private void initUrlSelector() 151 { 152 boolean trace = log.isTraceEnabled(); 153 154 List urlsList = new ArrayList (); 155 String urlsStr = getConnectionURL(); 156 String url; 157 int urlStart = 0; 158 int urlEnd = urlsStr.indexOf(urlDelimeter); 159 while(urlEnd > 0) 160 { 161 url = urlsStr.substring(urlStart, urlEnd); 162 urlsList.add(url); 163 urlStart = ++urlEnd; 164 urlEnd = urlsStr.indexOf(urlDelimeter, urlEnd); 165 if (trace) 166 log.trace("added HA connection url: " + url); 167 } 168 169 if(urlStart != urlsStr.length()) 170 { 171 url = urlsStr.substring(urlStart, urlsStr.length()); 172 urlsList.add(url); 173 if (trace) 174 log.trace("added HA connection url: " + url); 175 } 176 177 this.urlSelector = new URLSelector(urlsList); 178 } 179 180 182 public static class URLSelector 183 { 184 private final List urls; 185 private int urlIndex; 186 private String url; 187 188 public URLSelector(List urls) 189 { 190 if(urls == null || urls.size() == 0) 191 { 192 throw new IllegalStateException ("Expected non-empty list of connection URLs but got: " + urls); 193 } 194 this.urls = Collections.unmodifiableList(urls); 195 } 196 197 public synchronized String getUrl() 198 { 199 if(url == null) 200 { 201 if(urlIndex == urls.size()) 202 { 203 urlIndex = 0; 204 } 205 url = (String )urls.get(urlIndex++); 206 } 207 return url; 208 } 209 210 public synchronized void failedUrl(String url) 211 { 212 if(url.equals(this.url)) 213 { 214 this.url = null; 215 } 216 } 217 218 public List getUrlList() 219 { 220 return urls; 221 } 222 } 223 } 224 | Popular Tags |