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

pag at mulgara.org pag at mulgara.org
Thu Aug 23 12:12:38 UTC 2007


Author: pag
Date: 2007-08-23 07:12:37 -0500 (Thu, 23 Aug 2007)
New Revision: 372

Added:
   branches/nw-interface/src/jar/connection/java/org/mulgara/connection/Connection.java
   branches/nw-interface/src/jar/connection/java/org/mulgara/connection/ConnectionException.java
   branches/nw-interface/src/jar/connection/java/org/mulgara/connection/ConnectionFactory.java
Log:
Connection classes for managing sessions with the new grammar parsers

Added: 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 12:11:22 UTC (rev 371)
+++ branches/nw-interface/src/jar/connection/java/org/mulgara/connection/Connection.java	2007-08-23 12:12:37 UTC (rev 372)
@@ -0,0 +1,149 @@
+/*
+ * 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;
+  
+  /**
+   * 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);
+  }
+
+
+  /**
+   * 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
+      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;
+  }
+  
+}

Added: branches/nw-interface/src/jar/connection/java/org/mulgara/connection/ConnectionException.java
===================================================================
--- branches/nw-interface/src/jar/connection/java/org/mulgara/connection/ConnectionException.java	2007-08-23 12:11:22 UTC (rev 371)
+++ branches/nw-interface/src/jar/connection/java/org/mulgara/connection/ConnectionException.java	2007-08-23 12:12:37 UTC (rev 372)
@@ -0,0 +1,46 @@
+/*
+ * 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;
+
+/**
+ * An exception indicating a connection problem.
+ *
+ * @created 2007-08-22
+ * @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 ConnectionException extends Exception {
+
+  /** Regenerate this ID if non-private methods are added or removed. */
+  private static final long serialVersionUID = 3768510944925963668L;
+
+
+  /**
+   * Create an exception with a message.
+   * @param message The message to use.
+   */
+  public ConnectionException(String message) {
+    super(message);
+  }
+
+
+  /**
+   * Create an exception caused by another exception, and with a message.
+   * @param message The message to use.
+   * @param cause The original throwable causing this exception.
+   */
+  public ConnectionException(String message, Throwable cause) {
+    super(message, cause);
+  }
+  
+}

Added: 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 12:11:22 UTC (rev 371)
+++ branches/nw-interface/src/jar/connection/java/org/mulgara/connection/ConnectionFactory.java	2007-08-23 12:12:37 UTC (rev 372)
@@ -0,0 +1,25 @@
+/*
+ * 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;
+
+/**
+ * Creates new connections or reloads from a cache when possible connections.
+ *
+ * @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 ConnectionFactory {
+
+  
+}




More information about the Mulgara-svn mailing list