[exo-jcr-commits] exo-jcr SVN: r1406 - in jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src: main/java/org/exoplatform/services/jcr/impl/core/lock and 2 other directories.

do-not-reply at jboss.org do-not-reply at jboss.org
Fri Jan 15 08:00:59 EST 2010


Author: sergiykarpenko
Date: 2010-01-15 08:00:59 -0500 (Fri, 15 Jan 2010)
New Revision: 1406

Added:
   jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/lock/jbosscache/
   jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/lock/jbosscache/CacheLockImpl.java
   jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/lock/jbosscache/CacheableLockManager.java
   jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/lock/jbosscache/LockData.java
Modified:
   jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/RepositoryContainer.java
   jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/test/resources/conf/standalone/test-jbosscache-lockconfig.xml
Log:
EXOJCR-332: CacheableLockManager moved to ../lock/jbosscache package


Modified: jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/RepositoryContainer.java
===================================================================
--- jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/RepositoryContainer.java	2010-01-15 12:48:11 UTC (rev 1405)
+++ jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/RepositoryContainer.java	2010-01-15 13:00:59 UTC (rev 1406)
@@ -41,7 +41,7 @@
 import org.exoplatform.services.jcr.impl.core.WorkspaceInitializer;
 import org.exoplatform.services.jcr.impl.core.access.DefaultAccessManagerImpl;
 import org.exoplatform.services.jcr.impl.core.lock.LockManagerImpl;
-import org.exoplatform.services.jcr.impl.core.lock.cache.CacheableLockManager;
+import org.exoplatform.services.jcr.impl.core.lock.jbosscache.CacheableLockManager;
 import org.exoplatform.services.jcr.impl.core.nodetype.NodeTypeDataManagerImpl;
 import org.exoplatform.services.jcr.impl.core.nodetype.NodeTypeManagerImpl;
 import org.exoplatform.services.jcr.impl.core.nodetype.registration.JCRNodeTypeDataPersister;

Added: jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/lock/jbosscache/CacheLockImpl.java
===================================================================
--- jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/lock/jbosscache/CacheLockImpl.java	                        (rev 0)
+++ jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/lock/jbosscache/CacheLockImpl.java	2010-01-15 13:00:59 UTC (rev 1406)
@@ -0,0 +1,142 @@
+/*
+ * Copyright (C) 2003-2010 eXo Platform SAS.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Affero General Public License
+ * as published by the Free Software Foundation; either version 3
+ * of the License, or (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see<http://www.gnu.org/licenses/>.
+ */
+package org.exoplatform.services.jcr.impl.core.lock.cache;
+
+import org.exoplatform.services.jcr.impl.core.SessionImpl;
+import org.exoplatform.services.jcr.impl.core.lock.LockImpl;
+
+import javax.jcr.Node;
+import javax.jcr.RepositoryException;
+import javax.jcr.lock.LockException;
+
+/**
+ * Created by The eXo Platform SAS.
+ * 
+ * <br/>Date: 
+ *
+ * @author <a href="karpenko.sergiy at gmail.com">Karpenko Sergiy</a> 
+ * @version $Id: CacheLockImpl.java 111 2008-11-11 11:11:11Z serg $
+ */
+public class CacheLockImpl extends LockImpl
+{
+   private boolean live;
+
+   private LockData lockData;
+
+   private SessionImpl session;
+
+   private CacheableLockManager lockManager;
+
+   public CacheLockImpl(SessionImpl session, LockData lockData, CacheableLockManager lockManager)
+   {
+      this.lockData = lockData;
+      this.session = session;
+      this.lockManager = lockManager;
+      this.live = true;
+   }
+
+   public String getLockOwner()
+   {
+      return lockData.getOwner();
+   }
+
+   public String getLockToken()
+   {
+      return lockManager.getLockToken(session.getId(), lockData);
+   }
+
+   public boolean isLive()
+   {
+      // it is already not alive
+      if (!live)
+      {
+         return false;
+      }
+      // update from cache. If it is not present there - means it is not live
+      // node unlocked and/or re-locked with another session 
+      LockData newlockData = lockManager.getLockDataById(lockData.getNodeIdentifier());
+      if (newlockData == null || newlockData.getToken() != lockData.getToken())
+      {
+         live = false;
+         return false;
+      }
+      else
+      {
+         // update, whether time to death changed, or whatever
+         this.lockData = newlockData;
+         return true;
+      }
+   }
+
+   public void refresh() throws LockException, RepositoryException
+   {
+      if (!isLive())
+         throw new LockException("Lock is not live");
+
+      LockData newLockData =
+         new LockData(lockData.getNodeIdentifier(), lockData.getToken(), lockData.isDeep(), lockData.isSessionScoped(),
+            lockData.getOwner(), lockData.getTimeOut());
+
+      lockManager.refresh(newLockData);
+      lockData = newLockData;
+   }
+
+   public Node getNode()
+   {
+      try
+      {
+         return (Node)session.getTransientNodesManager().getItemByIdentifier(lockData.getNodeIdentifier(), true);
+      }
+      catch (RepositoryException e)
+      {
+         e.printStackTrace();
+      }
+      return null;
+   }
+
+   public boolean isDeep()
+   {
+
+      return lockData.isDeep();
+   }
+
+   public boolean isSessionScoped()
+   {
+      return lockData.isSessionScoped();
+   }
+
+   public long getTimeToDeath()
+   {
+      return lockData.getTimeToDeath();
+   }
+
+   protected void setTimeOut(long timeOut)
+   {
+      lockData.setTimeOut(timeOut);
+
+      //reset lock data
+      try
+      {
+         lockManager.refresh(lockData);
+      }
+      catch (LockException e)
+      {
+         // TODO remove or change this
+         e.printStackTrace();
+      }
+   }
+}

Added: jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/lock/jbosscache/CacheableLockManager.java
===================================================================
--- jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/lock/jbosscache/CacheableLockManager.java	                        (rev 0)
+++ jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/lock/jbosscache/CacheableLockManager.java	2010-01-15 13:00:59 UTC (rev 1406)
@@ -0,0 +1,912 @@
+/*
+ * Copyright (C) 2003-2010 eXo Platform SAS.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Affero General Public License
+ * as published by the Free Software Foundation; either version 3
+ * of the License, or (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see<http://www.gnu.org/licenses/>.
+ */
+package org.exoplatform.services.jcr.impl.core.lock.cache;
+
+import org.exoplatform.management.annotations.Managed;
+import org.exoplatform.management.annotations.ManagedDescription;
+import org.exoplatform.management.jmx.annotations.NameTemplate;
+import org.exoplatform.management.jmx.annotations.Property;
+import org.exoplatform.services.jcr.access.SystemIdentity;
+import org.exoplatform.services.jcr.config.RepositoryConfigurationException;
+import org.exoplatform.services.jcr.config.WorkspaceEntry;
+import org.exoplatform.services.jcr.core.ExtendedSession;
+import org.exoplatform.services.jcr.dataflow.ChangesLogIterator;
+import org.exoplatform.services.jcr.dataflow.CompositeChangesLog;
+import org.exoplatform.services.jcr.dataflow.DataManager;
+import org.exoplatform.services.jcr.dataflow.ItemState;
+import org.exoplatform.services.jcr.dataflow.ItemStateChangesLog;
+import org.exoplatform.services.jcr.dataflow.PlainChangesLog;
+import org.exoplatform.services.jcr.dataflow.PlainChangesLogImpl;
+import org.exoplatform.services.jcr.dataflow.TransactionChangesLog;
+import org.exoplatform.services.jcr.datamodel.ItemData;
+import org.exoplatform.services.jcr.datamodel.NodeData;
+import org.exoplatform.services.jcr.datamodel.PropertyData;
+import org.exoplatform.services.jcr.datamodel.QPathEntry;
+import org.exoplatform.services.jcr.impl.Constants;
+import org.exoplatform.services.jcr.impl.core.NodeImpl;
+import org.exoplatform.services.jcr.impl.core.SessionImpl;
+import org.exoplatform.services.jcr.impl.core.lock.LockImpl;
+import org.exoplatform.services.jcr.impl.core.lock.LockManagerImpl;
+import org.exoplatform.services.jcr.impl.core.lock.LockPersister;
+import org.exoplatform.services.jcr.impl.core.lock.LockRemover;
+import org.exoplatform.services.jcr.impl.dataflow.TransientItemData;
+import org.exoplatform.services.jcr.impl.dataflow.TransientPropertyData;
+import org.exoplatform.services.jcr.impl.dataflow.persistent.WorkspacePersistentDataManager;
+import org.exoplatform.services.jcr.observation.ExtendedEvent;
+import org.exoplatform.services.jcr.util.IdGenerator;
+import org.exoplatform.services.log.ExoLogger;
+import org.exoplatform.services.log.Log;
+import org.jboss.cache.Cache;
+import org.jboss.cache.CacheFactory;
+import org.jboss.cache.DefaultCacheFactory;
+import org.jboss.cache.Fqn;
+import org.jboss.cache.Node;
+
+import java.io.Serializable;
+import java.math.BigInteger;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import javax.jcr.AccessDeniedException;
+import javax.jcr.RepositoryException;
+import javax.jcr.UnsupportedRepositoryOperationException;
+import javax.jcr.lock.Lock;
+import javax.jcr.lock.LockException;
+
+/**
+ * Created by The eXo Platform SAS.
+ * 
+ * <br/>Date: 
+ *
+ * @author <a href="karpenko.sergiy at gmail.com">Karpenko Sergiy</a> 
+ * @version $Id: CacheableLockManager.java 111 2008-11-11 11:11:11Z serg $
+ */
+ at Managed
+ at NameTemplate(@Property(key = "service", value = "lockmanager"))
+public class CacheableLockManager extends LockManagerImpl
+{
+   /**
+    * Default lock time out. 30min
+    */
+   public static final long DEFAULT_LOCK_TIMEOUT = 1000 * 60 * 30;
+
+   // Search constants
+   /**
+    * The exact lock token.
+    */
+   private static final int SEARCH_EXECMATCH = 1;
+
+   /**
+    * Lock token of closed parent
+    */
+   private static final int SEARCH_CLOSEDPARENT = 2;
+
+   /**
+    * Lock token of closed child
+    */
+   private static final int SEARCH_CLOSEDCHILD = 4;
+
+   /**
+    * Path to jbosscache-configuration
+    */
+   public static final String JBOSSCACHE_CONFIG = "jbosscache-configuration";
+
+   public static final String LOCK_DATA = "$LOCK_DATA";
+
+   public static final String LOCKS = "$LOCKS";
+
+   /**
+    * Logger
+    */
+   private final Log log = ExoLogger.getLogger("jcr.lock.LockManager");
+
+   /**
+    * Data manager.
+    */
+   private final DataManager dataManager;
+
+   /**
+    * Map NodeIdentifier -- lockData
+    */
+   private final Map<String, LockData> pendingLocks;
+
+   /**
+    * [session id , [lock tokens id..]]
+    */
+   private final Map<String, Set<String>> lockTokenHolders;
+
+   /**
+    * Run time lock time out.
+    */
+   private long lockTimeOut;
+
+   /**
+    * Lock remover thread.
+    */
+   private LockRemover lockRemover;
+
+   private Cache<Serializable, Object> cache;
+
+   private Node<Serializable, Object> lockRoot;
+
+   /**
+    * Constructor for workspace without LockPersister
+    * 
+    * @param dataManager
+    * @param config
+    */
+   public CacheableLockManager(WorkspacePersistentDataManager dataManager, WorkspaceEntry config)
+      throws RepositoryConfigurationException
+   {
+      this(dataManager, config, null);
+   }
+
+   public CacheableLockManager(WorkspacePersistentDataManager dataManager, WorkspaceEntry config,
+      LockPersister persister) throws RepositoryConfigurationException
+   {
+      super(dataManager, config, null);
+      this.dataManager = dataManager;
+      if (config.getLockManager() != null)
+      {
+         lockTimeOut =
+            config.getLockManager().getTimeout() > 0 ? config.getLockManager().getTimeout() : DEFAULT_LOCK_TIMEOUT;
+      }
+      else
+         lockTimeOut = DEFAULT_LOCK_TIMEOUT;
+
+      lockTokenHolders = new HashMap<String, Set<String>>();
+      pendingLocks = new HashMap<String, LockData>();
+
+      // dataManager.addItemPersistenceListener(this);
+
+      // make cache
+      if (config.getLockManager() != null && config.getLockManager().getCacheConfig() != null)
+      {
+         String pathToConfig = config.getLockManager().getCacheConfig();
+         CacheFactory<Serializable, Object> factory = new DefaultCacheFactory<Serializable, Object>();
+         cache = factory.createCache(pathToConfig, false);
+         cache.create();
+      }
+      else
+      {
+         throw new RepositoryConfigurationException("Cache configuration not found");
+      }
+   }
+
+   /*
+    * (non-Javadoc)
+    * @see
+    * org.exoplatform.services.jcr.impl.core.lock.LockManager#lockTokenAdded(org.exoplatform.services
+    * .jcr.impl.core.SessionImpl, java.lang.String)
+    */
+   public synchronized void addLockToken(String sessionId, String lt)
+   {
+      holdLockToken(sessionId, lt);
+   }
+
+   /*
+    * (non-Javadoc)
+    * @see
+    * org.exoplatform.services.jcr.impl.core.lock.LockManager#addPendingLock(org.exoplatform.services
+    * .jcr.impl.core.NodeImpl, boolean, boolean, long)
+    */
+   public synchronized Lock addPendingLock(NodeImpl node, boolean isDeep, boolean isSessionScoped, long timeOut)
+      throws LockException
+   {
+      LockData lData = getLockData((NodeData)node.getData(), SEARCH_EXECMATCH | SEARCH_CLOSEDPARENT);
+      if (lData != null)
+      {
+         if (lData.getNodeIdentifier().equals(node.getInternalIdentifier()))
+         {
+            throw new LockException("Node already locked: " + node.getData().getQPath());
+         }
+         else if (lData.isDeep())
+         {
+            throw new LockException("Parent node has deep lock.");
+         }
+      }
+
+      if (isDeep && getLockData((NodeData)node.getData(), SEARCH_CLOSEDCHILD) != null)
+      {
+         throw new LockException("Some child node is locked.");
+      }
+
+      String lockToken = IdGenerator.generate();
+      lData =
+         new LockData(node.getInternalIdentifier(), lockToken, isDeep, isSessionScoped, node.getSession().getUserID(),
+            timeOut > 0 ? timeOut : lockTimeOut);
+
+      holdLockToken(node.getSession().getId(), lockToken);
+      pendingLocks.put(node.getInternalIdentifier(), lData);
+
+      LockImpl lock = new CacheLockImpl(node.getSession(), lData, this);
+      return lock;
+   }
+
+   @Managed
+   @ManagedDescription("Remove the expired locks")
+   public void cleanExpiredLocks()
+   {
+      removeExpired();
+   }
+
+   /*
+    * (non-Javadoc)
+    * @see
+    * org.exoplatform.services.jcr.impl.core.lock.LockManager#getLock(org.exoplatform.services.jcr
+    * .impl.core.NodeImpl)
+    */
+   public LockImpl getLock(NodeImpl node) throws LockException, RepositoryException
+   {
+
+      LockData lData = getLockData((NodeData)node.getData(), SEARCH_EXECMATCH | SEARCH_CLOSEDPARENT);
+
+      if (lData == null || (!node.getInternalIdentifier().equals(lData.getNodeIdentifier()) && !lData.isDeep()))
+      {
+         throw new LockException("Node not locked: " + node.getData().getQPath());
+      }
+      return new CacheLockImpl(node.getSession(), lData, this);
+   }
+
+   /*
+    * (non-Javadoc)
+    * @see org.exoplatform.services.jcr.impl.core.lock.LockManager#getLockTokens(java.lang.String)
+    */
+   public synchronized String[] getLockTokens(String sessionID)
+   {
+      Set<String> lockTokens = lockTokenHolders.get(sessionID);
+
+      if (lockTokens != null)
+      {
+         String[] arr = new String[lockTokens.size()];
+         lockTokens.toArray(arr);
+         return arr;
+      }
+      else
+      {
+         return new String[0];
+      }
+   }
+
+   /**
+    * Returns real token, if session has it
+    * 
+    * @param lockData
+    * @return
+    */
+   //   public String getLockToken(LockData lockData)
+   //   {
+   //      return tokenHash.get(lockData.getTokenHash());
+   //   }
+
+   @Managed
+   @ManagedDescription("The number of active locks")
+   public int getNumLocks()
+   {
+      return lockRoot.getChildrenNames().size();
+   }
+
+   /*
+    * (non-Javadoc)
+    * @see
+    * org.exoplatform.services.jcr.impl.core.lock.LockManager#holdsLock(org.exoplatform.services.
+    * jcr.impl.core.NodeImpl)
+    */
+   public boolean holdsLock(NodeData node) throws RepositoryException
+   {
+      return getLockData(node, SEARCH_EXECMATCH) != null;
+   }
+
+   /*
+    * (non-Javadoc)
+    * @see
+    * org.exoplatform.services.jcr.impl.core.lock.LockManager#isLocked(org.exoplatform.services.jcr
+    * .datamodel.NodeData)
+    */
+   public boolean isLocked(NodeData node)
+   {
+      LockData lData = getLockData(node, SEARCH_EXECMATCH | SEARCH_CLOSEDPARENT);
+
+      if (lData == null || (!node.getIdentifier().equals(lData.getNodeIdentifier()) && !lData.isDeep()))
+      {
+         return false;
+      }
+      return true;
+   }
+
+   /*
+    * (non-Javadoc)
+    * @see
+    * org.exoplatform.services.jcr.impl.core.lock.LockManager#isLockHolder(org.exoplatform.services
+    * .jcr.impl.core.NodeImpl)
+    */
+   public boolean isLockHolder(NodeImpl node) throws RepositoryException
+   {
+      LockData lData = getLockData((NodeData)node.getData(), SEARCH_EXECMATCH | SEARCH_CLOSEDPARENT);
+      return lData != null && isLockHolder(node.getSession().getId(), lData.getToken());
+   }
+
+   /**
+    * {@inheritDoc}
+    */
+   public boolean isTXAware()
+   {
+      return true;
+   }
+
+   /*
+    * (non-Javadoc)
+    * @see
+    * org.exoplatform.services.jcr.core.SessionLifecycleListener#onCloseSession(org.exoplatform.services
+    * .jcr.core.ExtendedSession)
+    */
+   public synchronized void onCloseSession(ExtendedSession session)
+   {
+      SessionImpl sessionImpl = (SessionImpl)session;
+
+      for (LockData lockData : getLockList())
+      {
+         //TODO check is live or remove it
+         //if (lockData.isLive())
+         //{
+         // check is lock holder
+         if (lockTokenHolders.containsKey(session.getId())
+            && lockTokenHolders.get(session.getId()).contains(lockData.getToken()))
+         {
+            if (lockData.isSessionScoped())
+            {
+               // if no session currently holds lock except this
+               try
+               {
+                  ((NodeImpl)sessionImpl.getTransientNodesManager().getItemByIdentifier(lockData.getNodeIdentifier(),
+                     false)).unlock();
+               }
+               catch (UnsupportedRepositoryOperationException e)
+               {
+                  log.error(e.getLocalizedMessage());
+               }
+               catch (LockException e)
+               {
+                  log.error(e.getLocalizedMessage());
+               }
+               catch (AccessDeniedException e)
+               {
+                  log.error(e.getLocalizedMessage());
+               }
+               catch (RepositoryException e)
+               {
+                  log.error(e.getLocalizedMessage());
+               }
+
+            }
+            else
+            {
+               this.removeLockToken(session.getId(), lockData.getToken());
+            }
+         }
+         //TODO
+         //         }
+         //         else
+         //         {
+         //            entries.remove();
+         //         }
+      }
+
+   }
+
+   /*
+    * (non-Javadoc)
+    * @seeorg.exoplatform.services.jcr.dataflow.persistent.ItemsPersistenceListener#onSaveItems(org.
+    * exoplatform.services.jcr.dataflow.ItemStateChangesLog)
+    */
+   public void onSaveItems(ItemStateChangesLog changesLog)
+   {
+      List<PlainChangesLog> chengesLogList = new ArrayList<PlainChangesLog>();
+      if (changesLog instanceof TransactionChangesLog)
+      {
+         ChangesLogIterator logIterator = ((TransactionChangesLog)changesLog).getLogIterator();
+
+         while (logIterator.hasNextLog())
+         {
+            chengesLogList.add(logIterator.nextLog());
+         }
+      }
+      else if (changesLog instanceof PlainChangesLog)
+      {
+         chengesLogList.add((PlainChangesLog)changesLog);
+      }
+      else if (changesLog instanceof CompositeChangesLog)
+      {
+         for (ChangesLogIterator iter = ((CompositeChangesLog)changesLog).getLogIterator(); iter.hasNextLog();)
+         {
+            chengesLogList.add(iter.nextLog());
+         }
+      }
+
+      for (PlainChangesLog currChangesLog : chengesLogList)
+      {
+         String nodeIdentifier;
+         try
+         {
+            switch (currChangesLog.getEventType())
+            {
+               case ExtendedEvent.LOCK :
+                  if (currChangesLog.getSize() < 2)
+                  {
+                     log.error("Incorrect changes log  of type ExtendedEvent.LOCK size=" + currChangesLog.getSize()
+                        + "<2 \n" + currChangesLog.dump());
+                     break;
+                  }
+                  nodeIdentifier = currChangesLog.getAllStates().get(0).getData().getParentIdentifier();
+
+                  if (pendingLocks.containsKey(nodeIdentifier))
+                  {
+                     internalLock(nodeIdentifier);
+                  }
+                  else
+                  {
+                     throw new LockException("Lock must exist in pending locks.");
+                  }
+                  break;
+               case ExtendedEvent.UNLOCK :
+                  if (currChangesLog.getSize() < 2)
+                  {
+                     log.error("Incorrect changes log  of type ExtendedEvent.UNLOCK size=" + currChangesLog.getSize()
+                        + "<2 \n" + currChangesLog.dump());
+                     break;
+                  }
+
+                  internalUnLock(currChangesLog.getSessionId(), currChangesLog.getAllStates().get(0).getData()
+                     .getParentIdentifier());
+                  break;
+               default :
+                  HashSet<String> removedLock = new HashSet<String>();
+                  for (ItemState itemState : currChangesLog.getAllStates())
+                  {
+                     // this is a node and node is locked
+                     if (itemState.getData().isNode() && lockExist(itemState.getData().getIdentifier()))
+                     {
+                        nodeIdentifier = itemState.getData().getIdentifier();
+                        if (itemState.isDeleted())
+                        {
+                           removedLock.add(nodeIdentifier);
+                        }
+                        else if (itemState.isAdded() || itemState.isRenamed() || itemState.isUpdated())
+                        {
+                           removedLock.remove(nodeIdentifier);
+                        }
+                     }
+                  }
+                  for (String identifier : removedLock)
+                  {
+                     internalUnLock(currChangesLog.getSessionId(), identifier);
+                  }
+                  break;
+            }
+         }
+         catch (LockException e)
+         {
+            log.error(e.getLocalizedMessage(), e);
+         }
+         catch (IllegalStateException e)
+         {
+            log.error(e.getLocalizedMessage(), e);
+         }
+      }
+   }
+
+   /**
+    * Refreshed lock data in cache
+    * 
+    * @param newLockData
+    */
+   public void refresh(LockData newLockData) throws LockException
+   {
+      //first look pending locks
+      if (pendingLocks.containsKey(newLockData.getNodeIdentifier()))
+      {
+         pendingLocks.put(newLockData.getNodeIdentifier(), newLockData);
+      }
+      else
+      {
+         Fqn<String> id = Fqn.fromString(newLockData.getNodeIdentifier());
+         if (lockRoot.hasChild(id))
+         {
+            lockRoot.addChild(id);
+         }
+         else
+         {
+            throw new LockException("Can't refresh lock for node " + newLockData.getNodeIdentifier()
+               + " since lock is not exist");
+         }
+      }
+   }
+
+   /**
+    * Remove expired locks. Used from LockRemover.
+    */
+   public synchronized void removeExpired()
+   {
+      final List<String> removeLockList = new ArrayList<String>();
+
+      for (LockData lock : getLockList())
+      {
+         if (!lock.isSessionScoped() && lock.getTimeToDeath() < 0)
+         {
+            removeLockList.add(lock.getNodeIdentifier());
+         }
+      }
+
+      for (String rLock : removeLockList)
+      {
+         removeLock(rLock);
+      }
+   }
+
+   /*
+    * (non-Javadoc)
+    * @see
+    * org.exoplatform.services.jcr.impl.core.lock.LockManager#lockTokenRemoved(org.exoplatform.services
+    * .jcr.impl.core.SessionImpl, java.lang.String)
+    */
+   public synchronized void removeLockToken(String sessionId, String lt)
+   {
+      if (lockTokenHolders.containsKey(sessionId) && lockTokenHolders.get(sessionId).contains(lt))
+      {
+         lockTokenHolders.get(sessionId).remove(lt);
+         if (lockTokenHolders.get(sessionId).size() == 0)
+         {
+            lockTokenHolders.remove(sessionId);
+         }
+      }
+   }
+
+   /*
+    * (non-Javadoc)
+    * @see org.picocontainer.Startable#start()
+    */
+   public void start()
+   {
+      cache.start();
+      //tokenRoot = cache.getRoot().addChild(Fqn.fromString(TOKENS));
+      lockRoot = cache.getRoot().addChild(Fqn.fromString(LOCKS));
+      lockRoot.setResident(true);
+      lockRemover = new LockRemover(this);
+   }
+
+   /*
+    * (non-Javadoc)
+    * @see org.picocontainer.Startable#stop()
+    */
+   public void stop()
+   {
+      lockRemover.halt();
+      lockRemover.interrupt();
+      pendingLocks.clear();
+
+      // clear lockTokenHolders
+      for (String sessionId : lockTokenHolders.keySet())
+      {
+         lockTokenHolders.get(sessionId).clear();
+      }
+      lockTokenHolders.clear();
+
+      cache.stop();
+   }
+
+   /**
+    * Copy <code>PropertyData prop<code> to new TransientItemData
+    * 
+    * @param prop
+    * @return
+    * @throws RepositoryException
+    */
+   private TransientItemData copyItemData(PropertyData prop) throws RepositoryException
+   {
+
+      if (prop == null)
+         return null;
+
+      // make a copy, value may be null for deleting items
+      TransientPropertyData newData =
+         new TransientPropertyData(prop.getQPath(), prop.getIdentifier(), prop.getPersistedVersion(), prop.getType(),
+            prop.getParentIdentifier(), prop.isMultiValued(), prop.getValues());
+
+      return newData;
+   }
+
+   /**
+    * Calculates md5 hash of string.
+    * 
+    * @param token
+    * @return
+    */
+   private String getHash(String token)
+   {
+      String hash = "";
+      try
+      {
+         MessageDigest m = MessageDigest.getInstance("MD5");
+         m.update(token.getBytes(), 0, token.length());
+         hash = new BigInteger(1, m.digest()).toString(16);
+      }
+      catch (NoSuchAlgorithmException e)
+      {
+         log.error("Can't get instanse of MD5 MessageDigest!", e);
+      }
+      return hash;
+   }
+
+   /**
+    * Search lock in maps.
+    * 
+    * @param data
+    * @param searchType
+    * @return
+    */
+   private LockData getLockData(NodeData data, int searchType)
+   {
+      if (data == null || getNumLocks() == 0)
+         return null;
+      LockData retval = null;
+      try
+      {
+         if ((searchType & SEARCH_EXECMATCH) != 0)
+         {
+            retval = getLockDataById(data.getIdentifier());
+         }
+         if (retval == null && (searchType & SEARCH_CLOSEDPARENT) != 0)
+         {
+
+            NodeData parentData = (NodeData)dataManager.getItemData(data.getParentIdentifier());
+            if (parentData != null)
+            {
+               retval = getLockDataById(parentData.getIdentifier());
+               // parent not found try to fo upper
+               if (retval == null)
+               {
+                  retval = getLockData(parentData, SEARCH_CLOSEDPARENT);
+               }
+            }
+         }
+         if (retval == null && (searchType & SEARCH_CLOSEDCHILD) != 0)
+         {
+
+            List<NodeData> childData = dataManager.getChildNodesData(data);
+            for (NodeData nodeData : childData)
+            {
+               retval = getLockDataById(nodeData.getIdentifier());
+               if (retval != null)
+                  break;
+            }
+            if (retval == null)
+            {
+               // child not found try to find diper
+               for (NodeData nodeData : childData)
+               {
+                  retval = getLockData(nodeData, SEARCH_CLOSEDCHILD);
+                  if (retval != null)
+                     break;
+               }
+            }
+         }
+      }
+      catch (RepositoryException e)
+      {
+         return null;
+      }
+
+      return retval;
+   }
+
+   private void holdLockToken(String sessionId, String lt)
+   {
+      if (lockTokenHolders.get(sessionId) == null)
+      {
+         lockTokenHolders.put(sessionId, new HashSet<String>());
+      }
+      lockTokenHolders.get(sessionId).add(lt);
+   }
+
+   /**
+    * Internal lock
+    * 
+    * @param nodeIdentifier
+    * @throws LockException
+    */
+   private synchronized void internalLock(String nodeIdentifier) throws LockException
+   {
+      LockData lockData = pendingLocks.get(nodeIdentifier);
+      if (lockData != null)
+      {
+         Fqn<String> lockPath = Fqn.fromString(lockData.getNodeIdentifier());
+
+         // addChild will add if absent or return old if present
+         Node<Serializable, Object> node = lockRoot.addChild(lockPath);
+
+         // this will prevent from deleting by eviction.
+         node.setResident(true);
+
+         // this will return null if success. And old data if something exists...
+         LockData oldLockData = (LockData)node.putIfAbsent(LOCK_DATA, lockData);
+
+         if (oldLockData != null)
+         {
+            throw new LockException("Unable to write LockData. Node [" + lockData.getNodeIdentifier()
+               + "] already has LockData!");
+         }
+         pendingLocks.remove(nodeIdentifier);
+      }
+      else
+      {
+         throw new LockException("No lock in pending locks");
+      }
+   }
+
+   /**
+    * For locks comes from remote JCRs (replication usecase)
+    * 
+    * @param sessionId
+    *          String
+    * @param nodeIdentifier
+    *          String
+    * @param lockToken
+    *          String
+    * @param isDeep
+    *          boolean
+    * @param sessionScoped
+    *          boolean
+    * @param owner
+    *          String
+    * @return LockData
+    */
+   //   private synchronized LockData createRemoteLock(String sessionId, String nodeIdentifier, String lockToken,
+   //      boolean isDeep, boolean sessionScoped, String owner)
+   //   {
+   //      LockData lData = new LockData(nodeIdentifier, lockToken, isDeep, sessionScoped, owner, lockTimeOut);
+   //      lData.addLockHolder(sessionId);
+   //      locks.put(nodeIdentifier, lData);
+   //      tokensMap.put(lockToken, lData);
+   //
+   //      return lData;
+   //   }
+
+   /**
+    * Internal unlock.
+    * 
+    * @param sessionId
+    * @param nodeIdentifier
+    * @throws LockException
+    */
+   private synchronized void internalUnLock(String sessionId, String nodeIdentifier) throws LockException
+   {
+      LockData lData = getLockDataById(nodeIdentifier);
+
+      if (lData != null)
+      {
+         //tokenRoot.removeChild(Fqn.fromString(lData.getToken()));
+         lockRoot.removeChild(Fqn.fromString(nodeIdentifier));
+
+         // remove session holder
+         if (lockTokenHolders.containsKey(sessionId))
+         {
+            lockTokenHolders.get(sessionId).remove(lData.getToken());
+            if (lockTokenHolders.get(sessionId).size() == 0)
+            {
+               lockTokenHolders.remove(sessionId);
+            }
+         }
+      }
+   }
+
+   private boolean isLockHolder(String sessionId, String lockToken)
+   {
+      Set<String> lockTokens = lockTokenHolders.get(sessionId);
+      if (lockTokens != null)
+      {
+         return lockTokens.contains(lockToken);
+      }
+      return false;
+   }
+
+   private boolean lockExist(String nodeId)
+   {
+      return lockRoot.hasChild(Fqn.fromString(nodeId));
+   }
+
+   protected LockData getLockDataById(String nodeId)
+   {
+      LockData lockData = null;
+      Node<Serializable, Object> node = lockRoot.getChild(Fqn.fromString(nodeId));
+      if (node != null)
+      {
+         lockData = (LockData)node.get(LOCK_DATA);
+      }
+      return lockData;
+   }
+
+   protected synchronized List<LockData> getLockList()
+   {
+      Set<Node<Serializable, Object>> lockSet = lockRoot.getChildren();
+
+      List<LockData> locksData = new ArrayList<LockData>();
+      for (Node<Serializable, Object> node : lockSet)
+      {
+         if (node != null)
+         {
+            LockData lockData = (LockData)node.get(LOCK_DATA);
+            if (lockData != null)
+            {
+               locksData.add(lockData);
+            }
+         }
+      }
+      return locksData;
+   }
+
+   /**
+    * Remove lock, used by Lock remover.
+    * 
+    * @param nodeIdentifier String
+    */
+   protected void removeLock(String nodeIdentifier)
+   {
+      try
+      {
+         NodeData nData = (NodeData)dataManager.getItemData(nodeIdentifier);
+         PlainChangesLog changesLog =
+            new PlainChangesLogImpl(new ArrayList<ItemState>(), SystemIdentity.SYSTEM, ExtendedEvent.UNLOCK);
+
+         ItemData lockOwner =
+            copyItemData((PropertyData)dataManager.getItemData(nData, new QPathEntry(Constants.JCR_LOCKOWNER, 1)));
+
+         changesLog.add(ItemState.createDeletedState(lockOwner));
+
+         ItemData lockIsDeep =
+            copyItemData((PropertyData)dataManager.getItemData(nData, new QPathEntry(Constants.JCR_LOCKISDEEP, 1)));
+         changesLog.add(ItemState.createDeletedState(lockIsDeep));
+
+         // lock probably removed by other thread
+         if (lockOwner == null && lockIsDeep == null)
+            return;
+         dataManager.save(new TransactionChangesLog(changesLog));
+
+      }
+      catch (RepositoryException e)
+      {
+         log.error("Error occur during removing lock" + e.getLocalizedMessage(), e);
+      }
+   }
+
+   public String getLockToken(String sessionId, LockData lockData)
+   {
+      if (lockTokenHolders.containsKey(sessionId) && lockTokenHolders.get(sessionId).contains(lockData.getToken()))
+      {
+         return lockData.getToken();
+      }
+      else
+      {
+         return null;
+      }
+   }
+}

Added: jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/lock/jbosscache/LockData.java
===================================================================
--- jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/lock/jbosscache/LockData.java	                        (rev 0)
+++ jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/lock/jbosscache/LockData.java	2010-01-15 13:00:59 UTC (rev 1406)
@@ -0,0 +1,243 @@
+/*
+ * 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 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.exoplatform.services.jcr.impl.core.lock.cache;
+
+import org.exoplatform.services.jcr.impl.Constants;
+
+import java.io.Externalizable;
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+
+/**
+ * Created by The eXo Platform SAS.
+ * 
+ * @author <a href="mailto:gennady.azarenkov at exoplatform.com">Gennady Azarenkov</a>
+ * @version $Id: LockData.java 787 2009-11-20 11:36:15Z nzamosenchuk $
+ */
+
+public class LockData implements Externalizable //extends org.exoplatform.services.jcr.impl.core.lock.LockData
+{
+   /**
+    * The time of birth. From this time we start count the time of death. death = birthday+TIME_OUT;
+    */
+   private long birthday;
+
+   /**
+    * If isDeep is true then the lock applies to this node and all its descendant nodes; if false,
+    * the lock applies only to this, the holding node.
+    */
+   private boolean deep;
+
+   /**
+    * A lock token is a string that uniquely identifies a particular lock and acts as a “key”
+    * allowing a user to alter a locked node. LockData stores only token hash.
+    */
+   private String token;
+
+   /**
+    * Identifier of locked node.
+    */
+   private String nodeIdentifier;
+
+   /**
+    * The owner of the locked node.
+    */
+   private String owner;
+
+   /**
+    * If isSessionScoped is true then this lock will expire upon the expiration of the current
+    * session (either through an automatic or explicit Session.logout); if false, this lock does not
+    * expire until explicitly unlocked or automatically unlocked due to a implementation-specific
+    * limitation, such as a timeout.
+    */
+   private boolean sessionScoped;
+
+   /**
+    * <B>8.4.9 Timing Out</B> An implementation may unlock any lock at any time due to
+    * implementation-specific criteria, such as time limits on locks.
+    */
+   private long timeOut;
+
+   // Need for Externalizable
+   public LockData()
+   {
+      this.sessionScoped = false;
+      this.deep = false;
+   }
+
+   /**
+    * @param nodeIdentifier
+    * @param lockToken
+    * @param deep
+    * @param sessionScoped
+    * @param owner
+    * @param timeOut
+    *       is seconds!
+    */
+   public LockData(String nodeIdentifier, String lockToken, boolean deep, boolean sessionScoped, String owner,
+      long timeOut)
+   {
+      this.nodeIdentifier = nodeIdentifier;
+      this.token = lockToken;
+      this.deep = deep;
+      this.sessionScoped = sessionScoped;
+      this.owner = owner;
+      this.timeOut = timeOut;
+      this.birthday = System.currentTimeMillis() / 1000;
+   }
+
+   /*
+    * (non-Javadoc)
+    * @see java.lang.Object#equals(java.lang.Object)
+    */
+   @Override
+   public boolean equals(Object obj)
+   {
+      if (super.equals(obj))
+      {
+         return true;
+      }
+      if (obj instanceof LockData)
+      {
+         return hashCode() == obj.hashCode();
+      }
+      return false;
+   }
+
+   /**
+    * @return the nodeIdentifier
+    */
+   public String getNodeIdentifier()
+   {
+      return nodeIdentifier;
+   }
+
+   /**
+    * @return
+    */
+   public String getOwner()
+   {
+      return owner;
+   }
+
+   /**
+    * @return The time to death in millis
+    */
+   public long getTimeToDeath()
+   {
+      return birthday + timeOut - System.currentTimeMillis() / 1000;
+   }
+
+   public String getToken()
+   {
+      return token;
+   }
+
+   /*
+    * (non-Javadoc)
+    * @see java.lang.Object#hashCode()
+    */
+   @Override
+   public int hashCode()
+   {
+      return token.hashCode();
+   }
+
+   public boolean isDeep()
+   {
+      return deep;
+   }
+
+   /**
+    * @return
+    */
+   public boolean isSessionScoped()
+   {
+      return sessionScoped;
+   }
+
+   /**
+    * @see java.io.Externalizable#readExternal(java.io.ObjectInput)
+    */
+   public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
+   {
+      // read boolean
+      this.deep = in.readBoolean();
+      this.sessionScoped = in.readBoolean();
+      // read long
+      this.birthday = in.readLong();
+      this.timeOut = in.readLong();
+      //read strings
+      // read uuid
+      byte[] buf;
+      buf = new byte[in.readInt()];
+      in.readFully(buf);
+      this.nodeIdentifier = new String(buf, Constants.DEFAULT_ENCODING);
+      // read owner
+      buf = new byte[in.readInt()];
+      in.readFully(buf);
+      this.owner = new String(buf, Constants.DEFAULT_ENCODING);
+      // read token
+      buf = new byte[in.readInt()];
+      in.readFully(buf);
+      this.token = new String(buf, Constants.DEFAULT_ENCODING);
+   }
+
+   /**
+    * @see java.io.Externalizable#writeExternal(java.io.ObjectOutput)
+    */
+   public void writeExternal(ObjectOutput out) throws IOException
+   {
+      // write boolean
+      out.writeBoolean(deep);
+      out.writeBoolean(sessionScoped);
+      // write long
+      out.writeLong(birthday);
+      out.writeLong(timeOut);
+      // write string
+      // node uuid
+      byte[] ptbuf = nodeIdentifier.getBytes(Constants.DEFAULT_ENCODING);
+      out.writeInt(ptbuf.length);
+      out.write(ptbuf);
+      // node owner
+      ptbuf = owner.getBytes(Constants.DEFAULT_ENCODING);
+      out.writeInt(ptbuf.length);
+      out.write(ptbuf);
+      // node token
+      ptbuf = token.getBytes(Constants.DEFAULT_ENCODING);
+      out.writeInt(ptbuf.length);
+      out.write(ptbuf);
+
+   }
+
+   /**
+    * @return
+    */
+   protected long getTimeOut()
+   {
+      return timeOut;
+   }
+
+   public void setTimeOut(long timeOut)
+   {
+      this.timeOut = timeOut;
+   }
+
+}

Modified: jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/test/resources/conf/standalone/test-jbosscache-lockconfig.xml
===================================================================
--- jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/test/resources/conf/standalone/test-jbosscache-lockconfig.xml	2010-01-15 12:48:11 UTC (rev 1405)
+++ jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/test/resources/conf/standalone/test-jbosscache-lockconfig.xml	2010-01-15 13:00:59 UTC (rev 1406)
@@ -3,10 +3,4 @@
 
    <locking useLockStriping="false" concurrencyLevel="50000" lockParentForChildInsertRemove="false" lockAcquisitionTimeout="3600000"/>
    
-   <!-- Configure the TransactionManager -->
-   <transaction transactionManagerLookupClass="org.jboss.cache.transaction.JBossStandaloneJTAManagerLookup" />
-
-   <!-- Enable batching -->
-   <invocationBatching enabled="true" />
-
 </jbosscache>



More information about the exo-jcr-commits mailing list