📄 studentcmpclient.java
字号:
package com.learnweblogic.examples.ch9.cmp;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import javax.ejb.CreateException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import com.learnweblogic.examples.BaseClient;
public final class StudentCMPClient extends BaseClient {
private final static boolean debug = true;
private final static boolean verbose = true;
private StudentHome studentHome;
private BookHome bookHome;
private TeacherHome teacherHome;
private LockerHome lockerHome;
private int studentCount;
private int booksPerStudent;
private Context ctx;
public StudentCMPClient(String [] argv)
throws NamingException
{
super(argv);
ctx = getInitialContext();
studentHome =
(StudentHome) narrow(ctx.lookup("StudentCMPEJB"), StudentHome.class);
bookHome = (BookHome) narrow(ctx.lookup("BookCMPEJB"), BookHome.class);
teacherHome =
(TeacherHome) narrow(ctx.lookup("TeacherCMPEJB"), TeacherHome.class);
lockerHome = (LockerHome) narrow(ctx.lookup("LockerCMPEJB"),
LockerHome.class);
}
///////////////////////////////////////////////////////////////////////
// Utility code
private void deleteOldLockers(int N) {
for (int i=0; i<N; i++) {
try {
lockerHome.remove(new Integer(i));
} catch (Exception ignore) {}
}
}
private void deleteOldStudents(int N) {
for (int i=0; i<N; i++) {
try {
studentHome.remove(new Integer(i));
} catch (Exception ignore) {}
}
}
private void deleteOldTeachers(int N) {
for (int i=0; i<N; i++) {
try {
teacherHome.remove(new Integer(i));
} catch (Exception ignore) {}
}
}
private void deleteOldBooks(int N, int numCopies) {
BookPK pk;
for (int i=0; i<N; i++) {
for (int j=0; j<numCopies; j++) {
try {
pk = new BookPK(String.valueOf(i), j);
bookHome.remove(pk);
} catch (Exception ignore) {}
}
}
}
private void pause() {
try {
Thread.sleep(10000);
} catch (Exception e) {}
}
///////////////////////////////////////////////////////////////////////
// Create Beans For CMP Examples
private Student [] createStudents()
throws CreateException, RemoteException
{
System.err.println("Creating Student CMP Entity beans.....");
String [] names = { "Andrew", "Evelyn", "Joe", "Matt", "Jim" };
deleteOldStudents(names.length);
Student [] students = new Student[names.length];
for (int i=0; i<names.length; i++) {
// create stuydents in the 10th grade
students[i] = studentHome.create(names[i], i, 10);
}
return students;
}
private Locker [] createLockers(int N)
throws CreateException, RemoteException
{
System.err.println("Creating Locker CMP Entity beans.....");
deleteOldLockers(N);
Locker [] lockers = new Locker[N];
for (int i=0; i<N; i++) {
// create stuydents in the 10th grade
lockers[i] = lockerHome.create(new Integer(i));
}
return lockers;
}
private Teacher [] createTeachers()
throws CreateException, RemoteException
{
System.err.println("Creating Teacher CMP Entity beans.....");
String [] names = { "George", "John", "Thomas", "James"};
deleteOldTeachers(names.length);
Teacher [] teachers = new Teacher[names.length];
for (int i=0; i<names.length; i++) {
// create our teachers
teachers[i] = teacherHome.create(names[i], new Integer(i));
}
return teachers;
}
private Book [] createBooks(int numCopies)
throws CreateException, RemoteException
{
System.err.println("Creating Book CMP Entity beans.....");
String [] titles = {"Hamlet", "Moby Dick", "The Stranger"};
String [] authors = {"Shakespeare", "Melville", "Camus"};
deleteOldBooks(titles.length, numCopies);
Book [] books = new Book[titles.length * numCopies];
for (int i=0; i<titles.length; i++) {
for (int j=0; j<numCopies; j++) {
books[i*numCopies+j] =
bookHome.create(authors[i], titles[i], String.valueOf(i), j);
}
}
return books;
}
private void runClient()
throws Exception
{
System.err.println("");
System.err.println("Running EJB 2.0 CMP Example:");
System.err.println("");
Student [] students = null;
Locker [] lockers = null;
Book [] books = null;
Teacher [] teachers = null;
try {
students = createStudents();
// create 1 locker for each student
lockers = createLockers(students.length);
// create 1 book for each student;
books = createBooks(students.length);
teachers = createTeachers();
} catch (Exception e) {
System.err.println("Unable to create the example beans.");
System.err.println("The error was: ");
e.printStackTrace();
throw e;
}
System.err.println("");
System.err.println("Updating relationships....");
System.err.println("");
// Assign each Student a Locker
System.err.println("Assigning each Student to a Locker.");
for (int i=0; i<students.length; i++) {
students[i].assignLocker(lockers[i]);
}
// Assign the books out to the students
System.err.println("Assigning books to each Student.");
for (int i=0; i<books.length; i++) {
students[i % students.length].assignBook(books[i]);
}
// Assign students to teachers
// Student 0 gets student 0
// Student 1 gets students 0,1 etc.
System.err.println("Assigning Students to Teachers.");
for (int i=0; i<teachers.length; i++) {
for (int j=0; j<i; j++) {
students[j].assignTeacher(teachers[i]);
}
}
System.err.println("");
System.err.println("Finding All the Students in grade 10.");
System.err.println("");
try {
Collection c = studentHome.findStudentsInGrade(10);
if (c.isEmpty()) {
System.err.println("No Students were found in grade 10.");
} else {
Iterator it = c.iterator();
while (it.hasNext()) {
Student s = (Student) narrow(it.next(), Student.class);
System.err.println("Found student named: "+s.getName()+
" in grade 10.");
}
System.err.println("");
}
} catch (Exception e) {
System.err.println("Unable to find students in grade 10.");
System.err.println("The error was: ");
e.printStackTrace();
throw e;
}
System.err.println("");
System.err.println("Finding All the Students with a Locker.");
System.err.println("");
try {
Collection c = studentHome.findStudentsWithALocker();
if (c.isEmpty()) {
System.err.println("No Students were found with Lockers.");
} else {
Iterator it = c.iterator();
while (it.hasNext()) {
Student s = (Student) narrow(it.next(), Student.class);
System.err.println("Found student named: "+s.getName()+
" with a Locker.");
}
System.err.println("");
}
} catch (Exception e) {
System.err.println("Unable to find students WITH Lockers.");
System.err.println("The error was: ");
e.printStackTrace();
throw e;
}
System.err.println("");
System.err.println("Finding All the Students with a Locker.");
System.err.println("");
try {
Collection c = studentHome.findStudentsWithBooksTitled("Hamlet");
if (c.isEmpty()) {
System.err.println("No Students were found with books named Hamlet.");
} else {
Iterator it = c.iterator();
while (it.hasNext()) {
Student s = (Student) narrow(it.next(), Student.class);
System.err.println("Found student named: "+s.getName()+
" with a book named Hamlet.");
}
System.err.println("");
}
} catch (Exception e) {
System.err.println("Unable to find students WITH books named Hamlet.");
System.err.println("The error was: ");
e.printStackTrace();
throw e;
}
}
public static void main(String[] argv)
throws Exception
{
StudentCMPClient sc = new StudentCMPClient(argv);
sc.runClient();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -