Skip to content

Commit 2eac0dc

Browse files
authored
Merge pull request #1002 from lprimak/hz-4-5
[SHIRO-816] Hazelcast support does not support HZ v4+ by @cstamas
2 parents 0a037b6 + 468684a commit 2eac0dc

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

support/hazelcast/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
<packaging>bundle</packaging>
3232

3333
<properties>
34-
<hazelcast.osgi.importRange>[3, 4)</hazelcast.osgi.importRange>
34+
<hazelcast.osgi.importRange>[3, 5)</hazelcast.osgi.importRange>
3535
</properties>
3636

3737
<dependencies>

support/hazelcast/src/main/java/org/apache/shiro/hazelcast/cache/HazelcastCacheManager.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
import org.slf4j.Logger;
3232
import org.slf4j.LoggerFactory;
3333

34+
import java.lang.invoke.MethodHandle;
35+
import java.lang.invoke.MethodHandles;
36+
import java.lang.invoke.MethodType;
3437
import java.util.Map;
3538

3639
/**
@@ -75,6 +78,25 @@
7578
*/
7679
public class HazelcastCacheManager implements CacheManager, Initializable, Destroyable {
7780

81+
private static final Class<?> IMAP_CLASS;
82+
83+
private static final MethodType GET_MAP_METHOD_TYPE;
84+
85+
static {
86+
Class<?> klazz;
87+
try {
88+
klazz = HazelcastCacheManager.class.getClassLoader().loadClass( "com.hazelcast.core.IMap" );
89+
} catch ( ClassNotFoundException e1 ) {
90+
try {
91+
klazz = HazelcastCacheManager.class.getClassLoader().loadClass( "com.hazelcast.map.IMap" );
92+
} catch ( ClassNotFoundException e2 ) {
93+
throw new IllegalStateException("Could not find Hazelcast v3 or v4 on classpath");
94+
}
95+
}
96+
IMAP_CLASS = klazz;
97+
GET_MAP_METHOD_TYPE = MethodType.methodType( IMAP_CLASS, String.class );
98+
}
99+
78100
public static final Logger log = LoggerFactory.getLogger(HazelcastCacheManager.class);
79101

80102
private boolean implicitlyCreated = false;
@@ -95,9 +117,16 @@ public class HazelcastCacheManager implements CacheManager, Initializable, Destr
95117
* @see #ensureHazelcastInstance()
96118
*
97119
*/
120+
@SuppressWarnings("unchecked")
98121
public <K, V> Cache<K, V> getCache(String name) throws CacheException {
99-
Map<K, V> map = ensureHazelcastInstance().getMap(name); //returned map is a ConcurrentMap
100-
return new MapCache<K, V>(name, map);
122+
try {
123+
MethodHandle getMapHandle = MethodHandles
124+
.lookup().bind(ensureHazelcastInstance(), "getMap", GET_MAP_METHOD_TYPE);
125+
Map<K, V> map = (Map) getMapHandle.invoke(name); //returned map is a ConcurrentMap
126+
return new MapCache<>(name, map);
127+
} catch (Throwable e) {
128+
throw new CacheException("Unable to get IMap", e);
129+
}
101130
}
102131

103132
/**
@@ -242,4 +271,5 @@ public Config getConfig() {
242271
public void setConfig(Config config) {
243272
this.config = config;
244273
}
274+
245275
}

0 commit comments

Comments
 (0)