Table of Contents
This chapter describes additional functions that are used to implement EJB in JEUS.
The Work Area service enables a program to continuously propagate a particular implicit context. Like with a security context or transaction context, even if the implicit context is not propagated as a separate argument, it will automatically be transmitted during a local or remote method invocation. Work Area can be used when it is needed to transmit a context or there are multiple method arguments.
As a kind of user repository, Work Area is stored as a map in the form of a name-value pair. When Work Area is newly started, it gets transmitted with the current thread, and so it can continuously be used inside the same component as that of the invoked method or EJB. It also gets propagated during a remote EJB invocation.
The UserWorkArea interface, jeus.workarea.UserWorkArea, is used to access the Work Area service.
For detailed information on the API, refer to the jeus.workarea package in JEUS API JavaDoc.
Currently, the remote Work Area propagation function is available only when using EJB 3.0 business interface. It is unavailable when using the EJBObject remote component interface, or IIOP invocation instead of JEUS basic RMI invocation.
The UserWorkArea interface defines all methods required to start, close, and operate UserWorkArea as follows.
[Example 12.1] <<UserWorkArea Interface>>
public interface UserWorkArea { public void begin(String name); public void complete() throws NoWorkAreaException, NotOriginatorException; public String getName(); public String[] retrieveAllKeys(); public void set(String key, java.io.Serializable value) throws NoWorkAreaException, NotOriginatorException, PropertyReadOnlyException; public void set(String key, java.io.Serializable value, PropertyModeType mode) throws NoWorkAreaException, NotOriginatorException, PropertyReadOnlyException; public java.io.Serializable get(String key); public PropertyModeType getMode(String key); public void remove(String key) throws NoWorkAreaException, NotOriginatorException, PropertyReadOnlyException; }
For more information about method definition, refer to JavaDoc.
Each value stored in the WorkArea has its own PropertyMode setting.
Each PropertyMode Type is as follows:
Exceptions defined for UserWorkArea are as follows:
UserWorkArea can be nested. If a new UserWorkArea is started while one already exists, the new one is nested inside the existing UserWorkArea.
The nested UserWorkArea can use values from the parent UserWorkArea, and can also add new values. The values added by the nested UserWorkArea are available only in the nested UserWorkArea, and they will be removed when the nested UserWorkArea is terminated.
Registration information in the nested UserWorkAreas is as follows:
This section shows an example of EJBs using the UserWorkArea interface.
The example consists of two EJBs, the UserWorkAreaSampleSender and UserWorkAreaSampleReceiver. In the example, the sender creates the UserWorkArea and transmits data, and the receiver creates a message using the specified value in UserWorkArea and returns the message to the original sender.
UserWorkArea Access
To use the UserWorkArea, JNDI has to look up the UserWorkArea.
The following example shows how JNDI looks up the UserWorkArea.
[Example 12.2] Access to UserWorkArea : <<UserWorkAreaSampleSenderBean.java>>
public class UserWorkAreaSampleSenderBean implements UserWorkAreaSampleSender {
public String getMessage() {
InitialContext ic;
String message = null;
try {
ic = new InitialContext();
//Brings UserWorkArea from JNDI.
UserWorkArea userWorkArea
= (UserWorkArea) ic.lookup("java:comp/UserWorkArea");
} catch (NamingException e) {
// Do Something...
}
return message;
}
}
Starting a New UserWorkArea
A new UserWorkArea needs to be started, because there was no information available for the first UserWorkArea that was looked up in JNDI. If a UserWorkArea name returned is null, NullPointerException occurs.
[Example 12.3] Starting New UserWorkArea : <<UserWorkAreaSampleSenderBean.java>>
public class UserWorkAreaSampleSenderBean implements UserWorkAreaSampleSender {
public String getMessage() {
InitialContext ic;
String message = null;
try {
ic = new InitialContext();
UserWorkArea userWorkArea
= (UserWorkArea) ic.lookup("java:comp/UserWorkArea");
// Starts a new UserWorkArea.
userWorkArea.begin("UserWorkArea1");
} catch (NamingException e) {
// Do Something...
}
return message;
}
}
Configuring Registration in WorkArea
Configuring registration information in the new UserWorkArea. The registration information includes <key, value, mode>. The key is String, and the value is a serializable object.
If there is no UserWorkArea when configuring the value, a NoWorkAreaException occurs. If the UserWorkArea has not started yet, a NotOriginatorException occurs, and if a value which is already set to READ_ONLY is tried to be modified, a PropertyReadOnlyException occurs.
[Example 12.4] Configuring Registration in WorkArea: <<UserWorkAreaSampleSenderBean.java>>
public class UserWorkAreaSampleSenderBean implements UserWorkAreaSampleSender {
public String getMessage() {
InitialContext ic;
String message = null;
try {
ic = new InitialContext();
UserWorkArea userWorkArea
= (UserWorkArea) ic.lookup("java:comp/UserWorkArea");
userWorkArea.begin("UserWorkArea1");
//Sets to NORMAL in UserWorkArea.
userWorkArea.set("name", "johan");
//Sets to READ_ONLY in UserWorkArea.
userWorkArea.set("company", "TmaxSoft", PropertyModeType.READ_ONLY);
UserWorkAreaSampleReceiver receiver
= (UserWorkAreaSampleReceiver) ic.lookup("receiver");
message = receiver.createMessage();
} catch (NamingException e) {
// Do Something...
} catch (NoWorkAreaException e) {
// Do Something...
} catch (PropertyReadOnlyException e) {
// Do Something...
} catch (NotOriginatorException e) {
// Do Something...
}
return message;
}
}
Obtaining Registration Information Configured in WorkArea
Creates a message by using the registration information configured in UserWorkArea that was propagated from the receiver. Null is returned if a key, which does not exist in the UserWorkArea, is used when getting the information.
[Example 12.5] Obtaining Registration Information Configured in WorkArea: <<UserWorkAreaSampleReceiverBean.java>>
public class UserWorkAreaSampleReceiverBean implements UserWorkAreaSampleReceiver {
public String createMessage() {
InitialContext ic;
String message = null;
try {
ic = new InitialContext();
UserWorkArea userWorkArea
= (UserWorkArea) ic.lookup("java:comp/UserWorkArea");
//Gets the stored values from UserWorkArea.
String name = (String)userWorkArea.get("name");
String company = (String)userWorkArea.get("company");
message = "Hello " + name + " from " + company;
} catch (NamingException e) {
// Do Something...
}
return message;
}
}
Completing UserWorkArea
Completes the UserWorkArea that was started. Only the originator who started the UserWorkArea can complete it, and if others attempt to complete it, a NotOriginatorException occurs.
[Example 12.6] Completing UserWorkArea: <<UserWorkAreaSampleSenderBean.java>>
public class UserWorkAreaSampleSenderBean implements UserWorkAreaSampleSender {
public String getMessage() {
InitialContext ic;
String message = null;
try {
ic = new InitialContext();
UserWorkArea userWorkArea
= (UserWorkArea) ic.lookup("java:comp/UserWorkArea");
userWorkArea.begin("UserWorkArea1");
userWorkArea.set("name", "user1");
userWorkArea.set("company", "TmaxSoft", PropertyModeType.READ_ONLY);
UserWorkAreaSampleReceiver receiver
= (UserWorkAreaSampleReceiver) ic.lookup("receiver");
message = receiver.createMessage();
userWorkArea.complete(); // Closes UserWorkArea.
} catch (NamingException e) {
// Do Something...
} catch (NoWorkAreaException e) {
// Do Something...
} catch (PropertyReadOnlyException e) {
// Do Something...
} catch (NotOriginatorException e) {
// Do Something...
}
return message;
}
}