You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by sk...@apache.org on 2008/02/16 15:48:30 UTC
svn commit: r628314 - in
/myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/connectionManager:
ConnectionManagerDataSource.java DisconnectableConnection.java
DisconnectableConnectionFactory.java
Author: skitching
Date: Sat Feb 16 06:48:29 2008
New Revision: 628314
URL: http://svn.apache.org/viewvc?rev=628314&view=rev
Log:
Convert tab to space only
Modified:
myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/connectionManager/ConnectionManagerDataSource.java
myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/connectionManager/DisconnectableConnection.java
myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/connectionManager/DisconnectableConnectionFactory.java
Modified: myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/connectionManager/ConnectionManagerDataSource.java
URL: http://svn.apache.org/viewvc/myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/connectionManager/ConnectionManagerDataSource.java?rev=628314&r1=628313&r2=628314&view=diff
==============================================================================
--- myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/connectionManager/ConnectionManagerDataSource.java (original)
+++ myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/connectionManager/ConnectionManagerDataSource.java Sat Feb 16 06:48:29 2008
@@ -56,215 +56,215 @@
*/
public class ConnectionManagerDataSource implements DataSource
{
- private DataSource dataSource;
- private String jndiName;
+ private DataSource dataSource;
+ private String jndiName;
- // List of connections that have been borrowed by this thread but not returned.
- // When using a threadpool, it is required that the releaseAllBorrowedConnections
- // method be called before the thread is returned to the pool; that ensures this
- // threadlocal is reset to null.
- private static ThreadLocal borrowedConnections = new ThreadLocal()
- {
- protected Object initialValue()
- {
- return new HashSet();
- }
- };
-
- public ConnectionManagerDataSource()
- {
- }
-
- void onAfterBorrowConnection(Connection con)
- {
- ((Set) borrowedConnections.get()).add(con);
- }
-
- void onAfterReleaseConnection(Connection con)
- {
- ((Set) borrowedConnections.get()).remove(con);
- }
-
- /**
- * If the calling thread has allocated connections via this datasource, then return the
- * underlying real connections to the underlying datasource.
- * <p>
- * To code that holds references to the proxy connection returned by this datasource,
- * this operation is generally transparent. They continue to hold a reference to the
- * proxy, and if a method is ever called on that proxy in the future then the proxy
- * will transparently allocate a new underlying Connection at that time.
- * <p>
- * This is expected to be called just before a thread is returned to a threadpool,
- * eg via a ServletFilter just before returning from processing a request.
- */
- public static void releaseAllBorrowedConnections()
- {
- DisconnectableConnection[] connections = new DisconnectableConnection[((Set) borrowedConnections.get()).size()];
- ((Set) borrowedConnections.get()).toArray(connections);
-
- for (int i = 0; i<connections.length; i++)
- {
- DisconnectableConnection connection = connections[i];
- connection.disconnect();
- }
-
- ((Set) borrowedConnections.get()).clear();
- }
-
- /**
- * Set the underlying datasource via an explicit call.
- * See also method setJndiName.
- */
- public void setDataSource(DataSource dataSource)
- {
- this.dataSource = dataSource;
- }
-
- /**
- * Return the underlying datasource for this wrapper.
- * <p>
- * If method setJndiName was used to specify the datasource, then it is retrieved
- * from JNDI if necessary.
- *
- * @throw IllegalArgumentException if neither setDataSource nor setJndiName was called.
- * @throw IllegalArgumentException if an invalid jndi name was specified.
- */
- public DataSource getDataSource()
- {
- if (dataSource != null)
- {
- return dataSource;
- }
-
- try
- {
- Context ctx = new InitialContext();
- dataSource = (DataSource) ctx.lookup(jndiName);
- }
- catch (NamingException e)
- {
- throw (IllegalArgumentException) new IllegalArgumentException(jndiName).initCause(e);
- }
-
- return dataSource;
- }
-
- /**
- * Specify that the underlying datasource should be retrieved via JNDI.
- */
- public void setJndiName(String jndiName)
- {
- this.jndiName = jndiName;
- }
-
- /**
- * Return a proxy that wraps a connection of the underlying datasource.
- */
- public Connection getConnection() throws SQLException
- {
- return DisconnectableConnectionFactory.create(this);
- }
-
- /**
- * Not supported. Always throws UnsupportedOperationException.
- */
- public Connection getConnection(String username, String password) throws SQLException
- {
- throw new UnsupportedOperationException();
- }
-
- /** @inherited */
- public PrintWriter getLogWriter() throws SQLException
- {
- return getDataSource().getLogWriter();
- }
-
- /** @inherited */
- public void setLogWriter(PrintWriter out) throws SQLException
- {
- getDataSource().setLogWriter(out);
- }
-
- /** @inherited */
- public void setLoginTimeout(int seconds) throws SQLException
- {
- getDataSource().setLoginTimeout(seconds);
- }
-
- /** @inherited */
- public int getLoginTimeout() throws SQLException
- {
- return getDataSource().getLoginTimeout();
- }
-
- /**
- * Always throws UnsupportedOperationException.
- * <p>
- * Note that this method was only introduced in java 1.6, and therefore
- * cannot be implemented on platforms earlier than this without using
- * reflection. Orchestra supports pre-1.6 JVMs, and this is a very
- * rarely used method so currently no support is offered for this
- * method.
- */
- public Object unwrap(Class iface) throws SQLException
- {
- throw new UnsupportedOperationException();
- /*
- try
- {
- if (iface.isAssignableFrom(dataSource.getClass()))
- {
- return dataSource;
- }
-
- Method method = dataSource.getClass().getMethod("unwrap", new Class[]{Class.class});
- return method.invoke(dataSource, new Object[] { iface });
- }
- catch (NoSuchMethodException e)
- {
- throw new UnsupportedOperationException();
- }
- catch (IllegalAccessException e)
- {
- throw new SQLException(e);
- }
- catch (InvocationTargetException e)
- {
- throw new SQLException(e);
- }
- */
- }
-
- /**
- * Always throws UnsupportedOperationException.
- * See method unwrap.
- */
- public boolean isWrapperFor(Class iface) throws SQLException
- {
- throw new UnsupportedOperationException();
-
- /*
- try
- {
- if (iface.isAssignableFrom(dataSource.getClass()))
- {
- return true;
- }
- Method method = dataSource.getClass().getMethod("isWrapperFor", new Class[]{Class.class});
- return Boolean.TRUE.equals(method.invoke(dataSource, new Object[] { iface }));
- }
- catch (NoSuchMethodException e)
- {
- throw new UnsupportedOperationException();
- }
- catch (IllegalAccessException e)
- {
- throw new SQLException(e);
- }
- catch (InvocationTargetException e)
- {
- throw new SQLException(e);
- }
- */
- }
+ // List of connections that have been borrowed by this thread but not returned.
+ // When using a threadpool, it is required that the releaseAllBorrowedConnections
+ // method be called before the thread is returned to the pool; that ensures this
+ // threadlocal is reset to null.
+ private static ThreadLocal borrowedConnections = new ThreadLocal()
+ {
+ protected Object initialValue()
+ {
+ return new HashSet();
+ }
+ };
+
+ public ConnectionManagerDataSource()
+ {
+ }
+
+ void onAfterBorrowConnection(Connection con)
+ {
+ ((Set) borrowedConnections.get()).add(con);
+ }
+
+ void onAfterReleaseConnection(Connection con)
+ {
+ ((Set) borrowedConnections.get()).remove(con);
+ }
+
+ /**
+ * If the calling thread has allocated connections via this datasource, then return the
+ * underlying real connections to the underlying datasource.
+ * <p>
+ * To code that holds references to the proxy connection returned by this datasource,
+ * this operation is generally transparent. They continue to hold a reference to the
+ * proxy, and if a method is ever called on that proxy in the future then the proxy
+ * will transparently allocate a new underlying Connection at that time.
+ * <p>
+ * This is expected to be called just before a thread is returned to a threadpool,
+ * eg via a ServletFilter just before returning from processing a request.
+ */
+ public static void releaseAllBorrowedConnections()
+ {
+ DisconnectableConnection[] connections = new DisconnectableConnection[((Set) borrowedConnections.get()).size()];
+ ((Set) borrowedConnections.get()).toArray(connections);
+
+ for (int i = 0; i<connections.length; i++)
+ {
+ DisconnectableConnection connection = connections[i];
+ connection.disconnect();
+ }
+
+ ((Set) borrowedConnections.get()).clear();
+ }
+
+ /**
+ * Set the underlying datasource via an explicit call.
+ * See also method setJndiName.
+ */
+ public void setDataSource(DataSource dataSource)
+ {
+ this.dataSource = dataSource;
+ }
+
+ /**
+ * Return the underlying datasource for this wrapper.
+ * <p>
+ * If method setJndiName was used to specify the datasource, then it is retrieved
+ * from JNDI if necessary.
+ *
+ * @throw IllegalArgumentException if neither setDataSource nor setJndiName was called.
+ * @throw IllegalArgumentException if an invalid jndi name was specified.
+ */
+ public DataSource getDataSource()
+ {
+ if (dataSource != null)
+ {
+ return dataSource;
+ }
+
+ try
+ {
+ Context ctx = new InitialContext();
+ dataSource = (DataSource) ctx.lookup(jndiName);
+ }
+ catch (NamingException e)
+ {
+ throw (IllegalArgumentException) new IllegalArgumentException(jndiName).initCause(e);
+ }
+
+ return dataSource;
+ }
+
+ /**
+ * Specify that the underlying datasource should be retrieved via JNDI.
+ */
+ public void setJndiName(String jndiName)
+ {
+ this.jndiName = jndiName;
+ }
+
+ /**
+ * Return a proxy that wraps a connection of the underlying datasource.
+ */
+ public Connection getConnection() throws SQLException
+ {
+ return DisconnectableConnectionFactory.create(this);
+ }
+
+ /**
+ * Not supported. Always throws UnsupportedOperationException.
+ */
+ public Connection getConnection(String username, String password) throws SQLException
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ /** @inherited */
+ public PrintWriter getLogWriter() throws SQLException
+ {
+ return getDataSource().getLogWriter();
+ }
+
+ /** @inherited */
+ public void setLogWriter(PrintWriter out) throws SQLException
+ {
+ getDataSource().setLogWriter(out);
+ }
+
+ /** @inherited */
+ public void setLoginTimeout(int seconds) throws SQLException
+ {
+ getDataSource().setLoginTimeout(seconds);
+ }
+
+ /** @inherited */
+ public int getLoginTimeout() throws SQLException
+ {
+ return getDataSource().getLoginTimeout();
+ }
+
+ /**
+ * Always throws UnsupportedOperationException.
+ * <p>
+ * Note that this method was only introduced in java 1.6, and therefore
+ * cannot be implemented on platforms earlier than this without using
+ * reflection. Orchestra supports pre-1.6 JVMs, and this is a very
+ * rarely used method so currently no support is offered for this
+ * method.
+ */
+ public Object unwrap(Class iface) throws SQLException
+ {
+ throw new UnsupportedOperationException();
+ /*
+ try
+ {
+ if (iface.isAssignableFrom(dataSource.getClass()))
+ {
+ return dataSource;
+ }
+
+ Method method = dataSource.getClass().getMethod("unwrap", new Class[]{Class.class});
+ return method.invoke(dataSource, new Object[] { iface });
+ }
+ catch (NoSuchMethodException e)
+ {
+ throw new UnsupportedOperationException();
+ }
+ catch (IllegalAccessException e)
+ {
+ throw new SQLException(e);
+ }
+ catch (InvocationTargetException e)
+ {
+ throw new SQLException(e);
+ }
+ */
+ }
+
+ /**
+ * Always throws UnsupportedOperationException.
+ * See method unwrap.
+ */
+ public boolean isWrapperFor(Class iface) throws SQLException
+ {
+ throw new UnsupportedOperationException();
+
+ /*
+ try
+ {
+ if (iface.isAssignableFrom(dataSource.getClass()))
+ {
+ return true;
+ }
+ Method method = dataSource.getClass().getMethod("isWrapperFor", new Class[]{Class.class});
+ return Boolean.TRUE.equals(method.invoke(dataSource, new Object[] { iface }));
+ }
+ catch (NoSuchMethodException e)
+ {
+ throw new UnsupportedOperationException();
+ }
+ catch (IllegalAccessException e)
+ {
+ throw new SQLException(e);
+ }
+ catch (InvocationTargetException e)
+ {
+ throw new SQLException(e);
+ }
+ */
+ }
}
Modified: myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/connectionManager/DisconnectableConnection.java
URL: http://svn.apache.org/viewvc/myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/connectionManager/DisconnectableConnection.java?rev=628314&r1=628313&r2=628314&view=diff
==============================================================================
--- myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/connectionManager/DisconnectableConnection.java (original)
+++ myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/connectionManager/DisconnectableConnection.java Sat Feb 16 06:48:29 2008
@@ -27,19 +27,19 @@
*/
public interface DisconnectableConnection extends Connection
{
- /**
- * Close the underlying connection object.
- * <p>
- * Note that if methods are called on this object that require a connection, then a
- * fresh connection will transparently be allocated and cached.
- */
- public void disconnect();
-
- /**
- * Get the real underlying Connection object.
- * <p>
- * If this object is not currently connected, then null will be returned. Note that
- * a call to any proxied method that needs a connection will then allocate one.
- */
- public Connection getConnection();
+ /**
+ * Close the underlying connection object.
+ * <p>
+ * Note that if methods are called on this object that require a connection, then a
+ * fresh connection will transparently be allocated and cached.
+ */
+ public void disconnect();
+
+ /**
+ * Get the real underlying Connection object.
+ * <p>
+ * If this object is not currently connected, then null will be returned. Note that
+ * a call to any proxied method that needs a connection will then allocate one.
+ */
+ public Connection getConnection();
}
Modified: myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/connectionManager/DisconnectableConnectionFactory.java
URL: http://svn.apache.org/viewvc/myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/connectionManager/DisconnectableConnectionFactory.java?rev=628314&r1=628313&r2=628314&view=diff
==============================================================================
--- myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/connectionManager/DisconnectableConnectionFactory.java (original)
+++ myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/connectionManager/DisconnectableConnectionFactory.java Sat Feb 16 06:48:29 2008
@@ -36,106 +36,106 @@
*/
public class DisconnectableConnectionFactory
{
- private DisconnectableConnectionFactory()
- {
- }
-
- public static DisconnectableConnection create(final ConnectionManagerDataSource connectionManager)
- {
- return (DisconnectableConnection) Proxy.newProxyInstance(
-
- DisconnectableConnection.class.getClassLoader(),
-
- new Class[]
- {
- DisconnectableConnection.class
- },
-
- new InvocationHandler()
- {
- private Map connectionConfiguration = new HashMap();
- private Connection connection;
-
- public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
- {
- if ("equals".equals(method.getName()) || "hashCode".equals(method.getName())) // NON-NLS
- {
- // do not pass these methods to the connection as we dont want to change the
- // identity of the connection proxy
- return method.invoke(this, args);
- }
- else if ("close".equals(method.getName())) // NON-NLS
- {
- try
- {
- if (connection != null)
- {
- connection.close();
- }
- connection = null;
- }
- finally
- {
- connectionManager.onAfterReleaseConnection((Connection) proxy);
- }
- return null;
- }
- else if ("disconnect".equals(method.getName())) // NON-NLS
- {
- try
- {
- if (connection != null)
- {
- try
- {
- if (!connection.isClosed())
- {
- connection.close();
- }
- }
- catch (SQLException e)
- {
- LogFactory.getLog(DisconnectableConnectionFactory.class).warn(e.getLocalizedMessage(), e);
- }
- }
- }
- finally
- {
- connectionManager.onAfterReleaseConnection((Connection) proxy);
- connection = null;
- }
- return null;
- }
- else if ("getConnection".equals(method.getName())) // NON-NLS
- {
- return connection;
- }
-
- if (connection == null)
- {
- connection = connectionManager.getDataSource().getConnection();
-
- // if we have a configuration, we have to replay it now
- Iterator iterConfiguration = connectionConfiguration.entrySet().iterator();
- while (iterConfiguration.hasNext())
- {
- Map.Entry config = (Map.Entry) iterConfiguration.next();
- ((Method) config.getKey()).invoke(connection, (Object[]) config.getValue());
- }
-
- connectionManager.onAfterBorrowConnection((Connection) proxy);
- }
-
- Object ret = method.invoke(connection, args);
-
- // capture the connection configuration
- if (method.getName().startsWith("set")) // NON-NLS
- {
- connectionConfiguration.put(method, args);
- }
-
- return ret;
- }
- });
- }
+ private DisconnectableConnectionFactory()
+ {
+ }
+
+ public static DisconnectableConnection create(final ConnectionManagerDataSource connectionManager)
+ {
+ return (DisconnectableConnection) Proxy.newProxyInstance(
+
+ DisconnectableConnection.class.getClassLoader(),
+
+ new Class[]
+ {
+ DisconnectableConnection.class
+ },
+
+ new InvocationHandler()
+ {
+ private Map connectionConfiguration = new HashMap();
+ private Connection connection;
+
+ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
+ {
+ if ("equals".equals(method.getName()) || "hashCode".equals(method.getName())) // NON-NLS
+ {
+ // do not pass these methods to the connection as we dont want to change the
+ // identity of the connection proxy
+ return method.invoke(this, args);
+ }
+ else if ("close".equals(method.getName())) // NON-NLS
+ {
+ try
+ {
+ if (connection != null)
+ {
+ connection.close();
+ }
+ connection = null;
+ }
+ finally
+ {
+ connectionManager.onAfterReleaseConnection((Connection) proxy);
+ }
+ return null;
+ }
+ else if ("disconnect".equals(method.getName())) // NON-NLS
+ {
+ try
+ {
+ if (connection != null)
+ {
+ try
+ {
+ if (!connection.isClosed())
+ {
+ connection.close();
+ }
+ }
+ catch (SQLException e)
+ {
+ LogFactory.getLog(DisconnectableConnectionFactory.class).warn(e.getLocalizedMessage(), e);
+ }
+ }
+ }
+ finally
+ {
+ connectionManager.onAfterReleaseConnection((Connection) proxy);
+ connection = null;
+ }
+ return null;
+ }
+ else if ("getConnection".equals(method.getName())) // NON-NLS
+ {
+ return connection;
+ }
+
+ if (connection == null)
+ {
+ connection = connectionManager.getDataSource().getConnection();
+
+ // if we have a configuration, we have to replay it now
+ Iterator iterConfiguration = connectionConfiguration.entrySet().iterator();
+ while (iterConfiguration.hasNext())
+ {
+ Map.Entry config = (Map.Entry) iterConfiguration.next();
+ ((Method) config.getKey()).invoke(connection, (Object[]) config.getValue());
+ }
+
+ connectionManager.onAfterBorrowConnection((Connection) proxy);
+ }
+
+ Object ret = method.invoke(connection, args);
+
+ // capture the connection configuration
+ if (method.getName().startsWith("set")) // NON-NLS
+ {
+ connectionConfiguration.put(method, args);
+ }
+
+ return ret;
+ }
+ });
+ }
}