23
23
import org .hibernate .cache .cfg .spi .DomainDataRegionConfig ;
24
24
import org .hibernate .cache .internal .DefaultCacheKeysFactory ;
25
25
import org .hibernate .cache .jcache .ConfigSettings ;
26
+ import org .hibernate .cache .jcache .JCacheHelper ;
26
27
import org .hibernate .cache .jcache .MissingCacheStrategy ;
27
28
import org .hibernate .cache .spi .CacheKeysFactory ;
28
29
import org .hibernate .cache .spi .DomainDataRegion ;
@@ -43,6 +44,12 @@ public class JCacheRegionFactory extends RegionFactoryTemplate {
43
44
private volatile CacheManager cacheManager ;
44
45
private volatile MissingCacheStrategy missingCacheStrategy ;
45
46
47
+ // need to save the classloader from Caching.getDefaultClassLoader to reset it
48
+ // when this service is released again.
49
+ private volatile ClassLoader oldJsr107CacheClassLoader ;
50
+ // in case caches were already set up beforehand with a different configuration
51
+ private volatile CacheManager preInitializedCacheManager ;
52
+
46
53
@ SuppressWarnings ("unused" )
47
54
public JCacheRegionFactory () {
48
55
this ( DefaultCacheKeysFactory .INSTANCE );
@@ -85,7 +92,8 @@ protected DomainDataStorageAccess createDomainDataStorageAccess(
85
92
86
93
protected Cache <Object , Object > getOrCreateCache (String unqualifiedRegionName , SessionFactoryImplementor sessionFactory ) {
87
94
verifyStarted ();
88
- assert !RegionNameQualifier .INSTANCE .isQualified ( unqualifiedRegionName , sessionFactory .getSessionFactoryOptions () );
95
+ assert !RegionNameQualifier .INSTANCE .isQualified ( unqualifiedRegionName ,
96
+ sessionFactory .getSessionFactoryOptions () );
89
97
90
98
final String qualifiedRegionName = RegionNameQualifier .INSTANCE .qualify (
91
99
unqualifiedRegionName ,
@@ -94,6 +102,13 @@ protected Cache<Object, Object> getOrCreateCache(String unqualifiedRegionName, S
94
102
95
103
final Cache <Object , Object > cache = cacheManager .getCache ( qualifiedRegionName );
96
104
if ( cache == null ) {
105
+ if ( preInitializedCacheManager != null ) {
106
+ final Cache <Object , Object > cacheFromBefore = preInitializedCacheManager .getCache (
107
+ qualifiedRegionName );
108
+ if ( cacheFromBefore != null ) {
109
+ return cacheFromBefore ;
110
+ }
111
+ }
97
112
return createCache ( qualifiedRegionName );
98
113
}
99
114
return cache ;
@@ -110,7 +125,8 @@ protected Cache<Object, Object> createCache(String regionName) {
110
125
case CREATE :
111
126
return cacheManager .createCache ( regionName , new MutableConfiguration <>() );
112
127
case FAIL :
113
- throw new CacheException ( "On-the-fly creation of JCache Cache objects is not supported [" + regionName + "]" );
128
+ throw new CacheException (
129
+ "On-the-fly creation of JCache Cache objects is not supported [" + regionName + "]" );
114
130
default :
115
131
throw new IllegalStateException ( "Unsupported missing cache strategy: " + missingCacheStrategy );
116
132
}
@@ -121,7 +137,9 @@ protected boolean cacheExists(String unqualifiedRegionName, SessionFactoryImplem
121
137
unqualifiedRegionName ,
122
138
sessionFactory .getSessionFactoryOptions ()
123
139
);
124
- return cacheManager .getCache ( qualifiedRegionName ) != null ;
140
+ return cacheManager .getCache ( qualifiedRegionName ) != null ||
141
+ (preInitializedCacheManager != null &&
142
+ preInitializedCacheManager .getCache ( qualifiedRegionName ) != null );
125
143
}
126
144
127
145
@ Override
@@ -155,9 +173,9 @@ protected StorageAccess createTimestampsRegionStorageAccess(
155
173
}
156
174
157
175
protected final String defaultRegionName (String regionName , SessionFactoryImplementor sessionFactory ,
158
- String defaultRegionName , List <String > legacyDefaultRegionNames ) {
176
+ String defaultRegionName , List <String > legacyDefaultRegionNames ) {
159
177
if ( defaultRegionName .equals ( regionName )
160
- && !cacheExists ( regionName , sessionFactory ) ) {
178
+ && !cacheExists ( regionName , sessionFactory ) ) {
161
179
// Maybe the user configured caches explicitly with legacy names; try them and use the first that exists
162
180
163
181
for ( String legacyDefaultRegionName : legacyDefaultRegionNames ) {
@@ -172,7 +190,6 @@ protected final String defaultRegionName(String regionName, SessionFactoryImplem
172
190
}
173
191
174
192
175
-
176
193
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
177
194
// Lifecycle
178
195
@@ -182,7 +199,14 @@ protected boolean isStarted() {
182
199
}
183
200
184
201
@ Override
185
- protected void prepareForUse (SessionFactoryOptions settings , Map <String ,Object > configValues ) {
202
+ protected void prepareForUse (SessionFactoryOptions settings , Map <String , Object > configValues ) {
203
+ final ClassLoader serviceClassLoader = getServiceClassLoader ( settings );
204
+ if ( serviceClassLoader != null ) {
205
+ preInitializedCacheManager = JCacheHelper .locateStandardCacheManager ();
206
+ oldJsr107CacheClassLoader = Caching .getDefaultClassLoader ();
207
+ Caching .setDefaultClassLoader ( serviceClassLoader );
208
+ }
209
+
186
210
this .cacheManager = resolveCacheManager ( settings , configValues );
187
211
if ( this .cacheManager == null ) {
188
212
throw new CacheException ( "Could not locate/create CacheManager" );
@@ -192,30 +216,38 @@ protected void prepareForUse(SessionFactoryOptions settings, Map<String,Object>
192
216
);
193
217
}
194
218
195
- protected CacheManager resolveCacheManager (SessionFactoryOptions settings , Map <String ,Object > properties ) {
219
+ protected CacheManager resolveCacheManager (SessionFactoryOptions settings , Map <String , Object > properties ) {
196
220
final Object explicitCacheManager = properties .get ( ConfigSettings .CACHE_MANAGER );
197
221
if ( explicitCacheManager != null ) {
198
222
return useExplicitCacheManager ( settings , explicitCacheManager );
199
223
}
200
224
201
- final CachingProvider cachingProvider = getCachingProvider ( properties );
225
+ final ClassLoader serviceClassLoader = getServiceClassLoader ( settings );
226
+ final CachingProvider cachingProvider = getCachingProvider ( properties , serviceClassLoader );
202
227
final CacheManager cacheManager ;
203
228
final URI cacheManagerUri = getUri ( settings , properties );
229
+ final ClassLoader classLoader = getClassLoader ( cachingProvider , serviceClassLoader );
204
230
if ( cacheManagerUri != null ) {
205
- cacheManager = cachingProvider .getCacheManager ( cacheManagerUri , getClassLoader ( cachingProvider ) );
231
+ cacheManager = cachingProvider .getCacheManager ( cacheManagerUri , classLoader );
206
232
}
207
233
else {
208
- cacheManager = cachingProvider .getCacheManager ( cachingProvider .getDefaultURI (), getClassLoader ( cachingProvider ) );
234
+ cacheManager = cachingProvider .getCacheManager ( cachingProvider .getDefaultURI (), classLoader );
209
235
}
210
236
return cacheManager ;
211
237
}
212
238
213
- protected ClassLoader getClassLoader (CachingProvider cachingProvider ) {
214
- // todo (5.3) : shouldn't this use Hibernate's AggregatedClassLoader?
215
- return cachingProvider .getDefaultClassLoader ();
239
+ private ClassLoader getServiceClassLoader (SessionFactoryOptions settings ) {
240
+ final ClassLoaderService classLoaderService = settings .getServiceRegistry ()
241
+ .getService ( ClassLoaderService .class );
242
+ return (classLoaderService == null ) ? null :
243
+ classLoaderService .workWithClassLoader ( classLoader -> classLoader );
244
+ }
245
+
246
+ protected ClassLoader getClassLoader (CachingProvider cachingProvider , ClassLoader serviceClassLoader ) {
247
+ return (serviceClassLoader != null ) ? serviceClassLoader : cachingProvider .getDefaultClassLoader ();
216
248
}
217
249
218
- protected URI getUri (SessionFactoryOptions settings , Map <String ,Object > properties ) {
250
+ protected URI getUri (SessionFactoryOptions settings , Map <String , Object > properties ) {
219
251
String cacheManagerUri = getProp ( properties , ConfigSettings .CONFIG_URI );
220
252
if ( cacheManagerUri == null ) {
221
253
return null ;
@@ -241,14 +273,14 @@ private String getProp(Map<String,Object> properties, String prop) {
241
273
return properties != null ? (String ) properties .get ( prop ) : null ;
242
274
}
243
275
244
- protected CachingProvider getCachingProvider (final Map <String ,Object > properties ) {
276
+ protected CachingProvider getCachingProvider (final Map <String ,Object > properties , ClassLoader classLoader ) {
245
277
final CachingProvider cachingProvider ;
246
278
final String provider = getProp ( properties , ConfigSettings .PROVIDER );
247
279
if ( provider != null ) {
248
- cachingProvider = Caching .getCachingProvider ( provider );
280
+ cachingProvider = Caching .getCachingProvider ( provider , classLoader );
249
281
}
250
282
else {
251
- cachingProvider = Caching .getCachingProvider ();
283
+ cachingProvider = Caching .getCachingProvider ( classLoader );
252
284
}
253
285
return cachingProvider ;
254
286
}
@@ -283,6 +315,9 @@ protected void releaseFromUse() {
283
315
// - when the explicit `setting` passed to `#useExplicitCacheManager` is
284
316
// a CacheManager instance
285
317
cacheManager .close ();
318
+ if ( oldJsr107CacheClassLoader != null ) {
319
+ Caching .setDefaultClassLoader ( oldJsr107CacheClassLoader );
320
+ }
286
321
}
287
322
finally {
288
323
cacheManager = null ;
0 commit comments