From 468684af08aef3d4044dbb28e7f4ef22455836c3 Mon Sep 17 00:00:00 2001 From: lprimak Date: Tue, 4 Jul 2023 20:33:32 -0500 Subject: [PATCH] [SHIRO-816] Hazelcast support does not support HZ v4+ by @cstamas --- support/hazelcast/pom.xml | 2 +- .../cache/HazelcastCacheManager.java | 34 +++++++++++++++++-- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/support/hazelcast/pom.xml b/support/hazelcast/pom.xml index f286eb20e1..5691da6eae 100644 --- a/support/hazelcast/pom.xml +++ b/support/hazelcast/pom.xml @@ -31,7 +31,7 @@ bundle - [3, 4) + [3, 5) diff --git a/support/hazelcast/src/main/java/org/apache/shiro/hazelcast/cache/HazelcastCacheManager.java b/support/hazelcast/src/main/java/org/apache/shiro/hazelcast/cache/HazelcastCacheManager.java index 68db4c9ae4..c6232d626a 100644 --- a/support/hazelcast/src/main/java/org/apache/shiro/hazelcast/cache/HazelcastCacheManager.java +++ b/support/hazelcast/src/main/java/org/apache/shiro/hazelcast/cache/HazelcastCacheManager.java @@ -31,6 +31,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.lang.invoke.MethodHandle; +import java.lang.invoke.MethodHandles; +import java.lang.invoke.MethodType; import java.util.Map; /** @@ -75,6 +78,25 @@ */ public class HazelcastCacheManager implements CacheManager, Initializable, Destroyable { + private static final Class IMAP_CLASS; + + private static final MethodType GET_MAP_METHOD_TYPE; + + static { + Class klazz; + try { + klazz = HazelcastCacheManager.class.getClassLoader().loadClass( "com.hazelcast.core.IMap" ); + } catch ( ClassNotFoundException e1 ) { + try { + klazz = HazelcastCacheManager.class.getClassLoader().loadClass( "com.hazelcast.map.IMap" ); + } catch ( ClassNotFoundException e2 ) { + throw new IllegalStateException("Could not find Hazelcast v3 or v4 on classpath"); + } + } + IMAP_CLASS = klazz; + GET_MAP_METHOD_TYPE = MethodType.methodType( IMAP_CLASS, String.class ); + } + public static final Logger log = LoggerFactory.getLogger(HazelcastCacheManager.class); private boolean implicitlyCreated = false; @@ -95,9 +117,16 @@ public class HazelcastCacheManager implements CacheManager, Initializable, Destr * @see #ensureHazelcastInstance() * */ + @SuppressWarnings("unchecked") public Cache getCache(String name) throws CacheException { - Map map = ensureHazelcastInstance().getMap(name); //returned map is a ConcurrentMap - return new MapCache(name, map); + try { + MethodHandle getMapHandle = MethodHandles + .lookup().bind(ensureHazelcastInstance(), "getMap", GET_MAP_METHOD_TYPE); + Map map = (Map) getMapHandle.invoke(name); //returned map is a ConcurrentMap + return new MapCache<>(name, map); + } catch (Throwable e) { + throw new CacheException("Unable to get IMap", e); + } } /** @@ -242,4 +271,5 @@ public Config getConfig() { public void setConfig(Config config) { this.config = config; } + }