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

pag at mulgara.org pag at mulgara.org
Wed Sep 26 04:48:34 UTC 2007


Author: pag
Date: 2007-09-25 23:48:34 -0500 (Tue, 25 Sep 2007)
New Revision: 453

Added:
   branches/nw-interface/src/jar/query/java/org/mulgara/connection/DummyConnection.java
   branches/nw-interface/src/jar/query/java/org/mulgara/connection/SessionConnection.java
Removed:
   branches/nw-interface/src/jar/query/java/org/mulgara/connection/Connection.java
Modified:
   branches/nw-interface/src/jar/query/java/org/mulgara/connection/ConnectionFactory.java
Log:
Shifted connections around so a dummy connection can be used for managing the autocommit state when no connection has been established.

Deleted: branches/nw-interface/src/jar/query/java/org/mulgara/connection/Connection.java
===================================================================
--- branches/nw-interface/src/jar/query/java/org/mulgara/connection/Connection.java	2007-09-26 03:37:33 UTC (rev 452)
+++ branches/nw-interface/src/jar/query/java/org/mulgara/connection/Connection.java	2007-09-26 04:48:34 UTC (rev 453)
@@ -1,206 +0,0 @@
-/*
- * The contents of this file are subject to the Open Software License
- * Version 3.0 (the "License"); you may not use this file except in
- * compliance with the License. You may obtain a copy of the License at
- * http://www.opensource.org/licenses/osl-3.0.txt
- *
- * Software distributed under the License is distributed on an "AS IS"
- * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
- * the License for the specific language governing rights and limitations
- * under the License.
- */
-package org.mulgara.connection;
-
-import java.net.URI;
-
-import org.apache.log4j.Logger;
-import org.mulgara.query.QueryException;
-import org.mulgara.server.NonRemoteSessionException;
-import org.mulgara.server.Session;
-import org.mulgara.server.SessionFactory;
-import org.mulgara.server.driver.SessionFactoryFinder;
-import org.mulgara.server.driver.SessionFactoryFinderException;
-
-/**
- * A connection for sending commands to a server.
- *
- * @created 2007-08-21
- * @author Paul Gearon
- * @copyright &copy; 2007 <a href="mailto:pgearon at users.sourceforge.net">Paul Gearon</a>
- * @licence <a href="{@docRoot}/../../LICENCE.txt">Open Software License v3.0</a>
- */
-public class Connection {
-
-  /** Logger. */
-  private static final Logger logger = Logger.getLogger(Connection.class.getName());
-  
-  /** The URI for the server to establish a session on. */
-  private URI serverUri;
-  
-  /** The security domain URI. */
-  private URI securityDomainUri;
-  
-  /** The session to use for this connection. */
-  private Session session;
-
-  /** Indicates the current autocommit state */
-  private boolean autoCommit = false;
-
-  /**
-   * Creates a new connection, given a URI to a server.
-   * @param serverUri The URI to connect to.
-   * @throws ConnectionException There was a problem establishing the details needed for a connection.
-   */
-  public Connection(URI serverUri) throws ConnectionException {
-    setServerUri(serverUri);
-  }
-
-
-  /**
-   * 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.
-   */
-  public Connection(Session session) {
-    this(session, null);
-  }
-  
-  
-  /**
-   * 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.
-   */
-  public Connection(Session session, URI securityDomainUri) {
-    if (session == null) throw new IllegalArgumentException("Cannot create a connection without a server.");
-    setSession(session, securityDomainUri);
-  }
-  
-  
-  /**
-   * Give login credentials and security domain to the current session.  This should only be needed
-   * once since the session does not change.
-   * @param securityDomainUri The security domain for the login.
-   * @param user The username.
-   * @param password The password for the given username.
-   */
-  public void setCredentials(URI securityDomainUri, String user, char[] password) {
-    if (securityDomainUri == null) throw new IllegalArgumentException("Must have a security domain to yuse credentials");
-    this.securityDomainUri = securityDomainUri;
-    setCredentials(user, password);
-  }
-
-
-  /**
-   * Give login credentials for the current security domain to the current session.
-   * This should only be needed
-   * once since the session does not change.
-   * @param user The username.
-   * @param password The password for the given username.
-   */
-  public void setCredentials(String user, char[] password) {
-    if (securityDomainUri == null) throw new IllegalArgumentException("Must have a security domain to yuse credentials");
-    session.login(securityDomainUri, user, password);
-  }
-
-
-  /**
-   * @return the session
-   */
-  public Session getSession() {
-    return session;
-  }
-
-
-  /**
-   * Starts and commits transactions on this connection, by turning the autocommit
-   * flag on and off. 
-   * @param autocommit <code>true</code> if the flag is to be on.
-   * @throws QueryException The session could not change state.
-   */
-  public void setAutoCommit(boolean autoCommit) throws QueryException {
-    this.autoCommit = autoCommit;
-    session.setAutoCommit(autoCommit);
-  }
-
-
-  /**
-   * @return the autoCommit value
-   */
-  public boolean getAutoCommit() {
-    return autoCommit;
-  }
-
-
-  /**
-   * Closes the current connection.
-   */
-  public void close() throws QueryException {
-    if (session != null) session.close();
-  }
-
-  // Private methods //
-
-  /**
-   * @return the serverUri
-   */
-  URI getServerUri() {
-    return serverUri;
-  }
-
-
-  /**
-   * @return the securityDomainUri
-   */
-  URI getSecurityDomainUri() {
-    return securityDomainUri;
-  }
-
-
-  /**
-   * Sets the session information for this connection
-   * @param session The session to set to.
-   * @param securityDomainURI The security domain to use for the session.
-   */
-  private void setSession(Session session, URI securityDomainUri) {
-    this.session = session;
-    this.securityDomainUri = securityDomainUri;
-  }
-
-
-  /**
-   * Establishes a session for this connection.
-   * @param uri The URI to set for the server.
-   * @throws ConnectionException There was a problem establishing a session.
-   */
-  private void setServerUri(URI uri) throws ConnectionException {
-    
-    try {
-      if (uri == null) {
-        // no model given, and the factory didn't cache a connection, so make one up.
-        serverUri = SessionFactoryFinder.findServerURI();
-      } else {
-        serverUri = uri;
-      }
-      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);
-      if (logger.isDebugEnabled()) logger.debug("Found " + sessionFactory.getClass() +
-          " 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) {
-      throw new ConnectionException("Unable to connect to a server", e);
-    } catch (NonRemoteSessionException e) {
-      throw new ConnectionException("Error connecting to the local server", e);
-    } catch (QueryException e) {
-      throw new ConnectionException("Data error in connection attempt", e);
-    }
-    assert session != null;
-  }
-  
-}

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-09-26 03:37:33 UTC (rev 452)
+++ branches/nw-interface/src/jar/query/java/org/mulgara/connection/ConnectionFactory.java	2007-09-26 04:48:34 UTC (rev 453)
@@ -35,16 +35,16 @@
   private final static Logger logger = Logger.getLogger(ConnectionFactory.class.getName());
 
   /** Cache of Connections, based on their server URI. */
-  private Map<URI,Connection> cacheOnUri;
+  private Map<URI,SessionConnection> cacheOnUri;
   /** Cache of Connections, based on their session data. */
-  private Map<Session,Connection> cacheOnSession;
+  private Map<Session,SessionConnection> cacheOnSession;
 
   /**
    * Default constructor.
    */
   public ConnectionFactory() {
-    cacheOnUri = new HashMap<URI,Connection>();
-    cacheOnSession = new HashMap<Session,Connection>();
+    cacheOnUri = new HashMap<URI,SessionConnection>();
+    cacheOnSession = new HashMap<Session,SessionConnection>();
   }
 
   /**
@@ -54,9 +54,9 @@
    * @throws ConnectionException There was an error getting a connection.
    */
   public Connection newConnection(URI serverUri) throws ConnectionException {
-    Connection c = cacheOnUri.get(serverUri);
+    SessionConnection c = cacheOnUri.get(serverUri);
     if (c == null) {
-      c = new Connection(serverUri);
+      c = new SessionConnection(serverUri);
       cacheOnUri.put(serverUri, c);
       cacheOnSession.put(c.getSession(), c);
     }
@@ -71,9 +71,9 @@
    * @throws ConnectionException There was an error getting a connection.
    */
   public Connection newConnection(Session session) throws ConnectionException {
-    Connection c = cacheOnSession.get(session);
+    SessionConnection c = cacheOnSession.get(session);
     if (c == null) {
-      c = new Connection(session);
+      c = new SessionConnection(session);
       cacheOnSession.put(session, c);
       cacheOnUri.put(c.getServerUri(), c);
     }
@@ -94,7 +94,7 @@
    * Closes all connections in a collection. Exceptions are logged, but not acted on.
    * @param connections The connections to close.
    */
-  private void safeCloseAll(Iterable<Connection> connections) {
+  private void safeCloseAll(Iterable<SessionConnection> connections) {
     for (Connection c: connections) {
       try {
         c.close();

Added: branches/nw-interface/src/jar/query/java/org/mulgara/connection/DummyConnection.java
===================================================================
--- branches/nw-interface/src/jar/query/java/org/mulgara/connection/DummyConnection.java	2007-09-26 03:37:33 UTC (rev 452)
+++ branches/nw-interface/src/jar/query/java/org/mulgara/connection/DummyConnection.java	2007-09-26 04:48:34 UTC (rev 453)
@@ -0,0 +1,98 @@
+/*
+ * The contents of this file are subject to the Open Software License
+ * Version 3.0 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ * http://www.opensource.org/licenses/osl-3.0.txt
+ *
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
+ * the License for the specific language governing rights and limitations
+ * under the License.
+ */
+package org.mulgara.connection;
+
+import java.net.URI;
+
+import org.apache.log4j.Logger;
+import org.mulgara.query.QueryException;
+import org.mulgara.server.Session;
+
+/**
+ * A connection for accepting state changes at the local end with no server involvement
+ *
+ * @created 2007-09-25
+ * @author Paul Gearon
+ * @copyright &copy; 2007 <a href="mailto:pgearon at users.sourceforge.net">Paul Gearon</a>
+ * @licence <a href="{@docRoot}/../../LICENCE.txt">Open Software License v3.0</a>
+ */
+public class DummyConnection implements Connection {
+
+  /** Logger. */
+  private static final Logger logger = Logger.getLogger(DummyConnection.class.getName());
+  
+  /** Indicates the current autocommit state */
+  private boolean autoCommit = true;
+
+  /**
+   * Creates a new connection.
+   */
+  public DummyConnection() {
+  }
+
+
+  /**
+   * Give login credentials and security domain to a session.  This operation is ignored.
+   * @param securityDomainUri The security domain for the login.
+   * @param user The username.
+   * @param password The password for the given username.
+   */
+  public void setCredentials(URI securityDomainUri, String user, char[] password) {
+    logger.warn("Setting credentials on a dummy connection");
+  }
+
+
+  /**
+   * Give login credentials for the current security domain to the current session.
+   * This operation is ignored.
+   * @param user The username.
+   * @param password The password for the given username.
+   */
+  public void setCredentials(String user, char[] password) {
+    logger.warn("Setting credentials on a dummy connection");
+  }
+
+
+  /**
+   * @return always null
+   */
+  public Session getSession() {
+    return null;
+  }
+
+
+  /**
+   * Starts and commits transactions on this connection, by turning the autocommit
+   * flag on and off. 
+   * @param autocommit <code>true</code> if the flag is to be on.
+   * @throws QueryException The session could not change state.
+   */
+  public void setAutoCommit(boolean autoCommit) throws QueryException {
+    this.autoCommit = autoCommit;
+  }
+
+
+  /**
+   * @return the autoCommit value
+   */
+  public boolean getAutoCommit() {
+    return autoCommit;
+  }
+
+
+  /**
+   * Closes the current connection.  Does nothing for this class.
+   */
+  public void close() throws QueryException {
+  }
+  
+}

Copied: branches/nw-interface/src/jar/query/java/org/mulgara/connection/SessionConnection.java (from rev 448, branches/nw-interface/src/jar/query/java/org/mulgara/connection/Connection.java)
===================================================================
--- branches/nw-interface/src/jar/query/java/org/mulgara/connection/Connection.java	2007-09-24 21:00:24 UTC (rev 448)
+++ branches/nw-interface/src/jar/query/java/org/mulgara/connection/SessionConnection.java	2007-09-26 04:48:34 UTC (rev 453)
@@ -0,0 +1,206 @@
+/*
+ * The contents of this file are subject to the Open Software License
+ * Version 3.0 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ * http://www.opensource.org/licenses/osl-3.0.txt
+ *
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
+ * the License for the specific language governing rights and limitations
+ * under the License.
+ */
+package org.mulgara.connection;
+
+import java.net.URI;
+
+import org.apache.log4j.Logger;
+import org.mulgara.query.QueryException;
+import org.mulgara.server.NonRemoteSessionException;
+import org.mulgara.server.Session;
+import org.mulgara.server.SessionFactory;
+import org.mulgara.server.driver.SessionFactoryFinder;
+import org.mulgara.server.driver.SessionFactoryFinderException;
+
+/**
+ * A connection for sending commands to a server using a session object.
+ *
+ * @created 2007-08-21
+ * @author Paul Gearon
+ * @copyright &copy; 2007 <a href="mailto:pgearon at users.sourceforge.net">Paul Gearon</a>
+ * @licence <a href="{@docRoot}/../../LICENCE.txt">Open Software License v3.0</a>
+ */
+public class SessionConnection implements Connection {
+
+  /** Logger. */
+  private static final Logger logger = Logger.getLogger(SessionConnection.class.getName());
+  
+  /** The URI for the server to establish a session on. */
+  private URI serverUri;
+  
+  /** The security domain URI. */
+  private URI securityDomainUri;
+  
+  /** The session to use for this connection. */
+  private Session session;
+
+  /** Indicates the current autocommit state */
+  private boolean autoCommit = true;
+
+  /**
+   * Creates a new connection, given a URI to a server.
+   * @param serverUri The URI to connect to.
+   * @throws ConnectionException There was a problem establishing the details needed for a connection.
+   */
+  public SessionConnection(URI serverUri) throws ConnectionException {
+    setServerUri(serverUri);
+  }
+
+
+  /**
+   * 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.
+   */
+  public SessionConnection(Session session) {
+    this(session, null);
+  }
+  
+  
+  /**
+   * 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.
+   */
+  public SessionConnection(Session session, URI securityDomainUri) {
+    if (session == null) throw new IllegalArgumentException("Cannot create a connection without a server.");
+    setSession(session, securityDomainUri);
+  }
+  
+  
+  /**
+   * Give login credentials and security domain to the current session.  This should only be needed
+   * once since the session does not change.
+   * @param securityDomainUri The security domain for the login.
+   * @param user The username.
+   * @param password The password for the given username.
+   */
+  public void setCredentials(URI securityDomainUri, String user, char[] password) {
+    if (securityDomainUri == null) throw new IllegalArgumentException("Must have a security domain to yuse credentials");
+    this.securityDomainUri = securityDomainUri;
+    setCredentials(user, password);
+  }
+
+
+  /**
+   * Give login credentials for the current security domain to the current session.
+   * This should only be needed
+   * once since the session does not change.
+   * @param user The username.
+   * @param password The password for the given username.
+   */
+  public void setCredentials(String user, char[] password) {
+    if (securityDomainUri == null) throw new IllegalArgumentException("Must have a security domain to yuse credentials");
+    session.login(securityDomainUri, user, password);
+  }
+
+
+  /**
+   * @return the session
+   */
+  public Session getSession() {
+    return session;
+  }
+
+
+  /**
+   * Starts and commits transactions on this connection, by turning the autocommit
+   * flag on and off. 
+   * @param autocommit <code>true</code> if the flag is to be on.
+   * @throws QueryException The session could not change state.
+   */
+  public void setAutoCommit(boolean autoCommit) throws QueryException {
+    this.autoCommit = autoCommit;
+    session.setAutoCommit(autoCommit);
+  }
+
+
+  /**
+   * @return the autoCommit value
+   */
+  public boolean getAutoCommit() {
+    return autoCommit;
+  }
+
+
+  /**
+   * Closes the current connection.
+   */
+  public void close() throws QueryException {
+    if (session != null) session.close();
+  }
+
+  // Private methods //
+
+  /**
+   * @return the serverUri
+   */
+  URI getServerUri() {
+    return serverUri;
+  }
+
+
+  /**
+   * @return the securityDomainUri
+   */
+  URI getSecurityDomainUri() {
+    return securityDomainUri;
+  }
+
+
+  /**
+   * Sets the session information for this connection
+   * @param session The session to set to.
+   * @param securityDomainURI The security domain to use for the session.
+   */
+  private void setSession(Session session, URI securityDomainUri) {
+    this.session = session;
+    this.securityDomainUri = securityDomainUri;
+  }
+
+
+  /**
+   * Establishes a session for this connection.
+   * @param uri The URI to set for the server.
+   * @throws ConnectionException There was a problem establishing a session.
+   */
+  private void setServerUri(URI uri) throws ConnectionException {
+    
+    try {
+      if (uri == null) {
+        // no model given, and the factory didn't cache a connection, so make one up.
+        serverUri = SessionFactoryFinder.findServerURI();
+      } else {
+        serverUri = uri;
+      }
+      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);
+      if (logger.isDebugEnabled()) logger.debug("Found " + sessionFactory.getClass() +
+          " 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) {
+      throw new ConnectionException("Unable to connect to a server", e);
+    } catch (NonRemoteSessionException e) {
+      throw new ConnectionException("Error connecting to the local server", e);
+    } catch (QueryException e) {
+      throw new ConnectionException("Data error in connection attempt", e);
+    }
+    assert session != null;
+  }
+  
+}




More information about the Mulgara-svn mailing list