SeamFramework.orgCommunity Documentation

附錄 A. 將 Web Bean RI 整合入其它環境中

目前,Web Bean RI 只能在 JBoss AS 5 中執行;要將 RI 整合入其它 EE 環境中(比方說另一個像是 Glassfish 的應用程式伺服器)、整合入一個 servlet 容器(例如 Tomcat)中,或是和一個崁入式的 EJB3.1 實做整合都是相當容易的。在此附錄中,我們將簡略地討論所需的步驟。

您可在一個 SE 環境下執行 Web Bean,不過您必須進行較多步驟,包括新增您自己的 context 和生命週期。Web Bean RI 目前並不會顯示生命週期的延伸點,因此您必須針對於 Web Bean RI 的 class 來直接地進行程式撰寫。

Web Bean SPI 位於 webbeans-ri-spi 模組中,並且被封裝為 webbeans-ri-spi.jar。有些 SPI 為可選的,若您需要置換預設的特性,您則需要其它 SPI。

SPI 中所有的介面都支援裝飾器模式並提供了一個 Forwarding class。

Web Bean RI 也會委派 EJB3 bean discovery 至 container,因此它便無須掃描 EJB3 記號或剖析 ejb-jar.xml。針對於應用程式中的各個 EJB 都應該能發現一個 EJBDescriptor:

public interface EjbDiscovery

{
   public static final String PROPERTY_NAME = EjbDiscovery.class.getName();
   
   /**
    * 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 規格中所定義地來回傳相關的 metadata。除了這兩個介面,還有個代表本地商業介面的 BusinessInterfaceDescriptor(包含了使用來查詢某個 EJB instance 的 interface class 以及 jndi 名稱)。

The Web Beans RI must delegate JTA activities to the container. The SPI provides a couple hooks to easily achieve this with the TransactionServices interface.

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();
}

The enumeration Status is a convenience for implementors to be able to keep track of whether a synchronization is supposed to notify an observer only when the transaction is successful, or after a failure, or regardless of the status of the transaction.

Any javax.transaction.Synchronization implementation may be passed to the registerSynchronization() method and the SPI implementation should immediately register the synchronization with the JTA transaction manager used for the EJBs.

To make it easier to determine whether or not a transaction is currently active for the requesting thread, the isTransactionActive() method can be used. The SPI implementation should query the same JTA transaction manager used for the EJBs.

Web Bean RI 對於 container 有些需求以便達到 API 實做之外的正確的功能。