How to inject and look up custom resources in Glassfish

In my previous post, I wrote about how to create a custom resource in Glassfish. In this entry I will talk about how to inject and look up such custom resources after they have been created in the server.

1. Make these custom resource classes and factory classes available to the server. There are various ways to do that, and I suggest copying them to domains\\domain1\\lib\\classes. This is the content of this directory after copying:


/cygdrive/c/ws/sjsas90/publish/glassfish/domains/domain1/lib/classes > /bin/find .
.
./foo
./foo/Widget.class
./foo/WidgetFactory.class

You may have noticed that the server does not have to load these classes when creating custom resources. But when injecting or looking up the resource, these resource classes must be loadable by the server. Restart the server so the server can pick them up.

2. Write your component classes, e.g., servelt, EJB3 bean class, application client main class, etc. For example:


package foo;

import java.io.\*;
import java.net.\*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.\*;
import javax.servlet.http.\*;

public class FooServlet extends HttpServlet {
    //inject resource into a servlet classinstance variable,    
    //assuming this resource is thread-safe.
    @Resource(name="widget", mappedName="custom/widget")
    private Widget widget;
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet FooServlet</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Servlet FooServlet at " + request.getContextPath() + "</h1>");
        out.println("<p>Injected custom resource: " + widget + "</p>");
        out.println("<p>Custom resource from JNDI lookup: " + lookupWidget() + "</p>");
        out.println("</body>");
        out.println("</html>");
    }
    
    private Widget lookupWidget() throws ServletException {
        Widget widget = null;
        try {
            javax.naming.InitialContext ic = new javax.naming.InitialContext();
            widget = (Widget) ic.lookup("java:comp/env/widget");
        } catch (NamingException ex) {
            throw new ServletException(ex);
        }
        return widget;
    }
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
}


web.xml simply declares and maps this servlet:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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/web-app_2_5.xsd">
    <servlet>
        <servlet-name>FooServlet</servlet-name>
        <servlet-class>foo.FooServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>FooServlet</servlet-name>
        <url-pattern>/FooServlet</url-pattern>
    </servlet-mapping>
</web-app>

Note that for this simple webapp, we don't need sun-web.xml, or any appserver-specific deployment plans. The injected custom resource is resolved by its mappedBy attribute. Portable application should not rely on mappedBy attribute or mapped-by xml element in deployment descriptors.

Another point worth mentioning is, we can look up an injected resource with JNDI. It is particularly useful when we need to access the injected resource outside the injection target class but within the same environment context.

3. Build and deploy the war:


C:\\ws\\sjsas90\\publish\\glassfish\\bin> asadmin deploy \\ws\\nb\\WebApplication1\\dist\\WebApplication1.war
Command deploy executed successfully.

4. View the servlet output http://localhost:8080/WebApplication1/FooServlet



Comments:

 

Posted by guest on July 04, 2011 at 01:43 AM EDT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Cheng Fang

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today