Skip to content

Commit

Permalink
Merge pull request #1002 from lprimak/hz-4-5
Browse files Browse the repository at this point in the history
[SHIRO-816] Hazelcast support does not support HZ v4+ by @cstamas
  • Loading branch information
lprimak committed Jul 5, 2023
2 parents 0a037b6 + 468684a commit 2eac0dc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
2 changes: 1 addition & 1 deletion support/hazelcast/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<packaging>bundle</packaging>

<properties>
<hazelcast.osgi.importRange>[3, 4)</hazelcast.osgi.importRange>
<hazelcast.osgi.importRange>[3, 5)</hazelcast.osgi.importRange>
</properties>

<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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;
Expand All @@ -95,9 +117,16 @@ public class HazelcastCacheManager implements CacheManager, Initializable, Destr
* @see #ensureHazelcastInstance()
*
*/
@SuppressWarnings("unchecked")
public <K, V> Cache<K, V> getCache(String name) throws CacheException {
Map<K, V> map = ensureHazelcastInstance().getMap(name); //returned map is a ConcurrentMap
return new MapCache<K, V>(name, map);
try {
MethodHandle getMapHandle = MethodHandles
.lookup().bind(ensureHazelcastInstance(), "getMap", GET_MAP_METHOD_TYPE);
Map<K, V> 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);
}
}

/**
Expand Down Expand Up @@ -242,4 +271,5 @@ public Config getConfig() {
public void setConfig(Config config) {
this.config = config;
}

}

0 comments on commit 2eac0dc

Please sign in to comment.