Tuesday, January 16, 2007

SCBCD - Client View of a Session Bean

1)Consider the following fragment of client code involving session beans. Which of the following statements regarding this code are true? [Select all correct answers]

Assume that all reference variables have been declared 
properly and the code has been compiled successfully.

4. Context initialContext = new InitialContext();
5. BasketHome basketHome = (BasketHome)
6. initialContext.lookup("java:comp/env/ejb/basket");
7. Basket basket1 = basketHome.create(discountVoucher);
8. Basket basket2 = basketHome.create(discountVoucher);
1)basket1 and basket2 are stateful session beans.
2)If basket1.isIdentical(basket2) is invoked after line 8, false is returned.
3)If basket1.isIdentical(basket2) is invoked after line 8, the client must handle javax.ejb.EJBException.
4)If basket1.getHandle() is invoked after line 8, a handle that can be serialized and written to stable storage is returned.
5)It is not possible to determine whether the client is local or remote.

2)Which of the following statements regarding a remote component interface of a session bean are true? [Select all correct answers]

1)All methods are defined to throw java.rmi.RemoteException.
2)Business methods can throw application specific exceptions.
3)Any methods invoked by a remote client may receive java.rmi.NoSuchObjectException.
4)The remove() method is defined to throw java.rmi.RemoveException.
5)The getHandle() method is defined to throw javax.ejb.ObjectNotFoundException.

3)Which of the following statements regarding a remote component interface of a session bean are true? [Select all correct answers]

1)Allows a client to get the primary key of the remote session bean.
2)Allows a client to get a handle for the session object.
3)Allows a client to get the remote home object.
4)Allows a client to get the EJBMetaData interface for the session bean.
5)Extends javax.ejb.EJBLocalObject either directory or indirectly.

4)Which of the following statements regarding a local home interface of a session bean is true?

1)Extends javax.ejb.EJBHome either directory or indirectly.
2)Allows a client to remove a session object.
3)Allows a client to get the metadata for the session bean through the javax.ejb.EJBMetaData interface.
4)Allows a client to find an existing session bean instance.
5)Does not allow a client to get a handle to the home interface.

5)Which of the following statements regarding a local component interface of a session bean are true? [Select all correct answers]

1)Allows a client to remove the session object.
2)Allows a client to get the local home object.
3)Allows a client to get a handle for the session object.
4)Allows a client to get the primary key of the local session bean.
5)Extends javax.ejb.EJBLocalObject either directly or indirectly.

6)Which of the following statements regarding the removal of a session object are true? [Select all correct answers]

1)The remote home interface allows a client to remove a session object.
2)The local home interface allows a client to remove a session object.
3)The home interface allows a client to remove a session object using a primary key.
4)A remote client must handle both java.rmi.RemoteException and javax.ejb.RemoveException when removing a session object.
5)A local client must handle both javax.ejb.EJBException and javax.ejb.RemoveException when removing a session object.

7)Consider the following fragment of client code that locates a session bean home interface through JNDI and creates a session object. Which of the following statements regarding this code is correct?
Assume that all reference variables have been declared properly

4. Context initialContext = new InitialContext();
5. TigerHome tigerHome = (TigerHome)
6. initialContext.lookup("java:comp/env/ejb/tiger");
7. Tiger tiger = tigerHome.create();
8. ...
1)javax.rmi.PortableRemoteObject.narrow(...) method must be used to convert the result of the JNDI lookup to the home interface type.
2)Invoking tiger.getHandle() returns a handle that can be serialized and written to stable storage.
3)Invoking a method throws javax.ejb.NoSuchObjectLocalException if the session object has been removed by the container.
4)Invoking tigerHome.getEJBMetaData() returns a EJBMetaData interface that can be used to discover metadata information about the bean.
5)Invoking tigerHome.getHomeHandle() returns a handle for the home object that can be serialized and written to stable storage.

8)Which functionality is provided that is common to both a session bean's remote and local component interfaces? [Select all correct answers]

1)Obtaining the session object's home interface.
2)Obtaining the session object's primary key.
3)Obtaining the handle of the session object.
4)Removing the session object.
5)Testing if a given session object is identical to the invoked session object.

9)Which of the following statements regarding session beans are true? [Select all correct answers]

1)Invoking javax.ejb.EJBObject.getPrimaryKey() on a session bean results in javax.ejb.EJBException been thrown.
2)Invoking javax.ejb.EJBLocalObject.getPrimaryKey() on a session bean results in javax.ejb.EJBException been thrown.
3)Invoking javax.ejb.EJBHome.remove(java.lang.Object primaryKey) on a session bean results in javax.ejb.RemoveException been thrown.
4)Invoking javax.ejb.EJBLocalHome.remove(java.lang.Object primaryKey) on a session bean results in javax.ejb.RemoveException been thrown.
5)Invoking javax.ejb.EJBHome.remove(java.lang.Object primaryKey) on a session bean results in java.rmi.RemoteException been thrown.

10)Which of the following statements regarding remote session object interfaces is true?

1)All methods in the remote home and remote component interfaces are defined to throw javax.ejb.RemoteException.
2)A handle for the session object that can be serialized and written to stable storage is obtained from the remote home interface.
3)The remote home interface must define a find method to allow a session object to be retrieved by its primary key.
4)The remote component interface must be located through a JNDI lookup and narrowed using javax.rmi.PortableRemoteObject.narrow(...).
5)All methods in the remote home and remote component interfaces are defined to throw java.rmi.RemoteException.

Answers:

1)1,2

Answers 1 and 2 are correct. At lines 7 and 8, the session objects are created by invoking the create method of the home interface and passing in discountVoucher as a parameter. A create method with parameters defined by a home interface implies that the session bean is stateful. Since basket1 and basket 2 are stateful session objects, the container will assign unique identities to each of them. The code basket1.isIdentical(basket2) therefore returns false.

After performing a JNDI lookup to locate the home interface, the client code at lines 5 and 6 does not perform a PortableRemoteObject.narrow() on the home object. Therefore, the home interface can be considered as a local home interface.

Answer 3 is incorrect. Although basket1 and basket2 are local session objects and javax.ejb.EJBLocalObject.isIdentical(EJBLocalObject obj) raises an EJBException in the event of system-level failure, the client code does not have to handle the exception because EJBException is an unchecked exception. Answer 4 is incorrect because a local component interface does not provide a method to get a handle that can be serialized and written to stable storage. Answer 5 is incorrect because the home interface lookup at lines 5 and 6 enable us to determine that the client is local.



2)1,2,3

Answers 1, 2 and 3 are correct. Answer 1 is correct. All methods defined by the remote component interface are defined to throw java.rmi.RemoteException. This enables the container to report any connection problems during a remote method invocation by wrapping the details in a RemoteException and throwing it to the client. Answer 2 is correct, as well as throwing RemoteException, a session object's business methods can be defined to throw additional application specific exceptions. Answer 3 is correct. java.rmi.NoSuchObjectException (a subclass of java.rmi.RemoteException) may be raised when a client makes a call to a session object that has been removed. Note that the container does not guarantee this behaviour.

Answer 4 is incorrect because java.rmi.RemoveException does not exist. The remove() method of the remote component interface is defined to throw java.rmi.RemoteException and javax.ejb.RemoveException. Answer 5 is incorrect because getHandle() is defined to throw java.rmi.RemoteException only.



3)2,3

Answers 2 and 3 are correct. A remote component interface provides javax.ejb.EJBObject.getHandle() to get a handle for the session object and javax.ejb.EJBObject.getEJBHome() to get the remote home object.

Answer 1 is incorrect because a session bean does not have a primary key. In contrast to session beans, which do not have identities, entity beans expose their identities as a primary key. Answer 4 is incorrect, the EJBMetaData interface for the session bean is obtained using a remote home interface. Answer 5 is incorrect because a remote component interface extends javax.ejb.EJBObject.

Note: A client never accesses instances of a session bean's class directly. Instead, a client uses a session object provided by the container that implements a remote component interface of a session bean. When a client invokes an operation on a session object, the call is intercepted by the container. By interposing between clients and the session bean instance at the method call level, containers can inject system-level services such remote calls to different machines, security, concurrency, transactions etc.



4)5

Answer 5 is correct because local clients cannot get handles for session EJBLocalHome objects. Handles can only be obtained for session EJBHome objects.

Answer 1 is incorrect because a local home interface must extend javax.ejb.EJBLocalHome. Answer 2 is incorrect because a local home interface only provides a method to remove an entity session object by its primary key. A local session object can be removed by calling the remove method on the local component interface. Answer 3 is incorrect. A client can only get metadata for a remote session bean; therefore, a local home interface does not define the getEJBMetaData method. Answer 4 is incorrect; from the client's perspective, session beans appear anonymous and do not have identities. In contrast to entity beans, which entity bean instances can be found using finder methods defined in their home interfaces, session beans do not define finder methods in their home interfaces.



5)1,2,5

Answers 1, 2 and 5 are correct. A local component interface extends javax.ejb.EJBLocalObject and provides javax.ejb.EJBLocalObject.remove() to allow the client to remove the session object and javax.ejb.EJBLocalObject.getEJBHome() to get the session object's local home interface. javax.ejb.EJBLocalObject.isIdentical(EJBObject obj) is also provided to test whether the session object is identical with another session object.

Answer 3 is incorrect because local clients cannot get handles for local session objects. Handles can only be obtained for remote session objects. Answer 4 is incorrect because a session bean does not have a primary key. In contrast to session beans, which do not have identities, entity beans expose their identities as a primary key.

Note: A client never accesses instances of a session bean's class directly. Instead, a client uses a session object provided by the container that implements a local component interface of a session bean. When a client invokes an operation on a session object, the call is intercepted by the container. By interposing between clients and the session bean instance at the method call level, containers can inject system-level services such security, concurrency, transactions etc.



6)4

Answers 1 and 4 are correct. Answer 1 is correct because a remote home interface provides the method javax.ejb.EJBHome.remove(Handle handle) to remove a session object. Answer 4 is correct. A remote client can either use the remove(Handle handle) method of the remote component interface or the remove() method of the remote home interface to remove a session object. Both remove methods throw the following checked exceptions: java.rmi.RemoteException and javax.ejb.javax.ejb.RemoveException; therefore, both exceptions have to be handled by the client.

Answer 2 is incorrect because a local home interface only provides a method to remove an entity session object by its primary key. A client can only remove a local session object by calling the remove method on the local component interface. Answer 3 is incorrect because a session bean does not have a primary key. In contrast to session beans, which do not have identities, entity beans expose their identities as a primary key. Therefore, a client is only allowed to remove entity beans using a primary key. Answer 5 is incorrect. When a local client removes a session object, the remove methods throw the following: javax.ejb.RemoveException (checked exception) and javax.ejb.EJBException (unchecked exception). Only the checked exception is required to be handled by the local client.



7)3

Answer 3 is correct. After performing a JNDI lookup to locate the home interface, the client code at lines 5 and 6 does not perform a PortableRemoteObject.narrow() on the home object. Therefore, the home interface can be considered as a local home interface. If the container removes the session object (tiger) after an expiration of a timeout specified by the deployer, when the client attempts to invoke a method on the session object, the container will throw javax.ejb.NoSuchObjectLocalException. Note that the Container would throw java.rmi.NoSuchObjectException if the client were a remote client.

Answer 1 is incorrect. javax.rmi.PortableRemoteObject.narrow(...) is not mandatory to convert the result of the JNDI lookup to the home interface type. Narrowing of the handle type is not required when locating a local home interface. Answers 2, 4 and 5 are incorrect because the session object interfaces are local.



8)1,4,5

Answers 1, 4 and 5 are correct. Answer 1 is correct. A remote client can obtain a session object's home interface by calling getEJBHome() on the session object's remote component interface. A local client can obtain a session object's home interface by calling getEJBLocalHome() on the session object's local component interface. Answer 4 is correct because both remote and local clients can remove a session object by calling remove() on the session object's component interface. Answer 5 is correct. Both remote and local clients can test if their corresponding session objects are identical to another session object. A remote client can use javax.ejb.EJBObject.isIdentical(EJBObject obj) and a local client can use javax.ejb.EJBLocalObject.isIdentical(EJBLocalObject obj).

Answer 2 is incorrect. Although both remote and local components define a getPrimaryKey() method, this functionality is not applicable to session beans. This method is used to get the identity of an entity bean. Answer 3 is incorrect because this functionality is only available in the remote component interface and not in the local component interface.



9)2,3,4
Answers 2, 3 and 4 are correct. Answer 2 is correct because the method javax.ejb.EJBLocalObject.getPrimaryKey() can only be invoked on an entity bean. An attempt to invoke the method on a session bean results in javax.ejb.EJBException been thrown. Answers 2 and 4 are correct because javax.ejb.EJBHome.remove(java.lang.Object primaryKey) and javax.ejb.EJBLocalHome.remove(java.lang.Object primaryKey) can only be invoked on an entity bean. An attempt to invoke the method on a session bean results in javax.ejb.RemoveException being thrown.

10)5

Answer 5 is correct. All the methods defined by the remote home interface are defined to throw java.rmi.RemoteException. This enables the container to report any connection problems during a remote method invocation by wrapping the details in a RemoteException and throwing it to the client. All methods defined by the remote home component interface (including business methods) are also defined to throw java.rmi.RemoteException.

Answer 1 is incorrect because no such exception exists. Answer 2 is incorrect. A handle for the session object can only be obtained from the remote component interface. Answer 3 is incorrect because finder methods are only defined for entity beans. Answer 4 is incorrect because the remote home must be located through a JNDI lookup and narrowed using the javax.rmi.PortableRemoteObject.narrow(...) method.




No comments: