Thursday March 08, 2007
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
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);
}
}
<?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>
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
Posted at 10:29AM Mar 08, 2007 by chengfang in Glassfish | Comments[0]