cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jp...@apache.org
Subject git commit: PMUD-5802 added ability to use a global ehcache manager
Date Sun, 15 Jun 2014 02:59:48 GMT
Repository: cxf
Updated Branches:
  refs/heads/2.6.x-fixes 32245004d -> fdbddc706


PMUD-5802 added ability to use a global ehcache manager


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/fdbddc70
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/fdbddc70
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/fdbddc70

Branch: refs/heads/2.6.x-fixes
Commit: fdbddc70638dca598b1ca1285756353338c6c36b
Parents: 3224500
Author: Jason Pell <jpell@apache.org>
Authored: Sun Jun 15 12:59:41 2014 +1000
Committer: Jason Pell <jpell@apache.org>
Committed: Sun Jun 15 12:59:41 2014 +1000

----------------------------------------------------------------------
 .../ws/security/cache/EHCacheManagerHolder.java | 59 ++++++++++++++------
 .../ws/security/cache/EHCacheReplayCache.java   |  6 ++
 .../security/tokenstore/EHCacheTokenStore.java  |  5 ++
 .../cache/EHCacheManagerHolderTest.java         | 46 +++++++++++++--
 4 files changed, 93 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/fdbddc70/rt/ws/security/src/main/java/org/apache/cxf/ws/security/cache/EHCacheManagerHolder.java
----------------------------------------------------------------------
diff --git a/rt/ws/security/src/main/java/org/apache/cxf/ws/security/cache/EHCacheManagerHolder.java
b/rt/ws/security/src/main/java/org/apache/cxf/ws/security/cache/EHCacheManagerHolder.java
index 2bd8410..19ff8d9 100644
--- a/rt/ws/security/src/main/java/org/apache/cxf/ws/security/cache/EHCacheManagerHolder.java
+++ b/rt/ws/security/src/main/java/org/apache/cxf/ws/security/cache/EHCacheManagerHolder.java
@@ -40,9 +40,12 @@ import org.apache.cxf.resource.ResourceManager;
  * We need to reference count the EHCacheManager things
  */
 public final class EHCacheManagerHolder {
+    public static final String GLOBAL_EHCACHE_MANAGER_NAME = 
+            "ws-security.global.ehcachemanager";
+    
     private static final ConcurrentHashMap<String, AtomicInteger> COUNTS 
         = new ConcurrentHashMap<String, AtomicInteger>(8, 0.75f, 2);
-
+    
     private static Method cacheManagerCreateMethodNoArg;
     private static Method createMethodURLArg;
     private static Method cacheManagerCreateMethodConfigurationArg;
@@ -91,30 +94,50 @@ public final class EHCacheManagerHolder {
     
     public static CacheManager getCacheManager(Bus bus, URL configFileURL) {
         CacheManager cacheManager = null;
-        if (configFileURL == null) {
-            //using the default
-            cacheManager = findDefaultCacheManager(bus);
-        }
+        
+        String globalCacheManagerName = getGlobalCacheManagerName(bus);
+        if (globalCacheManagerName != null) {
+            cacheManager = CacheManager.getCacheManager(globalCacheManagerName);
+        }
+        
+        // notice for a global cache manager, we skip the count stuff which
+        // means the release cache manager method below is a no op, as the
+        // COUNT will not have been initialised.
         if (cacheManager == null) {
             if (configFileURL == null) {
-                cacheManager = createCacheManager();
-            } else {
-                cacheManager = createCacheManager(configFileURL);
+                //using the default
+                cacheManager = findDefaultCacheManager(bus);
+            }
+            
+            if (cacheManager == null) {
+                if (configFileURL == null) {
+                    cacheManager = createCacheManager();
+                } else {
+                    cacheManager = createCacheManager(configFileURL);
+                }
+            }
+            
+            AtomicInteger a = COUNTS.get(cacheManager.getName());
+            if (a == null) {
+                COUNTS.putIfAbsent(cacheManager.getName(), new AtomicInteger());
+                a = COUNTS.get(cacheManager.getName());
+            }
+            if (a.incrementAndGet() == 1) {
+                //System.out.println("Create!! " + cacheManager.getName());
             }
-        }
-        AtomicInteger a = COUNTS.get(cacheManager.getName());
-        if (a == null) {
-            COUNTS.putIfAbsent(cacheManager.getName(), new AtomicInteger());
-            a = COUNTS.get(cacheManager.getName());
-        }
-        if (a.incrementAndGet() == 1) {
-            //System.out.println("Create!! " + cacheManager.getName());
         }
         return cacheManager;
     }
-    
-    private static CacheManager findDefaultCacheManager(Bus bus) {
 
+    private static String getGlobalCacheManagerName(Bus bus) {
+        if (bus != null) {
+            return (String) bus.getProperty(GLOBAL_EHCACHE_MANAGER_NAME);
+        } else {
+            return null;
+        }
+    }
+
+    private static CacheManager findDefaultCacheManager(Bus bus) {
         String defaultConfigFile = "cxf-ehcache.xml";
         URL configFileURL = null;
         if (bus != null) {

http://git-wip-us.apache.org/repos/asf/cxf/blob/fdbddc70/rt/ws/security/src/main/java/org/apache/cxf/ws/security/cache/EHCacheReplayCache.java
----------------------------------------------------------------------
diff --git a/rt/ws/security/src/main/java/org/apache/cxf/ws/security/cache/EHCacheReplayCache.java
b/rt/ws/security/src/main/java/org/apache/cxf/ws/security/cache/EHCacheReplayCache.java
index 2fef240..eef974d 100644
--- a/rt/ws/security/src/main/java/org/apache/cxf/ws/security/cache/EHCacheReplayCache.java
+++ b/rt/ws/security/src/main/java/org/apache/cxf/ws/security/cache/EHCacheReplayCache.java
@@ -129,9 +129,15 @@ public class EHCacheReplayCache implements ReplayCache, Closeable, BusLifeCycleL
 
     public synchronized void close() {
         if (cacheManager != null) {
+            // this step is especially important for global shared cache manager
+            if (cache != null) {
+                cacheManager.removeCache(cache.getName());
+            }
+            
             EHCacheManagerHolder.releaseCacheManger(cacheManager);
             cacheManager = null;
             cache = null;
+            
             if (bus != null) {
                 bus.getExtension(BusLifeCycleManager.class).unregisterLifeCycleListener(this);
             }

http://git-wip-us.apache.org/repos/asf/cxf/blob/fdbddc70/rt/ws/security/src/main/java/org/apache/cxf/ws/security/tokenstore/EHCacheTokenStore.java
----------------------------------------------------------------------
diff --git a/rt/ws/security/src/main/java/org/apache/cxf/ws/security/tokenstore/EHCacheTokenStore.java
b/rt/ws/security/src/main/java/org/apache/cxf/ws/security/tokenstore/EHCacheTokenStore.java
index 1d09401..7a2660f 100644
--- a/rt/ws/security/src/main/java/org/apache/cxf/ws/security/tokenstore/EHCacheTokenStore.java
+++ b/rt/ws/security/src/main/java/org/apache/cxf/ws/security/tokenstore/EHCacheTokenStore.java
@@ -178,6 +178,11 @@ public class EHCacheTokenStore implements TokenStore, Closeable, BusLifeCycleLis
 
     public void close() {
         if (cacheManager != null) {
+            // this step is especially important for global shared cache manager
+            if (cache != null) {
+                cacheManager.removeCache(cache.getName());
+            }
+            
             EHCacheManagerHolder.releaseCacheManger(cacheManager);
             cacheManager = null;
             cache = null;

http://git-wip-us.apache.org/repos/asf/cxf/blob/fdbddc70/rt/ws/security/src/test/java/org/apache/cxf/ws/security/cache/EHCacheManagerHolderTest.java
----------------------------------------------------------------------
diff --git a/rt/ws/security/src/test/java/org/apache/cxf/ws/security/cache/EHCacheManagerHolderTest.java
b/rt/ws/security/src/test/java/org/apache/cxf/ws/security/cache/EHCacheManagerHolderTest.java
index b062230..e1a8558 100644
--- a/rt/ws/security/src/test/java/org/apache/cxf/ws/security/cache/EHCacheManagerHolderTest.java
+++ b/rt/ws/security/src/test/java/org/apache/cxf/ws/security/cache/EHCacheManagerHolderTest.java
@@ -21,18 +21,15 @@ package org.apache.cxf.ws.security.cache;
 
 import net.sf.ehcache.CacheManager;
 import net.sf.ehcache.Status;
-
 import net.sf.ehcache.config.Configuration;
 import net.sf.ehcache.config.ConfigurationFactory;
 
+import org.apache.cxf.Bus;
+import org.apache.cxf.BusFactory;
 import org.junit.Assert;
 import org.junit.Test;
 
-/**
- * 
- */
 public class EHCacheManagerHolderTest extends Assert {
-
     @Test
     public void testCreateCacheManager() {
         Configuration conf = 
@@ -53,6 +50,45 @@ public class EHCacheManagerHolderTest extends Assert {
         
         manager2.shutdown();
         assertEquals(Status.STATUS_SHUTDOWN, manager2.getStatus());
+    }
+    
+    @Test
+    public void testUseGlobalManager() {
+        Bus bus = BusFactory.getThreadDefaultBus();
+        
+        Configuration conf = 
+                ConfigurationFactory.parseConfiguration(
+                        EHCacheManagerHolder.class.getResource("/cxf-test-ehcache.xml"));
+        conf.setName("myGlobalConfig");
+        
+        EHCacheManagerHolder.createCacheManager(conf);
+        
+        CacheManager manager = EHCacheManagerHolder.getCacheManager(bus, 
+                EHCacheManagerHolder.class.getResource("/cxf-test-ehcache.xml"));
+        
+        assertFalse(manager.getName().equals("myGlobalConfig"));
+        EHCacheManagerHolder.releaseCacheManger(manager);
+        assertEquals(Status.STATUS_SHUTDOWN, manager.getStatus());
+        
+        bus.setProperty(EHCacheManagerHolder.GLOBAL_EHCACHE_MANAGER_NAME, "myGlobalConfig");
+        
+        manager = EHCacheManagerHolder.getCacheManager(bus, 
+                EHCacheManagerHolder.class.getResource("/cxf-test-ehcache.xml"));
+        
+        assertEquals("myGlobalConfig", manager.getName());
+        EHCacheManagerHolder.releaseCacheManger(manager);
+        assertEquals(Status.STATUS_ALIVE, manager.getStatus());
+        
+        manager.shutdown();
+        assertEquals(Status.STATUS_SHUTDOWN, manager.getStatus());
+        
+        bus.setProperty(EHCacheManagerHolder.GLOBAL_EHCACHE_MANAGER_NAME, "myGlobalConfigXXX");
+        
+        manager = EHCacheManagerHolder.getCacheManager(bus, 
+                EHCacheManagerHolder.class.getResource("/cxf-test-ehcache.xml"));
         
+        assertFalse(manager.getName().equals("myGlobalConfig"));
+        EHCacheManagerHolder.releaseCacheManger(manager);
+        assertEquals(Status.STATUS_SHUTDOWN, manager.getStatus());
     }
 }


Mime
View raw message