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

pag at mulgara.org pag at mulgara.org
Mon Oct 22 21:21:40 UTC 2007


Author: pag
Date: 2007-10-22 16:21:39 -0500 (Mon, 22 Oct 2007)
New Revision: 490

Modified:
   branches/nw-interface/src/jar/query/java/org/mulgara/connection/ConnectionFactory.java
   branches/nw-interface/src/jar/query/java/org/mulgara/connection/SessionConnection.java
Log:
Fixed errors in handling local sessions, particularly when the session was provided by the user

Modified: branches/nw-interface/src/jar/query/java/org/mulgara/connection/ConnectionFactory.java
===================================================================
--- branches/nw-interface/src/jar/query/java/org/mulgara/connection/ConnectionFactory.java	2007-10-19 20:26:26 UTC (rev 489)
+++ branches/nw-interface/src/jar/query/java/org/mulgara/connection/ConnectionFactory.java	2007-10-22 21:21:39 UTC (rev 490)
@@ -11,8 +11,13 @@
  */
 package org.mulgara.connection;
 
+import java.net.InetAddress;
 import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.UnknownHostException;
 import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
 import java.util.Map;
 
 import org.apache.log4j.Logger;
@@ -34,10 +39,40 @@
   /** The logger. */
   private final static Logger logger = Logger.getLogger(ConnectionFactory.class.getName());
 
+  /** String constant for localhost */
+  private final static String LOCALHOST_NAME = "localhost";
+  
+  /** IP constant for localhost, saved as a string */
+  private final static String LOCALHOST_IP = "127.0.0.1";
+  
+  /** The scheme name for the local protocol */
+  private final static String LOCAL_PROTOCOL = "local";
+  
+  /** The list of known protocols. */
+  private final static String[] PROTOCOLS = { "rmi", "beep", LOCAL_PROTOCOL };
+  
+  /** The list of known local host aliases. */
+  private final static List<String> LOCALHOSTS = new LinkedList<String>();
+  
+  /** Initialize the list of local host aliases. */
+  static {
+    LOCALHOSTS.add(LOCALHOST_NAME);
+    LOCALHOSTS.add(LOCALHOST_IP);
+    try {
+      LOCALHOSTS.add(InetAddress.getLocalHost().getHostAddress());
+      LOCALHOSTS.add(InetAddress.getLocalHost().getHostName());
+    } catch (UnknownHostException e) {
+      logger.error("Unable to get local host address", e);
+    }
+  }
+
   /** Cache of Connections, based on their server URI. */
   private Map<URI,SessionConnection> cacheOnUri;
   /** Cache of Connections, based on their session data. */
   private Map<Session,SessionConnection> cacheOnSession;
+  
+  /** A local connection.  This is only used if a local session is provided. */
+  private SessionConnection localConnection = null;
 
   /**
    * Default constructor.
@@ -56,7 +91,12 @@
   public Connection newConnection(URI serverUri) throws ConnectionException {
     SessionConnection c = cacheOnUri.get(serverUri);
     if (c == null) {
-      c = new SessionConnection(serverUri);
+      if (isLocalServer(serverUri)) {
+        c = (localConnection != null) ? localConnection : new SessionConnection(serverUri, false);
+        addLocalConnection(serverUri, c);
+      } else {
+        c = new SessionConnection(serverUri);
+      }
       cacheOnUri.put(serverUri, c);
       cacheOnSession.put(c.getSession(), c);
     }
@@ -75,7 +115,12 @@
     if (c == null) {
       c = new SessionConnection(session);
       cacheOnSession.put(session, c);
-      cacheOnUri.put(c.getServerUri(), c);
+      URI serverURI = c.getServerUri();
+      if (serverURI != null) {
+        cacheOnUri.put(serverURI, c);
+        if (session.isLocal()) addLocalConnection(serverURI, c);
+      }
+      if (session.isLocal()) localConnection = c;
     }
     return c;
   }
@@ -103,4 +148,52 @@
       }
     }
   }
+  
+
+  /**
+   * Test if a given URI is a local URI.
+   * @param serverUri The URI to test.
+   * @return <code>true</code> if the URI is local.
+   */
+  static boolean isLocalServer(URI serverUri) {
+    String scheme = serverUri.getScheme();
+    if (LOCAL_PROTOCOL.equals(scheme)) return true;
+    
+    // check for known protocols
+    boolean found = false;
+    for (String protocol: PROTOCOLS) {
+      if (protocol.equals(serverUri.getScheme())) {
+        found = true;
+        break;
+      }
+    }
+    if (found == false) return false;
+
+    // protocol found.  Now test if the host appears in the localhost list
+    String host = serverUri.getHost();
+    for (String h: LOCALHOSTS) if (h.equalsIgnoreCase(host)) return true;
+
+    // no matching hostnames
+    return false;
+  }
+
+
+  /**
+   * Maps all the possible localhost aliases onto the requested connection.
+   * @param serverUri The basic form of the localhost URI.
+   * @param connection The connection to associate with the local host.
+   */
+  private void addLocalConnection(URI serverUri, SessionConnection connection) {
+    String path = serverUri.getRawPath();
+    for (String protocol: PROTOCOLS) {
+      for (String alias: LOCALHOSTS) {
+        try {
+          URI uri = new URI(protocol, alias, path);
+          cacheOnUri.put(uri, connection);
+        } catch (URISyntaxException e) {
+          logger.error("Unable to create a localhost alias URI.");
+        }
+      }
+    }
+  }
 }

Modified: branches/nw-interface/src/jar/query/java/org/mulgara/connection/SessionConnection.java
===================================================================
--- branches/nw-interface/src/jar/query/java/org/mulgara/connection/SessionConnection.java	2007-10-19 20:26:26 UTC (rev 489)
+++ branches/nw-interface/src/jar/query/java/org/mulgara/connection/SessionConnection.java	2007-10-22 21:21:39 UTC (rev 490)
@@ -52,11 +52,23 @@
    * @throws ConnectionException There was a problem establishing the details needed for a connection.
    */
   public SessionConnection(URI serverUri) throws ConnectionException {
-    setServerUri(serverUri);
+    this(serverUri, true);
   }
 
 
   /**
+   * Creates a new connection, given a URI to a server,
+   * and a flag to indicate if the server should be "remote".
+   * @param serverUri The URI to connect to.
+   * @param isRemote <code>true</code> for a remote session, <code>false</code> for local.
+   * @throws ConnectionException There was a problem establishing the details needed for a connection.
+   */
+  SessionConnection(URI serverUri, boolean isRemote) throws ConnectionException {
+    setServerUri(serverUri, isRemote);
+  }
+
+
+  /**
    * Creates a new connection, given a preassigned session.
    * @param session The session to connect with.
    * @throws ConnectionException There was a problem establishing the details needed for a connection.
@@ -171,9 +183,10 @@
   /**
    * Establishes a session for this connection.
    * @param uri The URI to set for the server.
+   * @param isRemote <code>true</code> for a remote session, <code>false</code> for local.
    * @throws ConnectionException There was a problem establishing a session.
    */
-  private void setServerUri(URI uri) throws ConnectionException {
+  private void setServerUri(URI uri, boolean isRemote) throws ConnectionException {
     
     try {
       if (uri == null) {
@@ -185,7 +198,8 @@
       if (logger.isDebugEnabled()) logger.debug("Set server URI to: " + serverUri);
 
       if (logger.isDebugEnabled()) logger.debug("Finding session factory for " + uri);
-      SessionFactory sessionFactory = SessionFactoryFinder.newSessionFactory(serverUri, true);
+      
+      SessionFactory sessionFactory = SessionFactoryFinder.newSessionFactory(serverUri, isRemote);
       if (logger.isDebugEnabled()) logger.debug("Found " + sessionFactory.getClass() +
           " session factory, obtaining session with " + uri);
 




More information about the Mulgara-svn mailing list