| ||||||||||||||||||||||
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 |
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. ResourcesA 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:
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 beanThe 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 configurationThe 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-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>
Using the resourceThe 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. 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] CompatibilityBecause 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.
|