SeamFramework.orgCommunity Documentation

附录 A. 将Web Bean参考实现整合到其他环境中

目前,Web Bean的参考实现只能运行在JBoss AS5中;将参考实现整合到其他EE环境中(例如像Glassfish的其他的应用服务器)以及一个Servlet容器(像Tomcat)中或者一个内嵌的EJB3.1实现中相当容易。在附录中我们将简要的讨论所需的步骤。

注意

Web Bean可以在SE环境中运行,但是你需要做更多的工作,添加你自己的上下文和生命周期。Web Bean参考实现目前没有暴露生命周期扩展点,所以你不得不直接编写Web Bean参考实现的类。

The Web Beans SPI is located in webbeans-spi module, and packaged as webbeans-spi.jar. Some SPIs are optional, if you need to override the default behavior, others are required.

所有的SPI接口都支持装饰器模式,并且提供一个 Forwarding 类。

Web Bean参考实现也将EJB3 Bean的发现委托给容器,以便它不用再扫描EJB3注释和解析 ejb-jar.xml 文件。对于应用中的每个EJB都应发现一个EJBDescriptor:

public interface EjbServices

{
   
   /**
    * Gets a descriptor for each EJB in the application
    * 
    * @return The bean class to descriptor map 
    */
   public Iterable<EjbDescriptor<?>> discoverEjbs();
public interface EjbDescriptor<T

> {
   
   /**
    * Gets the EJB type
    * 
    * @return The EJB Bean class
    */
   public Class<T
> getType();
   /**
    * Gets the local business interfaces of the EJB
    * 
    * @return An iterator over the local business interfaces
    */
   public Iterable<BusinessInterfaceDescriptor<?>
> getLocalBusinessInterfaces();
   
   /**
    * Gets the remote business interfaces of the EJB
    * 
    * @return An iterator over the remote business interfaces
    */
   public Iterable<BusinessInterfaceDescriptor<?>
> getRemoteBusinessInterfaces();
   
   /**
    * Get the remove methods of the EJB
    * 
    * @return An iterator over the remove methods
    */
   public Iterable<Method
> getRemoveMethods();
   /**
    * Indicates if the bean is stateless
    * 
    * @return True if stateless, false otherwise
    */
   public boolean isStateless();
   /**
    * Indicates if the bean is a EJB 3.1 Singleton
    * 
    * @return True if the bean is a singleton, false otherwise
    */
   public boolean isSingleton();
   /**
    * Indicates if the EJB is stateful
    * 
    * @return True if the bean is stateful, false otherwise
    */
   public boolean isStateful();
   /**
    * Indicates if the EJB is and MDB
    * 
    * @return True if the bean is an MDB, false otherwise
    */
   public boolean isMessageDriven();
   /**
    * Gets the EJB name
    * 
    * @return The name
    */
   public String getEjbName();
   
   
}

EjbDescriptor 具有相当的自解释性,应该返回EJB规范中定义的相关元数据。除了这两个接口,还有一个表示本地业务接口的 BusinessInterfaceDescriptor (封装了接口类和用于查询EJB实例的jndi名字)

The resolution of @EJB and @Resource is delegated to the container. You must provide an implementation of org.jboss.webbeans.ejb.spi.EjbServices which provides these operations. Web Beans passes in the javax.inject.manager.InjectionPoint the resolution is for, as well as the NamingContext in use for each resolution request.

Web Bean RI必须将JTA活动委托给容器。SPI提供者提供一些钩子(hooks)结合TransactionServices 接口来轻松完成这个任务。

public interface TransactionServices

{
   /**
    * Possible status conditions for a transaction. This can be used by SPI
    * providers to keep track for which status an observer is used.
    */
   public static enum Status
   {
      ALL, SUCCESS, FAILURE
   }
   /**
    * Registers a synchronization object with the currently executing
    * transaction.
    * 
    * @see javax.transaction.Synchronization
    * @param synchronizedObserver
    */
   public void registerSynchronization(Synchronization synchronizedObserver);
   /**
    * Queries the status of the current execution to see if a transaction is
    * currently active.
    * 
    * @return true if a transaction is active
    */
   public boolean isTransactionActive();
}

枚举 Status 对实现者来说是一个很方便的工具,可以追踪在事务成功或者失败或者无论事务成功还是失败的情况下,是否应该将一个同步通知给观察者。

任何 javax.transaction.Synchronization 实现必须传递给 registerSynchronization() 方法,SPI实现应该立刻将同步注册到EJB使用的JTA事务管理器。

为了更容易的决定对于请求线程来说一个事务在当前是否是活动的,我们可以使用isTransactionActive() 方法。SPI实现应该查询EJB使用的同一个JTA事务管理器。

为了接口实现之外的正确的功能,Web Beans参考实现对容器有大量的要求。