[exo-jcr-commits] exo-jcr SVN: r1048 - jcr/branches/1.12.0-JBC/component/core/src/main/java/org/exoplatform/services/jcr/impl/storage/jbosscache.

do-not-reply at jboss.org do-not-reply at jboss.org
Mon Dec 14 11:07:54 EST 2009


Author: nfilotto
Date: 2009-12-14 11:07:53 -0500 (Mon, 14 Dec 2009)
New Revision: 1048

Modified:
   jcr/branches/1.12.0-JBC/component/core/src/main/java/org/exoplatform/services/jcr/impl/storage/jbosscache/JDBCStorageConnectionFactory.java
Log:
EXOJCR-199: JBoss Cache persistence prototype
Debug capabilities added in JDBCStorageConnectionFactory, now we can switch to the debug mode thanks to
JDBCStorageConnectionFactory.DEBUG.set(true) to make it count the total amount of connections and show the trace
thanks to JDBCStorageConnectionFactory.SHOW_TRACE.set(true)

Modified: jcr/branches/1.12.0-JBC/component/core/src/main/java/org/exoplatform/services/jcr/impl/storage/jbosscache/JDBCStorageConnectionFactory.java
===================================================================
--- jcr/branches/1.12.0-JBC/component/core/src/main/java/org/exoplatform/services/jcr/impl/storage/jbosscache/JDBCStorageConnectionFactory.java	2009-12-14 15:30:42 UTC (rev 1047)
+++ jcr/branches/1.12.0-JBC/component/core/src/main/java/org/exoplatform/services/jcr/impl/storage/jbosscache/JDBCStorageConnectionFactory.java	2009-12-14 16:07:53 UTC (rev 1048)
@@ -1,13 +1,33 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 3 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
 package org.exoplatform.services.jcr.impl.storage.jbosscache;
 
+import org.exoplatform.services.jcr.impl.storage.jdbc.JDBCStorageConnection;
+import org.exoplatform.services.jcr.storage.WorkspaceDataContainer;
+
 import java.util.HashMap;
 import java.util.Map;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
 
 import javax.jcr.RepositoryException;
 
-import org.exoplatform.services.jcr.impl.storage.jdbc.JDBCStorageConnection;
-import org.exoplatform.services.jcr.storage.WorkspaceDataContainer;
-
 /**
  * This class is used to holds the JDBC connection open during the live time of
  * the {@link JBossCacheStorageConnection}. We keep one
@@ -17,63 +37,91 @@
  * @author nicolasfilotto
  * 
  */
-public class JDBCStorageConnectionFactory {
+public class JDBCStorageConnectionFactory
+{
 
-	/**
-	 * The {@link ThreadLocal} in which we store all the current opened
-	 * connections
-	 */
-	private static final ThreadLocal<Map<WorkspaceDataContainer, JDBCStorageConnection>> CONNECTIONS = new ThreadLocal<Map<WorkspaceDataContainer, JDBCStorageConnection>>();
+   /**
+    * The {@link ThreadLocal} in which we store all the current opened
+    * connections
+    */
+   private static final ThreadLocal<Map<WorkspaceDataContainer, JDBCStorageConnection>> CONNECTIONS =
+      new ThreadLocal<Map<WorkspaceDataContainer, JDBCStorageConnection>>();
 
-	/**
-	 * Gives the current opened connection for the related
-	 * {@link WorkspaceDataContainer} if it exists, returns a new one otherwise
-	 * 
-	 * @param dataContainer
-	 *            the data container for which we want a connection
-	 * @return an opened connection
-	 * @throws RepositoryException
-	 *             if an error occurs
-	 */
-	static JDBCStorageConnection get(WorkspaceDataContainer dataContainer)
-			throws RepositoryException {
-		Map<WorkspaceDataContainer, JDBCStorageConnection> connections = CONNECTIONS
-				.get();
-		if (connections == null) {
-			connections = new HashMap<WorkspaceDataContainer, JDBCStorageConnection>();
-			CONNECTIONS.set(connections);
-		}
-		JDBCStorageConnection connection = connections.get(dataContainer);
-		if (connection == null || !connection.isOpened()) {
-			connection = (JDBCStorageConnection) dataContainer.openConnection();
-			connections.put(dataContainer, connection);
-		}
-		return connection;
-	}
+   /**
+    * Indicates if we are in a debug mode
+    */
+   public static AtomicBoolean DEBUG = new AtomicBoolean(false);
 
-	/**
-	 * Close the current opened connection related to the given
-	 * {@link WorkspaceDataContainer}
-	 * 
-	 * @param dataContainer
-	 *            the data container for which we want to close the connection
-	 * @throws IllegalStateException
-	 *             if connection is already closed
-	 * @throws RepositoryException
-	 *             if some exception occured
-	 */
-	static void close(WorkspaceDataContainer dataContainer)
-			throws IllegalStateException, RepositoryException {
-		Map<WorkspaceDataContainer, JDBCStorageConnection> connections = CONNECTIONS
-				.get();
-		if (connections == null) {
-			return;
-		}
-		JDBCStorageConnection connection = connections.remove(dataContainer);
-		if (connection == null || !connection.isOpened()) {
-			return;
-		}
-		connection.close();
-		connection = null;
-	}
+   /**
+    * The total amount of connections. Used for debugging purpose only.
+    */
+   public static AtomicInteger TOTAL_CONNECTION = new AtomicInteger(0);
+
+   /**
+    * Indicates if we display the stack trace into the output stream. Used for
+    * debugging purpose only.
+    */
+   public static AtomicBoolean SHOW_TRACE = new AtomicBoolean(false);
+
+   /**
+    * Gives the current opened connection for the related
+    * {@link WorkspaceDataContainer} if it exists, returns a new one otherwise
+    * 
+    * @param dataContainer
+    *            the data container for which we want a connection
+    * @return an opened connection
+    * @throws RepositoryException
+    *             if an error occurs
+    */
+   static JDBCStorageConnection get(WorkspaceDataContainer dataContainer) throws RepositoryException
+   {
+      if (DEBUG.get())
+      {
+         if (SHOW_TRACE.get())
+         {
+            (new Exception()).printStackTrace();
+         }
+         TOTAL_CONNECTION.incrementAndGet();
+      }
+      Map<WorkspaceDataContainer, JDBCStorageConnection> connections = CONNECTIONS.get();
+      if (connections == null)
+      {
+         connections = new HashMap<WorkspaceDataContainer, JDBCStorageConnection>();
+         CONNECTIONS.set(connections);
+      }
+      JDBCStorageConnection connection = connections.get(dataContainer);
+      if (connection == null || !connection.isOpened())
+      {
+         connection = (JDBCStorageConnection)dataContainer.openConnection();
+         connections.put(dataContainer, connection);
+      }
+      return connection;
+   }
+
+   /**
+    * Close the current opened connection related to the given
+    * {@link WorkspaceDataContainer}
+    * 
+    * @param dataContainer
+    *            the data container for which we want to close the connection
+    * @throws IllegalStateException
+    *             if connection is already closed
+    * @throws RepositoryException
+    *             if some exception occured
+    */
+   static void close(WorkspaceDataContainer dataContainer) throws IllegalStateException, RepositoryException
+   {
+      Map<WorkspaceDataContainer, JDBCStorageConnection> connections = CONNECTIONS.get();
+      if (connections == null)
+      {
+         return;
+      }
+      JDBCStorageConnection connection = connections.remove(dataContainer);
+      if (connection == null || !connection.isOpened())
+      {
+         return;
+      }
+      connection.close();
+      connection = null;
+   }
 }



More information about the exo-jcr-commits mailing list