How to parameterize and configure custom resources

In my previous two posts (post1 and post2), I demonstrated how to create and inject custom resources in Glassfish application server. However, the custom resource used there has a hard-coded name attribute and thus cannot be parameterized. In this post, I will modify a few steps and files to make it configurable, so that the same resource type and factory can be applied to multiple resources.

1. Specify additional properties when creating custom resource:


asadmin create-custom-resource --restype foo.Widget 
--factoryclass foo.WidgetFactory
--property name=widget-two
--description "this is widget-two"
custom/widget-two

 

It's also possible to specify multiple key-value pairs in the form of key1=val1:key2=val2:key3=val3.

2. Modify foo.WidgetFactory.getObjectInstance method:


    public Object getObjectInstance(Object obj, 
                                    Name name, 
                                    Context nameCtx, 
                                    Hashtable<?, ?> environment) 
        throws Exception {
        Widget widget = new Widget();
        if(obj instanceof Reference) {
            Reference reference = (Reference) obj;
            Enumeration<RefAddr> attributes = reference.getAll();
            widget.init(attributes);
        }
        return widget;
    }

 

I also added a init method to foo.Widget to initialize fields:


    public void init(Enumeration<RefAddr> attributes) {
        while(attributes.hasMoreElements()) {
            RefAddr refAddr = (RefAddr) attributes.nextElement();
            if("name".equals(refAddr.getType())) {
                setName((String) refAddr.getContent());
            }
        }
    }

 

3. Modify the servlet class to inject multiple custom resources with different values but same type and factory class:


public class FooServlet extends HttpServlet {
    @Resource(name="widget-one", mappedName="custom/widget-one")
    private Widget widget1;

    @Resource(name="widget-two", mappedName="custom/widget-two")
    private Widget widget2;

    @Resource(name="widget-three", mappedName="custom/widget-three")
    private Widget widget3;
    
    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: " + widget1 + "<br>");
        out.println("Custom resource from JNDI lookup: " + lookupWidget("widget-one") + "</p>");
        out.println("<p>Injected custom resource: " + widget2 + "<br>");
        out.println("Custom resource from JNDI lookup: " + lookupWidget("widget-two") + "</p>");
        out.println("<p>Injected custom resource: " + widget3 + "<br>");
        out.println("Custom resource from JNDI lookup: " + lookupWidget("widget-three") + "</p>");
        out.println("</body>");
        out.println("</html>");
    }

 

To look up custom resources without using injection, just configure them in web.xml (or ejb-jar.xml, application-client.xml). Note that the you need to use resource-env-ref, not resource-ref elements:


    <resource-env-ref>
        <resource-env-ref-name>widget-one</resource-env-ref-name>
        <resource-env-ref-type>foo.Widget</resource-env-ref-type>
        <mapped-name>custom/widget-one</mapped-name>
    </resource-env-ref>
    <resource-env-ref>
        <resource-env-ref-name>widget-two</resource-env-ref-name>
        <resource-env-ref-type>foo.Widget</resource-env-ref-type>
        <mapped-name>custom/widget-two</mapped-name>
    </resource-env-ref>
    <resource-env-ref>
        <resource-env-ref-name>widget-three</resource-env-ref-name>
        <resource-env-ref-type>foo.Widget</resource-env-ref-type>
        <mapped-name>custom/widget-three</mapped-name>
    </resource-env-ref>

 

4. Build, deploy the war and view the servlet output at http://localhost:8080/WebApplication1/FooServlet


Comments:

thanks we sure appreciate your kind comments.

Posted by muztagh on February 10, 2008 at 03:39 PM EST #

thanks for this post.

Posted by Ice road on February 14, 2008 at 09:25 PM EST #

Could any one tell me whether WSHttpBinding is Interoparable with java?

Posted by replica watches on February 22, 2010 at 01:26 PM EST #

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