[gatein-commits] gatein SVN: r4993 - in portal/trunk/component/wsrp/src/main/java/org/gatein/portal/wsrp/state: consumer and 4 other directories.

do-not-reply at jboss.org do-not-reply at jboss.org
Tue Nov 9 18:02:33 EST 2010


Author: chris.laprun at jboss.com
Date: 2010-11-09 18:02:33 -0500 (Tue, 09 Nov 2010)
New Revision: 4993

Modified:
   portal/trunk/component/wsrp/src/main/java/org/gatein/portal/wsrp/state/JCRPersister.java
   portal/trunk/component/wsrp/src/main/java/org/gatein/portal/wsrp/state/consumer/JCRConsumerRegistry.java
   portal/trunk/component/wsrp/src/main/java/org/gatein/portal/wsrp/state/migration/JCRMigrationService.java
   portal/trunk/component/wsrp/src/main/java/org/gatein/portal/wsrp/state/producer/configuration/JCRProducerConfigurationService.java
   portal/trunk/component/wsrp/src/main/java/org/gatein/portal/wsrp/state/producer/registrations/JCRRegistrationPersistenceManager.java
   portal/trunk/component/wsrp/src/main/java/org/gatein/portal/wsrp/state/producer/state/JCRPortletStatePersistenceManager.java
Log:
- GTNWSRP-84: Made JCRPersister thread-safe.

Modified: portal/trunk/component/wsrp/src/main/java/org/gatein/portal/wsrp/state/JCRPersister.java
===================================================================
--- portal/trunk/component/wsrp/src/main/java/org/gatein/portal/wsrp/state/JCRPersister.java	2010-11-09 21:41:18 UTC (rev 4992)
+++ portal/trunk/component/wsrp/src/main/java/org/gatein/portal/wsrp/state/JCRPersister.java	2010-11-09 23:02:33 UTC (rev 4993)
@@ -1,28 +1,28 @@
 /*
-* JBoss, a division of Red Hat
-* Copyright 2008, Red Hat Middleware, LLC, and individual contributors as indicated
-* by the @authors tag. See the copyright.txt in the distribution for a
-* full listing of individual contributors.
-*
-* 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 2.1 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.
-*/
+ * JBoss, a division of Red Hat
+ * Copyright 2010, Red Hat Middleware, LLC, and individual
+ * contributors as indicated by the @authors tag. See the
+ * copyright.txt in the distribution for a full listing of
+ * individual contributors.
+ *
+ * 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 2.1 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.gatein.portal.wsrp.state;
 
-import EDU.oswego.cs.dl.util.concurrent.FJTask;
 import org.chromattic.api.Chromattic;
 import org.chromattic.api.ChromatticBuilder;
 import org.chromattic.api.ChromatticSession;
@@ -59,6 +59,8 @@
    private String workspaceName;
    private Map<Class, Class<? extends BaseMapping>> modelToMapping;
 
+   private ThreadLocal<ChromatticSession> sessionHolder = new ThreadLocal<ChromatticSession>();
+
    public JCRPersister(ExoContainer container, String workspaceName)
    {
       this.workspaceName = workspaceName;
@@ -87,7 +89,7 @@
          if (BaseMapping.class.isAssignableFrom(mappingClass))
          {
             Type[] interfaces = mappingClass.getGenericInterfaces();
-            if(ParameterValidation.existsAndIsNotEmpty(interfaces))
+            if (ParameterValidation.existsAndIsNotEmpty(interfaces))
             {
                Class type = (Class)((ParameterizedType)interfaces[0]).getActualTypeArguments()[0];
                modelToMapping.put(type, mappingClass);
@@ -101,28 +103,41 @@
 
    public ChromatticSession getSession()
    {
-      return chrome.openSession();
+      if (sessionHolder.get() == null)
+      {
+         ChromatticSession session = chrome.openSession();
+         sessionHolder.set(session);
+         return session;
+      }
+      else
+      {
+         return sessionHolder.get();
+      }
    }
 
-   public void closeSession(ChromatticSession session, boolean save)
+   public void closeSession(boolean save)
    {
+      ChromatticSession session = getSession();
       if (save)
       {
-         session.save();
+         synchronized (this)
+         {
+            session.save();
+         }
       }
       session.close();
    }
 
-   public void save(ChromatticSession session)
+   public synchronized void save()
    {
-      session.save();
+      getSession().save();
    }
 
    public <T> boolean delete(T toDelete, StoresByPathManager<T> manager)
    {
       Class<? extends Object> modelClass = toDelete.getClass();
       Class<? extends BaseMapping> baseMappingClass = modelToMapping.get(modelClass);
-      if(baseMappingClass == null)
+      if (baseMappingClass == null)
       {
          throw new IllegalArgumentException("Cannot find a mapping class for " + modelClass.getName());
       }
@@ -134,12 +149,12 @@
       if (old != null)
       {
          session.remove(old);
-         closeSession(session, true);
+         closeSession(true);
          return true;
       }
       else
       {
-         closeSession(session, false);
+         closeSession(false);
          return false;
       }
 

Modified: portal/trunk/component/wsrp/src/main/java/org/gatein/portal/wsrp/state/consumer/JCRConsumerRegistry.java
===================================================================
--- portal/trunk/component/wsrp/src/main/java/org/gatein/portal/wsrp/state/consumer/JCRConsumerRegistry.java	2010-11-09 21:41:18 UTC (rev 4992)
+++ portal/trunk/component/wsrp/src/main/java/org/gatein/portal/wsrp/state/consumer/JCRConsumerRegistry.java	2010-11-09 23:02:33 UTC (rev 4993)
@@ -1,6 +1,6 @@
 /*
  * JBoss, a division of Red Hat
- * Copyright 2009, Red Hat Middleware, LLC, and individual
+ * Copyright 2010, Red Hat Middleware, LLC, and individual
  * contributors as indicated by the @authors tag. See the
  * copyright.txt in the distribution for a full listing of
  * individual contributors.
@@ -49,7 +49,7 @@
  */
 public class JCRConsumerRegistry extends AbstractConsumerRegistry implements StoresByPathManager<ProducerInfo>
 {
-//   private NewJCRPersister persister;
+   //   private NewJCRPersister persister;
    private JCRPersister persister;
    private static final String PRODUCER_INFOS_PATH = ProducerInfosMapping.NODE_NAME;
 
@@ -78,12 +78,12 @@
          info.setKey(key);
          pim.initFrom(info);
 
-         persister.closeSession(session, true);
+         persister.closeSession(true);
       }
       catch (Exception e)
       {
          e.printStackTrace();  // todo: fix me
-         persister.closeSession(session, false);
+         persister.closeSession(false);
       }
    }
 
@@ -113,7 +113,7 @@
       String newId = producerInfo.getId();
       pim.initFrom(producerInfo);
 
-      persister.closeSession(session, true);
+      persister.closeSession(true);
 
       // if the consumer's id has changed, return the old one so that state can be updated
       return (oldId.equals(newId)) ? null : oldId;
@@ -127,7 +127,7 @@
 
       List<ProducerInfoMapping> mappings = producerInfosMapping.getProducerInfos();
 
-      persister.closeSession(session, true);
+      persister.closeSession(true);
 
       return new MappingToProducerInfoIterator(mappings.iterator());
    }

Modified: portal/trunk/component/wsrp/src/main/java/org/gatein/portal/wsrp/state/migration/JCRMigrationService.java
===================================================================
--- portal/trunk/component/wsrp/src/main/java/org/gatein/portal/wsrp/state/migration/JCRMigrationService.java	2010-11-09 21:41:18 UTC (rev 4992)
+++ portal/trunk/component/wsrp/src/main/java/org/gatein/portal/wsrp/state/migration/JCRMigrationService.java	2010-11-09 23:02:33 UTC (rev 4993)
@@ -85,7 +85,7 @@
          exportInfos.add(eim.toModel(null));
       }
 
-      persister.closeSession(session, false);
+      persister.closeSession(false);
 
       exportInfosCount = exportInfos.size();
 
@@ -95,7 +95,7 @@
    private ExportInfosMapping getExportInfosMapping(ChromatticSession session)
    {
       ExportInfosMapping exportInfosMapping = session.findByPath(ExportInfosMapping.class, ExportInfosMapping.NODE_NAME);
-      if(exportInfosMapping == null)
+      if (exportInfosMapping == null)
       {
          exportInfosMapping = session.insert(ExportInfosMapping.class, ExportInfosMapping.NODE_NAME);
          exportInfosCount = 0;
@@ -110,7 +110,7 @@
 
       ExportInfoMapping eim = session.findByPath(ExportInfoMapping.class, getPathFor(exportTime));
 
-      if(eim != null)
+      if (eim != null)
       {
          return eim.toModel(null);
       }
@@ -126,7 +126,7 @@
 
       ExportInfoMapping eim = session.findByPath(ExportInfoMapping.class, getChildPath(info));
       long exportTime = info.getExportTime();
-      if(eim != null)
+      if (eim != null)
       {
          throw new IllegalArgumentException("An ExportInfo with export time "
             + exportTime + " already exists!");
@@ -139,7 +139,7 @@
          session.persist(exportInfosMapping, exportInfo, exportTimeAsString);
          exportInfo.initFrom(info);
 
-         persister.closeSession(session, true);
+         persister.closeSession(true);
          exportInfosCount++;
       }
    }
@@ -159,7 +159,7 @@
 
    public boolean isAvailableExportInfosEmpty()
    {
-      if(exportInfosCount == -1)
+      if (exportInfosCount == -1)
       {
          ChromatticSession session = persister.getSession();
          ExportInfosMapping mappings = getExportInfosMapping(session);

Modified: portal/trunk/component/wsrp/src/main/java/org/gatein/portal/wsrp/state/producer/configuration/JCRProducerConfigurationService.java
===================================================================
--- portal/trunk/component/wsrp/src/main/java/org/gatein/portal/wsrp/state/producer/configuration/JCRProducerConfigurationService.java	2010-11-09 21:41:18 UTC (rev 4992)
+++ portal/trunk/component/wsrp/src/main/java/org/gatein/portal/wsrp/state/producer/configuration/JCRProducerConfigurationService.java	2010-11-09 23:02:33 UTC (rev 4993)
@@ -1,6 +1,6 @@
 /*
  * JBoss, a division of Red Hat
- * Copyright 2009, Red Hat Middleware, LLC, and individual
+ * Copyright 2010, Red Hat Middleware, LLC, and individual
  * contributors as indicated by the @authors tag. See the
  * copyright.txt in the distribution for a full listing of
  * individual contributors.
@@ -47,7 +47,7 @@
    private static String PRODUCER_CONFIGURATION_PATH = ProducerConfigurationMapping.NODE_NAME;
 
    private InputStream defaultConfigurationIS;
-//   private NewJCRPersister persister;
+   //   private NewJCRPersister persister;
    private JCRPersister persister;
 
    public JCRProducerConfigurationService(ExoContainer container) throws Exception
@@ -96,7 +96,7 @@
       }
 
 
-      persister.closeSession(session, true);
+      persister.closeSession(true);
    }
 
    public void saveConfiguration() throws Exception
@@ -110,6 +110,6 @@
       }
       pcm.initFrom(configuration);
 
-      persister.closeSession(session, true);
+      persister.closeSession(true);
    }
 }

Modified: portal/trunk/component/wsrp/src/main/java/org/gatein/portal/wsrp/state/producer/registrations/JCRRegistrationPersistenceManager.java
===================================================================
--- portal/trunk/component/wsrp/src/main/java/org/gatein/portal/wsrp/state/producer/registrations/JCRRegistrationPersistenceManager.java	2010-11-09 21:41:18 UTC (rev 4992)
+++ portal/trunk/component/wsrp/src/main/java/org/gatein/portal/wsrp/state/producer/registrations/JCRRegistrationPersistenceManager.java	2010-11-09 23:02:33 UTC (rev 4993)
@@ -1,24 +1,25 @@
 /*
-* JBoss, a division of Red Hat
-* Copyright 2008, Red Hat Middleware, LLC, and individual contributors as indicated
-* by the @authors tag. See the copyright.txt in the distribution for a
-* full listing of individual contributors.
-*
-* 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 2.1 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.
-*/
+ * JBoss, a division of Red Hat
+ * Copyright 2010, Red Hat Middleware, LLC, and individual
+ * contributors as indicated by the @authors tag. See the
+ * copyright.txt in the distribution for a full listing of
+ * individual contributors.
+ *
+ * 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 2.1 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.gatein.portal.wsrp.state.producer.registrations;
 
@@ -51,7 +52,7 @@
  */
 public class JCRRegistrationPersistenceManager extends RegistrationPersistenceManagerImpl
 {
-//   private NewJCRPersister persister;
+   //   private NewJCRPersister persister;
    private JCRPersister persister;
    private ConsumersAndGroupsMapping mappings;
 
@@ -73,7 +74,7 @@
       {
          mappings = session.insert(ConsumersAndGroupsMapping.class, ConsumersAndGroupsMapping.NODE_NAME);
       }
-      persister.save(session); // needed right now as the session must still be open to iterate over nodes
+      persister.save(); // needed right now as the session must still be open to iterate over nodes
 
       for (ConsumerGroupMapping cgm : mappings.getConsumerGroups())
       {
@@ -92,7 +93,7 @@
          }
       }
 
-      persister.closeSession(session, false);
+      persister.closeSession(false);
    }
 
    @Override
@@ -115,12 +116,12 @@
          RegistrationMapping rm = cm.createAndAddRegistrationMappingFrom(null);
          registration = newRegistrationSPI(consumer, registrationProperties, rm.getPersistentKey());
          rm.initFrom(registration);
-         persister.closeSession(session, true);
+         persister.closeSession(true);
       }
       catch (Exception e)
       {
          e.printStackTrace(); // todo fix me
-         persister.closeSession(session, false);
+         persister.closeSession(false);
       }
 
       return registration;
@@ -138,7 +139,7 @@
    {
       ChromatticSession session = persister.getSession();
       session.remove(session.findById(clazz, id));
-      persister.closeSession(session, true);
+      persister.closeSession(true);
    }
 
    @Override
@@ -154,12 +155,12 @@
          mappings.getConsumers().add(cm);
          cm.initFrom(consumer);
          consumer.setPersistentKey(cm.getPersistentKey());
-         persister.closeSession(session, true);
+         persister.closeSession(true);
       }
       catch (Exception e)
       {
          e.printStackTrace(); // todo: fix me
-         persister.closeSession(session, false);
+         persister.closeSession(false);
       }
 
       return consumer;
@@ -175,34 +176,34 @@
       {
          ConsumerMapping cm = session.findById(ConsumerMapping.class, consumer.getPersistentKey());
          cm.initFrom(consumer);
-         persister.closeSession(session, true);
+         persister.closeSession(true);
       }
       catch (Exception e)
       {
          e.printStackTrace();  // todo: fix me
-         persister.closeSession(session, false);
+         persister.closeSession(false);
       }
 
       return consumerSPI;
    }
-   
+
    protected RegistrationSPI internalSaveChangesTo(Registration registration)
    {
       RegistrationSPI registrationSPI = super.internalSaveChangesTo(registration);
-      
+
       ChromatticSession session = persister.getSession();
       try
       {
          RegistrationMapping cm = session.findById(RegistrationMapping.class, registration.getPersistentKey());
          cm.initFrom(registration);
-         persister.closeSession(session, true);
+         persister.closeSession(true);
       }
       catch (Exception e)
       {
          e.printStackTrace(); //todo: fix me
-         persister.closeSession(session, false);
+         persister.closeSession(false);
       }
-      
+
       return registrationSPI;
    }
 
@@ -234,12 +235,12 @@
          mappings.getConsumerGroups().add(cgm);
          group.setPersistentKey(cgm.getPersistentKey());
          cgm.initFrom(group);
-         persister.closeSession(session, true);
+         persister.closeSession(true);
       }
       catch (Exception e)
       {
          e.printStackTrace();  // todo: fix me
-         persister.closeSession(session, false);
+         persister.closeSession(false);
       }
 
       return group;

Modified: portal/trunk/component/wsrp/src/main/java/org/gatein/portal/wsrp/state/producer/state/JCRPortletStatePersistenceManager.java
===================================================================
--- portal/trunk/component/wsrp/src/main/java/org/gatein/portal/wsrp/state/producer/state/JCRPortletStatePersistenceManager.java	2010-11-09 21:41:18 UTC (rev 4992)
+++ portal/trunk/component/wsrp/src/main/java/org/gatein/portal/wsrp/state/producer/state/JCRPortletStatePersistenceManager.java	2010-11-09 23:02:33 UTC (rev 4993)
@@ -83,7 +83,7 @@
       PortletStateMapping psm = pscm.getState();
       psm.setProperties(propertyMap);
 
-      persister.closeSession(session, true);
+      persister.closeSession(true);
    }
 
 
@@ -103,7 +103,7 @@
          context = pscm.toPortletStateContext();
       }
 
-      persister.closeSession(session, false);
+      persister.closeSession(false);
 
       return context;
    }
@@ -127,7 +127,7 @@
       psm.setPortletID(pscm.getPortletId());
       psm.setProperties(propertyMap);
 
-      persister.closeSession(session, true);
+      persister.closeSession(true);
 
       return pscm.getPersistentKey();
    }
@@ -150,7 +150,7 @@
          result = pscm.toPortletStateContext();
       }
 
-      persister.closeSession(session, true);
+      persister.closeSession(true);
       return result;
    }
 



More information about the gatein-commits mailing list