[jboss-cvs] JBossAS SVN: r107762 - projects/ejb3/branches/infinispan-int/core/src/main/java/org/jboss/ejb3/cache/infinispan.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Tue Aug 24 11:19:08 EDT 2010


Author: pferraro
Date: 2010-08-24 11:19:07 -0400 (Tue, 24 Aug 2010)
New Revision: 107762

Added:
   projects/ejb3/branches/infinispan-int/core/src/main/java/org/jboss/ejb3/cache/infinispan/CacheSource.java
   projects/ejb3/branches/infinispan-int/core/src/main/java/org/jboss/ejb3/cache/infinispan/DefaultCacheSource.java
Modified:
   projects/ejb3/branches/infinispan-int/core/src/main/java/org/jboss/ejb3/cache/infinispan/InfinispanStatefulCache.java
   projects/ejb3/branches/infinispan-int/core/src/main/java/org/jboss/ejb3/cache/infinispan/InfinispanStatefulCacheFactory.java
Log:
Refactor cache configuration logic into CacheSource abstraction.

Added: projects/ejb3/branches/infinispan-int/core/src/main/java/org/jboss/ejb3/cache/infinispan/CacheSource.java
===================================================================
--- projects/ejb3/branches/infinispan-int/core/src/main/java/org/jboss/ejb3/cache/infinispan/CacheSource.java	                        (rev 0)
+++ projects/ejb3/branches/infinispan-int/core/src/main/java/org/jboss/ejb3/cache/infinispan/CacheSource.java	2010-08-24 15:19:07 UTC (rev 107762)
@@ -0,0 +1,33 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file 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.jboss.ejb3.cache.infinispan;
+
+import org.infinispan.Cache;
+import org.jboss.ejb3.stateful.StatefulContainer;
+
+/**
+ * @author Paul Ferraro
+ */
+public interface CacheSource
+{
+   <K, V> Cache<K, V> getCache(StatefulContainer container);
+}

Added: projects/ejb3/branches/infinispan-int/core/src/main/java/org/jboss/ejb3/cache/infinispan/DefaultCacheSource.java
===================================================================
--- projects/ejb3/branches/infinispan-int/core/src/main/java/org/jboss/ejb3/cache/infinispan/DefaultCacheSource.java	                        (rev 0)
+++ projects/ejb3/branches/infinispan-int/core/src/main/java/org/jboss/ejb3/cache/infinispan/DefaultCacheSource.java	2010-08-24 15:19:07 UTC (rev 107762)
@@ -0,0 +1,127 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file 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.jboss.ejb3.cache.infinispan;
+
+import org.infinispan.Cache;
+import org.infinispan.config.Configuration;
+import org.infinispan.config.Configuration.CacheMode;
+import org.infinispan.manager.EmbeddedCacheManager;
+import org.jboss.ejb3.annotation.CacheConfig;
+import org.jboss.ejb3.stateful.StatefulContainer;
+import org.jboss.ha.ispn.CacheContainerRegistry;
+import org.jboss.ha.ispn.DefaultCacheContainerRegistry;
+
+/**
+ * @author Paul Ferraro
+ *
+ */
+public class DefaultCacheSource implements CacheSource
+{
+   public static final String DEFAULT_CACHE_CONTAINER = "sfsb";
+   
+   private String defaultContainerName = DEFAULT_CACHE_CONTAINER;
+   private CacheContainerRegistry registry = DefaultCacheContainerRegistry.getInstance();
+   
+   /**
+    * {@inheritDoc}
+    * @see org.jboss.ejb3.cache.infinispan.CacheSource#getCache(org.jboss.ejb3.stateful.StatefulContainer)
+    */
+   @Override
+   public <K, V> Cache<K, V> getCache(StatefulContainer ejbContainer)
+   {
+      String containerName = this.defaultContainerName;
+      CacheConfig cacheConfig = ejbContainer.getAnnotation(CacheConfig.class);
+      String templateCacheName = cacheConfig.name();
+      
+      if ((templateCacheName != null) && !templateCacheName.trim().isEmpty())
+      {
+         // Parse cache container name
+         String[] parts = templateCacheName.split(":");
+         if (parts.length == 2)
+         {
+            containerName = parts[0];
+            templateCacheName = parts[1];
+         }
+      }
+      else
+      {
+         templateCacheName = CacheConfig.DEFAULT_CLUSTERED_OBJECT_NAME;
+      }
+      
+      String cacheName = ejbContainer.getDeploymentPropertyListString();
+      
+      EmbeddedCacheManager container = this.registry.getCacheContainer(containerName);
+      Cache<?, ?> templateCache = container.getCache(templateCacheName);
+      Configuration configuration = templateCache.getConfiguration().clone();
+      
+      int backups = cacheConfig.backups();
+      CacheConfig.Mode mode = cacheConfig.mode();
+
+      CacheMode cacheMode = configuration.getCacheMode();
+      
+      if (backups != CacheConfig.DEFAULT_BACKUPS)
+      {
+         configuration.setNumOwners(backups);
+         
+         if (backups == CacheConfig.NO_BACKUPS)
+         {
+            cacheMode = CacheMode.LOCAL;
+         }
+         else
+         {
+            boolean synchronous = cacheMode.isSynchronous();
+            if (backups > 0)
+            {
+               cacheMode = synchronous ? CacheMode.DIST_SYNC : CacheMode.DIST_ASYNC;
+            }
+            else // Negative backups means total replication
+            {
+               cacheMode = synchronous ? CacheMode.REPL_SYNC : CacheMode.REPL_ASYNC;
+            }
+         }
+      }
+      
+      switch (mode)
+      {
+         case SYNCHRONOUS:
+         {
+            cacheMode = cacheMode.toSync();
+            break;
+         }
+         case ASYNCHRONOUS:
+         {
+            cacheMode = cacheMode.toAsync();
+            break;
+         }
+         case DEFAULT:
+         {
+            // Do nothing
+         }
+      }
+      
+      configuration.setCacheMode(cacheMode);
+      
+      container.defineConfiguration(cacheName, configuration);
+      
+      return container.getCache(cacheName);
+   }
+}

Modified: projects/ejb3/branches/infinispan-int/core/src/main/java/org/jboss/ejb3/cache/infinispan/InfinispanStatefulCache.java
===================================================================
--- projects/ejb3/branches/infinispan-int/core/src/main/java/org/jboss/ejb3/cache/infinispan/InfinispanStatefulCache.java	2010-08-24 13:48:12 UTC (rev 107761)
+++ projects/ejb3/branches/infinispan-int/core/src/main/java/org/jboss/ejb3/cache/infinispan/InfinispanStatefulCache.java	2010-08-24 15:19:07 UTC (rev 107762)
@@ -1,3 +1,24 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file 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.jboss.ejb3.cache.infinispan;
 
 import java.lang.ref.WeakReference;
@@ -16,12 +37,8 @@
 import javax.ejb.NoSuchEJBException;
 
 import org.infinispan.Cache;
-import org.infinispan.config.Configuration;
-import org.infinispan.config.Configuration.CacheMode;
 import org.infinispan.distribution.DistributionManager;
 import org.infinispan.lifecycle.ComponentStatus;
-import org.infinispan.manager.CacheContainer;
-import org.infinispan.manager.EmbeddedCacheManager;
 import org.infinispan.notifications.Listener;
 import org.infinispan.notifications.cachelistener.annotation.CacheEntryActivated;
 import org.infinispan.notifications.cachelistener.annotation.CacheEntryPassivated;
@@ -34,7 +51,6 @@
 import org.jboss.ejb3.stateful.NestedStatefulBeanContext;
 import org.jboss.ejb3.stateful.StatefulBeanContext;
 import org.jboss.ejb3.stateful.StatefulContainer;
-import org.jboss.ha.ispn.CacheContainerRegistry;
 import org.jboss.ha.ispn.invoker.CacheInvoker;
 import org.jboss.logging.Logger;
 import org.jboss.util.loading.ContextClassLoaderSwitcher;
@@ -42,7 +58,6 @@
 
 /**
  * @author Paul Ferraro
- *
  */
 @Listener
 public class InfinispanStatefulCache implements ClusteredStatefulCache
@@ -51,9 +66,8 @@
    // Need to cast since ContextClassLoaderSwitcher.NewInstance does not generically implement PrivilegedAction<ContextClassLoaderSwitcher>
    private final ContextClassLoaderSwitcher switcher = (ContextClassLoaderSwitcher) AccessController.doPrivileged(ContextClassLoaderSwitcher.INSTANTIATOR);
    
-   private final CacheContainerRegistry registry;
+   private final CacheSource source;
    private final CacheInvoker invoker;
-   private final String defaultContainerName;
    final ThreadFactory threadFactory;
 
    private final AtomicInteger createCount = new AtomicInteger(0);
@@ -75,12 +89,11 @@
    private Cache<Object, StatefulBeanContext> cache;
    private WeakReference<ClassLoader> classLoaderRef;
    
-   public InfinispanStatefulCache(CacheContainerRegistry registry, String defaultContainerName, CacheInvoker invoker, ThreadFactory threadFactory)
+   public InfinispanStatefulCache(CacheSource source, CacheInvoker invoker, ThreadFactory threadFactory)
    {
-      this.registry = registry;
+      this.source = source;
       this.invoker = invoker;
       this.threadFactory = threadFactory;
-      this.defaultContainerName = defaultContainerName;
    }
 
    @Override
@@ -89,8 +102,9 @@
       this.ejbContainer = (StatefulContainer) container;
       this.log = Logger.getLogger(getClass().getName() + "." + this.ejbContainer.getEjbName());
       
+      this.cache = this.source.getCache(this.ejbContainer);
       this.cacheConfig = this.ejbContainer.getAnnotation(CacheConfig.class);
-
+      
       this.removalTimeout = this.cacheConfig.removalTimeoutSeconds() * 1000L;
       
       if (this.removalTimeout > 0)
@@ -103,86 +117,6 @@
    public void start()
    {
       this.classLoaderRef = new WeakReference<ClassLoader>(this.ejbContainer.getClassloader());
-
-      if (this.cache == null)
-      {
-         String containerName = this.defaultContainerName;
-         String templateCacheName = this.cacheConfig.name();
-         
-         if ((templateCacheName != null) && !templateCacheName.trim().isEmpty())
-         {
-            // Parse cache container name
-            String[] parts = templateCacheName.split(":");
-            if (parts.length == 2)
-            {
-               containerName = parts[0];
-               templateCacheName = parts[1];
-            }
-         }
-         else
-         {
-            templateCacheName = CacheConfig.DEFAULT_CLUSTERED_OBJECT_NAME;
-         }
-         
-         String cacheName = this.ejbContainer.getDeploymentPropertyListString();
-         
-         CacheContainer container = this.registry.getCacheContainer(containerName);
-         Cache<?, ?> templateCache = container.getCache(templateCacheName);
-         Configuration configuration = templateCache.getConfiguration().clone();
-         
-         int backups = this.cacheConfig.backups();
-         CacheConfig.Mode mode = this.cacheConfig.mode();
-
-         CacheMode cacheMode = configuration.getCacheMode();
-         
-         if (backups != CacheConfig.DEFAULT_BACKUPS)
-         {
-            configuration.setNumOwners(backups);
-            
-            if (backups == CacheConfig.NO_BACKUPS)
-            {
-               cacheMode = CacheMode.LOCAL;
-            }
-            else
-            {
-               boolean synchronous = cacheMode.isSynchronous();
-               if (backups > 0)
-               {
-                  cacheMode = synchronous ? CacheMode.DIST_SYNC : CacheMode.DIST_ASYNC;
-               }
-               else // Negative backups means total replication
-               {
-                  cacheMode = synchronous ? CacheMode.REPL_SYNC : CacheMode.REPL_ASYNC;
-               }
-            }
-         }
-         
-         switch (mode)
-         {
-            case SYNCHRONOUS:
-            {
-               cacheMode = cacheMode.toSync();
-               break;
-            }
-            case ASYNCHRONOUS:
-            {
-               cacheMode = cacheMode.toAsync();
-               break;
-            }
-            case DEFAULT:
-            {
-               // Do nothing
-            }
-         }
-         
-         configuration.setCacheMode(cacheMode);
-         
-         EmbeddedCacheManager manager = (EmbeddedCacheManager) templateCache.getCacheManager();
-         
-         manager.defineConfiguration(cacheName, configuration);
-         
-         this.cache = container.getCache(cacheName);
-      }
       
       this.cache.start();
       

Modified: projects/ejb3/branches/infinispan-int/core/src/main/java/org/jboss/ejb3/cache/infinispan/InfinispanStatefulCacheFactory.java
===================================================================
--- projects/ejb3/branches/infinispan-int/core/src/main/java/org/jboss/ejb3/cache/infinispan/InfinispanStatefulCacheFactory.java	2010-08-24 13:48:12 UTC (rev 107761)
+++ projects/ejb3/branches/infinispan-int/core/src/main/java/org/jboss/ejb3/cache/infinispan/InfinispanStatefulCacheFactory.java	2010-08-24 15:19:07 UTC (rev 107762)
@@ -25,8 +25,6 @@
 import java.util.concurrent.ThreadFactory;
 
 import org.jboss.ejb3.cache.Ejb3CacheFactory;
-import org.jboss.ha.ispn.CacheContainerRegistry;
-import org.jboss.ha.ispn.DefaultCacheContainerRegistry;
 import org.jboss.ha.ispn.invoker.CacheInvoker;
 import org.jboss.ha.ispn.invoker.RetryingCacheInvoker;
 
@@ -35,10 +33,7 @@
  */
 public class InfinispanStatefulCacheFactory implements Ejb3CacheFactory
 {
-   private static final String DEFAULT_CACHE_CONTAINER = "sfsb";
-   
-   private CacheContainerRegistry registry = DefaultCacheContainerRegistry.getInstance();
-   private String defaultContainerName = DEFAULT_CACHE_CONTAINER;
+   private CacheSource source = new DefaultCacheSource();
    private ThreadFactory threadFactory = Executors.defaultThreadFactory();
    private CacheInvoker invoker = new RetryingCacheInvoker(0, 0);
 
@@ -50,12 +45,12 @@
    @SuppressWarnings("deprecation")
    public org.jboss.ejb3.cache.StatefulCache createCache()
    {
-      return new InfinispanStatefulCache(this.registry, this.defaultContainerName, this.invoker, this.threadFactory);
+      return new InfinispanStatefulCache(this.source, this.invoker, this.threadFactory);
    }
    
-   public void setCacheContainerRegistry(CacheContainerRegistry registry)
+   public void setCacheSource(CacheSource source)
    {
-      this.registry = registry;
+      this.source = source;
    }
    
    public void setThreadFactory(ThreadFactory threadFactory)
@@ -63,11 +58,6 @@
       this.threadFactory = threadFactory;
    }
    
-   public void setDefaultCacheContainerName(String defaultContainerName)
-   {
-      this.defaultContainerName = defaultContainerName;
-   }
-   
    public void setCacheInvoker(CacheInvoker invoker)
    {
       this.invoker = invoker;



More information about the jboss-cvs-commits mailing list