com.caucho.resin
Class ResinBeanContainer

java.lang.Object
  extended by com.caucho.resin.ResinBeanContainer

public class ResinBeanContainer
extends java.lang.Object

Embeddable Resin context for testing of bean container components (CDI managed beans, EJBs, JPA) in an environment that mirrors a production runtime but without the overhead of the Resin server. The ResinBeanContainer can be embedded into any Java SE environment, including a JUnit test. Note, the bean container does not support Servlet-based APIs. In order to test the web tier, ResinEmbed should be used.

 static void main(String []args)
 {
   ResinBeanContainer beans = new ResinBeanContainer();
 
   beans.addModule("test.jar");
   beans.start();
 
   RequestContext req = beans.beginRequest();
   try {
     MyMain main = beans.getInstance(MyMain.class);
 
     main.main(args);
   } finally {
     req.close();
   }
 
   beans.close();
 }
 

Configuration File

The optional configuration file for the ResinContext allows the same environment and bean configuration as the resin-web.xml, but without the servlet-specific configuration.
 
 <beans xmlns="http://caucho.com/ns/resin"
              xmlns:resin="urn:java:com.caucho.resin">
 
    <resin:import path="${__DIR__}/my-include.xml"/>
 
    <database name="my-database">
      <driver ...>
        ...
      </driver>
    </database>
 
    <mypkg:MyBean xmlns:mypkg="urn:java:com.mycom.mypkg">
      <my-property>my-data</my-property>
    </mypkg:MyBean>
 </beans>
 
 


Constructor Summary
ResinBeanContainer()
          Creates a new Resin context.
 
Method Summary
 void addBeansXml(java.lang.String pathName)
          Adds a Resin beans configuration file, allowing creation of databases or bean configuration.
 void addClassPath(java.lang.String classPath)
          Adds a new module (jar or exploded classes directory)
 void addPackageModule(java.lang.String packageName)
          Adds a package in the classpath as module root.
 void addPackageModule(java.lang.String modulePath, java.lang.String packageName)
          Adds a package as module root.
 void addResourceRoot(Path path)
           
 BeanContainerRequest beginRequest()
          Enters the Resin context and begins a new request on the thread.
 void close()
          Shuts the context down.
 void completeRequest(BeanContainerRequest context)
          Completes the thread's request and exits the Resin context.
 java.lang.Object getBeanByName(java.lang.String name)
          Returns an instance of the bean with the given name.
 InjectManager getCdiManager()
           
 java.lang.ClassLoader getClassLoader()
           
<T> T
getInstance(java.lang.Class<T> type, java.lang.annotation.Annotation... qualifiers)
          Returns a new instance of the given type with optional qualifiers.
 java.lang.Object getInstance(java.lang.String className, java.lang.annotation.Annotation... qualifiers)
          Returns a new instance of the given type with optional bindings.
 void request(java.lang.Runnable runnable)
          Executes code in the Resin bean classloader and creates a request scope.
 void setId(java.lang.String id)
           
 void setModule(java.lang.String modulePath)
           
 void setWorkDirectory(java.lang.String path)
          Sets the work directory for Resin to use when generating temporary files.
 void start()
          Initializes the context.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ResinBeanContainer

public ResinBeanContainer()
Creates a new Resin context.

Method Detail

setId

public void setId(java.lang.String id)

getCdiManager

public InjectManager getCdiManager()

setModule

public void setModule(java.lang.String modulePath)

addClassPath

public void addClassPath(java.lang.String classPath)
Adds a new module (jar or exploded classes directory)


addPackageModule

public void addPackageModule(java.lang.String modulePath,
                             java.lang.String packageName)
Adds a package as module root.

Parameters:
packageName - the name of the package to be treated as a virtual module root.

addPackageModule

public void addPackageModule(java.lang.String packageName)
Adds a package in the classpath as module root.

Parameters:
packageName - the name of the package to be treated as a virtual module root.

addBeansXml

public void addBeansXml(java.lang.String pathName)
Adds a Resin beans configuration file, allowing creation of databases or bean configuration.

Parameters:
pathName - URL/path to the configuration file

addResourceRoot

public void addResourceRoot(Path path)

setWorkDirectory

public void setWorkDirectory(java.lang.String path)
Sets the work directory for Resin to use when generating temporary files.


start

public void start()
Initializes the context.


getInstance

public java.lang.Object getInstance(java.lang.String className,
                                    java.lang.annotation.Annotation... qualifiers)
Returns a new instance of the given type with optional bindings. If the type is a managed bean, it will be injected before returning.

Parameters:
className - the className of the bean to instantiate
qualifier - optional @Qualifier annotations to select the bean

getInstance

public <T> T getInstance(java.lang.Class<T> type,
                         java.lang.annotation.Annotation... qualifiers)
Returns a new instance of the given type with optional qualifiers.


getBeanByName

public java.lang.Object getBeanByName(java.lang.String name)
Returns an instance of the bean with the given name. If the type is a managed bean, it will be injected before returning.

Parameters:
name - the @Named of the bean to instantiate

request

public void request(java.lang.Runnable runnable)
Executes code in the Resin bean classloader and creates a request scope.
 resinBean.request (new Runnable() {
   doMyCode();
 });
 


beginRequest

public BeanContainerRequest beginRequest()
Enters the Resin context and begins a new request on the thread. The the returned context must be passed to the completeRequest. To ensure the request is properly closed, use the following pattern:
 ResinContext resinContext = ...;
 
 RequestContext cxt = resinContext.beginRequest();
 
 try {
    // ... actions inside the Resin request context
 } finally {
   resinContext.completeRequest(cxt);
 }
 

Returns:
the RequestContext which must be passed to completeContext

completeRequest

public void completeRequest(BeanContainerRequest context)
Completes the thread's request and exits the Resin context.


close

public void close()
Shuts the context down.


getClassLoader

public java.lang.ClassLoader getClassLoader()

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object