Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Enterprise JavaBeans™ Specification, v1.1 - Sun Microsystems.pdf
Скачиваний:
11
Добавлен:
24.05.2014
Размер:
1.62 Mб
Скачать

Sun Microsystem Inc

EJBObject

Enterprise JavaBeans v1.1, Final Release

Client View of a Session Bean

5.4 EJBObject

A client never directly accesses instances of the session bean’s class. A client always uses the session bean’s remote interface to access a session bean’s instance. The class that implements the session bean’s remote interface is provided by the container; its instances are called session EJBObjects.

A session EJBObject supports:

The business logic methods of the object. The session EJBObject delegates invocation of a business method to the session bean instance.

The methods of the javax.ejb.EJBObject interface. These methods allow the client to:

Get the session object’s home interface.

Get the session object’s handle.

Test if the session object is identical with another session object.

Remove the session object.

The implementation of the methods defined in the javax.ejb.EJBObject interface is provided by the container. They are not delegated to the instances of the session bean class.

5.5 Session object identity

Session objects are intended to be private resources used only by the client that created them. For this reason, session objects, from the client’s perspective, appear anonymous. In contrast to entity objects, which expose their identity as a primary key, session objects hide their identity. As a result, the EJBObject.getPrimaryKey() and EJBHome.remove(Object primaryKey) methods result in a java.rmi.RemoteException if called on a session bean. If the EJBMetaData.getPrimaryKeyClass()method is invoked on a EJBMetaData object for a Session bean, the method throws the java.lang.RuntimeException.

Since all session objects hide their identity, there is no need to provide a finder for them. The home interface of a session bean must not define any finder methods.

A session object handle can be held beyond the life of a client process by serializing the handle to persistent store. When the handle is later deserialized, the session object it returns will work as long as the session object still exists on the server. (An earlier timeout or server crash may have destroyed the session object.)

The client code must use the javax.rmi.PortableRemoteObject.narrow(...)method to convert the result of the getEJBObject() method invoked on a handle to the remote interface type.

A handle is not a capability, in the security sense, that would automatically grant its holder the right to invoke methods on the object. When a reference to a session object is obtained from a handle, and then a method on the session object is invoked, the container performs the usual access checks based on the caller’s principal.

43

11/24/99

 

 

 

Sun Microsystems Inc.

 

 

 

 

Client View of a Session Bean

Enterprise JavaBeans v1.1, Final Release

Client view of session object’s life cycle

5.6 Client view of session object’s life cycle

 

From a client point of view, the life cycle of a session object is illustrated below

 

 

 

 

 

 

 

 

 

 

 

 

 

Figure 4

Lifecycle of a session object.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

client’s method on reference

 

 

 

 

 

 

 

generates NoSuchObjectException

 

 

does not exist

 

 

 

release reference

does not exist

 

 

and

 

 

 

 

 

 

 

and

 

 

 

 

 

 

 

 

 

 

 

not referenced

 

 

 

 

 

 

 

referenced

 

 

 

 

 

 

 

 

 

 

 

 

object.remove(),

 

 

 

 

 

 

 

 

 

 

 

 

home.remove(...),

 

 

 

 

 

 

 

home.create(...)

 

system exception in bean,

 

 

 

Container crash,

 

 

 

 

 

bean timeout,

 

 

 

 

 

 

 

 

or

 

 

 

or bean timeout

 

 

 

 

 

 

 

 

 

 

 

 

 

Container crash

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

handle.getEJBObject()

 

 

 

 

exists

exists

 

 

 

 

 

 

 

 

 

 

 

and

 

 

 

 

 

 

 

and

 

 

not referenced

 

 

 

 

 

 

 

referenced

 

 

 

 

 

 

 

 

 

release reference

client’s method on reference

A session object does not exist until it is created. When a client creates a session object, the client has a reference to the newly created session object’s remote interface.

A client that has a reference to a session object can then do any of the following:

Invoke business methods defined in the session object’s remote interface.

Get a reference to the session object’s home interface.

Get a handle for the session object.

Pass the reference as a parameter or return value within the scope of the client.

Remove the session object. A container may also remove the session object automatically when the session object’s lifetime expires.

11/24/99

44

Sun Microsystem Inc

Creating and using a session object

Enterprise JavaBeans v1.1, Final Release

Client View of a Session Bean

 

 

 

It is invalid to reference a session object that does not exist. Attempted invocations on a session object

 

 

 

that does not exist result in java.rmi.NoSuchObjectException.

 

5.7 Creating and using a session object

 

 

 

 

 

 

 

 

An example of the session bean runtime objects is illustrated by the following diagram:

 

 

 

 

 

Figure 5

 

 

Session Bean Example Objects

 

 

 

 

container

 

Cart

client

CartBean

CartHome

 

A client creates a Cart session object (which provides a shopping service) using a create(...) method of the Cart’s home interface. The client then uses this session object to fill the cart with items and to purchase its contents.

Suppose that the end-user wishes to start the shopping session, suspend the shopping session temporarily for a day or two, and later complete the session. The client might implement this feature by getting the session object’s handle, saving the serialized handle in persistent storage, then using it later to reestablish access to the original Cart.

For the following example, we start by looking up the Cart’s home interface in JNDI. We then use the home interface to create a Cart session object and add a few items to it:

CartHome cartHome = (CartHome)javax.rmi.PortableRemoteObject.narrow( initialContext.lookup(...), CartHome.class);

Cart cart = cartHome.create(...); cart.addItem(66); cart.addItem(22);

45

11/24/99