31
31
import org .slf4j .Logger ;
32
32
import org .slf4j .LoggerFactory ;
33
33
34
+ import java .lang .invoke .MethodHandle ;
35
+ import java .lang .invoke .MethodHandles ;
36
+ import java .lang .invoke .MethodType ;
34
37
import java .util .Map ;
35
38
36
39
/**
75
78
*/
76
79
public class HazelcastCacheManager implements CacheManager , Initializable , Destroyable {
77
80
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
+
78
100
public static final Logger log = LoggerFactory .getLogger (HazelcastCacheManager .class );
79
101
80
102
private boolean implicitlyCreated = false ;
@@ -95,9 +117,16 @@ public class HazelcastCacheManager implements CacheManager, Initializable, Destr
95
117
* @see #ensureHazelcastInstance()
96
118
*
97
119
*/
120
+ @ SuppressWarnings ("unchecked" )
98
121
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
+ }
101
130
}
102
131
103
132
/**
@@ -242,4 +271,5 @@ public Config getConfig() {
242
271
public void setConfig (Config config ) {
243
272
this .config = config ;
244
273
}
274
+
245
275
}
0 commit comments