From ae1731fb2eab98eb1d87406ed21fab521c053c8e Mon Sep 17 00:00:00 2001
From: Aaron <donovan_aaron@bah.com>
Date: Thu, 21 Jul 2011 22:24:06 -0400
Subject: [PATCH 2/2] Allow creation of local or client sessions for jax-rx
 depending upon a system property.

---
 src/main/java/org/basex/api/jaxrx/BXCode.java      |   30 ++-------
 .../java/org/basex/api/jaxrx/SessionFactory.java   |   68 ++++++++++++++++++++
 2 files changed, 74 insertions(+), 24 deletions(-)
 create mode 100644 src/main/java/org/basex/api/jaxrx/SessionFactory.java

diff --git a/src/main/java/org/basex/api/jaxrx/BXCode.java b/src/main/java/org/basex/api/jaxrx/BXCode.java
index 0335b19..31f21a3 100644
--- a/src/main/java/org/basex/api/jaxrx/BXCode.java
+++ b/src/main/java/org/basex/api/jaxrx/BXCode.java
@@ -4,29 +4,22 @@ import static org.jaxrx.core.JaxRxConstants.*;
 
 import java.io.IOException;
 
-import javax.ws.rs.core.HttpHeaders;
-import javax.ws.rs.core.Response.ResponseBuilder;
-
 import org.basex.data.DataText;
 import org.basex.io.serial.SerializerProp;
-import org.basex.server.ClientSession;
-import org.basex.server.LoginException;
-import org.basex.util.Util;
+import org.basex.server.Session;
 import org.jaxrx.core.JaxRxException;
 import org.jaxrx.core.QueryParameter;
 import org.jaxrx.core.ResourcePath;
 
-import com.sun.jersey.core.spi.factory.ResponseBuilderImpl;
-
 /**
  * Wrapper class for running JAX-RX code.
- *
+ * 
  * @author BaseX Team 2005-11, BSD License
  * @author Christian Gruen
  */
 abstract class BXCode {
   /** Client session. */
-  final ClientSession cs;
+  final Session cs;
 
   /**
    * Constructor, creating a new client session instance with user name and
@@ -35,18 +28,7 @@ abstract class BXCode {
    *          credentials.
    */
   BXCode(final ResourcePath path) {
-    try {
-      cs = JaxRxServer.login(path);
-    } catch(final LoginException ex) {
-      final ResponseBuilder rb = new ResponseBuilderImpl();
-      rb.header(HttpHeaders.WWW_AUTHENTICATE, "Basic ");
-      rb.status(401);
-      rb.entity("Username/password is wrong.");
-      throw new JaxRxException(rb.build());
-    } catch(final Exception ex) {
-      Util.stack(ex);
-      throw new JaxRxException(ex);
-    }
+    cs = SessionFactory.getSession(path);
   }
 
   /**
@@ -101,8 +83,8 @@ abstract class BXCode {
     } catch(final NumberFormatException ex) {
       /* exception follows for both branches. */
     }
-    throw new JaxRxException(400,
-        "Parameter '" + qp + "' is no valid integer: " + val);
+    throw new JaxRxException(400, "Parameter '" + qp
+        + "' is no valid integer: " + val);
   }
 
   /**
diff --git a/src/main/java/org/basex/api/jaxrx/SessionFactory.java b/src/main/java/org/basex/api/jaxrx/SessionFactory.java
new file mode 100644
index 0000000..7478efd
--- /dev/null
+++ b/src/main/java/org/basex/api/jaxrx/SessionFactory.java
@@ -0,0 +1,68 @@
+package org.basex.api.jaxrx;
+
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.Response.ResponseBuilder;
+
+import org.basex.core.Context;
+import org.basex.server.LocalSession;
+import org.basex.server.LoginException;
+import org.basex.server.Session;
+import org.basex.util.Util;
+import org.jaxrx.core.JaxRxException;
+import org.jaxrx.core.ResourcePath;
+
+import com.sun.jersey.core.spi.factory.ResponseBuilderImpl;
+
+/**
+ * 
+ * @author 524506
+ * 
+ */
+public final class SessionFactory {
+
+  /**
+   * Context used to create local sessions
+   */
+  private static Context context;
+
+  /**
+   * Should local sessions be created.
+   */
+  private static boolean localSession = false;
+
+  static {
+    localSession = Boolean.parseBoolean(System.getProperty(
+        "org.basex.jaxrx.local", "false"));
+    if(localSession) {
+      context = new Context();
+    }
+  }
+
+  /**
+   * Prevent instance creation
+   */
+  private SessionFactory() {}
+
+  /**
+   * 
+   * @param path provides authentication information for client sessions
+   * @return a local or client session depending upon properties.
+   */
+  static Session getSession(ResourcePath path) {
+    if(localSession) {
+      return new LocalSession(context);
+    }
+    try {
+      return JaxRxServer.login(path);
+    } catch(final LoginException ex) {
+      final ResponseBuilder rb = new ResponseBuilderImpl();
+      rb.header(HttpHeaders.WWW_AUTHENTICATE, "Basic ");
+      rb.status(401);
+      rb.entity("Username/password is wrong.");
+      throw new JaxRxException(rb.build());
+    } catch(final Exception ex) {
+      Util.stack(ex);
+      throw new JaxRxException(ex);
+    }
+  }
+}
-- 
1.7.5

