[jboss-cvs] JBossCache/src/org/jboss/cache ...

Manik Surtani msurtani at jboss.com
Thu Oct 12 19:03:57 EDT 2006


  User: msurtani
  Date: 06/10/12 19:03:57

  Modified:    src/org/jboss/cache           TreeCache.java Region.java
                        Cache.java TreeCacheProxyImpl.java
                        TreeCacheView.java CacheSPI.java Fqn.java
                        RegionImpl.java
  Added:       src/org/jboss/cache           RegionManager.java
  Removed:     src/org/jboss/cache           TreeCacheMBean.java
  Log:
  a LOT of changes around Regions, in an attempt to unify eviction and marshalling regions
  
  Revision  Changes    Path
  1.252     +22 -202   JBossCache/src/org/jboss/cache/TreeCache.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: TreeCache.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/TreeCache.java,v
  retrieving revision 1.251
  retrieving revision 1.252
  diff -u -b -r1.251 -r1.252
  --- TreeCache.java	11 Oct 2006 13:20:26 -0000	1.251
  +++ TreeCache.java	12 Oct 2006 23:03:57 -0000	1.252
  @@ -31,11 +31,8 @@
   import org.jboss.cache.marshall.MethodCall;
   import org.jboss.cache.marshall.MethodCallFactory;
   import org.jboss.cache.marshall.MethodDeclarations;
  -import org.jboss.cache.marshall.Region;
  -import org.jboss.cache.marshall.RegionManager;
   import org.jboss.cache.marshall.RegionNameConflictException;
   import org.jboss.cache.marshall.RegionNotFoundException;
  -import org.jboss.cache.marshall.TreeCacheMarshaller;
   import org.jboss.cache.marshall.VersionAwareMarshaller;
   import org.jboss.cache.notifications.Notifier;
   import org.jboss.cache.optimistic.DataVersion;
  @@ -94,7 +91,7 @@
    * @author <a href="mailto:manik at jboss.org">Manik Surtani (manik at jboss.org)</a>
    * @author Brian Stansberry
    * @author Daniel Huang (dhuang at jboss.org)
  - * @version $Id: TreeCache.java,v 1.251 2006/10/11 13:20:26 msurtani Exp $
  + * @version $Id: TreeCache.java,v 1.252 2006/10/12 23:03:57 msurtani Exp $
    *          <p/>
    * @see <a href="http://labs.jboss.com/portal/jbosscache/docs">JBossCache doc</a>
    */
  @@ -111,10 +108,8 @@
       */
      protected DataNode root = NodeFactory.getInstance().createRootDataNode(NodeFactory.NODE_TYPE_TREENODE, this);
   
  -   /**
  -    * A registry of regions that have been defined.
  -    */
  -   Map<Fqn, org.jboss.cache.Region> regionsRegistry = new HashMap<Fqn, org.jboss.cache.Region>();
  +   private RegionManager regionManager = new RegionManager();
  +
   
      final static Object NULL = new Object();
   
  @@ -185,12 +180,7 @@
      protected VersionAwareMarshaller marshaller_ = null;
   
      /**
  -    * RegionManager used by marshaller
  -    */
  -   protected RegionManager regionManager_ = null;
  -
  -   /**
  -    * RegionManager used by cache eviction
  +    * MarshRegionManager used by cache eviction
       */
      protected org.jboss.cache.eviction.RegionManager evictionRegionManager_ = null;
   
  @@ -699,6 +689,8 @@
            }
         }
   
  +      regionManager.setDefaultInactive(configuration.isInactiveOnStartup());
  +
         useCreateService = true;
      }
   
  @@ -974,7 +966,7 @@
      /**
       * Registers a specific classloader for a region defined by a fully
       * qualified name.
  -    * A instance of {@link TreeCacheMarshaller} is used for marshalling.
  +    * A instance of {@link org.jboss.cache.marshall.Marshaller} is used for marshalling.
       *
       * @param fqn The fqn region. Children of this fqn will use this classloader for (un)marshalling.
       * @param cl  The class loader to use
  @@ -982,17 +974,9 @@
       * @throws IllegalStateException       if marshalling is not being used
       * @see #getMarshaller
       */
  -   public void registerClassLoader(String fqn, ClassLoader cl)
  -           throws RegionNameConflictException
  +   public void registerClassLoader(String fqn, ClassLoader cl) throws RegionNameConflictException
      {
  -      if (!configuration.isUseRegionBasedMarshalling())
  -      {
  -         throw new IllegalStateException("useRegionBasedMarshalling is false; cannot use this method");
  -      }
  -
  -      // Use the getter method here, as it will create the marshaller
  -      // if this method is called before we do it in create()
  -      getMarshaller().registerClassLoader(fqn, cl);
  +      regionManager.registerClassLoader(Fqn.fromString(fqn), cl);
      }
   
      /**
  @@ -1004,14 +988,7 @@
       */
      public void unregisterClassLoader(String fqn) throws RegionNotFoundException
      {
  -      if (!configuration.isUseRegionBasedMarshalling())
  -      {
  -         throw new IllegalStateException("useRegionBasedMarshalling is false; cannot use this method");
  -      }
  -
  -      // Use the getter method here, as it will create the marshaller
  -      // if this method is called before we do it in create()
  -      getMarshaller().unregisterClassLoader(fqn);
  +      regionManager.unregisterClassLoader(Fqn.fromString(fqn));
      }
   
      /**
  @@ -1029,24 +1006,16 @@
       *                   portion of the tree that should be activated.
       * @throws RegionNotEmptyException if the node <code>subtreeFqn</code>
       *                                 exists and has either data or children
  -    * @throws IllegalStateException   if {@link Configuration#useRegionBasedMarshalling} is <code>false</code>
       */
  -   public void activateRegion(String subtreeFqn)
  -           throws RegionNotEmptyException, RegionNameConflictException, CacheException
  -   {
  -      if (!configuration.isUseRegionBasedMarshalling())
  +   public void activateRegion(String subtreeFqn) throws RegionNameConflictException, CacheException
         {
  -         throw new IllegalStateException("TreeCache.activateRegion(). useRegionBasedMarshalling flag is not set!");
  -      }
  -
         Fqn fqn = Fqn.fromString(subtreeFqn);
   
         // Check whether the node already exists and has data
         DataNode subtreeRoot = findNode(fqn);
         if (!(isNodeEmpty(subtreeRoot)))
         {
  -         throw new RegionNotEmptyException("Node " + subtreeRoot.getFqn() +
  -                 " already exists and is not empty");
  +         throw new RegionNotEmptyException("Node " + subtreeRoot.getFqn() + " already exists and is not empty");
         }
   
         if (log.isDebugEnabled())
  @@ -1061,17 +1030,7 @@
            // so calls to _getState for the fqn can return quickly
            activationChangeNodes.add(fqn);
   
  -         // Start accepting messages for the subtree, but
  -         // queue them for later processing.  We do this early
  -         // to reduce the chance of discarding a prepare call
  -         // whose corresponding commit will thus fail after activation
  -         Region region = regionManager_.getRegion(fqn);
  -         if (region == null)
  -         {
  -            region = regionManager_.createRegion(fqn, null, true);
  -         }
  -
  -         region.startQueuing();
  +         Region region = regionManager.getRegion(fqn, true);
   
            // If a classloader is registered for the node's region, use it
            ClassLoader cl = region.getClassLoader();
  @@ -1111,14 +1070,7 @@
               }
            }
   
  -         // Lock out other activity on the region while we
  -         // we process the queue and activate the region
  -         List queue = region.getMethodCallQueue();
  -         synchronized (queue)
  -         {
  -            processQueuedMethodCalls(queue);
               region.activate();
  -         }
   
         }
         catch (Throwable t)
  @@ -1288,7 +1240,7 @@
            boolean inactive = marshaller_.isInactive(subtreeFqn);
            if (!inactive)
            {
  -            marshaller_.inactivate(subtreeFqn);
  +            regionManager.inactivate(subtreeFqn);
            }
   
            // Create a list with the Fqn in the main tree and any buddy backup trees
  @@ -1430,123 +1382,6 @@
      }
   
      /**
  -    * Called internally to enqueue a method call.
  -    *
  -    * @param subtree FQN of the subtree region
  -    * @see Region#getMethodCallQueue
  -    */
  -   public void _enqueueMethodCall(String subtree, MethodCall call)
  -           throws Throwable
  -   {
  -      MethodCall jbcCall = (MethodCall) call;
  -      Region region = regionManager_.getRegion(subtree);
  -      if (region == null)
  -      {
  -         throw new IllegalStateException("No region found for " + subtree);
  -      }
  -
  -      List queue = region.getMethodCallQueue();
  -      synchronized (queue)
  -      {
  -         // Confirm we're not active yet; if we are just invoke the method
  -         switch (region.getStatus())
  -         {
  -            case(Region.STATUS_ACTIVE):
  -               if (log.isTraceEnabled())
  -               {
  -                  log.trace("_enqueueMethodCall(): Invoking " + call.getName() +
  -                          " on subtree " + subtree);
  -               }
  -               call.invoke(this);
  -               break;
  -
  -            case(Region.STATUS_QUEUEING):
  -
  -               // Don't bother queueing a getState call
  -
  -               if (jbcCall.getMethodId() == MethodDeclarations.replicateMethod_id)
  -               {
  -                  MethodCall mc = (MethodCall) call.getArgs()[0];
  -                  if (mc.getMethodId() == MethodDeclarations.getPartialStateMethod_id)
  -                  {
  -                     return;
  -                  }
  -               }
  -               if (log.isTraceEnabled())
  -               {
  -                  log.trace("_enqueueMethodCall(): Enqueuing " + call.getName() +
  -                          " " + call.getArgs() + " on subtree " + subtree);
  -               }
  -               queue.add(jbcCall);
  -               break;
  -
  -            default:
  -               log.trace("_enqueueMethodCall(): Discarding " + call.getName() +
  -                       " on subtree " + subtree);
  -         }
  -      }
  -   }
  -
  -   private void processQueuedMethodCalls(List queue) throws Throwable
  -   {
  -      Map gtxMap = new HashMap();
  -      MethodCall call = null;
  -      MethodCall wrapped = null;
  -      for (Iterator iter = queue.iterator(); iter.hasNext();)
  -      {
  -         call = (MethodCall) iter.next();
  -         boolean forgive = false;
  -         if (call.getMethodId() == MethodDeclarations.replicateMethod_id)
  -         {
  -            Object[] args = call.getArgs();
  -            wrapped = (MethodCall) args[0];
  -            switch (wrapped.getMethodId())
  -            {
  -               case MethodDeclarations.prepareMethod_id:
  -                  args = wrapped.getArgs();
  -                  gtxMap.put(args[0], NULL);
  -                  break;
  -               case MethodDeclarations.commitMethod_id:
  -               case MethodDeclarations.rollbackMethod_id:
  -                  args = wrapped.getArgs();
  -                  // If we didn't see the prepare earlier, we'll forgive
  -                  // any error when we invoke the commit/rollback
  -                  // TODO maybe just skip the commit/rollback?
  -//                  forgive = (gtxMap.remove(args[0]) == null);
  -                  if (gtxMap.remove(args[0]) == null)
  -                  {
  -                     continue;
  -                  }
  -                  break;
  -            }
  -         }
  -
  -         if (log.isTraceEnabled())
  -         {
  -            log.trace("processing queued method call " + call.getName());
  -         }
  -
  -         try
  -         {
  -            call.invoke(this);
  -         }
  -         catch (Exception e)
  -         {
  -
  -            if (!forgive)
  -            {
  -               throw e;
  -            }
  -         }
  -         finally
  -         {
  -            // Clear any invocation context from this thread
  -            getInvocationContext().reset();
  -         }
  -      }
  -   }
  -
  -   /**
       * Returns the state for the portion of the tree named by <code>fqn</code>.
       * <p/>
       * State returned is a serialized byte[][], element 0 is the transient state
  @@ -1632,22 +1467,6 @@
         return result;
      }
   
  -   /**
  -    * Method provided to JGroups by
  -    * {@link TreeCacheMarshaller#objectFromByteBuffer(byte[])} when
  -    * it receives a replication event for an Fqn that has been marked
  -    * as inactive.  Currently a no-op.
  -    * <p/>
  -    * inactivate(Fqn)
  -    */
  -   public void notifyCallForInactiveSubtree(String fqn)
  -   {
  -      // do nothing
  -      //if (log.isTraceEnabled())
  -      //   log.trace(getLocalAddress() + " -- received call for inactive fqn " + fqn);
  -   }
  -
  -
      protected void fetchStateOnStartup() throws Exception
      {
         long start, stop;
  @@ -4168,18 +3987,19 @@
         return toReturn;
      }
   
  -   /**
  -    * Returns the region manager for this TreeCache.
  -    */
      public RegionManager getRegionManager()
      {
  -      if (regionManager_ == null)
  +      if (regionManager == null)
         {
  -         regionManager_ = new RegionManager();
  +         synchronized (this)
  +         {
  +            if (regionManager == null) regionManager = new RegionManager();
  +         }
         }
  -      return regionManager_;
  +      return regionManager;
      }
   
  +
      /**
       * Returns the eviction region manager for this TreeCache.
       */
  
  
  
  1.4       +8 -7      JBossCache/src/org/jboss/cache/Region.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: Region.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/Region.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -b -r1.3 -r1.4
  --- Region.java	30 Aug 2006 19:09:56 -0000	1.3
  +++ Region.java	12 Oct 2006 23:03:57 -0000	1.4
  @@ -7,38 +7,39 @@
   package org.jboss.cache;
   
   import org.jboss.cache.eviction.EvictionPolicyConfig;
  -import org.jboss.cache.marshall.RegionNameConflictException;
  -import org.jboss.cache.marshall.RegionNotFoundException;
   
   /**
    * Represents a section of the cache, and characteristics such as class loading and activaton can be applied to regions.
    *
    * @author <a href="mailto:manik at jboss.org">Manik Surtani (manik at jboss.org)</a>
    */
  -public interface Region
  +public interface Region extends Comparable
   {
      /**
       * Registers a specific {@link ClassLoader} (rather than the default) for a region, represented by a {@link Fqn}.
       *
       * @param classLoader
       */
  -   void registerContextClassLoader(ClassLoader classLoader) throws RegionNameConflictException;
  +   void registerContextClassLoader(ClassLoader classLoader);
   
      /**
       * Unregisters any specific {@link ClassLoader}s from a region.
       */
  -   void unregisterContextClassLoader() throws RegionNotFoundException;
  +   void unregisterContextClassLoader();
   
      /**
       * Activates a region for replication (by default, the entire cache is activated)
       */
  -   void activate() throws RegionNameConflictException;
  +   void activate();
   
      /**
       * Deactivates a region from being replicated.
       */
  -   void deactivate() throws RegionNameConflictException;
  +   void deactivate();
   
  +   /**
  +    * @return true if this region has been activated.
  +    */
      boolean isActive();
   
      /**
  
  
  
  1.10      +1 -1      JBossCache/src/org/jboss/cache/Cache.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: Cache.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/Cache.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -b -r1.9 -r1.10
  --- Cache.java	10 Oct 2006 13:51:42 -0000	1.9
  +++ Cache.java	12 Oct 2006 23:03:57 -0000	1.10
  @@ -142,7 +142,7 @@
       *
       * @param fqn            Fqn that is contained in a region.
       * @param createIfAbsent If true, will create a new associated region if not found.
  -    * @return a Region. Null if none is found.
  +    * @return a MarshRegion. Null if none is found.
       * @throws UnsupportedOperationException if the region cannot be defined.
       * @see Region
       */
  
  
  
  1.42      +3 -26     JBossCache/src/org/jboss/cache/TreeCacheProxyImpl.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: TreeCacheProxyImpl.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/TreeCacheProxyImpl.java,v
  retrieving revision 1.41
  retrieving revision 1.42
  diff -u -b -r1.41 -r1.42
  --- TreeCacheProxyImpl.java	10 Oct 2006 13:51:42 -0000	1.41
  +++ TreeCacheProxyImpl.java	12 Oct 2006 23:03:57 -0000	1.42
  @@ -9,7 +9,6 @@
   import org.apache.commons.logging.LogFactory;
   import org.jboss.cache.buddyreplication.BuddyManager;
   import org.jboss.cache.config.Configuration;
  -import org.jboss.cache.eviction.RegionManager;
   import org.jboss.cache.factories.InterceptorChainFactory;
   import org.jboss.cache.interceptors.Interceptor;
   import org.jboss.cache.loader.CacheLoader;
  @@ -170,7 +169,7 @@
         treeCache.addUndoOperation(tx, undo_op);
      }
   
  -   public RegionManager getEvictionRegionManager()
  +   public org.jboss.cache.eviction.RegionManager getEvictionRegionManager()
      {
         return treeCache.getEvictionRegionManager();
      }
  @@ -260,29 +259,7 @@
   
      public Region getRegion(Fqn fqn, boolean createIfAbsent)
      {
  -      // first see if a region for this specific Fqn exists
  -      if (treeCache.regionsRegistry.containsKey(fqn)) return treeCache.regionsRegistry.get(fqn);
  -
  -      // if not, attempt to create one ...
  -      if (createIfAbsent)
  -      {
  -         Region r = new RegionImpl(treeCache, fqn);
  -         treeCache.regionsRegistry.put(fqn, r);
  -         return r;
  -      }
  -
  -      // else try and find a parent which has a defined region, may return null if nothing is defined.
  -      Region r = null;
  -      Fqn nextFqn = fqn;
  -
  -      while (r == null)
  -      {
  -         nextFqn = nextFqn.getParent();
  -         r = treeCache.regionsRegistry.get(nextFqn);
  -         if (nextFqn.isRoot()) break;
  -      }
  -
  -      return r;
  +      return treeCache.getRegionManager().getRegion(fqn, createIfAbsent);
      }
   
      public void create() throws Exception
  @@ -540,7 +517,7 @@
         return node == null ? null : new TreeCacheProxyImpl(treeCache, node);
      }
   
  -   public org.jboss.cache.marshall.RegionManager getRegionManager()
  +   public RegionManager getRegionManager()
      {
         return treeCache.getRegionManager();
      }
  
  
  
  1.15      +11 -7     JBossCache/src/org/jboss/cache/TreeCacheView.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: TreeCacheView.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/TreeCacheView.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -b -r1.14 -r1.15
  --- TreeCacheView.java	7 Sep 2006 13:52:28 -0000	1.14
  +++ TreeCacheView.java	12 Oct 2006 23:03:57 -0000	1.15
  @@ -11,7 +11,6 @@
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
   import org.jboss.cache.config.Configuration;
  -import org.jboss.mx.util.MBeanProxyExt;
   import org.jgroups.View;
   
   import javax.management.MBeanServer;
  @@ -54,7 +53,7 @@
    * The view itself caches only the nodes, but doesn't cache any of the data (HashMap) associated with it. When
    * data needs to be displayed, the underlying tree will be accessed directly.
    *
  - * @version $Revision: 1.14 $
  + * @version $Revision: 1.15 $
    * @author<a href="mailto:bela at jboss.org">Bela Ban</a> March 27 2003
    */
   public class TreeCacheView implements TreeCacheViewMBean
  @@ -64,7 +63,7 @@
       */
      ObjectName cache_service = null;
      TreeCacheGui gui = null;
  -   TreeCacheMBean cache;
  +   TreeCache cache;
      Log log = LogFactory.getLog(TreeCacheView.class);
   
   
  @@ -139,7 +138,11 @@
            throw new Exception("TreeCacheView.init(): no MBeanServers found");
         srv = (MBeanServer) servers.get(0);
         log.info("init(): found MBeanServer " + srv);
  -      cache = (TreeCacheMBean) MBeanProxyExt.create(TreeCacheMBean.class, cache_service, srv);
  +      //cache = (TreeCacheMBean) MBeanProxyExt.create(TreeCacheMBean.class, cache_service, srv);
  +
  +      // TODO: Sort out proper JMX lookup
  +      throw new RuntimeException("Sort out proper JMX lookup");
  +
      }
   
   
  @@ -339,7 +342,7 @@
           TreeSelectionListener, TableModelListener
   {
      private static final long serialVersionUID = 8576324868563647538L;
  -   TreeCacheMBean cache;
  +   TreeCache cache;
      DefaultTreeModel tree_model = null;
      Log log = LogFactory.getLog(getClass());
      JTree jtree = null;
  @@ -356,14 +359,14 @@
      private static final int VAL_COL_WIDTH = 300;
   
   
  -   public TreeCacheGui(TreeCacheMBean cache) throws Exception
  +   public TreeCacheGui(TreeCache cache) throws Exception
      {
         this.cache = cache;
   
         //server.invoke(cache_service, "addTreeCacheListener",
         //            new Object[]{this},
         //          new String[]{TreeCacheListener.class.getName()});
  -      cache.addTreeCacheListener(this);
  +      cache.getNotifier().addCacheListener(this);
         addNotify();
         setTitle("TreeCacheGui: mbr=" + getLocalAddress());
   
  @@ -925,6 +928,7 @@
      /* -------------------------- End of Private Methods ------------------------------ */
   
      /*----------------------- Actions ---------------------------*/
  +
      class ExitAction extends AbstractAction
      {
         private static final long serialVersionUID = 8895044368299888998L;
  
  
  
  1.17      +2 -3      JBossCache/src/org/jboss/cache/CacheSPI.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: CacheSPI.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/CacheSPI.java,v
  retrieving revision 1.16
  retrieving revision 1.17
  diff -u -b -r1.16 -r1.17
  --- CacheSPI.java	20 Sep 2006 14:52:20 -0000	1.16
  +++ CacheSPI.java	12 Oct 2006 23:03:57 -0000	1.17
  @@ -7,7 +7,6 @@
   package org.jboss.cache;
   
   import org.jboss.cache.buddyreplication.BuddyManager;
  -import org.jboss.cache.eviction.RegionManager;
   import org.jboss.cache.interceptors.Interceptor;
   import org.jboss.cache.loader.CacheLoader;
   import org.jboss.cache.loader.CacheLoaderManager;
  @@ -124,11 +123,11 @@
   
      void addUndoOperation(GlobalTransaction tx, MethodCall undoOperation);
   
  -   RegionManager getEvictionRegionManager();
  +   org.jboss.cache.eviction.RegionManager getEvictionRegionManager();
   
      Map getLockTable();
   
  -   org.jboss.cache.marshall.RegionManager getRegionManager();
  +   RegionManager getRegionManager();
   
      /*
       * Retrieves the notifier attached with this instance of the cache.  See {@link Notifier}, a class
  
  
  
  1.40      +8 -2      JBossCache/src/org/jboss/cache/Fqn.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: Fqn.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/Fqn.java,v
  retrieving revision 1.39
  retrieving revision 1.40
  diff -u -b -r1.39 -r1.40
  --- Fqn.java	11 Oct 2006 10:02:07 -0000	1.39
  +++ Fqn.java	12 Oct 2006 23:03:57 -0000	1.40
  @@ -41,9 +41,9 @@
    * <p/>
    * Another way to look at it is that the "/" separarator is only parsed when it form sa  part of a String passed in to Fqn.fromString() and not otherwise.
    *
  - * @version $Revision: 1.39 $
  + * @version $Revision: 1.40 $
    */
  -public class Fqn implements Cloneable, Externalizable
  +public class Fqn implements Cloneable, Externalizable, Comparable
   {
   
      /**
  @@ -497,4 +497,10 @@
      {
         return Collections.unmodifiableList(elements);
      }
  +
  +   public int compareTo(Object o)
  +   {
  +      if (o == null) return 1;
  +      return toString().compareTo(o.toString());
  +   }
   }
  \ No newline at end of file
  
  
  
  1.4       +27 -23    JBossCache/src/org/jboss/cache/RegionImpl.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: RegionImpl.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/RegionImpl.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -b -r1.3 -r1.4
  --- RegionImpl.java	30 Aug 2006 19:31:23 -0000	1.3
  +++ RegionImpl.java	12 Oct 2006 23:03:57 -0000	1.4
  @@ -7,8 +7,6 @@
   package org.jboss.cache;
   
   import org.jboss.cache.eviction.EvictionPolicyConfig;
  -import org.jboss.cache.marshall.RegionNameConflictException;
  -import org.jboss.cache.marshall.RegionNotFoundException;
   
   /**
    * Default implementation of a {@link Region}
  @@ -17,55 +15,51 @@
    */
   public class RegionImpl implements Region
   {
  -   TreeCache treeCache;
  +   RegionManager regionManager;
      Fqn fqn;
  +   boolean active;
  +   ClassLoader classLoader;
   
      /**
  -    * Creates a Region from an fqn and registers the region in the region registry.
  +    * Creates a MarshRegion from an fqn and registers the region in the region registry.
       *
  -    * @param c
       * @param fqn
       */
  -   public RegionImpl(TreeCache c, Fqn fqn)
  +   public RegionImpl(Fqn fqn, RegionManager regionManager)
      {
  -      this.treeCache = c;
         this.fqn = fqn;
  -      this.treeCache.regionsRegistry.put(fqn, this);
  +      this.regionManager = regionManager;
  +      this.active = !regionManager.isDefaultInactive();
      }
   
  -   public void registerContextClassLoader(ClassLoader classLoader) throws RegionNameConflictException
  +   public void registerContextClassLoader(ClassLoader classLoader)
      {
  -      treeCache.registerClassLoader(fqn.toString(), classLoader);
  +      this.classLoader = classLoader;
      }
   
  -   public void unregisterContextClassLoader() throws RegionNotFoundException
  +   public void unregisterContextClassLoader()
      {
  -      treeCache.unregisterClassLoader(fqn.toString());
  +      this.classLoader = null;
      }
   
  -   public void activate() throws RegionNameConflictException
  +   public void activate()
      {
  -      treeCache.activateRegion(fqn.toString());
  +      active = true;
      }
   
  -   public void deactivate() throws RegionNameConflictException
  +   public void deactivate()
      {
  -      treeCache.inactivateRegion(fqn.toString());
  +      active = false;
      }
   
      public boolean isActive()
      {
  -      return getRegion().isActive();
  -   }
  -
  -   private org.jboss.cache.marshall.Region getRegion()
  -   {
  -      return treeCache.getRegionManager().getRegion(fqn);
  +      return active;
      }
   
      public ClassLoader getClassLoader()
      {
  -      return getRegion().getClassLoader();
  +      return classLoader;
      }
   
      public void setEvictionPolicy(EvictionPolicyConfig evictionPolicyConfig)
  @@ -90,4 +84,14 @@
                 "fqn=" + fqn +
                 '}';
      }
  +
  +   public int compareTo(Object o)
  +   {
  +      if (o instanceof Region)
  +      {
  +         Region other = (Region) o;
  +         return getFqn().compareTo(other.getFqn());
  +      }
  +      return 1;
  +   }
   }
  
  
  
  1.1      date: 2006/10/12 23:03:57;  author: msurtani;  state: Exp;JBossCache/src/org/jboss/cache/RegionManager.java
  
  Index: RegionManager.java
  ===================================================================
  package org.jboss.cache;
  
  import org.jboss.cache.buddyreplication.BuddyManager;
  
  import java.util.ArrayList;
  import java.util.Collections;
  import java.util.List;
  import java.util.Map;
  import java.util.concurrent.ConcurrentHashMap;
  
  /**
   * Encapsulates the concept of a {@link Region}, and manages instances of such regions.
   *
   * @author <a href="mailto:manik at jboss.org">Manik Surtani</a>
   * @since 2.0.0
   */
  public class RegionManager
  {
     /**
      * A registry of regions that have been defined.
      */
     Map<Fqn, Region> regionsRegistry = new ConcurrentHashMap<Fqn, Region>();
     boolean defaultInactive;
  
  
     public boolean isDefaultInactive()
     {
        return defaultInactive;
     }
  
     public void setDefaultInactive(boolean defaultInactive)
     {
        this.defaultInactive = defaultInactive;
     }
  
     /**
      * Helper utility that checks for a classloader registered for the
      * given Fqn, and if found sets it as the TCCL. If the given Fqn is
      * under the _BUDDY_BACKUP_ region, the equivalent region in the main
      * tree is used to find the classloader.
      *
      * @param fqn Fqn pointing to a region for which a special classloader
      *            may have been registered.
      */
     public void setContextClassLoaderAsCurrent(Fqn fqn)
     {
        if (fqn.isChildOf(BuddyManager.BUDDY_BACKUP_SUBTREE_FQN))
        {
           if (fqn.size() <= 2)
           {
              fqn = Fqn.ROOT;
           }
           else
           {
              fqn = fqn.getFqnChild(2, fqn.size());
           }
        }
        Region region = getRegion(fqn, false);
        ClassLoader regionCL = (region == null) ? null : region.getClassLoader();
        if (regionCL != null)
        {
           Thread.currentThread().setContextClassLoader(regionCL);
        }
  
     }
  
     public Region getRegion(Fqn fqn, boolean createIfAbsent)
     {
        // first see if a region for this specific Fqn exists
        if (regionsRegistry.containsKey(fqn)) return regionsRegistry.get(fqn);
  
        // if not, attempt to create one ...
        if (createIfAbsent)
        {
           Region r = new RegionImpl(fqn, this);
           regionsRegistry.put(fqn, r);
           return r;
        }
  
        // else try and find a parent which has a defined region, may return null if nothing is defined.
        Region r = null;
        Fqn nextFqn = fqn;
  
        while (r == null)
        {
           nextFqn = nextFqn.getParent();
           r = regionsRegistry.get(nextFqn);
           if (nextFqn.isRoot()) break;
        }
  
        return r;
  
     }
  
     public void registerClassLoader(Fqn fqn, ClassLoader cl)
     {
        Region existing = getRegion(fqn, false);
        if (existing == null)
        {
           existing = getRegion(fqn, true);
        }
        existing.registerContextClassLoader(cl);
     }
  
     public void unregisterClassLoader(Fqn fqn)
     {
        Region region = getRegion(fqn, false);
        if (region != null) region.unregisterContextClassLoader();
     }
  
     public void removeRegion(Fqn fqn)
     {
        regionsRegistry.remove(fqn);
     }
  
     /**
      * Activates unmarshalling of replication messages for the region
      * rooted in the given Fqn.
      *
      * @param fqnStr
      */
     public void activate(String fqnStr)
     {
        Fqn fqn = Fqn.fromString(fqnStr);
        Region r = getRegion(fqn, false);
        if (r != null)
        {
           if (!defaultInactive && r.getClassLoader() == null)
           {
              // This region's state will no match that of a non-existent one
              // So, there is no reason to keep this region any more
              removeRegion(fqn);
           }
           else
           {
              r.activate();
           }
        }
        else if (defaultInactive)
        {
           // "Active" region is not the default, so create a region
           r = getRegion(fqn, true);
           r.activate();
        }
     }
  
     public boolean hasRegion(Fqn fqn)
     {
        return regionsRegistry.containsKey(fqn);
     }
  
     /**
      * Disables unmarshalling of replication messages for the region
      * rooted in the given Fqn.
      *
      * @param fqnStr
      */
     public void inactivate(String fqnStr)
     {
        Fqn fqn = Fqn.fromString(fqnStr);
        Region region = getRegion(fqn, false);
  
        if (region != null)
        {
           if (defaultInactive && region.getClassLoader() == null)
           {
              // This region's state will no match that of a non-existent one
              // So, there is no reason to keep this region any more
              removeRegion(fqn);
           }
           else
           {
              region.deactivate();
           }
        }
        else if (!defaultInactive)
        {
           region = getRegion(fqn, true);
           region.deactivate();
        }
     }
  
     /**
      * @return an ordered list of all active regions with registered context class loaders.
      */
     public List<Region> getAllMarshallingRegions()
     {
        List<Region> regions = new ArrayList<Region>();
  
        for (Region r : regionsRegistry.values()) if (r.isActive() && r.getClassLoader() != null) regions.add(r);
  
        Collections.sort(regions);
  
        return regions;
     }
  
  }
  
  
  



More information about the jboss-cvs-commits mailing list