[jboss-cvs] JBossAS SVN: r62220 - trunk/connector/src/main/org/jboss/resource/adapter/jdbc/local.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Tue Apr 10 12:14:29 EDT 2007
Author: vicky.kak at jboss.com
Date: 2007-04-10 12:14:29 -0400 (Tue, 10 Apr 2007)
New Revision: 62220
Modified:
trunk/connector/src/main/org/jboss/resource/adapter/jdbc/local/LocalManagedConnectionFactory.java
Log:
[JBAS-3707] Changes
Modified: trunk/connector/src/main/org/jboss/resource/adapter/jdbc/local/LocalManagedConnectionFactory.java
===================================================================
--- trunk/connector/src/main/org/jboss/resource/adapter/jdbc/local/LocalManagedConnectionFactory.java 2007-04-10 16:13:54 UTC (rev 62219)
+++ trunk/connector/src/main/org/jboss/resource/adapter/jdbc/local/LocalManagedConnectionFactory.java 2007-04-10 16:14:29 UTC (rev 62220)
@@ -21,14 +21,18 @@
*/
package org.jboss.resource.adapter.jdbc.local;
+import org.jboss.resource.adapter.jdbc.URLSelectorStrategy;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
+import java.util.ArrayList;
+import java.util.Collections;
import java.util.Iterator;
import java.util.Properties;
+import java.util.List;
import java.util.Set;
import javax.resource.ResourceException;
@@ -56,6 +60,8 @@
private transient Driver driver;
private String connectionURL;
+
+ private URLSelectorStrategy urlSelector;
protected String connectionProperties;
@@ -79,9 +85,14 @@
*
* @param connectionURL Value to assign to ConnectionURL.
*/
- public void setConnectionURL(final String connectionURL)
+ public void setConnectionURL(final String connectionURL)
+ throws ResourceException
{
this.connectionURL = connectionURL;
+ if(urlDelimiter!=null)
+ {
+ initUrlSelector();
+ }
}
/**
@@ -161,23 +172,181 @@
}
log.trace("Using properties: " + logCopy);
}
+
+ if(urlSelector!=null)
+ {
+ return getHALocalManagedConnection(props,copy);
+ }
+ else
+ {
+ return getLocalManagedConnection(props,copy);
+ }
+ }
- try
+ private LocalManagedConnection getLocalManagedConnection(Properties props,Properties copy)
+ throws JBossResourceException
+ {
+ try
+ {
+ String url = getConnectionURL();
+ Driver d = getDriver(url);
+ Connection con = d.connect(url, copy);
+ if (con == null)
+ throw new JBossResourceException("Wrong driver class for this connection URL");
+
+ return new LocalManagedConnection(this, con, props, transactionIsolation, preparedStatementCacheSize);
+ }
+ catch (Exception e)
+ {
+ throw new JBossResourceException("Could not create connection", e);
+ }
+ }
+
+ private LocalManagedConnection getHALocalManagedConnection(Properties props,Properties copy)
+ throws JBossResourceException
+ {
+ boolean trace = log.isTraceEnabled();
+
+ // try to get a connection as many times as many urls we have in the list
+ for(int i = 0; i < urlSelector.getCustomSortedUrls().size(); ++i)
{
- String url = getConnectionURL();
- Driver d = getDriver(url);
- Connection con = d.connect(url, copy);
- if (con == null)
- throw new JBossResourceException("Wrong driver class for this connection URL");
+ String url = (String)urlSelector.getUrlObject();
+ if(trace)
+ {
+ log.trace("Trying to create a connection to " + url);
+ }
+ try
+ {
+ Driver d = getDriver(url);
+ Connection con = d.connect(url, copy);
+ if(con == null)
+ {
+ log.warn("Wrong driver class for this connection URL: " + url);
+ urlSelector.failedUrlObject(url);
+ }
+ else
+ {
+ return new LocalManagedConnection(this, con, props, transactionIsolation, preparedStatementCacheSize);
+ }
+ }
+ catch(Exception e)
+ {
+ log.warn("Failed to create connection for " + url + ": " + e.getMessage());
+ urlSelector.failedUrlObject(url);
+ }
+ }
- return new LocalManagedConnection(this, con, props, transactionIsolation, preparedStatementCacheSize);
+ // we have supposedly tried all the urls
+ throw new JBossResourceException(
+ "Could not create connection using any of the URLs: " + urlSelector.getAllUrlObjects());
+ }
+
+ public void setURLDelimiter(String urlDelimiter)
+ throws ResourceException
+ {
+ super.urlDelimiter = urlDelimiter;
+ if(getConnectionURL() != null)
+ {
+ initUrlSelector();
}
- catch (Exception e)
+ }
+
+ protected void initUrlSelector()
+ throws ResourceException
+ {
+ boolean trace = log.isTraceEnabled();
+
+ List urlsList = new ArrayList();
+ String urlsStr = getConnectionURL();
+ String url;
+ int urlStart = 0;
+ int urlEnd = urlsStr.indexOf(urlDelimiter);
+ while(urlEnd > 0)
{
- throw new JBossResourceException("Could not create connection", e);
+ url = urlsStr.substring(urlStart, urlEnd);
+ urlsList.add(url);
+ urlStart = ++urlEnd;
+ urlEnd = urlsStr.indexOf(urlDelimiter, urlEnd);
+ if (trace)
+ log.trace("added HA connection url: " + url);
}
+
+ if(urlStart != urlsStr.length())
+ {
+ url = urlsStr.substring(urlStart, urlsStr.length());
+ urlsList.add(url);
+ if (trace)
+ log.trace("added HA connection url: " + url);
+ }
+ if(getUrlSelectorStrategyClassName()==null)
+ {
+ this.urlSelector = new URLSelector(urlsList);
+ log.debug("Default URLSelectorStrategy is being used : "+urlSelector);
+ }
+ else
+ {
+ this.urlSelector = (URLSelectorStrategy)loadClass(getUrlSelectorStrategyClassName(),urlsList);
+ log.debug("Customized URLSelectorStrategy is being used : "+urlSelector);
+ }
}
+
+ // Default Implementaion of the URLSelectorStrategy
+ public static class URLSelector implements URLSelectorStrategy
+ {
+ private final List urls;
+ private int urlIndex;
+ private String url;
+ public URLSelector(List urls)
+ {
+ if(urls == null || urls.size() == 0)
+ {
+ throw new IllegalStateException("Expected non-empty list of connection URLs but got: " + urls);
+ }
+ this.urls = Collections.unmodifiableList(urls);
+ }
+
+ public synchronized String getUrl()
+ {
+ if(url == null)
+ {
+ if(urlIndex == urls.size())
+ {
+ urlIndex = 0;
+ }
+ url = (String)urls.get(urlIndex++);
+ }
+ return url;
+ }
+
+ public synchronized void failedUrl(String url)
+ {
+ if(url.equals(this.url))
+ {
+ this.url = null;
+ }
+ }
+
+ /* URLSelectorStrategy Implementation goes here*/
+ public List getCustomSortedUrls()
+ {
+ return urls;
+ }
+ public void failedUrlObject(Object urlObject)
+ {
+ failedUrl((String)urlObject);
+ }
+ public List getAllUrlObjects()
+ {
+ return urls;
+ }
+ public Object getUrlObject()
+ {
+ return getUrl();
+ }
+
+ }
+
public ManagedConnection matchManagedConnections(final Set mcs, final Subject subject,
final ConnectionRequestInfo cri) throws ResourceException
{
More information about the jboss-cvs-commits
mailing list