views:

174

answers:

2

I created a plain servlet within a seam-gen (2.1.2) application, now I would like to use injection. Thus I annotated it with @Name and it's recognized as component:

     INFO  [Component] Component: ConfigReport, 
scope: EVENT, type: JAVA_BEAN, class: com.mycompany.servlet.ConfigReport

Unfortunatly the injection of the logger doesn't work NullPointerException in init()

import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.log.Log;

@Name("ConfigReport")
public class ConfigReport extends HttpServlet {

    @Logger
    private Log log;

    public void init(ServletConfig config) throws ServletException {
        log.info( "BOOM" );
    }

}

Is my approach abusive?
What would be the alternatives (the client sending requests to the servlet is curl, not a browser)?

+1  A: 

Stacker, if you want Seam @In-ject any enabled component, It must intercept your request. For instance, @In-jection works when using EJB because Seam uses an EJB interceptor. It explains why you should declare Seam EJB interceptor when using EJB.

Java Server Faces, in other hand, Seam makes use of Expression Language resolver, to give you access To any Seam component, which is installed by default when Seam core jar is placed on the classpath.

The key to enable Seam integration with any web Technology is ContextFilter - Be aware you must enable Seam Filter to use ContextFilter - which is described according to Seam in Action book as follows

Enables Seam container and contexts for non-JSF requests. Should not be applied to JSF requests as it causes duplicate logic to be performed, leading to undefined results.

...

The ContextFilter opens access to the Seam container and its context variables to non-JSF servlets, such as Struts, Spring MVC, and Direct Web Remoting (DWR). Although a majority of Seam’s work is done in the JSF servlet, these additional filters allow Seam to extend the boundaries of its life cycle above and beyond the reach of the JSF servlet.

To enable ContextFilter you declare it in components.xml

<component name="org.jboss.seam.web.contextFilter">
    <property name="disabled">false</property>
</component>

Because no spare Time, i do not know how to use this kind of funcionality. Take a look at ContextFilter source code. It can give you a good insight.

Arthur Ronald F D Garcia
+ Thanks, just bought this book a few days ago, and you gave me the pointers
stacker
+2  A: 

For the records:

Besides the important hints arthur-ronald-f-d-garcia gave.

Injection of components still doesn't work, at least I can lookup the components manually, therefore a context is required ContextualHttpServletRequest.

components.xml

<web:context-filter regex-url-pattern="/config/*"/>

servlet.java

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        final HttpServletRequest req = request;
        final HttpServletResponse res = response;
         new ContextualHttpServletRequest(req) {
                public void process() throws Exception {
                    wrappedGet( req, res );
                }
              }.run();
    }

In the wrapped servlet code

entityManager = (EntityManager) Component.getInstance( "entityManager" );

Links:

stacker
@stacker Thank you. Good job (+1). I pretend to use ContextFilter with other web Frameworks.
Arthur Ronald F D Garcia