Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my Interceptor not working?

I have two different projects: A and B.

B contains one Interceptor that I'd like to use in project A and, in the future, projects C and D.

I'm using jboss-javaee-6.0 version 3.0.3.Final in both projects (this means, CDI version 1.0).

Project B (does not contain beans.xml):

@InterceptorBinding
@Target({TYPE, METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface PerformanceLog {
}

@Interceptor
@PerformanceLog
public class LoggingInterceptor {
    private static final Logger LOG = LoggerFactory.getLogger(LoggingInterceptor.class);

    @AroundInvoke
    public Object logMethodEntry(InvocationContext ctx) throws Exception {
        LOG.debug("Entered method (" + ctx.getMethod().getName() + ") of class (" + ctx.getMethod().getClass() + ").");
    }
}

Project A (contains beans.xml):

beans.xml (declares Interceptor in order to activate it):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">

<interceptors>
    <class>commons.utils.logging.performanceInterceptor.LoggingInterceptor</class>
</interceptors>
</beans>

MyControllerC:

@Named
@RequestScoped
public class MyControllerC implements Serializable {
     ... 

    @PerformanceLog
    public void init() {
       //do some BD access here
    }
}

When I deploy the application I get the error

weblogic.management.DeploymentException: org.jboss.weld.exceptions.DeploymentException: WELD-001417 Enabled interceptor class <class>commons.utils.logging.performanceInterceptor.LoggingInterceptor    </class> in file:/D:/projectos/myProject/target/myProject/WEB-INF/beans.xml@7 is neither annotated @Interceptor nor registered through a portable extension:org.jboss.weld.exceptions.DeploymentException:WELD-001417 Enabled interceptor class <class>commons.utils.logging.performanceInterceptor.LoggingInterceptor</class> in file:/D:/projectos/myProject/target/myProject/WEB-INF/beans.xml@7 is neither annotated @Interceptor nor registered through a portable extension
at org.jboss.weld.bootstrap.Validator.validateEnabledInterceptorClasses(Validator.java:503)
at org.jboss.weld.bootstrap.Validator.validateDeployment(Validator.java:373)
at org.jboss.weld.bootstrap.WeldBootstrap.validateBeans(WeldBootstrap.java:379)
at com.oracle.injection.provider.weld.WeldInjectionContainer.start(WeldInjectionContainer.java:110)
at com.oracle.injection.integration.CDIAppDeploymentExtension.initCdi(CDIAppDeploymentExtension.java:76)

My first tought was that because the Interceptor is inside a jar that's in project A lib, it was not visible. I upgraded to java-ee 7 version 1.0.3.Final and CDI 1.1 so that I could use the tag @Priority in LoggingInterceptor. The error disappeared, however it did not work (nothing was written to log). So I got back to java-ee 6 version 3.0.3.Final and CDI 1.0.

I'm guessing that maybe I cannot find the Interceptor because the path in beans.xml is not the correct one in the jar (it is inside \WEB-INF\lib\loggings-1.2.jar\commons\utils\logging\performanceInterceptor).

Does anyone have an idea of what might be wrong?

like image 420
Rita Avatar asked Sep 27 '22 05:09

Rita


2 Answers

Comments below your question are pointing you in a good direction.

Project B should be a so-called bean archive - and in Java EE 6 it is achieved by including a beans.xml inside such archive (even completely empty content would be enough).

You said:

Project B is not a web app that's why it doesn't have a beans.xml. It is a transversal project that provides common features to numerous projects

You're a bit wrong - beans.xml in your case still has to exist. In a JAR archive it should be placed in the src/main/resources/META-INF/ folder (if you are using maven).

For WAR packaging it should be placed in src/main/webapp/WEB-INF/.


Also one more thing: you said in the comments two opposite statements:

Project B is not a web app

and also:

If I add a beans.xml to Project B (...) (/webapp/WEB-INF/beans.xml because it is a war)

So what is the truth?

like image 132
G. Demecki Avatar answered Dec 31 '22 20:12

G. Demecki


I had a similar problem, where my Interceptor wasn't called for certain annotated methods and it worked for some other. The problem was that the class (RESTClient.java) that used the Interceptor annotation wasn't injected, but initialized through constructor. To solve the issue, I did something like this :

@Inject
private Provider<RESTClient> restClientProvider;
//..
RESTClient restClient = restClientProvider.get();
restClient.init(
//do whatever the constructor was doing here.
);
like image 21
Kartik Adhia Avatar answered Dec 31 '22 22:12

Kartik Adhia