You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by bu...@apache.org on 2021/02/26 22:05:03 UTC

[cxf-fediz] branch cxf340 updated: Support EhCache 3+

This is an automated email from the ASF dual-hosted git repository.

buhhunyx pushed a commit to branch cxf340
in repository https://gitbox.apache.org/repos/asf/cxf-fediz.git


The following commit(s) were added to refs/heads/cxf340 by this push:
     new 7159af4  Support EhCache 3+
7159af4 is described below

commit 7159af41389e63256332623acb9f0eb2bcb81ab5
Author: Alexey Markevich <bu...@gmail.com>
AuthorDate: Sat Feb 27 01:03:57 2021 +0300

    Support EhCache 3+
---
 plugins/core/pom.xml                               |   6 +
 plugins/core/src/main/resources/fediz-ehcache.xml  |  35 ------
 plugins/core/src/test/resources/logging.properties |   2 +-
 plugins/cxf/pom.xml                                |  36 ++----
 .../cxf/plugin/AbstractServiceProviderFilter.java  |  10 +-
 .../cxf/plugin/state/EHCacheSPStateManager.java    | 131 ++++++---------------
 plugins/cxf/src/main/resources/fediz-ehcache.xml   |  25 ++++
 .../plugin/state/EHCacheSPStateManagerTest.java    |  78 ++++++++++++
 pom.xml                                            |   2 +-
 .../cxf/fediz/systests/cxf/FederationTest.java     |   1 -
 .../src/main/resources/logging.properties          |   2 +-
 .../src/main/resources/logging.properties          |   2 +-
 12 files changed, 167 insertions(+), 163 deletions(-)

diff --git a/plugins/core/pom.xml b/plugins/core/pom.xml
index 8e0d119..f6ef8a0 100644
--- a/plugins/core/pom.xml
+++ b/plugins/core/pom.xml
@@ -74,6 +74,12 @@
             <version>${slf4j.version}</version>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>xerces</groupId>
+            <artifactId>xercesImpl</artifactId>
+            <version>2.12.1</version>
+            <scope>test</scope>
+       </dependency>
     </dependencies>
     <build>
         <plugins>
diff --git a/plugins/core/src/main/resources/fediz-ehcache.xml b/plugins/core/src/main/resources/fediz-ehcache.xml
deleted file mode 100644
index b888f0f..0000000
--- a/plugins/core/src/main/resources/fediz-ehcache.xml
+++ /dev/null
@@ -1,35 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!--
-  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.
--->
-<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>
diff --git a/plugins/core/src/test/resources/logging.properties b/plugins/core/src/test/resources/logging.properties
index 040b210..0d8d902 100644
--- a/plugins/core/src/test/resources/logging.properties
+++ b/plugins/core/src/test/resources/logging.properties
@@ -15,7 +15,7 @@
 # Note that these classes must be on the system classpath.
 # By default we only configure a ConsoleHandler, which will only
 # show messages at the WARNING and above levels.
-handlers= java.util.logging.ConsoleHandler
+#handlers= java.util.logging.ConsoleHandler
 #handlers= java.util.logging.FileHandler, java.util.logging.ConsoleHandler
 
 # Default global logging level.
diff --git a/plugins/cxf/pom.xml b/plugins/cxf/pom.xml
index 9d7239c..bf62ba2 100644
--- a/plugins/cxf/pom.xml
+++ b/plugins/cxf/pom.xml
@@ -39,12 +39,6 @@
             <scope>compile</scope>
         </dependency>
         <dependency>
-            <groupId>net.sf.ehcache</groupId>
-            <artifactId>ehcache</artifactId>
-            <version>${ehcache.version}</version>
-            <scope>compile</scope>
-        </dependency>
-        <dependency>
             <groupId>javax.servlet</groupId>
             <artifactId>javax.servlet-api</artifactId>
             <version>${servlet.version}</version>
@@ -68,25 +62,21 @@
             <version>${cxf.version}</version>
             <scope>compile</scope>
         </dependency>
+        <dependency>
+            <groupId>org.ehcache</groupId>
+            <artifactId>ehcache</artifactId>
+            <version>${ehcache3.version}</version>
+        </dependency>
+
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <version>${junit.version}</version>
+            <scope>test</scope>
+        </dependency> 
     </dependencies>
+
     <build>
-        <sourceDirectory>${basedir}/src/main/java</sourceDirectory>
-        <resources>
-            <resource>
-                <directory>src/main/java</directory>
-                <excludes>
-                    <exclude>**/*.java</exclude>
-                </excludes>
-            </resource>
-            <resource>
-                <directory>src/main/resources</directory>
-                <includes>
-                    <include>**/*</include>
-                </includes>
-            </resource>
-        </resources>
-        
-        
         <plugins>
             <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
diff --git a/plugins/cxf/src/main/java/org/apache/cxf/fediz/cxf/plugin/AbstractServiceProviderFilter.java b/plugins/cxf/src/main/java/org/apache/cxf/fediz/cxf/plugin/AbstractServiceProviderFilter.java
index 679f9e7..5ad15bf 100644
--- a/plugins/cxf/src/main/java/org/apache/cxf/fediz/cxf/plugin/AbstractServiceProviderFilter.java
+++ b/plugins/cxf/src/main/java/org/apache/cxf/fediz/cxf/plugin/AbstractServiceProviderFilter.java
@@ -21,7 +21,6 @@ package org.apache.cxf.fediz.cxf.plugin;
 import java.io.File;
 import java.io.IOException;
 import java.io.StringReader;
-import java.net.MalformedURLException;
 import java.net.URI;
 import java.net.URL;
 import java.util.List;
@@ -117,12 +116,9 @@ public abstract class AbstractServiceProviderFilter implements ContainerRequestF
             } catch (JAXBException e) {
                 LOG.error("Error in parsing configuration", e);
                 throw e;
-            } catch (MalformedURLException e) {
+            } catch (IOException e) {
                 LOG.error("Error in loading configuration file", e);
                 throw e;
-            } catch (Exception e) {
-                LOG.error("Error in loading configuration file", e);
-                throw new IOException(e);
             }
         }
 
@@ -146,7 +142,9 @@ public abstract class AbstractServiceProviderFilter implements ContainerRequestF
             }
         }
 
-        stateManager.close();
+        if (stateManager != null) {
+            stateManager.close();
+        }
     }
 
     protected boolean checkSecurityContext(FedizContext fedConfig, Message m, MultivaluedMap<String, String> params) {
diff --git a/plugins/cxf/src/main/java/org/apache/cxf/fediz/cxf/plugin/state/EHCacheSPStateManager.java b/plugins/cxf/src/main/java/org/apache/cxf/fediz/cxf/plugin/state/EHCacheSPStateManager.java
index 27c4bc6..159a40a 100644
--- a/plugins/cxf/src/main/java/org/apache/cxf/fediz/cxf/plugin/state/EHCacheSPStateManager.java
+++ b/plugins/cxf/src/main/java/org/apache/cxf/fediz/cxf/plugin/state/EHCacheSPStateManager.java
@@ -21,14 +21,12 @@ package org.apache.cxf.fediz.cxf.plugin.state;
 import java.io.IOException;
 import java.net.URL;
 
-import net.sf.ehcache.Cache;
-import net.sf.ehcache.CacheManager;
-import net.sf.ehcache.Ehcache;
-import net.sf.ehcache.Element;
-import net.sf.ehcache.config.CacheConfiguration;
 import org.apache.cxf.fediz.core.RequestState;
-import org.apache.wss4j.common.cache.EHCacheManagerHolder;
 import org.apache.wss4j.common.util.Loader;
+import org.ehcache.Cache;
+import org.ehcache.CacheManager;
+import org.ehcache.config.builders.CacheManagerBuilder;
+import org.ehcache.xml.XmlConfiguration;
 
 /**
  * An in-memory EHCache implementation of the SPStateManager interface.
@@ -36,137 +34,82 @@ import org.apache.wss4j.common.util.Loader;
  */
 public class EHCacheSPStateManager implements SPStateManager {
 
-    public static final long DEFAULT_TTL = 60L * 5L;
     public static final String REQUEST_CACHE_KEY = "cxf.fediz.samlp.request.state.cache";
     public static final String RESPONSE_CACHE_KEY = "cxf.fediz.samlp.response.state.cache";
 
-    private Ehcache requestCache;
-    private Ehcache responseCache;
+    private Cache<String, RequestState> requestCache;
+    private Cache<String, ResponseState> responseCache;
     private CacheManager cacheManager;
-    private long ttl = DEFAULT_TTL;
 
     public EHCacheSPStateManager(String configFile) {
         this(getConfigFileURL(configFile));
     }
 
     public EHCacheSPStateManager(URL configFileURL) {
-        this(EHCacheManagerHolder.getCacheManager("", configFileURL));
+        XmlConfiguration xmlConfig = new XmlConfiguration(configFileURL);
+        cacheManager = CacheManagerBuilder.newCacheManager(xmlConfig); 
+        cacheManager.init();
+        initCache();
     }
 
     public EHCacheSPStateManager(CacheManager cacheManager) {
         this.cacheManager = cacheManager;
-
-        CacheConfiguration requestCC = EHCacheManagerHolder.getCacheConfiguration(REQUEST_CACHE_KEY, cacheManager);
-
-        Ehcache newCache = new Cache(requestCC);
-        requestCache = cacheManager.addCacheIfAbsent(newCache);
-
-        CacheConfiguration responseCC = EHCacheManagerHolder.getCacheConfiguration(RESPONSE_CACHE_KEY, cacheManager);
-
-        newCache = new Cache(responseCC);
-        responseCache = cacheManager.addCacheIfAbsent(newCache);
+        initCache();
     }
 
-    private static URL getConfigFileURL(Object o) {
-        if (o instanceof String) {
-            try {
-                URL url = Loader.getResource((String)o);
-                if (url == null) {
-                    url = new URL((String)o);
-                }
-                return url;
-            } catch (IOException e) {
-                // Do nothing
+    private static URL getConfigFileURL(String configFile) {
+        try {
+            URL url = Loader.getResource(configFile);
+            if (url == null) {
+                url = new URL(configFile);
             }
-        } else if (o instanceof URL) {
-            return (URL)o;
+            return url;
+        } catch (IOException e) {
+            // Do nothing
         }
         return null;
     }
 
-    /**
-     * Set a new (default) TTL value in seconds
-     * @param newTtl a new (default) TTL value in seconds
-     */
-    public void setTTL(long newTtl) {
-        ttl = newTtl;
-    }
-
-    /**
-     * Get the (default) TTL value in seconds
-     * @return the (default) TTL value in seconds
-     */
-    public long getTTL() {
-        return ttl;
+    private void initCache() {
+        requestCache = cacheManager.getCache(REQUEST_CACHE_KEY, String.class, RequestState.class);
+        responseCache = cacheManager.getCache(RESPONSE_CACHE_KEY, String.class, ResponseState.class);
     }
 
     public void setRequestState(String relayState, RequestState state) {
-        if (relayState == null || "".equals(relayState)) {
-            return;
+        if (relayState != null && !relayState.isEmpty()) {
+            requestCache.put(relayState, state);
         }
-
-        int parsedTTL = (int)ttl;
-        if (ttl != (long)parsedTTL) {
-            // Fall back to 60 minutes if the default TTL is set incorrectly
-            parsedTTL = 3600;
-        }
-
-        Element element = new Element(relayState, state);
-        element.setTimeToLive(parsedTTL);
-        element.setTimeToIdle(parsedTTL);
-        requestCache.put(element);
     }
 
     public RequestState removeRequestState(String relayState) {
-        Element element = requestCache.get(relayState);
-        if (element != null) {
+        RequestState state = requestCache.get(relayState);
+        if (state != null) {
             requestCache.remove(relayState);
-            return (RequestState)element.getObjectValue();
         }
-        return null;
+        return state;
     }
 
     public ResponseState getResponseState(String securityContextKey) {
-        Element element = responseCache.get(securityContextKey);
-        if (element != null) {
-            if (responseCache.isExpired(element)) {
-                responseCache.remove(securityContextKey);
-                return null;
-            }
-            return (ResponseState)element.getObjectValue();
-        }
-        return null;
-    }
-
-    public ResponseState removeResponseState(String securityContextKey) {
-        Element element = responseCache.get(securityContextKey);
-        if (element != null) {
-            responseCache.remove(securityContextKey);
-            return (ResponseState)element.getObjectValue();
-        }
-        return null;
+        return responseCache.get(securityContextKey);
     }
 
     public void setResponseState(String securityContextKey, ResponseState state) {
-        if (securityContextKey == null || "".equals(securityContextKey)) {
-            return;
+        if (securityContextKey != null && !securityContextKey.isEmpty()) {
+            responseCache.put(securityContextKey, state);
         }
+    }
 
-        int parsedTTL = (int)ttl;
-        if (ttl != (long)parsedTTL) {
-            // Fall back to 5 minutes if the default TTL is set incorrectly
-            parsedTTL = 60 * 5;
+    public ResponseState removeResponseState(String securityContextKey) {
+        ResponseState state = responseCache.get(securityContextKey);
+        if (state != null) {
+            responseCache.remove(securityContextKey);
         }
-        Element element = new Element(securityContextKey, state);
-        element.setTimeToLive(parsedTTL);
-        element.setTimeToIdle(parsedTTL);
-
-        responseCache.put(element);
+        return state;
     }
 
     public void close() throws IOException {
         if (cacheManager != null) {
-            cacheManager.shutdown();
+            cacheManager.close();
             cacheManager = null;
             requestCache = null;
             responseCache = null;
diff --git a/plugins/cxf/src/main/resources/fediz-ehcache.xml b/plugins/cxf/src/main/resources/fediz-ehcache.xml
new file mode 100644
index 0000000..c45fb18
--- /dev/null
+++ b/plugins/cxf/src/main/resources/fediz-ehcache.xml
@@ -0,0 +1,25 @@
+<config
+    xmlns='http://www.ehcache.org/v3'
+    xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
+    xsi:schemaLocation="http://www.ehcache.org/v3 https://www.ehcache.org/schema/ehcache-core.xsd">
+
+    <cache-template name="cxf.fediz.samlp.state.cache.template">
+        <key-type>java.lang.String</key-type>
+        <expiry>
+            <ttl unit="seconds">300</ttl>
+        </expiry>
+        <resources>
+            <heap unit="entries">5000</heap>
+            <offheap unit="MB">10</offheap>
+        </resources>
+    </cache-template>
+
+    <cache alias="cxf.fediz.samlp.request.state.cache" uses-template="cxf.fediz.samlp.state.cache.template">
+        <value-type>org.apache.cxf.fediz.core.RequestState</value-type>
+    </cache>
+
+    <cache alias="cxf.fediz.samlp.response.state.cache" uses-template="cxf.fediz.samlp.state.cache.template">
+        <value-type>org.apache.cxf.fediz.cxf.plugin.state.ResponseState</value-type>
+    </cache>
+
+</config>
diff --git a/plugins/cxf/src/test/java/org/apache/cxf/fediz/cxf/plugin/state/EHCacheSPStateManagerTest.java b/plugins/cxf/src/test/java/org/apache/cxf/fediz/cxf/plugin/state/EHCacheSPStateManagerTest.java
new file mode 100644
index 0000000..2a25767
--- /dev/null
+++ b/plugins/cxf/src/test/java/org/apache/cxf/fediz/cxf/plugin/state/EHCacheSPStateManagerTest.java
@@ -0,0 +1,78 @@
+/**
+ * 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.fediz.cxf.plugin.state;
+
+import org.apache.cxf.fediz.core.RequestState;
+
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
+public class EHCacheSPStateManagerTest {
+
+    private static final int HEAP_ENTRIES = 5000;
+
+    private static SPStateManager stateManager;
+
+    @Test
+    public void testRequestState() {
+        RequestState requestState = new RequestState();
+        for (int i = 0; i < 2 * HEAP_ENTRIES; ++i) {
+            stateManager.setRequestState(String.valueOf(i), requestState);
+        }
+        for (int i = 0; i < 2 * HEAP_ENTRIES; ++i) {
+            assertNotNull(String.valueOf(i), stateManager.removeRequestState(String.valueOf(i)));
+            assertNull(stateManager.removeRequestState(String.valueOf(i)));
+        }
+    }
+
+    @Test
+    public void testResponseState() {
+        assertNull(stateManager.getResponseState(""));
+        ResponseState responseState = new ResponseState();
+        for (int i = 0; i < 2 * HEAP_ENTRIES; ++i) {
+            stateManager.setResponseState(String.valueOf(i), responseState);
+        }
+        for (int i = 0; i < 2 * HEAP_ENTRIES; ++i) {
+            assertNotNull(String.valueOf(i), stateManager.getResponseState(String.valueOf(i)));
+            assertNotNull(String.valueOf(i), stateManager.removeResponseState(String.valueOf(i)));
+            assertNull(stateManager.removeResponseState(String.valueOf(i)));
+        }
+    }
+
+    @Test
+    public void testTwoManagers() throws Exception {
+        try (SPStateManager stateManager = new EHCacheSPStateManager("fediz-ehcache.xml")) {
+            assertNotNull(stateManager);
+        }
+    }
+
+    @BeforeClass
+    public static void beforeClass() throws Exception {
+        stateManager = new EHCacheSPStateManager("fediz-ehcache.xml");
+    }
+
+    @AfterClass
+    public static void afterClass() throws Exception {
+        stateManager.close();
+    }
+}
diff --git a/pom.xml b/pom.xml
index 21cccd6..5a6e504 100644
--- a/pom.xml
+++ b/pom.xml
@@ -62,7 +62,7 @@
         <slf4j.version>1.7.30</slf4j.version>
         <spring.version>4.3.30.RELEASE</spring.version>
         <spring-ldap-core.version>2.3.3.RELEASE</spring-ldap-core.version>
-        <spring.security.version>4.2.19.RELEASE</spring.security.version>
+        <spring.security.version>4.2.20.RELEASE</spring.security.version>
         <spring-webflow.version>2.5.1.RELEASE</spring-webflow.version>
         <tomcat.version>9.0.43</tomcat.version>
         <validation-api.version>2.0.2</validation-api.version>
diff --git a/systests/cxf/src/test/java/org/apache/cxf/fediz/systests/cxf/FederationTest.java b/systests/cxf/src/test/java/org/apache/cxf/fediz/systests/cxf/FederationTest.java
index 53817e7..3636a8a 100644
--- a/systests/cxf/src/test/java/org/apache/cxf/fediz/systests/cxf/FederationTest.java
+++ b/systests/cxf/src/test/java/org/apache/cxf/fediz/systests/cxf/FederationTest.java
@@ -56,7 +56,6 @@ public class FederationTest extends AbstractTests {
     @BeforeClass
     public static void init() throws Exception {
         idpHttpsPort = System.getProperty("idp.https.port");
-        // idpHttpsPort = "12345";
         Assert.assertNotNull("Property 'idp.https.port' null", idpHttpsPort);
         rpHttpsPort = System.getProperty("rp.https.port");
         Assert.assertNotNull("Property 'rp.https.port' null", rpHttpsPort);
diff --git a/systests/federation/oidcIdpWebapp/src/main/resources/logging.properties b/systests/federation/oidcIdpWebapp/src/main/resources/logging.properties
index 20617fe..a173f06 100644
--- a/systests/federation/oidcIdpWebapp/src/main/resources/logging.properties
+++ b/systests/federation/oidcIdpWebapp/src/main/resources/logging.properties
@@ -15,7 +15,7 @@
 # Note that these classes must be on the system classpath.
 # By default we only configure a ConsoleHandler, which will only
 # show messages at the WARNING and above levels.
-handlers= java.util.logging.ConsoleHandler
+#handlers= java.util.logging.ConsoleHandler
 #handlers= java.util.logging.FileHandler, java.util.logging.ConsoleHandler
 
 # Default global logging level.
diff --git a/systests/federation/samlIdpWebapp/src/main/resources/logging.properties b/systests/federation/samlIdpWebapp/src/main/resources/logging.properties
index 20617fe..a173f06 100644
--- a/systests/federation/samlIdpWebapp/src/main/resources/logging.properties
+++ b/systests/federation/samlIdpWebapp/src/main/resources/logging.properties
@@ -15,7 +15,7 @@
 # Note that these classes must be on the system classpath.
 # By default we only configure a ConsoleHandler, which will only
 # show messages at the WARNING and above levels.
-handlers= java.util.logging.ConsoleHandler
+#handlers= java.util.logging.ConsoleHandler
 #handlers= java.util.logging.FileHandler, java.util.logging.ConsoleHandler
 
 # Default global logging level.