AnnotatedType replacement for EJB doesn't works
mischu Dec 28, 2011 8:09 AMHello
I'm working on a Weld cache extension. Now have a problem, for that i doesn't see a serious way to solve it.
For my caching solution i create proxy classes for each class that contains a cached method. The problem is now, when an EJB (Stateful or Stateless make to difference) should be replaced, both classes are available later as injection targets. I found a related Jiraissue for this: https://issues.jboss.org/browse/WELD-925. This issue seems as fixed, but replacement of the annotated EJB type with a proxy class (is also an EJB) results in a ambiguous dependency, also set veto for an EJB seems not to have an effect. With managed beans it works properly.
I have testet it in JBoss AS 7.0.0 and 7.0.2
Anyone have a suggestion?
Here the observers.
public <X> void setCachedAnnotatedTypes( @Observes ProcessAnnotatedType<X> pat, BeanManager beanManager )
{
AnnotatedType<X> annotatedType = pat.getAnnotatedType();
// process for ehCache configuration
if ( annotatedType.isAnnotationPresent( CacheConfig.class ) )
{
LOGGER.info( String.format( "Start reading CR-cache configuration on class: %s",
annotatedType.getJavaClass().getName() ) );
CacheConfigurationReader cacheConfigurationReader = new CacheConfigurationReader();
cacheConfigurationReader.read( annotatedType.getAnnotation( CacheConfig.class ) );
LOGGER.info( "Reading configuration of CR-cache successfully done!" );
}
if ( annotatedType.isAnnotationPresent( Cached.class ) )
{
String errorMessage = "could not build dependent objects for cache. Cache will not work";
ClassPool classPool = ClassPool.getDefault();
classPool.appendClassPath( new ClassPathImpl( getClass().getClassLoader() ) );
classPool.importPackage( CacheConstants.INVOCATION_WRAPPER_PACKAGE_NAME );
ProxyClassFactory proxyClassFactory = new ProxyClassFactory( classPool );
try
{
LOGGER.info( String.format( "found cached class with cached methods: %s",
annotatedType.getJavaClass().getName() ) );
Class<X> proxyClass = proxyClassFactory.getProxyClassFor( annotatedType.getJavaClass() );
// replace raw class with proxy
pat.setAnnotatedType( beanManager.<X>createAnnotatedType( proxyClass ) );
LOGGER.info( String.format( "new annotated type created. %s for native bean: %s",
proxyClass.getName(),
annotatedType.getJavaClass().getName() ) );
}
catch ( ClassNotFoundException e )
{
LOGGER.error( errorMessage, e );
}
catch ( NotFoundException e )
{
LOGGER.error( errorMessage, e );
}
catch ( CannotCompileException e )
{
LOGGER.error( errorMessage, e );
}
}
}
public <X> void checkInjectionTargets( @Observes ProcessInjectionTarget<X> pit )
{
if ( pit.getAnnotatedType().getJavaClass().isAnnotationPresent( Cached.class ) )
{
LOGGER.info(
String.format( "Cached injectionTarget: %s", pit.getAnnotatedType().getJavaClass().getName() ) );
}
}
And here the relevant logs:
INFO ch.rte.cache.extension.CRCacheExtension - Start reading CR-cache configuration on class: ch.rte.tempnet.controller.CachedEJBTest INFO ch.rte.cache.extension.CRCacheExtension - Reading configuration of CR-cache successfully done! INFO ch.rte.cache.extension.CRCacheExtension - found cached class with cached methods: ch.rte.tempnet.controller.CachedEJBTest INFO ch.rte.cache.factory.ProxyMethodFactory - Cache writing / reading method found and registred: ch.rte.tempnet.controller.CachedEJBTest#cachedAction WARN ch.rte.cache.factory.ProxyMethodFactory - Cache writing method(s) registred, but no method to clean cache found. You want never clean the cache? INFO ch.rte.cache.factory.ProxyClassFactory - Generating of a proxy class: ch.rte.tempnet.controller.CachedEJBTest$C for the raw class: ch.rte.tempnet.controller.CachedEJBTest INFO ch.rte.cache.extension.CRCacheExtension - new annotated type created. ch.rte.tempnet.controller.CachedEJBTest$C for native bean: ch.rte.tempnet.controller.CachedEJBTest INFO ch.rte.cache.extension.CRCacheExtension - Cached injectionTarget: ch.rte.tempnet.controller.CachedEJBTest$C INFO ch.rte.cache.extension.CRCacheExtension - Cached injectionTarget: ch.rte.tempnet.controller.CachedEJBTest