blob: d0c72291bb1f8ca20f5638247a63c655e45b0ed4 [file] [log] [blame]
Vinay Vishal57171472018-09-18 20:22:00 +05301/*
Gaurav Gupta36555072020-03-26 14:10:23 +05302 * Copyright (c) 2001, 2020 Oracle and/or its affiliates. All rights reserved.
Vinay Vishal57171472018-09-18 20:22:00 +05303 *
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v. 2.0, which is available at
6 * http://www.eclipse.org/legal/epl-2.0.
7 *
8 * This Source Code may also be made available under the following Secondary
9 * Licenses when the conditions for such availability set forth in the
10 * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11 * version 2 with the GNU Classpath Exception, which is available at
12 * https://www.gnu.org/software/classpath/license.html.
13 *
14 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15 */
16
17package com.sun.s1peqe.loadbalancing.client;
18
19import javax.naming.Context;
20import javax.naming.InitialContext;
21import javax.rmi.PortableRemoteObject;
Gaurav Gupta36555072020-03-26 14:10:23 +053022import jakarta.jms.*;
Vinay Vishal57171472018-09-18 20:22:00 +053023import java.util.*;
24import com.sun.s1peqe.ejb.bmp.enroller.ejb.*;
25import com.sun.ejte.ccl.reporter.SimpleReporterAdapter;
Vinay Vishal57171472018-09-18 20:22:00 +053026import org.glassfish.internal.api.Globals;
27import org.glassfish.internal.api.ORBLocator;
28
29public class EnrollerClient {
30
31 private SimpleReporterAdapter stat =
32 new SimpleReporterAdapter("appserv-tests");
33
34 public static void main(String[] args) {
35 EnrollerClient client = new EnrollerClient();
36
37 // run the tests
38 client.runTestClient(args);
39 }
40
41 public void runTestClient(String[] args) {
42 try{
43 stat.addDescription("Testing loadbalancing app.");
44 test01(args);
45 testInAppClientContainer();
46 stat.printSummary("loadbalancingAppID");
47 } catch (Exception ex) {
48 System.out.println("Exception in runTestClient: " + ex.toString());
49 ex.printStackTrace();
50 }
51 }
52
53
54 private void test01(String[] args) {
55 String enrollerString = "";
56 String queueString = "";
57 try {
58 if (args.length != 0 && args[0].equals("standalone")) {
59 enrollerString = "ejb/MyEnroller";
60 queueString = "jms/SampleQueue";
61 } else {
62 enrollerString = "java:comp/env/ejb/SimpleEnroller";
63 queueString = "java:comp/env/jms/SampleQueue";
64 }
65 Properties env = new Properties();
66 env.put("java.naming.factory.initial", "com.sun.jndi.cosnaming.CNCtxFactory");
67 ORBLocator orbLocator = Globals.getDefaultHabitat().getService(ORBLocator.class);
68
69 env.put("java.naming.corba.orb", orbLocator.getORB());
70 // Initialize the Context with JNDI specific properties
71 InitialContext ctx = new InitialContext(env);
72 System.out.println("looking up ejb/MyStudent using com.sun.jndi.cosnaming.CNCtxFactory...");
73 Object obj = ctx.lookup("ejb/MyStudent");
74 System.out.println("Looked up ejb/MyStudent with CnCtxFactory...");
75 StudentHome sH =
76 (StudentHome) PortableRemoteObject.narrow(obj,
77 StudentHome.class);
78 Student denise1 = sH.create("111", "Tiffany Moore");
79 System.out.println("Created student id 111 for Tiffany Moore");
80
81 Context initial = new InitialContext();
82 System.out.println("Looking up MEJB...");
83 Object objref = initial.lookup("ejb/mgmt/MEJB");
84 System.out.println("Looked up ejb/mgmt/MEJB");
85 System.out.println("Looking up EJB REFs whose jndi name is specified as a corbaname: url ==>");
86 System.out.println("Creating new Context 1..");
87
88 System.out.println("Using Context 1, Looking up EJB using corbaname: url with global jndi name ==>");
89
90 objref = initial.lookup("corbaname:iiop:localhost:3700#ejb/MyStudent");
91 System.out.println("Looked up corbaname:iiop:localhost:3700#ejb/MyStudent");
92
93 StudentHome sHome =
94 (StudentHome) PortableRemoteObject.narrow(objref,
95 StudentHome.class);
96 Student denise = sHome.create("823", "Denise Smith");
97
98
99 System.out.println("Using Context 1, looking up global jndi name ==>");
100 Object objRef = initial.lookup("ejb/MyCourse");
101 System.out.println("Looked up ejb/MyCourse");
102 CourseHome cHome = (CourseHome)
103 PortableRemoteObject.narrow(objRef,
104 CourseHome.class);
105 Course power = cHome.create("220", "Power J2EE Programming");
106
107 objref = initial.lookup(enrollerString);
108 System.out.println("Looked up " + enrollerString);
109 EnrollerHome eHome =
110 (EnrollerHome) PortableRemoteObject.narrow(objref,
111 EnrollerHome.class);
112 Enroller enroller = eHome.create();
113 enroller.enroll("823", "220");
114 enroller.enroll("823", "333");
115 enroller.enroll("823", "777");
116 enroller.enroll("456", "777");
117 enroller.enroll("388", "777");
118
119 System.out.println(denise.getName() + ":");
120 ArrayList courses = denise.getCourseIds();
121 Iterator i = courses.iterator();
122 while (i.hasNext()) {
123 String courseId = (String)i.next();
124 Course course = cHome.findByPrimaryKey(courseId);
125 System.out.println(courseId + " " + course.getName());
126 }
127 System.out.println();
128
129 Course intro = cHome.findByPrimaryKey("777");
130 System.out.println(intro.getName() + ":");
131 courses = intro.getStudentIds();
132 i = courses.iterator();
133 while (i.hasNext()) {
134 String studentId = (String)i.next();
135 Student student = sHome.findByPrimaryKey(studentId);
136 System.out.println(studentId + " " + student.getName());
137 }
138
139 System.out.println("Looking up JMS Resource Refs ==>");
140 System.out.println("Creating new Context 2..");
141 Context initial1 = new InitialContext();
Gaurav Gupta36555072020-03-26 14:10:23 +0530142 jakarta.jms.Queue queue = (jakarta.jms.Queue) initial1.lookup(queueString);
Vinay Vishal57171472018-09-18 20:22:00 +0530143 System.out.println("looked up " + queueString);
144
145 System.out.println("Creating new Context 3...");
146 Context initial2 = new InitialContext();
Gaurav Gupta36555072020-03-26 14:10:23 +0530147 jakarta.jms.QueueConnectionFactory queueConnectionFactory =
Vinay Vishal57171472018-09-18 20:22:00 +0530148 (QueueConnectionFactory)
149 initial2.lookup("jms/QCFactory");
150 System.out.println("Looked up jms/QCFactory");
151
152 stat.addStatus("load balancing", stat.PASS);
153 } catch (Exception ex) {
154 stat.addStatus("load balancing", stat.FAIL);
155 System.err.println("Caught an unexpected exception!");
156 ex.printStackTrace();
157 }
158 }
159
160 private void testInAppClientContainer() {
161 System.out.println("Creating new Context ...");
162 try {
163 InitialContext ctx = new InitialContext();
164 Object obj = ctx.lookup("java:comp/InAppClientContainer");
165 if (obj == null) {
166 stat.addStatus("testInAppClientContainer", stat.FAIL);
167 return;
168 }
169 Boolean result = (Boolean) obj;
170 if (!result) {
171 stat.addStatus("testInAppClientContainer", stat.FAIL);
172 }
173 System.out.println("Looked up java:comp/InAppClientContainer :"
174 + result);
175 stat.addStatus("testInAppClientContainer", stat.PASS);
176 } catch (Exception ex) {
177 stat.addStatus("testInAppClientContainer", stat.FAIL);
178 System.err.println("Caught an unexpected exception!");
179 ex.printStackTrace();
180 }
181 }
182}