-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use jakarta.inject for jcache guice integration test
- Loading branch information
Showing
5 changed files
with
132 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
jcache/src/test/java/com/github/benmanes/caffeine/jcache/guice/JakartaCacheModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* Copyright 2025 Ben Manes. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.github.benmanes.caffeine.jcache.guice; | ||
|
||
import javax.cache.annotation.CacheKeyGenerator; | ||
import javax.cache.annotation.CachePut; | ||
import javax.cache.annotation.CacheRemove; | ||
import javax.cache.annotation.CacheRemoveAll; | ||
import javax.cache.annotation.CacheResolverFactory; | ||
import javax.cache.annotation.CacheResult; | ||
|
||
import org.aopalliance.intercept.MethodInvocation; | ||
import org.jsr107.ri.annotations.CacheContextSource; | ||
import org.jsr107.ri.annotations.DefaultCacheKeyGenerator; | ||
import org.jsr107.ri.annotations.DefaultCacheResolverFactory; | ||
import org.jsr107.ri.annotations.guice.CacheLookupUtil; | ||
import org.jsr107.ri.annotations.guice.CachePutInterceptor; | ||
import org.jsr107.ri.annotations.guice.CacheRemoveAllInterceptor; | ||
import org.jsr107.ri.annotations.guice.CacheRemoveEntryInterceptor; | ||
import org.jsr107.ri.annotations.guice.CacheResultInterceptor; | ||
|
||
import com.google.inject.AbstractModule; | ||
import com.google.inject.Injector; | ||
import com.google.inject.TypeLiteral; | ||
import com.google.inject.matcher.Matchers; | ||
|
||
import jakarta.inject.Inject; | ||
import jakarta.inject.Singleton; | ||
|
||
/** | ||
* A version of {@link org.jsr107.ri.annotations.guice.module.CacheAnnotationsModule} to adapt the | ||
* the usages of javax.inject to their jakarta.inject counterparts. | ||
* | ||
* @author [email protected] (Ben Manes) | ||
*/ | ||
final class JakartaCacheModule extends AbstractModule { | ||
private static final TypeLiteral<CacheContextSource<MethodInvocation>> CACHE_LOOKUP = | ||
new TypeLiteral<>() {}; | ||
|
||
@Override | ||
protected void configure() { | ||
bind(CACHE_LOOKUP).to(JakartaCacheLookup.class); | ||
bind(CacheKeyGenerator.class).to(DefaultCacheKeyGenerator.class); | ||
bind(CacheResolverFactory.class).to(DefaultCacheResolverFactory.class); | ||
|
||
interceptCachePut(); | ||
incerceptCacheResult(); | ||
interceptCacheRemove(); | ||
interceptCacheRemoveAll(); | ||
} | ||
|
||
private void interceptCachePut() { | ||
var interceptor = new JakartaCachePutInterceptor(); | ||
requestInjection(interceptor); | ||
bindInterceptor(Matchers.annotatedWith(CachePut.class), Matchers.any(), interceptor); | ||
bindInterceptor(Matchers.any(), Matchers.annotatedWith(CachePut.class), interceptor); | ||
} | ||
|
||
private void interceptCacheRemove() { | ||
var interceptor = new JakartaCacheRemoveEntryInterceptor(); | ||
requestInjection(interceptor); | ||
bindInterceptor(Matchers.annotatedWith(CacheRemove.class), Matchers.any(), interceptor); | ||
bindInterceptor(Matchers.any(), Matchers.annotatedWith(CacheRemove.class), interceptor); | ||
} | ||
|
||
private void incerceptCacheResult() { | ||
var interceptor = new JakartaCacheResultInterceptor(); | ||
requestInjection(interceptor); | ||
bindInterceptor(Matchers.annotatedWith(CacheResult.class), Matchers.any(), interceptor); | ||
bindInterceptor(Matchers.any(), Matchers.annotatedWith(CacheResult.class), interceptor); | ||
} | ||
|
||
private void interceptCacheRemoveAll() { | ||
var interceptor = new JakartaCacheRemoveAllInterceptor(); | ||
requestInjection(interceptor); | ||
bindInterceptor(Matchers.annotatedWith(CacheRemoveAll.class), Matchers.any(), interceptor); | ||
bindInterceptor(Matchers.any(), Matchers.annotatedWith(CacheRemoveAll.class), interceptor); | ||
} | ||
|
||
private static final class JakartaCachePutInterceptor extends CachePutInterceptor { | ||
@Inject public void inject(CacheContextSource<MethodInvocation> cacheContextSource) { | ||
setCacheContextSource(cacheContextSource); | ||
} | ||
} | ||
|
||
private static final class JakartaCacheResultInterceptor extends CacheResultInterceptor { | ||
@Inject public void inject(CacheContextSource<MethodInvocation> cacheContextSource) { | ||
setCacheContextSource(cacheContextSource); | ||
} | ||
} | ||
|
||
private static final class JakartaCacheRemoveEntryInterceptor extends CacheRemoveEntryInterceptor { | ||
@Inject public void inject(CacheContextSource<MethodInvocation> cacheContextSource) { | ||
setCacheContextSource(cacheContextSource); | ||
} | ||
} | ||
|
||
private static final class JakartaCacheRemoveAllInterceptor extends CacheRemoveAllInterceptor { | ||
@Inject public void inject(CacheContextSource<MethodInvocation> cacheContextSource) { | ||
setCacheContextSource(cacheContextSource); | ||
} | ||
} | ||
|
||
@Singleton | ||
private static final class JakartaCacheLookup extends CacheLookupUtil { | ||
@Inject public JakartaCacheLookup(Injector injector, CacheKeyGenerator defaultCacheKeyGenerator, | ||
CacheResolverFactory defaultCacheResolverFactory) { | ||
super(injector, defaultCacheKeyGenerator, defaultCacheResolverFactory); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters