KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > enroller > EnrollerClient


1 package enroller;
2 /*
3  * Copyright (c) 2004 Sun Microsystems, Inc. All rights reserved. U.S.
4  * Government Rights - Commercial software. Government users are subject
5  * to the Sun Microsystems, Inc. standard license agreement and
6  * applicable provisions of the FAR and its supplements. Use is subject
7  * to license terms.
8  *
9  * This distribution may include materials developed by third parties.
10  * Sun, Sun Microsystems, the Sun logo, Java and J2EE are trademarks
11  * or registered trademarks of Sun Microsystems, Inc. in the U.S. and
12  * other countries.
13  *
14  * Copyright (c) 2004 Sun Microsystems, Inc. Tous droits reserves.
15  *
16  * Droits du gouvernement americain, utilisateurs gouvernementaux - logiciel
17  * commercial. Les utilisateurs gouvernementaux sont soumis au contrat de
18  * licence standard de Sun Microsystems, Inc., ainsi qu'aux dispositions
19  * en vigueur de la FAR (Federal Acquisition Regulations) et des
20  * supplements a celles-ci. Distribue par des licences qui en
21  * restreignent l'utilisation.
22  *
23  * Cette distribution peut comprendre des composants developpes par des
24  * tierces parties. Sun, Sun Microsystems, le logo Sun, Java et J2EE
25  * sont des marques de fabrique ou des marques deposees de Sun
26  * Microsystems, Inc. aux Etats-Unis et dans d'autres pays.
27  */

28
29
30 import javax.naming.Context JavaDoc;
31 import javax.naming.InitialContext JavaDoc;
32 import javax.rmi.PortableRemoteObject JavaDoc;
33 import java.util.*;
34
35
36 public class EnrollerClient {
37     public static void main(String JavaDoc[] args) {
38         try {
39             
40             
41             
42             
43             Context JavaDoc initial = new InitialContext JavaDoc();
44             //Context initial = new InitialContext();
45
Object JavaDoc objref = initial.lookup("ejb/SimpleStudent");
46             StudentRemoteHome sHome =
47                 (StudentRemoteHome) PortableRemoteObject.narrow(objref,
48                     StudentRemoteHome.class);
49
50             StudentRemote denise = sHome.create("823", "Denise Smith");
51
52             objref = initial.lookup("ejb/SimpleCourse");
53
54             CourseRemoteHome cHome =
55                 (CourseRemoteHome) PortableRemoteObject.narrow(objref,
56                     CourseRemoteHome.class);
57
58             CourseRemote power = cHome.create("220", "Power J2EE Programming");
59
60             objref = initial.lookup("ejb/SimpleEnroller");
61
62             EnrollerRemoteHome eHome =
63                 (EnrollerRemoteHome) PortableRemoteObject.narrow(objref,
64                     EnrollerRemoteHome.class);
65
66             EnrollerRemote enroller = eHome.create();
67
68             enroller.enroll("823", "220");
69             enroller.enroll("823", "333");
70             enroller.enroll("823", "777");
71             enroller.enroll("456", "777");
72             enroller.enroll("388", "777");
73
74             System.out.println(denise.getName() + ":");
75
76             ArrayList courses = denise.getCourseIds();
77             Iterator i = courses.iterator();
78
79             while (i.hasNext()) {
80                 String JavaDoc courseId = (String JavaDoc) i.next();
81                 CourseRemote course = cHome.findByPrimaryKey(courseId);
82
83                 System.out.println(courseId + " " + course.getName());
84             }
85
86             System.out.println();
87
88             CourseRemote intro = cHome.findByPrimaryKey("777");
89
90             System.out.println(intro.getName() + ":");
91             courses = intro.getStudentIds();
92             i = courses.iterator();
93
94             while (i.hasNext()) {
95                 String JavaDoc studentId = (String JavaDoc) i.next();
96                 StudentRemote student = sHome.findByPrimaryKey(studentId);
97
98                 System.out.println(studentId + " " + student.getName());
99             }
100
101             System.exit(0);
102         } catch (Exception JavaDoc ex) {
103             System.err.println("Caught an unexpected exception!");
104             ex.printStackTrace();
105         }
106     }
107 }
108
Popular Tags