[Mulgara-svn] r374 - branches/nw-interface/src/jar/connection/java/org/mulgara/connection

pag at mulgara.org pag at mulgara.org
Thu Aug 23 17:23:56 UTC 2007


Author: pag
Date: 2007-08-23 12:23:56 -0500 (Thu, 23 Aug 2007)
New Revision: 374

Modified:
   branches/nw-interface/src/jar/connection/java/org/mulgara/connection/Connection.java
   branches/nw-interface/src/jar/connection/java/org/mulgara/connection/ConnectionFactory.java
Log:
Added factory methods for creating and re-using connections

Modified: branches/nw-interface/src/jar/connection/java/org/mulgara/connection/Connection.java
===================================================================
--- branches/nw-interface/src/jar/connection/java/org/mulgara/connection/Connection.java	2007-08-23 17:23:07 UTC (rev 373)
+++ branches/nw-interface/src/jar/connection/java/org/mulgara/connection/Connection.java	2007-08-23 17:23:56 UTC (rev 374)
@@ -101,7 +101,33 @@
   }
 
 
+  // Private methods //
+
   /**
+   * @return the serverUri
+   */
+  URI getServerUri() {
+    return serverUri;
+  }
+
+
+  /**
+   * @return the securityDomainUri
+   */
+  URI getSecurityDomainUri() {
+    return securityDomainUri;
+  }
+
+
+  /**
+   * @return the session
+   */
+  Session getSession() {
+    return session;
+  }
+
+
+  /**
    * Sets the session information for this connection
    * @param session The session to set to.
    * @param securityDomainURI The security domain to use for the session.
@@ -134,6 +160,7 @@
           " session factory, obtaining session with " + uri);
 
       // create a new session and set this connection to it
+      if (securityDomainUri == null) securityDomainUri = sessionFactory.getSecurityDomain();
       setSession(sessionFactory.newSession(), sessionFactory.getSecurityDomain());
 
     } catch (SessionFactoryFinderException e) {

Modified: branches/nw-interface/src/jar/connection/java/org/mulgara/connection/ConnectionFactory.java
===================================================================
--- branches/nw-interface/src/jar/connection/java/org/mulgara/connection/ConnectionFactory.java	2007-08-23 17:23:07 UTC (rev 373)
+++ branches/nw-interface/src/jar/connection/java/org/mulgara/connection/ConnectionFactory.java	2007-08-23 17:23:56 UTC (rev 374)
@@ -11,8 +11,16 @@
  */
 package org.mulgara.connection;
 
+import java.net.URI;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.mulgara.server.Session;
+
+
 /**
  * Creates new connections or reloads from a cache when possible connections.
+ * This must NOT be shared between users, as it is designed to cache security credentials!
  *
  * @created 2007-08-21
  * @author Paul Gearon
@@ -21,5 +29,42 @@
  */
 public class ConnectionFactory {
 
-  
+  /** Cache of Connections, based on their server URI. */
+  static private Map<URI,Connection> cacheOnUri = new HashMap<URI,Connection>();
+  /** Cache of Connections, based on their session data. */
+  static private Map<Session,Connection> cacheOnSession = new HashMap<Session,Connection>();
+
+  /**
+   * Retrieve a connection based on a server URI.
+   * @param serverUri The URI to get the connection to.
+   * @return The new Connection.
+   * @throws ConnectionException There was an error getting a connection.
+   */
+  static Connection newConnection(URI serverUri) throws ConnectionException {
+    Connection c = cacheOnUri.get(serverUri);
+    if (c == null) {
+      c = new Connection(serverUri);
+      cacheOnUri.put(serverUri, c);
+      cacheOnSession.put(c.getSession(), c);
+    }
+    return c;
+  }
+
+
+  /**
+   * Retrieve a connection for a given session.
+   * @param session The Session the Connection will use..
+   * @return The new Connection.
+   * @throws ConnectionException There was an error getting a connection.
+   */
+  static Connection newConnection(Session session) throws ConnectionException {
+    Connection c = cacheOnSession.get(session);
+    if (c == null) {
+      c = new Connection(session);
+      cacheOnSession.put(session, c);
+      cacheOnUri.put(c.getServerUri(), c);
+    }
+    return c;
+  }
+
 }




More information about the Mulgara-svn mailing list