Tuesday 12 May 2009

4-How to Develop Container-Managed-Relationships

FILE Client.java

import cmrex.ClientLineItem;
import cmrex.OrderARS;
import cmrex.OrderARSHome;

import java.util.*;
import javax.naming.*;
import javax.ejb.*;
public class Client {
public static void main(String[] args) {
print("Starting Client . . .\n");
Context initialContext = null;
OrderARSHome orderHome = null;
OrderARS order = null;
try {
print("Looking up the order home via JNDI.\n");
initialContext = new InitialContext();
Object object = initialContext.lookup("cmrex/OrderARS");
orderHome = (OrderARSHome)
javax.rmi.PortableRemoteObject.narrow(object,OrderARSHome.class);
object = (OrderARS)orderHome.create("1", "Submitted", 100);
order = (OrderARS)
javax.rmi.PortableRemoteObject.narrow(object,OrderARS.class);
String orderId = order.getOrderId();
print("Created a new order:" + orderId + " .\n");
print("Adding some line items to the order.\n");
order.addLineItem("1", 200);
order.addLineItem("2", 300);
order.addLineItem("3", 300);
print("Retrieving line items of the order.\n");
Collection collection = order.getOrderLineItems();
Iterator it = collection.iterator();
while (it.hasNext()) {
ClientLineItem item =(ClientLineItem)javax.rmi.PortableRemoteObject.narrow
(it.next(), ClientLineItem.class);
print("Item id:" + item.getOrderLineItemId() +
" Course id:" + item.getCourseId() +
" Fee:" + item.getFee()
);
}
print("Removing order:" + orderId + " .\n");
order.remove();
} catch ( Exception e) {
e.printStackTrace();
}
}
static void print(String s) {
System.out.println(s);
}
}