Repository: cxf Updated Branches: refs/heads/master ca2de0d7e -> 657ab96e8
CXF-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/657ab96e Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/657ab96e Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/657ab96e Branch: refs/heads/master Commit: 657ab96e82579f0a8edc2b785c6857d46d60a7ec Parents: ca2de0d Author: Jason Pell <[email protected]> Authored: Sun Jun 15 16:12:51 2014 +1000 Committer: Jason Pell <[email protected]> Committed: Sun Jun 15 16:12:51 2014 +1000 ---------------------------------------------------------------------- .../security/cache/CXFEHCacheReplayCache.java | 10 ++- .../cxf/ws/security/cache/EHCacheUtils.java | 61 ++++++++++++++++ .../security/tokenstore/EHCacheTokenStore.java | 12 ++-- .../cxf/ws/security/cache/EHCacheUtilsTest.java | 73 ++++++++++++++++++++ .../src/test/resources/cxf-test-ehcache.xml | 16 +++++ .../cxf/sts/cache/EHCacheIdentityCache.java | 10 ++- 6 files changed, 172 insertions(+), 10 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/657ab96e/rt/ws/security/src/main/java/org/apache/cxf/ws/security/cache/CXFEHCacheReplayCache.java ---------------------------------------------------------------------- diff --git a/rt/ws/security/src/main/java/org/apache/cxf/ws/security/cache/CXFEHCacheReplayCache.java b/rt/ws/security/src/main/java/org/apache/cxf/ws/security/cache/CXFEHCacheReplayCache.java index 26375cc..982975f 100644 --- a/rt/ws/security/src/main/java/org/apache/cxf/ws/security/cache/CXFEHCacheReplayCache.java +++ b/rt/ws/security/src/main/java/org/apache/cxf/ws/security/cache/CXFEHCacheReplayCache.java @@ -24,7 +24,6 @@ import java.net.URL; import org.apache.cxf.Bus; import org.apache.cxf.buslifecycle.BusLifeCycleListener; import org.apache.cxf.buslifecycle.BusLifeCycleManager; -import org.apache.wss4j.common.cache.EHCacheManagerHolder; import org.apache.wss4j.common.cache.EHCacheReplayCache; /** @@ -32,11 +31,10 @@ import org.apache.wss4j.common.cache.EHCacheReplayCache; * the cache is shutdown correctly. */ public class CXFEHCacheReplayCache extends EHCacheReplayCache implements BusLifeCycleListener { - private Bus bus; public CXFEHCacheReplayCache(String key, Bus bus, URL configFileURL) { - super(key, EHCacheManagerHolder.getCacheManager(bus.getId(), configFileURL)); + super(key, EHCacheUtils.getCacheManager(bus, configFileURL)); this.bus = bus; if (bus != null) { bus.getExtension(BusLifeCycleManager.class).registerLifeCycleListener(this); @@ -45,7 +43,13 @@ public class CXFEHCacheReplayCache extends EHCacheReplayCache implements BusLife @Override public void close() { + // TODO - this code can be removed when WSS4J is updated to do it as part WSS-503 + if (cacheManager != null && cache != null) { + cacheManager.removeCache(cache.getName()); + } + super.close(); + if (bus != null) { bus.getExtension(BusLifeCycleManager.class).unregisterLifeCycleListener(this); } http://git-wip-us.apache.org/repos/asf/cxf/blob/657ab96e/rt/ws/security/src/main/java/org/apache/cxf/ws/security/cache/EHCacheUtils.java ---------------------------------------------------------------------- diff --git a/rt/ws/security/src/main/java/org/apache/cxf/ws/security/cache/EHCacheUtils.java b/rt/ws/security/src/main/java/org/apache/cxf/ws/security/cache/EHCacheUtils.java new file mode 100644 index 0000000..22f9cde --- /dev/null +++ b/rt/ws/security/src/main/java/org/apache/cxf/ws/security/cache/EHCacheUtils.java @@ -0,0 +1,61 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.cxf.ws.security.cache; + +import java.net.URL; + +import net.sf.ehcache.CacheManager; + +import org.apache.cxf.Bus; +import org.apache.wss4j.common.cache.EHCacheManagerHolder; + +public final class EHCacheUtils { + public static final String GLOBAL_EHCACHE_MANAGER_NAME = + "ws-security.global.ehcachemanager"; + + private EHCacheUtils() { + } + + public static CacheManager getCacheManager(Bus bus, URL configFileURL) { + CacheManager cacheManager = null; + + String globalCacheManagerName = getGlobalCacheManagerName(bus); + if (globalCacheManagerName != null) { + cacheManager = CacheManager.getCacheManager(globalCacheManagerName); + } + + if (cacheManager == null) { + String confName = ""; + if (bus != null) { + confName = bus.getId(); + } + cacheManager = EHCacheManagerHolder.getCacheManager(confName, configFileURL); + } + return cacheManager; + } + + private static String getGlobalCacheManagerName(Bus bus) { + if (bus != null) { + return (String) bus.getProperty(GLOBAL_EHCACHE_MANAGER_NAME); + } else { + return null; + } + } +} http://git-wip-us.apache.org/repos/asf/cxf/blob/657ab96e/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 2fbaa26..af274b8 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 @@ -33,6 +33,7 @@ import org.apache.cxf.Bus; import org.apache.cxf.buslifecycle.BusLifeCycleListener; import org.apache.cxf.buslifecycle.BusLifeCycleManager; import org.apache.cxf.common.util.StringUtils; +import org.apache.cxf.ws.security.cache.EHCacheUtils; import org.apache.wss4j.common.cache.EHCacheManagerHolder; /** @@ -54,11 +55,7 @@ public class EHCacheTokenStore implements TokenStore, Closeable, BusLifeCycleLis if (bus != null) { b.getExtension(BusLifeCycleManager.class).registerLifeCycleListener(this); } - String confName = ""; - if (bus != null) { - confName = bus.getId(); - } - cacheManager = EHCacheManagerHolder.getCacheManager(confName, configFileURL); + cacheManager = EHCacheUtils.getCacheManager(bus, configFileURL); // Cannot overflow to disk as SecurityToken Elements can't be serialized @SuppressWarnings("deprecation") CacheConfiguration cc = EHCacheManagerHolder.getCacheConfiguration(key, cacheManager) @@ -125,6 +122,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/657ab96e/rt/ws/security/src/test/java/org/apache/cxf/ws/security/cache/EHCacheUtilsTest.java ---------------------------------------------------------------------- diff --git a/rt/ws/security/src/test/java/org/apache/cxf/ws/security/cache/EHCacheUtilsTest.java b/rt/ws/security/src/test/java/org/apache/cxf/ws/security/cache/EHCacheUtilsTest.java new file mode 100644 index 0000000..92bcf0b --- /dev/null +++ b/rt/ws/security/src/test/java/org/apache/cxf/ws/security/cache/EHCacheUtilsTest.java @@ -0,0 +1,73 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +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.apache.wss4j.common.cache.EHCacheManagerHolder; +import org.junit.Assert; +import org.junit.Test; + +public class EHCacheUtilsTest extends Assert { + @Test + public void testUseGlobalManager() { + Bus bus = BusFactory.getThreadDefaultBus(); + + Configuration conf = + ConfigurationFactory.parseConfiguration( + EHCacheManagerHolder.class.getResource("/cxf-test-ehcache.xml")); + conf.setName("myGlobalConfig"); + + CacheManager.newInstance(conf); + + CacheManager manager = EHCacheUtils.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(EHCacheUtils.GLOBAL_EHCACHE_MANAGER_NAME, "myGlobalConfig"); + + manager = EHCacheUtils.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(EHCacheUtils.GLOBAL_EHCACHE_MANAGER_NAME, "myGlobalConfigXXX"); + + manager = EHCacheUtils.getCacheManager(bus, + EHCacheManagerHolder.class.getResource("/cxf-test-ehcache.xml")); + + assertFalse(manager.getName().equals("myGlobalConfig")); + EHCacheManagerHolder.releaseCacheManger(manager); + assertEquals(Status.STATUS_SHUTDOWN, manager.getStatus()); + } +} http://git-wip-us.apache.org/repos/asf/cxf/blob/657ab96e/rt/ws/security/src/test/resources/cxf-test-ehcache.xml ---------------------------------------------------------------------- diff --git a/rt/ws/security/src/test/resources/cxf-test-ehcache.xml b/rt/ws/security/src/test/resources/cxf-test-ehcache.xml new file mode 100644 index 0000000..eaef32e --- /dev/null +++ b/rt/ws/security/src/test/resources/cxf-test-ehcache.xml @@ -0,0 +1,16 @@ +<ehcache xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="false" monitoring="autodetect" dynamicConfig="true"> + + <diskStore path="java.io.tmpdir"/> + + <defaultCache + maxEntriesLocalHeap="5000" + eternal="false" + timeToIdleSeconds="3600" + timeToLiveSeconds="3600" + overflowToDisk="true" + maxElementsOnDisk="10000000" + diskPersistent="false" + diskExpiryThreadIntervalSeconds="120" + memoryStoreEvictionPolicy="LRU" + /> +</ehcache> http://git-wip-us.apache.org/repos/asf/cxf/blob/657ab96e/services/sts/sts-core/src/main/java/org/apache/cxf/sts/cache/EHCacheIdentityCache.java ---------------------------------------------------------------------- diff --git a/services/sts/sts-core/src/main/java/org/apache/cxf/sts/cache/EHCacheIdentityCache.java b/services/sts/sts-core/src/main/java/org/apache/cxf/sts/cache/EHCacheIdentityCache.java index afd1cca..fea5c09 100644 --- a/services/sts/sts-core/src/main/java/org/apache/cxf/sts/cache/EHCacheIdentityCache.java +++ b/services/sts/sts-core/src/main/java/org/apache/cxf/sts/cache/EHCacheIdentityCache.java @@ -49,6 +49,7 @@ import org.apache.cxf.management.annotation.ManagedOperation; import org.apache.cxf.management.annotation.ManagedResource; import org.apache.cxf.resource.ResourceManager; import org.apache.cxf.sts.IdentityMapper; +import org.apache.cxf.ws.security.cache.EHCacheUtils; import org.apache.cxf.ws.security.tokenstore.TokenStoreFactory; import org.apache.wss4j.common.cache.EHCacheManagerHolder; import org.apache.wss4j.common.principal.CustomTokenPrincipal; @@ -95,9 +96,9 @@ public class EHCacheIdentityCache } if (configFileURL != null) { - cacheManager = EHCacheManagerHolder.getCacheManager(bus.getId(), configFileURL); + cacheManager = EHCacheUtils.getCacheManager(bus, configFileURL); } else { - cacheManager = EHCacheManagerHolder.getCacheManager(bus.getId(), getDefaultConfigFileURL()); + cacheManager = EHCacheUtils.getCacheManager(bus, getDefaultConfigFileURL()); } CacheConfiguration cc = EHCacheManagerHolder.getCacheConfiguration(key, cacheManager); @@ -230,6 +231,11 @@ public class EHCacheIdentityCache 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;
