Hi Nicolas!

I got some minutes to quickly check it. 

A few things:

1.) Interceptors must be enabled as per the spec. This is required for 
annotated and programmatically added Interceptors.
You can enable an Interceptor either by
1.a) adding it to a beans.xml
1.b) adding a @Priority annotation to it - available since CDI-1.1 (OWB-1.7.x) 
but that might be a bit hard for java.lang.Object ;)
1.c) adding it via AfterTypeDiscovery - again available only since CDI-1.1 
(OWB-1.7.x) 

2.) The bean class of the interceptors is used for checking the enablement. 
Means your Bean<MyInterceptor> should return MyInterceptor.class in 
getBeanClass();

hope that helps.

LieGrue,
strub

PS: if you like to create annotations on the fly then please use new 
AnnotationLiteral<YourAnnotation>() {}


> Am 14.02.2017 um 11:34 schrieb Radde, Nicolas 
> <[email protected]>:
> 
> Hello Mark,
> Thank you for your ideas !
> I pushed my test project on github.com on 
> https://github.com/nickho/interceptor-multiple-dynamic-definition/
> 
> In the beans.xml you need to put a class that contain the annotation 
> @Interceptor (which is not the case here because the Interceptor was 
> dynamically created). Actually I tried, but it fails validation (rightfully).
> For the @Priority annotation, I think it was introduced in CDI-1.2. But even 
> then unfortunately, the annotation need to be on an class annotated with 
> @Interceptor, not a class that extends Interceptor (I think?).
> 
> But doing more tests revealed that it happens only when there is at least 2 
> Interceptors on a method with at least one being added with 
> AfterBeanDiscovery.addBean().
> 
> I found a lot of sample on how to add a Bean, but none on how to add an 
> Interceptor. I don't see what I'm missing :D.
> 
> Thanks,
> 
> --Nicolas
> 
> 
> -----Message d'origine-----
> De : Mark Struberg [mailto:[email protected]] 
> Envoyé : lundi 13 février 2017 21:01
> À : openwebbeans-user
> Objet : Re: Adding a dynamic interceptor with AfterBeanDiscovery.addBean()
> 
> Hi Nicolas!
> 
> I think the class you add still needs to be either have a @Priority 
> annotation or must be enabled in a beans.xml.
> 
> Do you have a sample project somewhere so we can look at it?
> 
> txs and LieGrue,
> strub
> 
> 
>> Am 10.02.2017 um 17:12 schrieb Radde, Nicolas 
>> <[email protected]>:
>> 
>> Hello OpenWebBeans users !
>> I’ve been trying to add a custom (dynamic) Interceptor using the 
>> AfterbeanDiscovery.addBean() method in an extension with no success. I 
>> always end up with the Exception :
>> java.lang.IllegalArgumentException: java.lang.Object is not an enabled 
>> interceptor!
>>       at 
>> org.apache.webbeans.intercept.InterceptorsManager.compareCdiInterceptors(InterceptorsManager.java:152)
>>       at 
>> org.apache.webbeans.intercept.InterceptorComparator.compare(InterceptorComparator.java:50)
>>       at 
>> org.apache.webbeans.intercept.InterceptorComparator.compare(InterceptorComparator.java:27)
>>       at java.util.Arrays.mergeSort(Arrays.java:2874)
>>       at java.util.Arrays.sort(Arrays.java:3278)
>>       at java.util.Arrays.sort(Arrays.java:3296)
>>       at java.util.Collections.sort(Collections.java:1966)
>>       at 
>> org.apache.webbeans.intercept.InterceptorsManager.resolveInterceptors(InterceptorsManager.java:211)
>>       at 
>> org.apache.webbeans.container.BeanManagerImpl.resolveInterceptors(BeanManagerImpl.java:478)
>>       at 
>> org.apache.webbeans.intercept.InterceptorResolutionService.calculateCdiMethodInterceptors(InterceptorResolutionService.java:501)
>>       at 
>> org.apache.webbeans.intercept.InterceptorResolutionService.calculateInterceptorInfo(InterceptorResolutionService.java:130)
>>       at 
>> org.apache.webbeans.portable.AbstractProducer.defineInterceptorStack(AbstractProducer.java:82)
>>       at 
>> org.apache.webbeans.config.BeansDeployer.validate(BeansDeployer.java:474)
>>       at 
>> org.apache.webbeans.config.BeansDeployer.validateInjectionPoints(BeansDeployer.java:422)
>>       at 
>> org.apache.webbeans.config.BeansDeployer.deploy(BeansDeployer.java:202)
>>       at 
>> org.apache.webbeans.lifecycle.AbstractLifeCycle.bootstrapApplication(AbstractLifeCycle.java:141)
>>       at 
>> org.apache.webbeans.lifecycle.AbstractLifeCycle.startApplication(AbstractLifeCycle.java:107)
>>       at 
>> org.apache.webbeans.web.lifecycle.WebContainerLifecycle.startApplication(WebContainerLifecycle.java:89)
>>       at 
>> org.apache.webbeans.servlet.WebBeansConfigurationListener.contextInitialized(WebBeansConfigurationListener.java:86)
>>       at 
>> com.ibm.ws.webcontainer.webapp.WebApp.notifyServletContextCreated(WebApp.java:1721)
>>       at 
>> com.ibm.ws.webcontainer.webapp.WebApp.commonInitializationFinish(WebApp.java:388)
>>       at 
>> com.ibm.ws.webcontainer.webapp.WebAppImpl.initialize(WebAppImpl.java:299)
>>       at 
>> com.ibm.ws.webcontainer.webapp.WebGroupImpl.addWebApplication(WebGroupImpl.java:100)
>>       at 
>> com.ibm.ws.webcontainer.VirtualHostImpl.addWebApplication(VirtualHostImpl.java:166)
>>       at 
>> com.ibm.ws.webcontainer.WSWebContainer.addWebApp(WSWebContainer.java:732)
>>       at 
>> com.ibm.ws.webcontainer.WSWebContainer.addWebApplication(WSWebContainer.java:617)
>>       at 
>> com.ibm.ws.webcontainer.component.WebContainerImpl.install(WebContainerImpl.java:376)
>>       at 
>> com.ibm.ws.webcontainer.component.WebContainerImpl.start(WebContainerImpl.java:668)
>>       at 
>> com.ibm.ws.runtime.component.ApplicationMgrImpl.start(ApplicationMgrImpl.java:1146)
>>       at 
>> com.ibm.ws.runtime.component.DeployedApplicationImpl.fireDeployedObjectStart(DeployedApplicationImpl.java:1320)
>>       at 
>> com.ibm.ws.runtime.component.DeployedModuleImpl.start(DeployedModuleImpl.java:611)
>>       at 
>> com.ibm.ws.runtime.component.ApplicationMgrImpl.startModule(ApplicationMgrImpl.java:1599)
>>       at 
>> com.ibm.ws.runtime.component.ApplicationMgrImpl._startModule(ApplicationMgrImpl.java:1547)
>>       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>>       at 
>> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
>>       at 
>> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
>>       at java.lang.reflect.Method.invoke(Method.java:611)
>>       at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:69)
>>       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>>       at 
>> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
>>       at 
>> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
>>       at java.lang.reflect.Method.invoke(Method.java:611)
>>       at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:272)
>>       at 
>> javax.management.modelmbean.RequiredModelMBean$4.run(RequiredModelMBean.java:1152)
>>       at 
>> java.security.AccessController.doPrivileged(AccessController.java:384)
>>       at 
>> com.ibm.oti.security.CheckedAccessControlContext.securityCheck(CheckedAccessControlContext.java:30)
>>       at 
>> sun.misc.JavaSecurityAccessWrapper.doIntersectionPrivilege(JavaSecurityAccessWrapper.java:41)
>>       at 
>> javax.management.modelmbean.RequiredModelMBean.invokeMethod(RequiredModelMBean.java:1146)
>>       at 
>> javax.management.modelmbean.RequiredModelMBean.invoke(RequiredModelMBean.java:999)
>>       at 
>> com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:847)
>>       at 
>> com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:783)
>>       at 
>> com.ibm.ws.management.AdminServiceImpl$1.run(AdminServiceImpl.java:1346)
>>       at 
>> com.ibm.ws.security.util.AccessController.doPrivileged(AccessController.java:118)
>>       at 
>> com.ibm.ws.management.AdminServiceImpl.invoke(AdminServiceImpl.java:1239)
>>       at 
>> com.ibm.ws.management.application.sync.StartDeploymentTask.startDeployment(StartDeploymentTask.java:248)
>>       at 
>> com.ibm.ws.management.application.sync.StartDeploymentTask.fineGrainUpdate(StartDeploymentTask.java:199)
>>       at 
>> com.ibm.ws.management.application.sync.StartDeploymentTask.performTask(StartDeploymentTask.java:106)
>>       at 
>> com.ibm.ws.management.application.sync.AppBinaryProcessor$ExpandApp.expand(AppBinaryProcessor.java:1682)
>>       at 
>> com.ibm.ws.management.application.sync.AppBinaryProcessor.postProcessSynchronousExt(AppBinaryProcessor.java:723)
>>       at 
>> com.ibm.ws.management.bla.sync.BLABinaryProcessor.postProcess(BLABinaryProcessor.java:575)
>>       at 
>> com.ibm.ws.management.bla.sync.BLABinaryProcessor.onChangeCompletion(BLABinaryProcessor.java:452)
>>       at 
>> com.ibm.ws.management.repository.FileRepository.postNotify(FileRepository.java:1916)
>>       at 
>> com.ibm.ws.management.repository.FileRepository.update(FileRepository.java:1425)
>>       at 
>> com.ibm.ws.management.repository.client.LocalConfigRepositoryClient.update(LocalConfigRepositoryClient.java:189)
>>       at 
>> com.ibm.ws.sm.workspace.impl.WorkSpaceMasterRepositoryAdapter.update(WorkSpaceMasterRepositoryAdapter.java:666)
>>       at 
>> com.ibm.ws.sm.workspace.impl.RepositoryContextImpl.update(RepositoryContextImpl.java:1954)
>>       at 
>> com.ibm.ws.sm.workspace.impl.RepositoryContextImpl.synch(RepositoryContextImpl.java:1902)
>>       at 
>> com.ibm.ws.sm.workspace.impl.WorkSpaceImpl.synch(WorkSpaceImpl.java:511)
>>       at 
>> com.ibm.ws.management.configservice.ConfigServiceImpl.save(ConfigServiceImpl.java:707)
>>       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>>       at 
>> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
>>       at 
>> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
>>       at java.lang.reflect.Method.invoke(Method.java:611)
>>       at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:69)
>>       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>>       at 
>> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
>>       at 
>> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
>>       at java.lang.reflect.Method.invoke(Method.java:611)
>>       at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:272)
>>       at 
>> javax.management.modelmbean.RequiredModelMBean$4.run(RequiredModelMBean.java:1152)
>>       at 
>> java.security.AccessController.doPrivileged(AccessController.java:384)
>>       at 
>> com.ibm.oti.security.CheckedAccessControlContext.securityCheck(CheckedAccessControlContext.java:30)
>>       at 
>> sun.misc.JavaSecurityAccessWrapper.doIntersectionPrivilege(JavaSecurityAccessWrapper.java:41)
>>       at 
>> javax.management.modelmbean.RequiredModelMBean.invokeMethod(RequiredModelMBean.java:1146)
>>       at 
>> javax.management.modelmbean.RequiredModelMBean.invoke(RequiredModelMBean.java:999)
>>       at 
>> com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:847)
>>       at 
>> com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:783)
>>       at 
>> com.ibm.ws.management.AdminServiceImpl$1.run(AdminServiceImpl.java:1346)
>>       at 
>> com.ibm.ws.security.util.AccessController.doPrivileged(AccessController.java:118)
>>       at 
>> com.ibm.ws.management.AdminServiceImpl.invoke(AdminServiceImpl.java:1239)
>>       at 
>> com.ibm.ws.management.connector.AdminServiceDelegator.invoke(AdminServiceDelegator.java:181)
>>       at 
>> com.ibm.ws.management.connector.ipc.CallRouter.route(CallRouter.java:242)
>>       at 
>> com.ibm.ws.management.connector.ipc.IPCConnectorInboundLink.doWork(IPCConnectorInboundLink.java:353)
>>       at 
>> com.ibm.ws.management.connector.ipc.IPCConnectorInboundLink$IPCConnectorReadCallback.complete(IPCConnectorInboundLink.java:595)
>>       at 
>> com.ibm.ws.ssl.channel.impl.SSLReadServiceContext$QueuedWork.run(SSLReadServiceContext.java:1987)
>>       at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1662)
>> 
>> This is because the InterceptorsManager.compareCdiInterceptors() method 
>> check if the interceptor is enabled in the beans.xml, but I can’t really add 
>> it to the file because it is a dynamically created Interceptor. We are still 
>> using owb-1.2.8 (last jdk1.6 version to my knowledge) for Websphere 7 
>> compatibility.
>> 
>> Here is the code of the Interceptor (There is nothing fancy in the 
>> @CustomInterceptorBinding annotation) :
>> 
>> public class CustomExtension implements Extension {
>> 
>>    public void afterBeanDiscovery(@Observes AfterBeanDiscovery abd, 
>> BeanManager beanManager) {
>>        abd.addBean(new Interceptor<Object>() {
>> 
>>            @Override
>>            public Set<Type> getTypes() {
>>                Set<Type> types = new HashSet<Type>();
>>                types.add(Object.class);
>>                return types;
>>            }
>> 
>>            @Override
>>            public Set<Annotation> getQualifiers() {
>>                return Collections.emptySet();
>>            }
>> 
>>            @Override
>>            public Class<? extends Annotation> getScope() {
>>                return Dependent.class;
>>            }
>> 
>>            @Override
>>            public String getName() {
>>                return null;
>>            }
>> 
>>            @Override
>>            public Set<Class<? extends Annotation>> getStereotypes() {
>>                return Collections.emptySet();
>>            }
>> 
>>            @Override
>>            public Class<?> getBeanClass() {
>>                return Object.class;
>>            }
>> 
>>            @Override
>>            public boolean isAlternative() {
>>                return false;
>>            }
>> 
>>            @Override
>>            public boolean isNullable() {
>>                return false;
>>            }
>> 
>>            @Override
>>            public Set<InjectionPoint> getInjectionPoints() {
>>                return Collections.emptySet();
>>            }
>> 
>>            @Override
>>            public Object create(CreationalContext<Object> creationalContext) 
>> {
>>                return new Object();
>>            }
>> 
>>            @Override
>>            public void destroy(Object instance, CreationalContext<Object> 
>> creationalContext) {
>>                // Nothing
>>            }
>> 
>>            @Override
>>            public Set<Annotation> getInterceptorBindings() {
>>                Set<Annotation> types = new HashSet<Annotation>();
>>                types.add(new CustomInterceptorBinding() {
>>                    @Override
>>                    public Class<? extends Annotation> annotationType() {
>>                        return CustomInterceptorBinding.class;
>>                    }
>>                });
>>                return types;
>>            }
>> 
>>            @Override
>>            public boolean intercepts(InterceptionType type) {
>>                return InterceptionType.AROUND_INVOKE == type;
>>            }
>> 
>>            @Override
>>            public Object intercept(InterceptionType type, Object instance, 
>> InvocationContext ctx) {
>>                System.out.println("Intercepted !");
>>                try {
>>                    return ctx.proceed();
>>                } catch (Exception e) {
>>                    throw new RuntimeException(e);
>>                }
>>            }
>> 
>>        });
>>    }
>> 
>> }
>> 
>> 
>> Any idea will be greatly appreciated :D
>> 
>> Thanks in advance.
>> Nicolas RADDE | LTS Team
>> ArcelorMittal
>> Looking for docs ? http://web-java
>> IT Supply Western Europe | 17 rue des Tilleuls
>> 57190 Florange | France
>> www.arcelormittal.com
> 

Reply via email to