Caucho Technology
documentation
examples
changes

amber (jpa)
ejb
database
ioc
jmx
jsf
messaging
quercus
remoting
servlet
security

basic resource
injection
periodic task
ioc appconfig

a simple resource bean


Resources are beans configured in the resin.conf or resin-web.xml and stored in the WebBeans registry. The tutorial shows the configuration of a trivial bean as a resource and using it from a JSP file.

Demo

Overview

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

There is also more documentation for Resin's IoC/Dependency Injection.

Some typical resources include:

  • Databases
  • JMS connections
  • EJB stateful and stateless session beans
  • JCA resources

Because resources are configured in the resin.conf/resin-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.

resin-web.xml configuration

The resin-web.xml (or resin.conf) configures the resource with the <bean> 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>.

resin-web.xml
<web-app xmlns="http://caucho.com/ns/resin">

  <bean class="test.TestResource">
    <init>
      <value>An Example Resource</value>
    </init>
  </bean>

</web-app>
TAGDESCRIPTION
beandefines the singleton service
classthe class name of the resource bean
initAny bean-style configuration goes here
namethe optional WebBeans name of the resource
valueThe example bean's setValue parameter.

Using the resource from PHP

Your PHP scripts can use any WebBeans resource by using the java_bean() method. The method will return the named resource.

test.php
<php?

$resource = java_bean("testResource");

echo "PHP resource: " . $resource . "\n";

?>

Using the resource from JSP

JSP pages can also use WebBeans resource in the JSP expression language.

test.jsp

JSP resource: ${testResource}

Using the resource from a Servlet

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

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 cluster. 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.webbeans.In;

public class TestServlet extends HttpServlet {
  @In private TestResource _resource;
  
  public void doGet(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException
  {
    PrintWriter out = res.getWriter();

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

Demo


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