You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@knox.apache.org by pz...@apache.org on 2024/11/20 19:42:56 UTC

(knox) branch master updated: KNOX-3067: Upgrade ehcache to 3.3.1 (#936)

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

pzampino pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/knox.git


The following commit(s) were added to refs/heads/master by this push:
     new e73cb1a71 KNOX-3067: Upgrade ehcache to 3.3.1 (#936)
e73cb1a71 is described below

commit e73cb1a711f86ead4611517f336e8d83ff007ddf
Author: hanicz <ha...@users.noreply.github.com>
AuthorDate: Wed Nov 20 20:42:52 2024 +0100

    KNOX-3067: Upgrade ehcache to 3.3.1 (#936)
    
    * KNOX-3067: Upgrade ehcache to 3.3.1
    
    * KNOX-3067: Adjust log messages
---
 gateway-provider-security-shiro/pom.xml            |   8 +-
 .../org/apache/knox/gateway/ShiroMessages.java     |  18 ++-
 .../knox/gateway/shirorealm/KnoxCacheManager.java  | 133 ++++++++++++++++++++-
 .../gateway/shirorealm/KnoxCacheManagerTest.java}  |  33 ++++-
 pom.xml                                            |  28 +++--
 5 files changed, 197 insertions(+), 23 deletions(-)

diff --git a/gateway-provider-security-shiro/pom.xml b/gateway-provider-security-shiro/pom.xml
index fb380c2e6..0bf0daf80 100644
--- a/gateway-provider-security-shiro/pom.xml
+++ b/gateway-provider-security-shiro/pom.xml
@@ -73,12 +73,12 @@
         </dependency>
 
         <dependency>
-            <groupId>org.apache.shiro</groupId>
-            <artifactId>shiro-ehcache</artifactId>
+            <groupId>org.ehcache</groupId>
+            <artifactId>ehcache</artifactId>
         </dependency>
         <dependency>
-            <groupId>net.sf.ehcache</groupId>
-            <artifactId>ehcache-core</artifactId>
+            <groupId>org.ehcache.integrations.shiro</groupId>
+            <artifactId>shiro-ehcache3</artifactId>
         </dependency>
 
         <dependency>
diff --git a/gateway-provider-security-shiro/src/main/java/org/apache/knox/gateway/ShiroMessages.java b/gateway-provider-security-shiro/src/main/java/org/apache/knox/gateway/ShiroMessages.java
index 5a51664bf..28879e724 100644
--- a/gateway-provider-security-shiro/src/main/java/org/apache/knox/gateway/ShiroMessages.java
+++ b/gateway-provider-security-shiro/src/main/java/org/apache/knox/gateway/ShiroMessages.java
@@ -20,12 +20,28 @@ package org.apache.knox.gateway;
 import org.apache.knox.gateway.i18n.messages.Message;
 import org.apache.knox.gateway.i18n.messages.MessageLevel;
 import org.apache.knox.gateway.i18n.messages.Messages;
+import org.apache.knox.gateway.i18n.messages.StackTrace;
 
 @Messages(logger="org.apache.knox.gateway")
 public interface ShiroMessages {
-  @Message(level = MessageLevel.INFO, text = "Request {0} matches unauthenticated path configured in topology, letting it through" )
+  @Message( level = MessageLevel.INFO, text = "Request {0} matches unauthenticated path configured in topology, letting it through" )
   void unauthenticatedPathBypass(String uri);
 
   @Message( level = MessageLevel.WARN, text = "Invalid URL pattern for rule: {0}" )
   void invalidURLPattern(String rule);
+
+  @Message( level = MessageLevel.TRACE, text = "Acquiring EhcacheShiro instance named {0}" )
+  void acquireEhcacheShiro(String name);
+
+  @Message( level = MessageLevel.INFO, text = "Cache with name {0} does not exist yet.  Creating now." )
+  void noCacheFound(String name);
+
+  @Message( level = MessageLevel.INFO, text = "Added EhcacheShiro named {0}" )
+  void ehcacheShiroAdded(String name);
+
+  @Message( level = MessageLevel.INFO, text = "Using existing EhcacheShiro named {0}" )
+  void usingExistingEhcacheShiro(String name);
+
+  @Message( level = MessageLevel.WARN, text = "There was an error closing the CacheManager, reason: {0}" )
+  void errorClosingManagedCacheManager(@StackTrace(level=MessageLevel.WARN) Exception e);
 }
diff --git a/gateway-provider-security-shiro/src/main/java/org/apache/knox/gateway/shirorealm/KnoxCacheManager.java b/gateway-provider-security-shiro/src/main/java/org/apache/knox/gateway/shirorealm/KnoxCacheManager.java
index 7b1ad9772..b5af9fa9d 100644
--- a/gateway-provider-security-shiro/src/main/java/org/apache/knox/gateway/shirorealm/KnoxCacheManager.java
+++ b/gateway-provider-security-shiro/src/main/java/org/apache/knox/gateway/shirorealm/KnoxCacheManager.java
@@ -17,11 +17,136 @@
  */
 package org.apache.knox.gateway.shirorealm;
 
-import org.apache.shiro.cache.ehcache.EhCacheManager;
+import org.apache.knox.gateway.ShiroMessages;
+import org.apache.knox.gateway.i18n.messages.MessagesFactory;
+import org.apache.shiro.ShiroException;
+import org.apache.shiro.cache.Cache;
+import org.apache.shiro.cache.CacheException;
+import org.apache.shiro.io.ResourceUtils;
+import org.apache.shiro.util.Destroyable;
+import org.apache.shiro.util.Initializable;
+import org.ehcache.CacheManager;
+import org.ehcache.config.CacheConfiguration;
+import org.ehcache.config.builders.CacheConfigurationBuilder;
+import org.ehcache.config.builders.CacheManagerBuilder;
+import org.ehcache.integrations.shiro.EhcacheShiro;
+import org.ehcache.xml.XmlConfiguration;
 
-public class KnoxCacheManager extends EhCacheManager {
+import java.net.MalformedURLException;
+import java.net.URL;
 
-  public KnoxCacheManager() {
-    setCacheManager(net.sf.ehcache.CacheManager.create());
+public class KnoxCacheManager implements org.apache.shiro.cache.CacheManager, Initializable, Destroyable {
+  private static final ShiroMessages LOG = MessagesFactory.get(ShiroMessages.class);
+
+  private org.ehcache.CacheManager manager;
+  private String cacheManagerConfigFile = "classpath:org/ehcache/integrations/shiro/ehcache.xml";
+  private boolean cacheManagerImplicitlyCreated;
+  private XmlConfiguration cacheConfiguration;
+
+  public CacheManager getCacheManager() {
+    return manager;
+  }
+
+  public void setCacheManager(CacheManager cacheManager) {
+    try {
+      destroy();
+    } catch (Exception e) {
+      LOG.errorClosingManagedCacheManager(e);
+    }
+    manager = cacheManager;
+    cacheManagerImplicitlyCreated = false;
+  }
+
+  public String getCacheManagerConfigFile() {
+    return cacheManagerConfigFile;
+  }
+
+  public void setCacheManagerConfigFile(String cacheManagerConfigFile) {
+    this.cacheManagerConfigFile = cacheManagerConfigFile;
+  }
+
+  @Override
+  public <K, V> Cache<K, V> getCache(String name) throws CacheException {
+    LOG.acquireEhcacheShiro(name);
+    try {
+      org.ehcache.Cache<Object, Object> cache = ensureCacheManager().getCache(name, Object.class, Object.class);
+
+      if (cache == null) {
+        LOG.noCacheFound(name);
+        cache = createCache(name);
+        LOG.ehcacheShiroAdded(name);
+      } else {
+        LOG.usingExistingEhcacheShiro(name);
+      }
+      return new EhcacheShiro<>(cache);
+    } catch (MalformedURLException | ClassNotFoundException | InstantiationException | IllegalAccessException e) {
+      throw new CacheException(e);
+    }
+  }
+
+  private synchronized org.ehcache.Cache<Object, Object> createCache(String name)
+          throws MalformedURLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
+    org.ehcache.Cache<Object, Object> cache = ensureCacheManager().getCache(name, Object.class, Object.class);
+    if (cache == null) {
+      XmlConfiguration xmlConfiguration = getConfiguration();
+      CacheConfigurationBuilder<Object, Object> configurationBuilder = xmlConfiguration.newCacheConfigurationBuilderFromTemplate(
+              "defaultCacheConfiguration", Object.class, Object.class);
+      CacheConfiguration<Object, Object> cacheConfiguration = configurationBuilder.build();
+      cache = ensureCacheManager().createCache(name, cacheConfiguration);
+    }
+    return cache;
+  }
+
+  private org.ehcache.CacheManager ensureCacheManager() throws MalformedURLException {
+    if (manager == null) {
+      manager = CacheManagerBuilder.newCacheManager(getConfiguration());
+      manager.init();
+
+      cacheManagerImplicitlyCreated = true;
+    }
+
+    return manager;
+  }
+
+  private URL getResource() {
+    String URL = ResourceUtils.hasResourcePrefix(this.cacheManagerConfigFile) ?
+            stripPrefix(this.cacheManagerConfigFile) : this.cacheManagerConfigFile;
+
+    ClassLoader loader = Thread.currentThread().getContextClassLoader();
+    if (loader == null) {
+      loader = this.getClass().getClassLoader();
+    }
+
+    return loader.getResource(URL);
+  }
+
+
+  private static String stripPrefix(String resourcePath) {
+    return resourcePath.substring(resourcePath.indexOf(':') + 1);
+  }
+
+  private XmlConfiguration getConfiguration() throws MalformedURLException {
+    if (cacheConfiguration == null) {
+      cacheConfiguration = new XmlConfiguration(getResource());
+    }
+
+    return cacheConfiguration;
+  }
+
+  @Override
+  public void destroy() {
+    if (cacheManagerImplicitlyCreated && manager != null) {
+      manager.close();
+      manager = null;
+    }
+  }
+
+  @Override
+  public void init() throws ShiroException {
+    try {
+      ensureCacheManager();
+    } catch (MalformedURLException e) {
+      throw new ShiroException(e);
+    }
   }
 }
diff --git a/gateway-provider-security-shiro/src/main/java/org/apache/knox/gateway/shirorealm/KnoxCacheManager.java b/gateway-provider-security-shiro/src/test/java/org/apache/knox/gateway/shirorealm/KnoxCacheManagerTest.java
similarity index 50%
copy from gateway-provider-security-shiro/src/main/java/org/apache/knox/gateway/shirorealm/KnoxCacheManager.java
copy to gateway-provider-security-shiro/src/test/java/org/apache/knox/gateway/shirorealm/KnoxCacheManagerTest.java
index 7b1ad9772..39d093235 100644
--- a/gateway-provider-security-shiro/src/main/java/org/apache/knox/gateway/shirorealm/KnoxCacheManager.java
+++ b/gateway-provider-security-shiro/src/test/java/org/apache/knox/gateway/shirorealm/KnoxCacheManagerTest.java
@@ -17,11 +17,32 @@
  */
 package org.apache.knox.gateway.shirorealm;
 
-import org.apache.shiro.cache.ehcache.EhCacheManager;
+import org.apache.shiro.cache.Cache;
+import org.junit.Test;
 
-public class KnoxCacheManager extends EhCacheManager {
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
 
-  public KnoxCacheManager() {
-    setCacheManager(net.sf.ehcache.CacheManager.create());
-  }
-}
+public class KnoxCacheManagerTest {
+
+    @Test
+    public void testConfigFileManipulation() {
+        KnoxCacheManager cacheManager = new KnoxCacheManager();
+        cacheManager.setCacheManagerConfigFile("config.xml");
+
+        assertEquals("config.xml", cacheManager.getCacheManagerConfigFile());
+    }
+
+    @Test
+    public void testGetCache() throws Exception {
+        KnoxCacheManager cacheManager = new KnoxCacheManager();
+        Cache<Object, Object> cache = cacheManager.getCache("cache");
+
+        cache.put("testK", "testV");
+        assertEquals("testV", cache.get("testK"));
+
+        cacheManager.destroy();
+        assertNull(cacheManager.getCacheManager());
+    }
+
+}
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index 0b0cf7602..2f9c3d1ba 100644
--- a/pom.xml
+++ b/pom.xml
@@ -199,7 +199,7 @@
         <dom4j.version>2.1.3</dom4j.version>
         <easymock.version>4.3</easymock.version>
         <eclipselink.version>2.7.8</eclipselink.version>
-        <ehcache.version>2.6.11</ehcache.version>
+        <ehcache.version>3.3.1</ehcache.version>
         <exec-maven-plugin.version>3.0.0</exec-maven-plugin.version>
         <fastinfoset.version>1.2.18</fastinfoset.version>
         <findsecbugs-plugin.version>1.11.0</findsecbugs-plugin.version>
@@ -267,6 +267,7 @@
         <!-- After JDK 11 we can use 0.12.4 from maven central -->
         <pty4j.version>0.11.4</pty4j.version>
         <rest-assured.version>4.3.3</rest-assured.version>
+        <shiro-ehcache3.version>1.0.0</shiro-ehcache3.version>
         <shiro.version>1.13.0</shiro.version>
         <shrinkwrap.version>1.2.6</shrinkwrap.version>
         <shrinkwrap.descriptors.version>2.0.0</shrinkwrap.descriptors.version>
@@ -2051,11 +2052,6 @@
                 <artifactId>shiro-core</artifactId>
                 <version>${shiro.version}</version>
             </dependency>
-            <dependency>
-                <groupId>org.apache.shiro</groupId>
-                <artifactId>shiro-ehcache</artifactId>
-                <version>${shiro.version}</version>
-            </dependency>
             <dependency>
                 <groupId>org.apache.shiro</groupId>
                 <artifactId>shiro-web</artifactId>
@@ -2063,11 +2059,27 @@
             </dependency>
 
             <dependency>
-                <groupId>net.sf.ehcache</groupId>
-                <artifactId>ehcache-core</artifactId>
+                <groupId>org.ehcache</groupId>
+                <artifactId>ehcache</artifactId>
                 <version>${ehcache.version}</version>
             </dependency>
 
+            <dependency>
+                <groupId>org.ehcache.integrations.shiro</groupId>
+                <artifactId>shiro-ehcache3</artifactId>
+                <version>${shiro-ehcache3.version}</version>
+                <exclusions>
+                    <exclusion>
+                        <groupId>org.ehcache</groupId>
+                        <artifactId>ehcache</artifactId>
+                    </exclusion>
+                    <exclusion>
+                        <groupId>org.apache.shiro</groupId>
+                        <artifactId>shiro-core</artifactId>
+                    </exclusion>
+                </exclusions>
+            </dependency>
+
             <dependency>
                 <groupId>org.glassfish.main.libpam4j</groupId>
                 <artifactId>libpam4j</artifactId>