/
ServletClient.java.src
381 lines (353 loc) · 14.4 KB
/
ServletClient.java.src
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
* Copyright (c) 2013, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package com.sun.ts.tests.jms.ee20.resourcedefs.annotations;
import com.sun.ts.lib.util.*;
import com.sun.ts.lib.porting.*;
import com.sun.ts.tests.jms.common.*;
import java.io.*;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.WebServlet;
import java.util.Enumeration;
import java.util.Properties;
import java.util.ArrayList;
import jakarta.transaction.*;
import javax.naming.*;
import jakarta.jms.*;
import jakarta.inject.Inject;
import jakarta.annotation.Resource;
//-------------------------------------
// JMS Destination Resource Definitions
//-------------------------------------
@JMSDestinationDefinition(
description="Define Queue ServletMyTestQueue",
interfaceName="jakarta.jms.Queue",
name="java:global/env/ServletMyTestQueue",
destinationName="ServletMyTestQueue"
)
@JMSDestinationDefinition(
description="Define Topic ServletMyTestTopic",
interfaceName="jakarta.jms.Topic",
name="java:app/env/ServletMyTestTopic",
destinationName="ServletMyTestTopic"
)
//-------------------------------------------
// JMS ConnectionFactory Resource Definitions
//-------------------------------------------
@JMSConnectionFactoryDefinition(
description="Define ConnectionFactory ServletMyTestConnectionFactory",
interfaceName="jakarta.jms.ConnectionFactory",
name="java:global/ServletMyTestConnectionFactory",
user = "@user@",
password = "@password@"
)
@JMSConnectionFactoryDefinition(
description="Define QueueConnectionFactory ServletMyTestQueueConnectionFactory",
interfaceName="jakarta.jms.QueueConnectionFactory",
name="java:app/ServletMyTestQueueConnectionFactory",
user = "@user@",
password = "@password@"
)
@JMSConnectionFactoryDefinition(
description="Define TopicConnectionFactory ServletMyTestTopicConnectionFactory",
interfaceName="jakarta.jms.TopicConnectionFactory",
name="java:module/ServletMyTestTopicConnectionFactory",
user = "@user@",
password = "@password@"
)
@JMSConnectionFactoryDefinition(
description="Define Durable TopicConnectionFactory ServletMyTestDurableTopicConnectionFactory",
interfaceName="jakarta.jms.TopicConnectionFactory",
name="java:comp/env/jms/ServletMyTestDurableTopicConnectionFactory",
user = "@user@",
password = "@password@",
clientId = "MyClientID",
properties = { "Property1=10", "Property2=20" },
transactional = false,
maxPoolSize = 30,
minPoolSize = 20
)
@WebServlet("/ServletTest")
public class ServletClient extends HttpServlet {
private static final long serialVersionUID = 1L;
// JMS objects
protected transient ConnectionFactory cf = null;
protected transient ConnectionFactory dcf = null;
protected transient ConnectionFactory cfra = null;
protected transient QueueConnectionFactory qcf = null;
protected transient TopicConnectionFactory tcf = null;
protected transient TopicConnectionFactory dtcf = null;
protected transient Topic topic = null;
protected transient Topic topica = null;
protected transient Queue queue = null;
protected transient JMSContext context = null;
protected transient JMSConsumer consumerQ = null;
protected transient JMSProducer producerQ = null;
protected transient JMSConsumer consumerT = null;
protected transient JMSProducer producerT = null;
protected boolean queueTest = false;
// Harness req's
protected Properties harnessProps = null;
// properties read from ts.jte file
protected long timeout;
protected String user;
protected String password;
protected String mode;
/*
* doCleanup()
*/
private void doCleanup() throws Exception {
try {
if(queueTest && consumerQ != null) {
TestUtil.logMsg("Flush any messages left on Queue");
Message rmsg = consumerQ.receive(timeout);
while (rmsg != null) {
rmsg = consumerQ.receiveNoWait();
if (rmsg == null) {
rmsg = consumerQ.receiveNoWait();
}
}
consumerQ.close();
}
if(consumerT != null)
consumerT.close();
TestUtil.logMsg("Close JMSContext Objects");
if(context != null)
context.close();
} catch (Exception e) {
TestUtil.logErr("Caught exception: " + e);
throw new Exception("doCleanup failed!", e);
}
}
public void init(ServletConfig config) throws ServletException
{
super.init(config);
}
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
boolean pass = true;
Properties p = new Properties();
res.setContentType("text/plain");
PrintWriter out = res.getWriter();
try {
String test = harnessProps.getProperty("TEST");
System.out.println("doGet: test to execute is: "+test);
if(test.equals("sendAndRecvQueueTestFromServletClient")) {
if(sendAndRecvQueueTestFromServletClient())
p.setProperty("TESTRESULT", "pass");
else
p.setProperty("TESTRESULT", "fail");
} else if(test.equals("sendAndRecvTopicTestFromServletClient")) {
if(sendAndRecvTopicTestFromServletClient())
p.setProperty("TESTRESULT", "pass");
else
p.setProperty("TESTRESULT", "fail");
} else if(test.equals("checkClientIDTestFromServletClient")) {
if(checkClientIDTestFromServletClient())
p.setProperty("TESTRESULT", "pass");
else
p.setProperty("TESTRESULT", "fail");
} else {
p.setProperty("TESTRESULT", "fail");
}
doCleanup();
p.list(out);
} catch (Exception e) {
TestUtil.logErr("doGet: Exception: " + e);
System.out.println("doGet: Exception: " + e);
p.setProperty("TESTRESULT", "fail");
p.list(out);
}
out.close();
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
harnessProps = new Properties();
Enumeration enumlist = req.getParameterNames();
while (enumlist.hasMoreElements()) {
String name = (String) enumlist.nextElement();
String value = req.getParameter(name);
harnessProps.setProperty(name, value);
}
try {
TestUtil.init(harnessProps);
// get props
timeout = Long.parseLong(harnessProps.getProperty("jms_timeout"));
user = harnessProps.getProperty("user");
password = harnessProps.getProperty("password");
mode = harnessProps.getProperty("platform.mode");
// check props for errors
if (timeout < 1) {
throw new Exception("'jms_timeout' (milliseconds) in ts.jte must be > 0");
}
if (user == null ) {
throw new Exception("'user' in ts.jte must not be null ");
}
if (password == null ) {
throw new Exception("'password' in ts.jte must not be null ");
}
if (mode == null) {
throw new Exception("'platform.mode' in ts.jte must not be null");
}
doLookupJMSObjects();
} catch (Exception e) {
System.out.println("doPost: Exception: " + e);
e.printStackTrace();
throw new ServletException("unable to initialize remote logging");
}
doGet(req, res);
harnessProps = null;
}
/*
* Lookup JMS Connection Factory and Destination Objects
*/
private void doLookupJMSObjects() throws Exception {
try {
TestUtil.logMsg("Lookup JMS factories defined by @JMSConnectionFactoryDefinitions");
TestUtil.logMsg("Lookup JMS destinations defined by @JMSDestinationDefinitions");
TSNamingContext namingctx = new TSNamingContext();
TestUtil.logMsg("Lookup java:comp/DefaultJMSConnectionFactory");
dcf = (ConnectionFactory)
namingctx.lookup("java:comp/DefaultJMSConnectionFactory");
TestUtil.logMsg("Lookup java:global/ServletMyTestConnectionFactory");
cf = (ConnectionFactory)
namingctx.lookup("java:global/ServletMyTestConnectionFactory");
TestUtil.logMsg("Lookup java:app/ServletMyTestQueueConnectionFactory");
qcf = (QueueConnectionFactory)
namingctx.lookup("java:app/ServletMyTestQueueConnectionFactory");
TestUtil.logMsg("Lookup java:module/ServletMyTestTopicConnectionFactory");
tcf = (TopicConnectionFactory)
namingctx.lookup("java:module/ServletMyTestTopicConnectionFactory");
TestUtil.logMsg("Lookup java:comp/env/jms/ServletMyTestDurableTopicConnectionFactory");
dtcf = (TopicConnectionFactory)
namingctx.lookup("java:comp/env/jms/ServletMyTestDurableTopicConnectionFactory");
TestUtil.logMsg("Lookup java:global/env/ServletMyTestQueue");
queue = (Queue) namingctx.lookup("java:global/env/ServletMyTestQueue");
TestUtil.logMsg("Lookup java:app/env/ServletMyTestTopic");
topic = (Topic) namingctx.lookup("java:app/env/ServletMyTestTopic");
TestUtil.logMsg("Create JMSContext, JMSProducer's and JMSConsumer's");
context = cf.createContext(user, password, JMSContext.AUTO_ACKNOWLEDGE);
producerQ = context.createProducer();
consumerQ = context.createConsumer(queue);
producerT = context.createProducer();
consumerT = context.createConsumer(topic);
} catch (Exception e) {
TestUtil.logErr("Caught exception: " + e);
throw e;
}
}
public boolean sendAndRecvQueueTestFromServletClient() {
boolean pass = true;
String message = "Where are you!";
try {
queueTest = true;
// send and receive TextMessage
TestUtil.logMsg("Creating TextMessage via JMSContext.createTextMessage(String)");
TextMessage expTextMessage = context.createTextMessage(message);
TestUtil.logMsg("Set some values in TextMessage");
expTextMessage.setStringProperty("COM_SUN_JMS_TESTNAME", "sendAndRecvQueueTestFromServletClient");
TestUtil.logMsg("Sending TextMessage via JMSProducer.send(Destination, Message)");
producerQ.send(queue, expTextMessage);
TestUtil.logMsg("Receive TextMessage via JMSconsumer.receive(long)");
TextMessage actTextMessage = (TextMessage) consumerQ.receive(timeout);
if (actTextMessage == null) {
TestUtil.logErr("Did not receive TextMessage");
pass = false;
} else {
TestUtil.logMsg("Check the value in TextMessage");
if(actTextMessage.getText().equals(expTextMessage.getText())) {
TestUtil.logMsg("TextMessage is correct");
} else {
TestUtil.logErr("TextMessage is incorrect expected " + expTextMessage.getText()
+ ", received " + actTextMessage.getText());
pass = false;
}
}
} catch (Exception e) {
TestUtil.logErr("Caught exception: " + e);
pass = false;
}
return pass;
}
public boolean sendAndRecvTopicTestFromServletClient() {
boolean pass = true;
String message = "Where are you!";
try {
queueTest = false;
// send and receive TextMessage
TestUtil.logMsg("Creating TextMessage via JMSContext.createTextMessage(String)");
TextMessage expTextMessage = context.createTextMessage(message);
TestUtil.logMsg("Set some values in TextMessage");
expTextMessage.setStringProperty("COM_SUN_JMS_TESTNAME", "sendAndRecvTopicTestFromServletClient");
TestUtil.logMsg("Sending TextMessage via JMSProducer.send(Destination, Message)");
producerT.send(topic, expTextMessage);
TestUtil.logMsg("Receive TextMessage via JMSconsumer.receive(long)");
TextMessage actTextMessage = (TextMessage) consumerT.receive(timeout);
if (actTextMessage == null) {
TestUtil.logErr("Did not receive TextMessage");
pass = false;
} else {
TestUtil.logMsg("Check the value in TextMessage");
if(actTextMessage.getText().equals(expTextMessage.getText())) {
TestUtil.logMsg("TextMessage is correct");
} else {
TestUtil.logErr("TextMessage is incorrect expected " + expTextMessage.getText()
+ ", received " + actTextMessage.getText());
pass = false;
}
}
} catch (Exception e) {
TestUtil.logErr("Caught exception: " + e);
pass = false;
}
return pass;
}
public boolean checkClientIDTestFromServletClient() {
boolean pass = true;
JMSContext context = null;
try {
queueTest = false;
TestUtil.logMsg("Create JMSContext from durable topic connection factory");
TestUtil.logMsg("Check the client id which is configured as MyClientID in the " +
"JMSConnectionFactoryDefinition annotation");
context = dtcf.createContext(user, password, JMSContext.AUTO_ACKNOWLEDGE);
String clientid = context.getClientID();
if(clientid == null) {
TestUtil.logErr("Client ID value is null (expected MyClientID)");
pass = false;
} else if(clientid.equals("MyClientID")) {
TestUtil.logMsg("Client ID value is correct (MyClientID)");
} else {
TestUtil.logErr("Client ID value is incorrect (expected MyClientID, got "+clientid+")");
pass = false;
}
} catch (Exception e) {
TestUtil.logErr("Caught exception: " + e);
pass = false;
} finally {
try {
if(context != null)
context.close();
doCleanup();
} catch(Exception e) {
TestUtil.logErr("Error in cleanup "+e);
}
}
return pass;
}
}