You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by dk...@apache.org on 2012/05/01 23:15:00 UTC

svn commit: r1332833 - in /cxf/trunk: rt/ws/security/src/main/java/org/apache/cxf/ws/security/cache/ rt/ws/security/src/main/java/org/apache/cxf/ws/security/policy/ rt/ws/security/src/main/java/org/apache/cxf/ws/security/tokenstore/ services/sts/systes...

Author: dkulp
Date: Tue May  1 21:15:00 2012
New Revision: 1332833

URL: http://svn.apache.org/viewvc?rev=1332833&view=rev
Log:
[CXF-4279] Try to make sure the ehcache cache manager gets shutdown when
we're done with it.

Added:
    cxf/trunk/rt/ws/security/src/main/java/org/apache/cxf/ws/security/cache/EHCacheManagerHolder.java   (with props)
Modified:
    cxf/trunk/rt/ws/security/src/main/java/org/apache/cxf/ws/security/cache/EHCacheReplayCache.java
    cxf/trunk/rt/ws/security/src/main/java/org/apache/cxf/ws/security/policy/WSSecurityPolicyLoader.java
    cxf/trunk/rt/ws/security/src/main/java/org/apache/cxf/ws/security/tokenstore/EHCacheTokenStore.java
    cxf/trunk/rt/ws/security/src/main/java/org/apache/cxf/ws/security/tokenstore/TokenStore.java
    cxf/trunk/services/sts/systests/basic/src/test/java/org/apache/cxf/systest/sts/asymmetric/AsymmetricBindingTest.java
    cxf/trunk/services/sts/systests/basic/src/test/java/org/apache/cxf/systest/sts/username_actas/UsernameActAsTest.java

Added: cxf/trunk/rt/ws/security/src/main/java/org/apache/cxf/ws/security/cache/EHCacheManagerHolder.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/ws/security/src/main/java/org/apache/cxf/ws/security/cache/EHCacheManagerHolder.java?rev=1332833&view=auto
==============================================================================
--- cxf/trunk/rt/ws/security/src/main/java/org/apache/cxf/ws/security/cache/EHCacheManagerHolder.java (added)
+++ cxf/trunk/rt/ws/security/src/main/java/org/apache/cxf/ws/security/cache/EHCacheManagerHolder.java Tue May  1 21:15:00 2012
@@ -0,0 +1,62 @@
+/**
+ * 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 java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import net.sf.ehcache.CacheManager;
+
+/**
+ * We need to reference count the EHCacheManager things
+ */
+public final class EHCacheManagerHolder {
+    private static final ConcurrentHashMap<String, AtomicInteger> COUNTS 
+        = new ConcurrentHashMap<String, AtomicInteger>(8, 0.75f, 2);
+    
+    private EHCacheManagerHolder() {
+        //utility
+    }
+    
+    public static CacheManager getCacheManager(URL configFileURL) {
+        CacheManager cacheManager;
+        if (configFileURL == null) {
+            cacheManager = CacheManager.create();
+        } else {
+            cacheManager = CacheManager.create(configFileURL);
+        }
+        AtomicInteger a = COUNTS.get(cacheManager.getName());
+        if (a == null) {
+            COUNTS.putIfAbsent(cacheManager.getName(), new AtomicInteger());
+            a = COUNTS.get(cacheManager.getName());
+        }
+        a.incrementAndGet();
+        return cacheManager;
+    }
+    
+    public static void releaseCacheManger(CacheManager cacheManager) {
+        AtomicInteger a = COUNTS.get(cacheManager.getName());
+        if (a.decrementAndGet() == 0) {
+            cacheManager.shutdown();
+        }
+    }
+    
+}

Propchange: cxf/trunk/rt/ws/security/src/main/java/org/apache/cxf/ws/security/cache/EHCacheManagerHolder.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/rt/ws/security/src/main/java/org/apache/cxf/ws/security/cache/EHCacheManagerHolder.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: cxf/trunk/rt/ws/security/src/main/java/org/apache/cxf/ws/security/cache/EHCacheReplayCache.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/ws/security/src/main/java/org/apache/cxf/ws/security/cache/EHCacheReplayCache.java?rev=1332833&r1=1332832&r2=1332833&view=diff
==============================================================================
--- cxf/trunk/rt/ws/security/src/main/java/org/apache/cxf/ws/security/cache/EHCacheReplayCache.java (original)
+++ cxf/trunk/rt/ws/security/src/main/java/org/apache/cxf/ws/security/cache/EHCacheReplayCache.java Tue May  1 21:15:00 2012
@@ -19,6 +19,8 @@
 
 package org.apache.cxf.ws.security.cache;
 
+import java.io.Closeable;
+import java.io.IOException;
 import java.net.URL;
 
 import net.sf.ehcache.Cache;
@@ -31,7 +33,7 @@ import org.apache.ws.security.cache.Repl
  * An in-memory EHCache implementation of the ReplayCache interface. The default TTL is 60 minutes and the
  * max TTL is 12 hours.
  */
-public class EHCacheReplayCache implements ReplayCache {
+public class EHCacheReplayCache implements ReplayCache, Closeable {
     
     public static final long DEFAULT_TTL = 3600L;
     public static final long MAX_TTL = DEFAULT_TTL * 12L;
@@ -40,13 +42,7 @@ public class EHCacheReplayCache implemen
     private long ttl = DEFAULT_TTL;
     
     public EHCacheReplayCache(String key, URL configFileURL) {
-        if (cacheManager == null) {
-            if (configFileURL == null) {
-                cacheManager = CacheManager.create();
-            } else {
-                cacheManager = CacheManager.create(configFileURL);
-            }
-        }
+        cacheManager = EHCacheManagerHolder.getCacheManager(configFileURL);
         if (!cacheManager.cacheExists(key)) {
             cache = new Cache(key, 50000, true, false, DEFAULT_TTL, DEFAULT_TTL);
             cacheManager.addCache(cache);
@@ -117,5 +113,13 @@ public class EHCacheReplayCache implemen
         }
         return false;
     }
+
+    public void close() throws IOException {
+        if (cacheManager != null) {
+            EHCacheManagerHolder.releaseCacheManger(cacheManager);
+            cacheManager = null;
+            cache = null;
+        }
+    }
     
 }

Modified: cxf/trunk/rt/ws/security/src/main/java/org/apache/cxf/ws/security/policy/WSSecurityPolicyLoader.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/ws/security/src/main/java/org/apache/cxf/ws/security/policy/WSSecurityPolicyLoader.java?rev=1332833&r1=1332832&r2=1332833&view=diff
==============================================================================
--- cxf/trunk/rt/ws/security/src/main/java/org/apache/cxf/ws/security/policy/WSSecurityPolicyLoader.java (original)
+++ cxf/trunk/rt/ws/security/src/main/java/org/apache/cxf/ws/security/policy/WSSecurityPolicyLoader.java Tue May  1 21:15:00 2012
@@ -19,6 +19,8 @@
 
 package org.apache.cxf.ws.security.policy;
 
+import java.io.Closeable;
+import java.io.IOException;
 import java.util.Arrays;
 import java.util.List;
 
@@ -26,12 +28,20 @@ import javax.xml.namespace.QName;
 
 import org.apache.cxf.Bus;
 import org.apache.cxf.common.injection.NoJSR250Annotations;
+import org.apache.cxf.endpoint.Client;
+import org.apache.cxf.endpoint.ClientLifeCycleListener;
+import org.apache.cxf.endpoint.ClientLifeCycleManager;
+import org.apache.cxf.endpoint.Server;
+import org.apache.cxf.endpoint.ServerLifeCycleListener;
+import org.apache.cxf.endpoint.ServerLifeCycleManager;
+import org.apache.cxf.service.model.EndpointInfo;
 import org.apache.cxf.ws.policy.AssertionBuilderLoader;
 import org.apache.cxf.ws.policy.AssertionBuilderRegistry;
 import org.apache.cxf.ws.policy.PolicyBuilder;
 import org.apache.cxf.ws.policy.PolicyInterceptorProviderLoader;
 import org.apache.cxf.ws.policy.PolicyInterceptorProviderRegistry;
 import org.apache.cxf.ws.policy.builder.primitive.PrimitiveAssertionBuilder;
+import org.apache.cxf.ws.security.SecurityConstants;
 import org.apache.cxf.ws.security.policy.builders.AlgorithmSuiteBuilder;
 import org.apache.cxf.ws.security.policy.builders.AsymmetricBindingBuilder;
 import org.apache.cxf.ws.security.policy.builders.ContentEncryptedElementsBuilder;
@@ -77,6 +87,8 @@ import org.apache.cxf.ws.security.policy
 import org.apache.cxf.ws.security.policy.interceptors.UsernameTokenInterceptorProvider;
 import org.apache.cxf.ws.security.policy.interceptors.WSSecurityInterceptorProvider;
 import org.apache.cxf.ws.security.policy.interceptors.WSSecurityPolicyInterceptorProvider;
+import org.apache.cxf.ws.security.tokenstore.TokenStore;
+import org.apache.ws.security.cache.ReplayCache;
 
 @NoJSR250Annotations
 public final class WSSecurityPolicyLoader implements PolicyInterceptorProviderLoader, AssertionBuilderLoader {
@@ -93,6 +105,49 @@ public final class WSSecurityPolicyLoade
             //and error out at that point.  If nothing uses ws-securitypolicy
             //no warnings/errors will display
         }
+        ServerLifeCycleManager m = b.getExtension(ServerLifeCycleManager.class);
+        if (m != null) {
+            m.registerListener(new ServerLifeCycleListener() {
+                public void startServer(Server server) {
+                }
+                public void stopServer(Server server) {
+                    shutdownResources(server.getEndpoint().getEndpointInfo());
+                }
+            });
+        }
+        ClientLifeCycleManager cm = b.getExtension(ClientLifeCycleManager.class);
+        if (cm != null) {
+            cm.registerListener(new ClientLifeCycleListener() {
+                public void clientCreated(Client client) {
+                }
+                @Override
+                public void clientDestroyed(Client client) {
+                    shutdownResources(client.getEndpoint().getEndpointInfo());
+                }
+            });
+        }
+    }
+    protected void shutdownResources(EndpointInfo info) {
+        TokenStore ts = (TokenStore)info.getProperty(SecurityConstants.TOKEN_STORE_CACHE_INSTANCE);
+        if (ts instanceof Closeable) {
+            close((Closeable)ts);
+        }
+        ReplayCache rc = (ReplayCache)info.getProperty(SecurityConstants.NONCE_CACHE_INSTANCE);
+        if (rc instanceof Closeable) {
+            close((Closeable)rc);
+        }
+        rc = (ReplayCache)info.getProperty(SecurityConstants.TIMESTAMP_CACHE_INSTANCE);
+        if (rc instanceof Closeable) {
+            close((Closeable)rc);
+        }
+    }
+    
+    private void close(Closeable ts) {
+        try {
+            ts.close();
+        } catch (IOException ex) {
+            //ignore, we're shutting down and nothing we can do
+        }
     }
     public void registerBuilders() {
         AssertionBuilderRegistry reg = bus.getExtension(AssertionBuilderRegistry.class);

Modified: cxf/trunk/rt/ws/security/src/main/java/org/apache/cxf/ws/security/tokenstore/EHCacheTokenStore.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/ws/security/src/main/java/org/apache/cxf/ws/security/tokenstore/EHCacheTokenStore.java?rev=1332833&r1=1332832&r2=1332833&view=diff
==============================================================================
--- cxf/trunk/rt/ws/security/src/main/java/org/apache/cxf/ws/security/tokenstore/EHCacheTokenStore.java (original)
+++ cxf/trunk/rt/ws/security/src/main/java/org/apache/cxf/ws/security/tokenstore/EHCacheTokenStore.java Tue May  1 21:15:00 2012
@@ -19,6 +19,7 @@
 
 package org.apache.cxf.ws.security.tokenstore;
 
+import java.io.Closeable;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.Collection;
@@ -31,12 +32,13 @@ import net.sf.ehcache.CacheManager;
 import net.sf.ehcache.Element;
 
 import org.apache.cxf.common.util.StringUtils;
+import org.apache.cxf.ws.security.cache.EHCacheManagerHolder;
 
 /**
  * An in-memory EHCache implementation of the TokenStore interface. The default TTL is 60 minutes
  * and the max TTL is 12 hours.
  */
-public class EHCacheTokenStore implements TokenStore {
+public class EHCacheTokenStore implements TokenStore, Closeable {
 
     public static final long DEFAULT_TTL = 3600L;
     public static final long MAX_TTL = DEFAULT_TTL * 12L;
@@ -47,14 +49,7 @@ public class EHCacheTokenStore implement
     private long ttl = DEFAULT_TTL;
     
     public EHCacheTokenStore(String key, URL configFileURL) {
-        if (cacheManager == null) {
-            if (configFileURL == null) {
-                cacheManager = CacheManager.create();
-            } else {
-                cacheManager = CacheManager.create(configFileURL);
-            }
-        }
-        
+        cacheManager = EHCacheManagerHolder.getCacheManager(configFileURL);
         if (!cacheManager.cacheExists(key)) {
             // Cannot overflow to disk as SecurityToken Elements can't be serialized
             cache = new Cache(key, MAX_ELEMENTS, false, false, DEFAULT_TTL, DEFAULT_TTL);
@@ -159,5 +154,13 @@ public class EHCacheTokenStore implement
         }
         return parsedTTL;
     }
+
+    public void close() {
+        if (cacheManager != null) {
+            EHCacheManagerHolder.releaseCacheManger(cacheManager);
+            cacheManager = null;
+            cache = null;
+        }
+    }
     
 }

Modified: cxf/trunk/rt/ws/security/src/main/java/org/apache/cxf/ws/security/tokenstore/TokenStore.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/ws/security/src/main/java/org/apache/cxf/ws/security/tokenstore/TokenStore.java?rev=1332833&r1=1332832&r2=1332833&view=diff
==============================================================================
--- cxf/trunk/rt/ws/security/src/main/java/org/apache/cxf/ws/security/tokenstore/TokenStore.java (original)
+++ cxf/trunk/rt/ws/security/src/main/java/org/apache/cxf/ws/security/tokenstore/TokenStore.java Tue May  1 21:15:00 2012
@@ -64,5 +64,5 @@ public interface TokenStore {
      * @return The requested <code>Token</code> identified by the given identifier
      */
     SecurityToken getToken(String identifier);
-    
+
 }

Modified: cxf/trunk/services/sts/systests/basic/src/test/java/org/apache/cxf/systest/sts/asymmetric/AsymmetricBindingTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/services/sts/systests/basic/src/test/java/org/apache/cxf/systest/sts/asymmetric/AsymmetricBindingTest.java?rev=1332833&r1=1332832&r2=1332833&view=diff
==============================================================================
--- cxf/trunk/services/sts/systests/basic/src/test/java/org/apache/cxf/systest/sts/asymmetric/AsymmetricBindingTest.java (original)
+++ cxf/trunk/services/sts/systests/basic/src/test/java/org/apache/cxf/systest/sts/asymmetric/AsymmetricBindingTest.java Tue May  1 21:15:00 2012
@@ -61,7 +61,7 @@ public class AsymmetricBindingTest exten
                 launchServer(Server.class, true)
         );
         String deployment = System.getProperty("sts.deployment");
-        if ("standalone".equals(deployment)) {
+        if ("standalone".equals(deployment) || deployment == null) {
             standalone = true;
             assertTrue(
                     "Server failed to launch",
@@ -98,6 +98,7 @@ public class AsymmetricBindingTest exten
         }
         
         doubleIt(asymmetricSaml1Port, 25);
+        bus.shutdown(true);
     }
 
     @org.junit.Test
@@ -123,6 +124,7 @@ public class AsymmetricBindingTest exten
         doubleIt(asymmetricSaml2Port, 30);
 
         TokenTestUtils.verifyToken(asymmetricSaml2Port);
+        bus.shutdown(true);
     }
 
     @org.junit.Test
@@ -146,6 +148,7 @@ public class AsymmetricBindingTest exten
         }
         
         doubleIt(asymmetricSaml1EncryptedPort, 40);
+        bus.shutdown(true);
     }
 
     private static void doubleIt(DoubleItPortType port, int numToDouble) {

Modified: cxf/trunk/services/sts/systests/basic/src/test/java/org/apache/cxf/systest/sts/username_actas/UsernameActAsTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/services/sts/systests/basic/src/test/java/org/apache/cxf/systest/sts/username_actas/UsernameActAsTest.java?rev=1332833&r1=1332832&r2=1332833&view=diff
==============================================================================
--- cxf/trunk/services/sts/systests/basic/src/test/java/org/apache/cxf/systest/sts/username_actas/UsernameActAsTest.java (original)
+++ cxf/trunk/services/sts/systests/basic/src/test/java/org/apache/cxf/systest/sts/username_actas/UsernameActAsTest.java Tue May  1 21:15:00 2012
@@ -67,7 +67,7 @@ public class UsernameActAsTest extends A
             launchServer(Server.class, true)
         );
         String deployment = System.getProperty("sts.deployment");
-        if ("standalone".equals(deployment)) {
+        if ("standalone".equals(deployment) || deployment == null) {
             standalone = true;
             assertTrue(
                     "Server failed to launch",