📄 studentfacadeejb.java
字号:
package day21ex.studentfacade;
import java.util.*;
import java.rmi.*;
import javax.rmi.*;
import javax.naming.*;
import javax.ejb.*;
import day21ex.enrollmentcart.*;
import day21ex.signon.*;
import day21ex.course.*;
import day21ex.order.*;
import day21ex.orderlineitem.*;
import day21ex.messagesender.*;
import day21ex.student.*;
/* day 21 exercise */
import day21ex.enrollment.*;
public class StudentFacadeEJB implements SessionBean {
private SessionContext ctx;
String login;
EnrollmentCart cart;
public StudentFacadeEJB() {
print("The container created this instance.\n");
login = null;
}
public void setSessionContext(SessionContext ctx) {
print("The container called the setSessionContext method ");
print("to associate session bean instance with its context.\n");
this.ctx = ctx;
}
public void ejbCreate() throws CreateException {
print("The container called the ejbCreate method.\n");
cart = getEnrollmentCart();
}
public void ejbActivate() {
print("This instance has just been reactivated.\n");
}
public void ejbPassivate() {
print("The container intends to passivate the instance.\n");
}
public void ejbRemove() {
print("This instance is in the process of being removed ");
print("by the container.\n");
}
public void addUser(String login, String password) throws RemoteException {
SignOn signOn = getSignOn();
signOn.addUser(login, password);
this.login = login;
}
public void addStudent(String loginName, String password, String firstName,
String lastName, String address, String emailAddress) throws RemoteException {
try {
StudentLocalHome home = getStudentLocalHome();
addUser(loginName, password);
StudentLocal student = home.create(loginName, firstName,
lastName, address, emailAddress);
}catch (Exception e) {
throw new EJBException(e);
}
}
public boolean validateUser(String login, String password)
throws InvalidLoginException, RemoteException {
SignOn signOn = getSignOn();
boolean status = signOn.validateUser(login, password);
if ( status )
{
this.login = login;
}
return status;
}
public void addCourses(String[] courseIds) throws RemoteException {
print("The container called addCourses method.\n");
if ( courseIds == null) {
return;
}
cart = getEnrollmentCart();
cart.addCourses(courseIds);
}
public Collection getCartItems() throws RemoteException {
cart = getEnrollmentCart();
ArrayList items = new ArrayList();
Collection collection = cart.getCourses();
Iterator it = collection.iterator();
while (it.hasNext()) {
String courseId = (String)it.next();
CourseItem item = null;
try {
CourseItem ci = getCourseItemDetails(courseId) ;
items.add(ci);
} catch (Exception cce) {
System.out.println("exception caught: " + cce);
}
}
return items;
}
public void empty() throws RemoteException {
cart = getEnrollmentCart();
cart.empty();
}
void print(String s) {
System.out.println("StudentFacadeEJB:"+ s);
}
public CourseItem getCourseItemDetails(String courseId) throws NamingException, FinderException {
Context ctx = new InitialContext();
CourseLocalHome courseHome = (CourseLocalHome)
PortableRemoteObject.narrow(
ctx.lookup("day21ex/Course"), CourseLocalHome.class);
CourseLocal course=courseHome.findByPrimaryKey(courseId);
CourseItem item=new CourseItem(course.getCourseId(),course.getName(),
course.getFee());
return item;
}
public Vector getCourseItemList(){
try{
Context ctx = new InitialContext();
CourseLocalHome courseHome = (CourseLocalHome)
PortableRemoteObject.narrow(
ctx.lookup("day21ex/Course"), CourseLocalHome.class);
Collection courses=courseHome.findAllCourses();
Enumeration items = Collections.enumeration(courses);
Vector courseItems=new Vector();
while (items.hasMoreElements()) {
CourseLocal course= (CourseLocal) items.nextElement();
CourseItem p=new CourseItem(course.getCourseId(),course.getName(),
course.getFee());
courseItems.add(p);
}
return courseItems;
}catch (Exception e) {
throw new EJBException(e);
}
}
public String placeOrder() {
try {
print("placeOrder called\n");
OrderLocalHome home=getOrderLocalHome();
String orderID = makeUniqueID();
OrderLocal order=home.create(orderID, login ,"unverified",100 );
Vector orderItems=addOrderLineItems(order);
order.setLineItems(orderItems);
sendMessage(orderID);
cart.empty(); // clear shopping cart
return orderID;
} catch (Exception e) {
throw new EJBException(e);
}
}
/* day 21 exercise */
public Collection getStudentEnrollments() {
Collection collection = new ArrayList();
CourseLocal course;
StudentLocal student;
EnrollmentLocal enrollment;
try {
EnrollmentLocalHome home= getEnrollmentLocalHome();
Collection enrollments = home.findStudentEnrollments(login);
Iterator it = enrollments.iterator();
while( it != null && it.hasNext() ) {
enrollment = (EnrollmentLocal) it.next();
try {
course = enrollment.getCourse();
student = enrollment.getStudent();
collection.add( new EnrollmentDetails(
enrollment.getEnrollmentId(),
student.getStudentId(),
course.getCourseId(),
course.getName() ) );
} catch (Exception e) {
e.printStackTrace();
System.err.println(e);
}
}
} catch(FinderException fe) {
print("FinderException while getStudentEnrollments :" +
fe.getMessage() + "\n" );
throw new EJBException("Unable to find enrollment"+
" for student: " + login +
fe.getMessage());
} catch(NamingException ne) {
print("NamingException while getStudentEnrollments :" +
ne.getMessage() + "\n");
throw new EJBException("NamingException while looking "
+ "for enrollments of student: " + login +
ne.getMessage());
} catch(Exception e) {
print("Exception while getStudentEnrollments :" +
e.getMessage() + "\n");
throw new EJBException("Exception while looking "
+ "for enrollments of student: " + login +
e.getMessage());
}
return collection;
}
private Vector addOrderLineItems(OrderLocal order){
try{
Context ctx = new InitialContext();
OrderLineItemLocalHome orderLineItemLocalHome = (OrderLineItemLocalHome)
PortableRemoteObject.narrow(
ctx.lookup("day21ex/OrderLineItem"), OrderLineItemLocalHome.class);
CourseLocalHome courseHome = (CourseLocalHome)
PortableRemoteObject.narrow(
ctx.lookup("day21ex/Course"), CourseLocalHome.class);
Collection items = getCartItems();
Iterator it = items.iterator();
Vector orderItems=new Vector();
while( it.hasNext() ) {
CourseItem item = (CourseItem) it.next();
CourseLocal course=courseHome.findByPrimaryKey(item.getCourseId());
String id = makeUniqueID();
OrderLineItemLocal orderLineItem =
orderLineItemLocalHome.create(id, order, course,item.getFee()) ;
orderItems.add(orderLineItem);
}
return orderItems;
} catch (Exception e) {
throw new EJBException(e);
}
}
private void sendMessage(String message) throws Exception{
try{
print("sendMessage called\n");
MessageSender sender=new MessageSender();
sender.sendAMessage(new InitialContext(), message);
} catch(Exception e){
throw new EJBException(e);
}
}
private String makeUniqueID(){
return new Long(System.currentTimeMillis()).toString();
}
private OrderLocalHome getOrderLocalHome(){
try{
Context ctx = new InitialContext();
OrderLocalHome home = (OrderLocalHome)
ctx.lookup("day21ex/Order");
return home;
} catch (Exception e) {
throw new EJBException(e);
}
}
private StudentLocalHome getStudentLocalHome(){
try{
Context ctx = new InitialContext();
StudentLocalHome home = (StudentLocalHome)
ctx.lookup("day21ex/Student");
return home;
} catch (Exception e) {
throw new EJBException(e);
}
}
SignOn getSignOn(){
SignOn signOn = null;
try {
Context ic = getInitialContext();
Object o = initialContext.lookup("day21ex/SignOn");
SignOnHome home = (SignOnHome)
javax.rmi.PortableRemoteObject.narrow(o, SignOnHome.class);
signOn = home.create();
} catch (Exception e) {
print(e.toString());
throw new EJBException(e);
}
return signOn;
}
EnrollmentCart getEnrollmentCart(){
if ( cart != null ) {
return cart;
}
try {
Context ic = getInitialContext();
Object o = initialContext.lookup("day21ex/EnrollmentCart");
EnrollmentCartHome home = (EnrollmentCartHome) javax.rmi.
PortableRemoteObject.narrow(o, EnrollmentCartHome.class);
cart = home.create();
} catch (Exception e) {
print(e.toString());
throw new EJBException(e);
}
return cart;
}
/* day 21 exercise */
private EnrollmentLocalHome getEnrollmentLocalHome()
throws NamingException {
InitialContext initial = new InitialContext();
Object o = initial.lookup("day21ex/EnrollmentLocal");
return ((EnrollmentLocalHome) o);
}
Context getInitialContext() throws NamingException {
if ( initialContext == null ) {
initialContext = new InitialContext();
}
return initialContext;
}
Context initialContext = null;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -