InterfacesThis appendix provides a quick reference of the EJB APIs. You should use this reference if you need to get quick information on which methods are implemented by a particular interface, the arguments for a method, or the return type. Although the exceptions are described in detail in Chapter 13, "Exception Handling," we provide a summary view of them here for your convenience. Figure A.1 shows a UML diagram of the EJB interfaces and their relationships. Figure A.1. A UML diagram representing the EJB interface hierarchy.
javax.ejb.EJBContextAn enterprise bean uses the EJBContext interface to gain access to the runtime context provided by the container. The EJBContext interface is the parent interface to the SessionContext and EntityContext interfaces, as shown in Figure A.1. Table A.1 shows the methods defined in the EJBContext interface. Table A.1. The Methods of the EJBContext Interface
getCallerIdentityThe getCallerIdentity method returns the java.security.Identity of the caller. Here is the method signature: public java.security.Identity getCallerIdentity(); Caution The getCallerIdentity method has been deprecated. You should use the getCallerPrincipal method instead. getCallerPrincipalThe getCallerPrincipal method returns a java.security.Principal , which identifies the caller. The method should never return null. Here is the method signature: public java.security.Principal getCallerPrincipal(); getEJBHomeThe getEJBHome method returns the home interface of the enterprise bean. Here is the method signature: public javax.ejb.EJBHome getEJBHome(); getEJBLocalHomeThe getEJBLocalHome method returns the local home interface of the enterprise bean. Here is the method signature: public javax.ejb.EJBLocalHome getEJBLocalHome(); getEnvironmentThe getEnvironment method returns the enterprise bean's environment properties. This method has been deprecated. Here is the method signature: public Properties getEnvironment(); Caution The getEnvironment method has been deprecated. You should use the JNDI naming context java:comp/env to access the bean's environment. getRollbackOnlyThe getRollbackOnly tests to see whether the transaction has been marked for rollback. Only enterprise beans with container-managed transactions are allowed to use this method. Here is the method signature: public boolean getRollbackOnly() throws IllegalStateException; An IllegalStateException will be thrown if the instance is not allowed to use this method. Typically, this is because the instance is using bean-managed transactions. getUserTransactionThe getUserTransaction method returns the transaction demarcation interface. Only enterprise beans with bean-managed transactions are allowed to use the UserTransaction interface. Because entity beans must always use container-managed transactions, only session beans and message-driven beans with bean-managed transactions are allowed to invoke this method. Here is the method signature: public javax.transaction.UserTransaction getUserTransaction() throws IllegalStateException; An IllegalStateException will be thrown if the instance is not allowed to use this method. Typically, this is because the instance is using container-managed transactions. isCallerInRoleThe isCallerInRole method tests to see whether the caller is in a given role. There are two variations of this method. Here are the method signatures: public boolean isCallerInRole(String roleName); public boolean isCallerInRole(java.security.Identity roleName); The second method, which takes an Identity as its argument, is deprecated and should not be used. The roleName argument in the non-deprecated version must be one of the security roles defined in the deployment descriptor. This method returns true if the caller of the bean method is in the role specified by the argument. Caution The isCallerInRole method that takes a java.security.Identity has been deprecated and should not be used. setRollbackOnlyThe setRollbackOnly method marks the current transaction for rollback. The transaction will become permanently marked for rollback. A transaction marked for rollback can never commit. Only enterprise beans with container-managed transactions are allowed to use this method. Here is the method signature: public void setRollbackOnly() throws IllegalStateException; An IllegalStateException will be thrown if the instance is not allowed to use this method. Typically, this is because the instance is using bean-managed transactions. javax.ejb.EJBHomeThe EJBHome interface defines the methods that allow a remote client to create, locate, and remove enterprise beans. A bean provider must create a home interface for an entity or session bean, and the home interface must extend the EJBHome interface. The message-driven bean is not exposed to clients and therefore does not require a home interface. Table A.2 shows the methods in the EJBHome interface. Table A.2. The Methods of the EJBHome Interface
getEJBMetaDataThe getEJBMetaData method obtains a reference to an object that implements the EJBMetaData interface for the enterprise bean. The EJBMetaData interface allows the client to obtain information about the enterprise bean. Here is the method signature: public EJBMetaData getEJBMetaData() throws java.rmi.RemoteException; getEJBHomeHandleThe getEJBHomeHandle returns a handle for the home object. The handle can be used at later time to re-obtain a reference to the home object, possibly in a different JVM. Here is the method signature: public EJBHomeHandle getEJBHomeHandle() throws java.rmi.RemoteException; removeThere are two variations of the remove method. Both cause the EJBObject to be removed. Here are the method signatures: public void remove(Handle handle) throws java.rmi.RemoteException, RemoveException; public void remove(Object primaryKey) throws java.rmi.RemoteException, RemoveException; Caution The remove method that takes a primary key can be used only for an entity bean. An attempt to call this method on a session bean will result in a RemoveException being thrown. With either variation of the remove method, a RemoveException will be thrown if the enterprise bean or the container does not allow the client to remove the object. The RemoveException is described later in this appendix. javax.ejb.EJBLocalHomeAll enterprise beans that use a local home interface must extend their local home interface from the EJBLocalHome interface. The local home interface for an enterprise bean defines the methods that allow local clients to create, find, and remove EJB objects. It also can contain home business methods that are not specific to a specific bean instance. These home business methods can be defined only for entity beans. removeThe remove method removes an object identified by its primary key. Here is the method signature: public void remove(Object primaryKey) throws RemoteException, EJBException; This method can be called only by local clients of an entity bean. An attempt to invoke this method on a session bean will result in a RemoveException . javax.ejb.EJBLocalObjectThe EJBLocalObject interface must be extended by all enterprise beans' local interfaces. An enterprise bean's local interface provides the local client view of an EJB object. Table A.3 shows the methods in the EJBLocalObject interface. Table A.3. The Methods of the EJBLocalObject Interface
getEJBLocalHomeObtain the enterprise bean's local home interface. The local home interface defines the enterprise bean's create , finder , remove , and home business methods that are available to local clients. Here is the method signature: public EJBLocalHome getEJBLocalHome() throws EJBException; getPrimaryKeyThis method can be called on an entity bean. An attempt to invoke this method on a session bean will result in an EJBException . Here is the method signature: public Object getPrimaryKey() throws RemoteException, EJBException; isIdenticalTest whether a given EJB local object is identical to the invoked EJB local object. Here is the method signature: public boolean isIdentical(EJBLocalObject obj) throws EJBException; removeRemoves the EJB local object. Here is the method signature: public void remove() throws RemoveException, EJBException; javax.ejb.EJBMetaDataA client uses the EJBMetaData interface to obtain metadata information about an enterprise bean. The metadata is typically used by development tools or scripting languages. The EJBMetaData interface does not extend the Remote interface and is not accessed remotely by the client. An instance of this class is sent to the client as a copy, and changes to the object are not reflected on the server. Table A.4 shows the methods in the EJBMetaData interface. Table A.4. The Methods of the EJBMetaData Interface
Tip The EJBMetaData class is designed for remote clients only. It is not supported with local clients. getEJBHomeThe getEJBHome method returns the home interface of the enterprise bean. Here is the method signature: public javax.ejb.EJBHome getEJBHome(); getHomeInterfaceClassThe getHomeInterfaceClass returns the Class object for the enterprise bean's home interface. Here is the method signature: public Class getHomeInterfaceClass(); getPrimaryKeyClassThe getPrimaryKeyClass returns the Class object for the enterprise bean's primary key class. Here is the method signature: public Class getPrimaryKeyClass(); getRemoteInterfaceClassThe getRemoteInterfaceClass returns the Class object for the enterprise bean's remote interface class. Here is the method signature: public Class getRemoteInterfaceClass(); isSessionThe isSession method returns true if the enterprise bean is a session bean. Otherwise, it will return false. Here is the method signature: public boolean isSession(); isStatelessSessionThe isStatelessSession method returns true if the enterprise bean is a stateless session bean. Otherwise it will return false. Here is the method signature: public boolean isStatelessSession(); javax.ejb.EJBObjectA bean provider creates a remote interface that extends the EJBObject interface. The remote interface defines the business methods that can be called by a remote client. Each enterprise bean, except for the message-driven, has a component interface. Table A.5 shows the methods in the EJBObject interface. Table A.5. The Methods of the EJBObject Interface
getEJBHomeThe getEJBHome method returns the home interface of the enterprise bean. Here is the method signature: public javax.ejb.EJBHome getEJBHome() throws java.rmi.RemoteException; getHandleThe getHandle method returns a handle for the EJB object. The handle can be used at later time to re-obtain a reference to the EJB object, possibly in a different JVM. Here is the method signature: public Handle getHandle() throws java.rmi.RemoteException; getPrimaryKeyThe getPrimaryKey method returns the primary key for an EJB object. This method can be called only on an entity bean. If you attempt to invoke this method on a session bean, a RemoteException will be thrown. Here is the method signature: public Object getPrimaryKey() throws java.rmi.RemoteException; isIdenticalThe isIdentical method tests whether a given EJB object is identical to the invoked EJB object. Here's the method signature: public boolean isIdentical(EJBObject obj) throws java.rmi.RemoteException; The method returns true if the given EJB object is identical to the one invoked. Otherwise false is returned. removeThe remove method removes the EJB object. Here is the method signature: public void remove() throws java.rmi.RemoteException, RemoveException; If the enterprise bean or the container does not allow destruction of the object, a RemoveException is thrown. javax.ejb.EnterpriseBeanEvery enterprise bean class must implement the EnterpriseBean interface. The EnterpriseBean interface does not define any methods itself but acts as a placeholder for future methods. It is a common superinterface for SessionBean and EntityBean . javax.ejb.EntityBeanEvery entity bean class must implement the EntityBean interface. The container uses the EntityBean methods to notify enterprise bean instances of lifecycle events. Table A.6 shows the methods in the EntityBean interface. Table A.6. The Methods of the EntityBean Interface
Tip In the next several methods, the RemoteException is defined in the method signature to provide backward compatibility with enterprise beans written for the EJB 1.0 Specification. Enterprise beans written for the EJB 1.1 and 2.0 Specifications should throw the javax.ejb.EJBException instead of the RemoteException . ejbActivateThe container invokes the ejbActivate method when the instance is taken out of the pool of available instances and becomes associated with a specific EJB object. Here is the method signature: public void ejbActivate() throws java.rmi.RemoteException, EJBException; ejbLoadThe container invokes the ejbLoad method to instruct the instance to synchronize its state by loading its state from the underlying database. Here is the method signature: public void ejbLoad() throws java.rmi.RemoteException, EJBException; This method always executes in the transaction context determined by the value of the transaction attribute in the deployment descriptor. ejbPassivateThe container invokes the ejbPassivate method on an instance before the instance becomes disassociated with a specific EJB object. After this method completes, the container will place the instance into the pool of available instances. Here is the method signature: public void ejbPassivate() throws java.rmi.RemoteException, EJBException; ejbRemoveThe container invokes the ejbRemove method before it removes the EJB object that is currently associated with the instance. This method is invoked when a client invokes a remove operation on the enterprise bean. Here is the method signature: public void ejbRemove() throws java.rmi.RemoteException, EJBException, RemoveException; ejbStoreThe container invokes the ejbStore method to instruct the instance to synchronize its state by storing it to the underlying database. This method always executes in the transaction context determined by the value of the transaction attribute in the deployment descriptor. Here is the method signature: public void ejbStore() throws java.rmi.RemoteException, EJBException; setEntityContextThe container invokes the ejbStore method on an instance after the instance has been created to set the associated entity context. The bean instance should store the context in an instance variable. Here is the method signature: public void setEntityContext(EntityContext ctx) throws java.rmi.RemoteException, EJBException; unsetEntityContextThe container invokes the ejbStore method on an instance before removing the instance. This is the last method that the container invokes on the instance. Here is the method signature: public void unsetEntityContext() throws java.rmi.RemoteException, EJBException; javax.ejb.EntityContextThe EntityContext interface provides an instance with access to the container-provided runtime context. The container passes the EntityContext interface to an entity enterprise bean instance after the instance has been created. The EntityContext interface remains associated with the instance for the lifetime of the instance. Table A.7 shows the methods in the EntityContext interface. Table A.7. The Methods of the EntityContext Interface
getEJBLocalObjectThe getEJBObject method returns a reference to the EJB local object that currently is associated with the instance. An instance can use this method, for example, when it wants to pass itself as a reference in a method argument or return value. Here is the method signature: public EJBLocalObject getEJBLocalObject() throws IllegalStateException; An IllegalStateException will be thrown if an instance invokes this method while the instance is in a state that does not allow this method to be invoked or if the enterprise bean does not have a local interface. getEJBObjectThe getEJBObject method returns a reference to the EJB object that is currently associated with the instance. An instance can use this method, for example, when it wants to pass a reference to itself in a method argument or result. Here is the method signature: public EJBObject getEJBObject() throws IllegalStateException; An IllegalStateException will be thrown if an instance invokes this method while the instance is in a state that does not allow this method to be invoked. getPrimaryKeyThe getPrimaryKey method returns the primary key of the EJB object that is currently associated with this instance. Only an instance of an entity bean can call this method. Here is the method signature: public Object getPrimaryKey() throws IllegalStateException; Note The result of this method is the same as the result of calling the getEJBObject().getPrimaryKey() . javax.ejb.HandleA handle is an abstraction of a network reference to an EJB object. A handle can be used as a persistent reference to an EJB object and can be re-created later. The Handle interface has one method, getEJBObject ; its return type is EJBObject . getEJBObjectThe getEJBObject method returns a reference to the EJB object that is currently associated with the instance. An instance can use this method, for example, when it wants to pass a reference to itself in a method argument or result. Here is the method signature: public EJBObject getEJBObject() throws java.rmi.RemoteException; javax.ejb.HomeHandleThe HomeHandle interface is an abstraction of a network reference to a home object. A HomeHandle can be used as a persistent reference to a home object and can be shared between clients. The HomeHandle interface has one method, getEJBHome ; its return type is EJBHome . getEJBHomeThe getEJBObject method returns a reference to the EJB object that is currently associated with the instance. An instance can use this method, for example, when it wants to pass a reference to itself in a method argument or result. Here is the method signature: public EJBObject getEJBObject() throws java.rmi.RemoteException; javax.ejb.MessageDrivenBeanEvery message-driven enterprise bean implements the MessageDrivenBean interface. The container uses the message-driven bean methods to notify the bean instances of lifecycle events. Table A.8 shows the methods in the MessageDrivenBean interface. Table A.8. The Methods of the MessageDrivenBean Interface
ejbRemoveThe container invokes the ejbRemove method before it ends the life of the message-driven object. This happens when a container decides to terminate the message-driven object. Here is the method signature: public void ejbRemove() throws EJBException; setMessageDrivenContextThe container calls the setMessageDrivenContext method after the instance creation. The enterprise bean instance should store the reference to the context object in an instance variable. Here is the method signature: public void setMessageDrivenContext(MessageDrivenContext ctx) throws EJBException; javax.ejb.MessageDrivenContextAs Figure A.1 shows, the MessageDrivenContext interface extends the EJBContext interface. In EJB 2.0, the MessageDrivenContext interface does not define any methods, but merely serves as a marker to identify a runtime context as belonging to a message-driven bean. javax.ejb.SessionBeanEvery session bean must implement the SessionBean interface. The container uses the SessionBean methods to notify the bean instances of lifecycle events. Table A.9 shows the methods in the SessionBean interface. Table A.9. The Methods of the SessionBean Interface
Tip With several of the methods that follow, the RemoteException is defined in the method signature to provide backward compatibility with enterprise beans written for the EJB 1.0 Specification. Enterprise beans written for the EJB 1.1 and 2.0 Specifications should throw the javax.ejb.EJBException instead of the RemoteException . ejbActivateThe ejbActivate method is called when the instance is activated from its "passive" state. The instance should acquire any resource that it needs for its life cycle. Here is the method signature: public void ejbActivate() throws java.rmi.RemoteException, EJBException; ejbPassivateThe ejbPassivate method is called before the instance enters the "passive" state. The instance should release any resources that it is holding on to. After the ejbPassivate method completes, the instance must be in a state that allows the container to use the Java Serialization protocol to externalize and store away the instance's state. Here is the method signature: public void ejbPassivate() throws java.rmi.RemoteException, EJBException; ejbRemoveThe container calls the ejbRemove method before it ends the life of the session object. This happens as a result of a client's invoking a remove operation, or when a container decides to terminate the session object after a timeout. Here is the method signature: public void ejbRemove() throws java.rmi.RemoteException, EJBException; setSessionContextThe container calls the setSessionContext method after the instance creation. The enterprise bean instance should store the reference to the context object in an instance variable. Here is the method signature: public void setSessionContext(SessionContext ctx) throws java.rmi.RemoteException, EJBException; javax.ejb.SessionContextThe SessionContext interface provides access to the runtime session context that the container provides for a session bean instance. The container passes the SessionContext interface to a session bean instance after the instance has been created. The session context remains associated with the instance for its lifetime. Table A.10 shows the methods in the SessionContext interface. Table A.10. The Methods of the SessionContext Interface
getEJBLocalObjectThe getEJBLocalObject returns a reference to the local EJB object that currently is associated with the instance. An instance can use this method, for example, when it wants to pass a reference to itself in a method argument or result. Here is the method signature: public EJBLocalObject getEJBLocalObject() throws IllegalStateException; An IllegalStateException will be thrown if an instance invokes this method while the instance is in a state that does not allow this method to be invoked or if the enterprise bean does not have a local interface. getEJBObjectThe getEJBObject returns a reference to the EJB object that is currently associated with the instance. An instance can use this method, for example, when it wants to pass a reference to itself in a method argument or result. Here is the method signature: public EJBObject getEJBObject() throws IllegalStateException; javax.ejb.SessionSynchronizationThe SessionSynchronization interface allows a session bean instance to be notified by the container of transaction boundaries. A session bean class is not required to implement this interface. Table A.11 shows the methods in the SessionSynchronization interface. Table A.11. The Methods of the SessionSynchronization Interface
Tip With several of the methods that follow, the RemoteException is defined in the method signature to provide backward compatibility with enterprise beans written for the EJB 1.0 Specification. Enterprise beans written for the EJB 1.1 and 2.0 Specifications should throw the javax.ejb.EJBException instead of the RemoteException . afterBeginThe afterBegin method notifies a session bean instance that a new transaction has started, and that the subsequent business methods on the instance will be invoked in the context of the transaction. The instance can use this method, for example, to read data from a database and cache the data in the instance fields. Here is the method signature: public void afterBegin() throws java.rmi.RemoteException, EJBException; afterCompletionThe afterCompletion method notifies a session bean instance that a transaction commit protocol has completed, and tells the instance whether the transaction has been committed or rolled back. Here is the method signature: public void afterCompletion(boolean committed) throws java.rmi.RemoteException, EJBException; The argument is true if the transaction has been committed, false if the transaction has been rolled back. beforeCompletionThe beforeCompletion method notifies a session bean instance that a transaction is about to be committed. The instance can use this method, for example, to write any cached data to a database. Here is the method signature: public void beforeCompletion() throws java.rmi.RemoteException, EJBException; |