A Simple Resource Bean
Resin 3.1

Documentation
Examples
Changes

Quercus
Database
Amber
EJB
SOA/ESB
IoC
JMS
Servlet
JMX
Hessian
Security

JAXB IoC
Basic Resource
Injection
Periodic Task
JNDI appconfig
JAXB IoC
IoC
Injection

Resources are beans configured in the resin.conf or web.xml and stored in JNDI. The tutorial shows the configuration of a trivial bean as a resource and looking it up using JNDI.

Demo

Resources

A resource in Resin is any bean configured in the resin.conf or web.xml. Resources are generally stored in JNDI. Because resources can be any Java class conforming to the bean configuration patterns, resources provide a flexible configuration.

Some typical resources include:

  • Databases
  • JMS connections
  • The EJB server
  • JCA resources

Because resources are configured in the resin.conf/web.xml and are created before any servlets start, they are very convenient for globally available beans, even allowing for more complex configuration beans extending the idea of <env-entry> (which stored simple Strings or Integers in JNDI.)

The TestResource bean

The TestResource bean is almost as simple as possible. It has a single String configuration parameter and does nothing but print its value using toString().

TestResource.java
package test;

public class TestResource {
  private String _value = "default";

  public void setValue(String value)
  {
    _value = value;
  }

  public String toString()
  {
    return "TestResource[" + _value + "]";
  }
}

In other words, you have lots of flexibility of things to configure as resources.

web.xml configuration

The web.xml (or resin.conf) configures the resource with the <resource> tag. The resource is created and stored in the environment where it is defined. So a resource configured in the <host> will be available for all web-apps in that host and a resource configured in a <web-app> will only be available for that <web-app>.

web.xml
<web-app xmlns="http://caucho.com/ns/resin">
  <resource jndi-name="test/basic" type="test.TestResource">
    <init>
      <value>An Example Resource</value>
    </init>
  </resource>
</web-app>
tagdescription
resourcedefines the resource
namethe JNDI name of the resource (in java:comp/env)
typethe class name of the resource bean
initAny bean-style configuration goes here
valueThe example bean's setValue parameter.

Using the resource

The example uses a servlet to demonstrate the resource, but any class could use JNDI to look up and use the resource. Because resources are stored in JNDI, they can avoid the complexity of passing objects around or storing in the servlet context.

Because JNDI is just a storage tree, the example looks up the resource once and then stores it in a private field.

If you save the resource, it's important that the saved field is reloaded when the web-app restarts. The resource has the same lifetime as its environment: web-app, host or server. When that environment closes, the resource is no longer valid and must be discarded. In particular, it is important to store any resource in an object's field, not in a static field or static hashtable.

TestServlet.java
package test;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.naming.InitialContext;
import javax.naming.Context;
import javax.naming.NamingException;

public class TestServlet extends HttpServlet {
  private TestResource _resource;

  public void init()
    throws ServletException
  {
    try {
      Context ic = new InitialContext();

      _resource = (TestResource) ic.lookup("java:comp/env/test/basic");
    } catch (NamingException e) {
      throw new ServletException(e);
    }
  }
  
  public void doGet(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException
  {
    PrintWriter out = res.getWriter();

    out.println("Resource: " + _resource);
  }
}
Resource: TestResource[An example resource]

Compatibility

Because the resource beans are just Java classes, the resources themselves are independent of Resin. The configuration, of course, is dependent on Resin.

You can configure resources in other server engines by creating a load-on-startup servlet that creates your resource and stores them into JNDI. Although that is less convenient than Resin's resource configuration, it will work with other servlet engines. So you can start creating and using resources without worrying excessively about compatibility issues.

Demo


JAXB IoC
IoC
Injection
Copyright © 1998-2006 Caucho Technology, Inc. All rights reserved.
Resin ® is a registered trademark, and Quercustm, Ambertm, and Hessiantm are trademarks of Caucho Technology.