1
1
package org .hibernate .cache .infinispan ;
2
+
2
3
import java .io .IOException ;
3
4
import java .util .Collections ;
4
5
import java .util .Enumeration ;
7
8
import java .util .Map ;
8
9
import java .util .Properties ;
9
10
import java .util .Set ;
11
+ import java .util .concurrent .ConcurrentHashMap ;
12
+ import java .util .concurrent .ConcurrentMap ;
10
13
import javax .transaction .TransactionManager ;
14
+
15
+ import org .hibernate .cache .infinispan .impl .BaseRegion ;
16
+ import org .hibernate .cache .infinispan .util .CacheCommandFactory ;
17
+ import org .hibernate .cache .infinispan .util .CacheCommandInitializer ;
11
18
import org .hibernate .cache .spi .CacheDataDescription ;
12
19
import org .hibernate .cache .CacheException ;
13
20
import org .hibernate .cache .spi .CollectionRegion ;
28
35
import org .hibernate .cfg .Settings ;
29
36
import org .hibernate .internal .util .config .ConfigurationHelper ;
30
37
import org .infinispan .AdvancedCache ;
31
- import org .infinispan .Cache ;
32
38
import org .infinispan .config .Configuration ;
39
+ import org .infinispan .factories .ComponentRegistry ;
40
+ import org .infinispan .factories .GlobalComponentRegistry ;
33
41
import org .infinispan .manager .DefaultCacheManager ;
34
42
import org .infinispan .manager .EmbeddedCacheManager ;
35
43
import org .infinispan .util .logging .Log ;
@@ -155,6 +163,9 @@ public class InfinispanRegionFactory implements RegionFactory {
155
163
private org .infinispan .transaction .lookup .TransactionManagerLookup transactionManagerlookup ;
156
164
157
165
private TransactionManager transactionManager ;
166
+
167
+ private ConcurrentMap <String , BaseRegion > allRegions =
168
+ new ConcurrentHashMap <String , BaseRegion >();
158
169
159
170
/**
160
171
* Create a new instance using the default configuration.
@@ -174,20 +185,20 @@ public InfinispanRegionFactory(Properties props) {
174
185
/** {@inheritDoc} */
175
186
public CollectionRegion buildCollectionRegion (String regionName , Properties properties , CacheDataDescription metadata ) throws CacheException {
176
187
if (log .isDebugEnabled ()) log .debug ("Building collection cache region [" + regionName + "]" );
177
- Cache cache = getCache (regionName , COLLECTION_KEY , properties );
188
+ AdvancedCache cache = getCache (regionName , COLLECTION_KEY , properties );
178
189
CacheAdapter cacheAdapter = CacheAdapterImpl .newInstance (cache );
179
190
CollectionRegionImpl region = new CollectionRegionImpl (cacheAdapter , regionName , metadata , transactionManager , this );
180
- region . start ( );
191
+ startRegion ( region , regionName );
181
192
return region ;
182
193
}
183
194
184
195
/** {@inheritDoc} */
185
196
public EntityRegion buildEntityRegion (String regionName , Properties properties , CacheDataDescription metadata ) throws CacheException {
186
197
if (log .isDebugEnabled ()) log .debug ("Building entity cache region [" + regionName + "]" );
187
- Cache cache = getCache (regionName , ENTITY_KEY , properties );
198
+ AdvancedCache cache = getCache (regionName , ENTITY_KEY , properties );
188
199
CacheAdapter cacheAdapter = CacheAdapterImpl .newInstance (cache );
189
200
EntityRegionImpl region = new EntityRegionImpl (cacheAdapter , regionName , metadata , transactionManager , this );
190
- region . start ( );
201
+ startRegion ( region , regionName );
191
202
return region ;
192
203
}
193
204
@@ -202,10 +213,10 @@ public QueryResultsRegion buildQueryResultsRegion(String regionName, Properties
202
213
if (!regionName .equals ("org.hibernate.cache.internal.StandardQueryCache" ))
203
214
cacheName = regionName ;
204
215
205
- Cache cache = getCache (cacheName , QUERY_KEY , properties );
216
+ AdvancedCache cache = getCache (cacheName , QUERY_KEY , properties );
206
217
CacheAdapter cacheAdapter = CacheAdapterImpl .newInstance (cache );
207
218
QueryResultsRegionImpl region = new QueryResultsRegionImpl (cacheAdapter , regionName , properties , transactionManager , this );
208
- region . start ( );
219
+ startRegion ( region , regionName );
209
220
return region ;
210
221
}
211
222
@@ -215,21 +226,17 @@ public QueryResultsRegion buildQueryResultsRegion(String regionName, Properties
215
226
public TimestampsRegion buildTimestampsRegion (String regionName , Properties properties )
216
227
throws CacheException {
217
228
if (log .isDebugEnabled ()) log .debug ("Building timestamps cache region [" + regionName + "]" );
218
- Cache cache = getCache (regionName , TIMESTAMPS_KEY , properties );
229
+ AdvancedCache cache = getCache (regionName , TIMESTAMPS_KEY , properties );
219
230
CacheAdapter cacheAdapter = CacheAdapterImpl .newInstance (cache );
220
231
TimestampsRegionImpl region = createTimestampsRegion (cacheAdapter , regionName );
221
- region . start ( );
232
+ startRegion ( region , regionName );
222
233
return region ;
223
234
}
224
235
225
236
protected TimestampsRegionImpl createTimestampsRegion (CacheAdapter cacheAdapter , String regionName ) {
226
237
return new TimestampsRegionImpl (cacheAdapter , regionName , transactionManager , this );
227
238
}
228
239
229
- protected TransactionManager getTransactionManager () {
230
- return transactionManager ;
231
- }
232
-
233
240
/**
234
241
* {@inheritDoc}
235
242
*/
@@ -270,7 +277,7 @@ public void start(Settings settings, Properties properties) throws CacheExceptio
270
277
Enumeration keys = properties .propertyNames ();
271
278
while (keys .hasMoreElements ()) {
272
279
String key = (String ) keys .nextElement ();
273
- int prefixLoc = - 1 ;
280
+ int prefixLoc ;
274
281
if ((prefixLoc = key .indexOf (PREFIX )) != -1 ) {
275
282
dissectProperty (prefixLoc , key , properties );
276
283
}
@@ -310,11 +317,15 @@ public Set<String> getDefinedConfigurations() {
310
317
return Collections .unmodifiableSet (definedConfigurations );
311
318
}
312
319
320
+ public BaseRegion getRegion (String regionName ) {
321
+ return allRegions .get (regionName );
322
+ }
323
+
313
324
protected EmbeddedCacheManager createCacheManager (Properties properties ) throws CacheException {
314
325
try {
315
326
String configLoc = ConfigurationHelper .getString (INFINISPAN_CONFIG_RESOURCE_PROP , properties , DEF_INFINISPAN_CONFIG_RESOURCE );
316
327
EmbeddedCacheManager manager = new DefaultCacheManager (configLoc , false );
317
- String globalStats = ConfigurationHelper . extractPropertyValue (INFINISPAN_GLOBAL_STATISTICS_PROP , properties );
328
+ String globalStats = extractProperty (INFINISPAN_GLOBAL_STATISTICS_PROP , properties );
318
329
if (globalStats != null ) {
319
330
manager .getGlobalConfiguration ().setExposeGlobalJmxStatistics (Boolean .parseBoolean (globalStats ));
320
331
}
@@ -325,6 +336,10 @@ protected EmbeddedCacheManager createCacheManager(Properties properties) throws
325
336
}
326
337
}
327
338
339
+ private void startRegion (BaseRegion region , String regionName ) {
340
+ allRegions .put (regionName , region );
341
+ }
342
+
328
343
private Map <String , TypeOverrides > initGenericDataTypeOverrides () {
329
344
TypeOverrides entityOverrides = new TypeOverrides ();
330
345
entityOverrides .setCacheName (DEF_ENTITY_RESOURCE );
@@ -342,31 +357,33 @@ private Map<String, TypeOverrides> initGenericDataTypeOverrides() {
342
357
}
343
358
344
359
private void dissectProperty (int prefixLoc , String key , Properties properties ) {
345
- TypeOverrides cfgOverride = null ;
346
- int suffixLoc = - 1 ;
360
+ TypeOverrides cfgOverride ;
361
+ int suffixLoc ;
347
362
if (!key .equals (INFINISPAN_CONFIG_RESOURCE_PROP ) && (suffixLoc = key .indexOf (CONFIG_SUFFIX )) != -1 ) {
348
363
cfgOverride = getOrCreateConfig (prefixLoc , key , suffixLoc );
349
- cfgOverride .setCacheName ( ConfigurationHelper . extractPropertyValue (key , properties ));
364
+ cfgOverride .setCacheName (extractProperty (key , properties ));
350
365
} else if ((suffixLoc = key .indexOf (STRATEGY_SUFFIX )) != -1 ) {
351
366
cfgOverride = getOrCreateConfig (prefixLoc , key , suffixLoc );
352
- cfgOverride .setEvictionStrategy ( ConfigurationHelper . extractPropertyValue (key , properties ));
367
+ cfgOverride .setEvictionStrategy (extractProperty (key , properties ));
353
368
} else if ((suffixLoc = key .indexOf (WAKE_UP_INTERVAL_SUFFIX )) != -1 ) {
354
369
cfgOverride = getOrCreateConfig (prefixLoc , key , suffixLoc );
355
- cfgOverride .setEvictionWakeUpInterval (Long .parseLong ( ConfigurationHelper . extractPropertyValue (key , properties )));
370
+ cfgOverride .setEvictionWakeUpInterval (Long .parseLong (extractProperty (key , properties )));
356
371
} else if ((suffixLoc = key .indexOf (MAX_ENTRIES_SUFFIX )) != -1 ) {
357
372
cfgOverride = getOrCreateConfig (prefixLoc , key , suffixLoc );
358
- cfgOverride .setEvictionMaxEntries ( ConfigurationHelper . getInt ( key , properties , - 1 ));
373
+ cfgOverride .setEvictionMaxEntries (Integer . parseInt ( extractProperty ( key , properties ) ));
359
374
} else if ((suffixLoc = key .indexOf (LIFESPAN_SUFFIX )) != -1 ) {
360
375
cfgOverride = getOrCreateConfig (prefixLoc , key , suffixLoc );
361
- cfgOverride .setExpirationLifespan (Long .parseLong ( ConfigurationHelper . extractPropertyValue (key , properties )));
376
+ cfgOverride .setExpirationLifespan (Long .parseLong (extractProperty (key , properties )));
362
377
} else if ((suffixLoc = key .indexOf (MAX_IDLE_SUFFIX )) != -1 ) {
363
378
cfgOverride = getOrCreateConfig (prefixLoc , key , suffixLoc );
364
- cfgOverride .setExpirationMaxIdle (Long .parseLong ( ConfigurationHelper . extractPropertyValue (key , properties )));
379
+ cfgOverride .setExpirationMaxIdle (Long .parseLong (extractProperty (key , properties )));
365
380
}
366
- // else if ((suffixLoc = key.indexOf(STATISTICS_SUFFIX)) != -1) {
367
- // cfgOverride = getOrCreateConfig(prefixLoc, key, suffixLoc);
368
- // cfgOverride.setExposeStatistics(Boolean.parseBoolean(PropertiesHelper.extractPropertyValue(key, properties)));
369
- // }
381
+ }
382
+
383
+ private String extractProperty (String key , Properties properties ) {
384
+ String value = ConfigurationHelper .extractPropertyValue (key , properties );
385
+ log .debugf ("Configuration override via property %s: %s" , key , value );
386
+ return value ;
370
387
}
371
388
372
389
private TypeOverrides getOrCreateConfig (int prefixLoc , String key , int suffixLoc ) {
@@ -395,7 +412,7 @@ private void defineGenericDataTypeCacheConfigurations(Settings settings, Propert
395
412
}
396
413
}
397
414
398
- private Cache getCache (String regionName , String typeKey , Properties properties ) {
415
+ private AdvancedCache getCache (String regionName , String typeKey , Properties properties ) {
399
416
TypeOverrides regionOverride = typeOverrides .get (regionName );
400
417
if (!definedConfigurations .contains (regionName )) {
401
418
String templateCacheName = null ;
@@ -420,11 +437,17 @@ private Cache getCache(String regionName, String typeKey, Properties properties)
420
437
manager .defineConfiguration (regionName , templateCacheName , regionCacheCfg );
421
438
definedConfigurations .add (regionName );
422
439
}
423
- Cache cache = manager .getCache (regionName );
440
+ AdvancedCache cache = manager .getCache (regionName ). getAdvancedCache ( );
424
441
if (!cache .getStatus ().allowInvocations ()) {
425
442
cache .start ();
426
443
}
427
- return createCacheWrapper (cache .getAdvancedCache ());
444
+ ComponentRegistry cr = cache .getComponentRegistry ();
445
+ cr .getComponent (CacheCommandInitializer .class ).setRegionFactory (this );
446
+ GlobalComponentRegistry globalCr = cache .getComponentRegistry ().getGlobalComponentRegistry ();
447
+ // TODO: This is a hack, make it easier to retrieve in Infinispan!
448
+ ((CacheCommandFactory ) ((Map ) globalCr .getComponent ("org.infinispan.modules.command.factories" ))
449
+ .values ().iterator ().next ()).setRegionFactory (this );
450
+ return createCacheWrapper (cache );
428
451
}
429
452
430
453
protected ClassLoaderAwareCache createCacheWrapper (AdvancedCache cache ) {
@@ -434,24 +457,26 @@ protected ClassLoaderAwareCache createCacheWrapper(AdvancedCache cache) {
434
457
private Configuration configureTransactionManager (Configuration regionOverrides , String templateCacheName , Properties properties ) {
435
458
// Get existing configuration to verify whether a tm was configured or not.
436
459
Configuration templateConfig = manager .defineConfiguration (templateCacheName , new Configuration ());
437
- String ispnTmLookupClassName = templateConfig .getTransactionManagerLookupClass ();
438
- String hbTmLookupClassName = org .hibernate .cache .infinispan .tm .HibernateTransactionManagerLookup .class .getName ();
439
- if (ispnTmLookupClassName != null && !ispnTmLookupClassName .equals (hbTmLookupClassName )) {
440
- log .debug ("Infinispan is configured [" + ispnTmLookupClassName + "] with a different transaction manager lookup " +
441
- "class than Hibernate [" + hbTmLookupClassName + "]" );
442
- } else {
443
- regionOverrides .setTransactionManagerLookup (transactionManagerlookup );
444
- }
460
+ if (templateConfig .isTransactionalCache ()) {
461
+ String ispnTmLookupClassName = templateConfig .getTransactionManagerLookupClass ();
462
+ String hbTmLookupClassName = org .hibernate .cache .infinispan .tm .HibernateTransactionManagerLookup .class .getName ();
463
+ if (ispnTmLookupClassName != null && !ispnTmLookupClassName .equals (hbTmLookupClassName )) {
464
+ log .debug ("Infinispan is configured [" + ispnTmLookupClassName + "] with a different transaction manager lookup " +
465
+ "class than Hibernate [" + hbTmLookupClassName + "]" );
466
+ } else {
467
+ regionOverrides .setTransactionManagerLookup (transactionManagerlookup );
468
+ }
445
469
446
- String useSyncProp = ConfigurationHelper .extractPropertyValue (INFINISPAN_USE_SYNCHRONIZATION_PROP , properties );
447
- boolean useSync = useSyncProp == null ? DEF_USE_SYNCHRONIZATION : Boolean .parseBoolean (useSyncProp );
448
- regionOverrides .fluent ().transaction ().useSynchronization (useSync );
470
+ String useSyncProp = extractProperty (INFINISPAN_USE_SYNCHRONIZATION_PROP , properties );
471
+ boolean useSync = useSyncProp == null ? DEF_USE_SYNCHRONIZATION : Boolean .parseBoolean (useSyncProp );
472
+ regionOverrides .fluent ().transaction ().useSynchronization (useSync );
473
+ }
449
474
450
475
return regionOverrides ;
451
476
}
452
477
453
478
private TypeOverrides overrideStatisticsIfPresent (TypeOverrides override , Properties properties ) {
454
- String globalStats = ConfigurationHelper . extractPropertyValue (INFINISPAN_GLOBAL_STATISTICS_PROP , properties );
479
+ String globalStats = extractProperty (INFINISPAN_GLOBAL_STATISTICS_PROP , properties );
455
480
if (globalStats != null ) {
456
481
override .setExposeStatistics (Boolean .parseBoolean (globalStats ));
457
482
}
0 commit comments