blob: 67eb919df1ce219e2b213717f18677b585176953 [file] [log] [blame]
Vinay Vishal57171472018-09-18 20:22:00 +05301/*
Gaurav Gupta36555072020-03-26 14:10:23 +05302 * Copyright (c) 2017, 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.s1asdev.security.mdb;
18
Gaurav Guptaed5e7312020-05-01 07:54:26 +053019import jakarta.ejb.AccessLocalException;
20import jakarta.ejb.MessageDriven;
21import jakarta.ejb.EJBException;
22import jakarta.ejb.NoSuchEJBException;
23import jakarta.ejb.EJB;
24import jakarta.ejb.TransactionManagement;
25import jakarta.ejb.TransactionManagementType;
26import jakarta.ejb.ActivationConfigProperty;
Vinay Vishal57171472018-09-18 20:22:00 +053027
Gaurav Gupta36555072020-03-26 14:10:23 +053028import jakarta.jms.MessageListener;
29import jakarta.jms.Message;
30import jakarta.jms.Queue;
31import jakarta.jms.QueueConnectionFactory;
32import jakarta.jms.QueueConnection;
33import jakarta.jms.QueueSession;
34import jakarta.jms.QueueSender;
35import jakarta.jms.TextMessage;
36import jakarta.jms.Session;
Vinay Vishal57171472018-09-18 20:22:00 +053037
Lukas Jungmanndf239082020-04-29 13:37:31 +020038import jakarta.annotation.Resource;
39import jakarta.annotation.security.RunAs;
Vinay Vishal57171472018-09-18 20:22:00 +053040
41@TransactionManagement(TransactionManagementType.BEAN)
42@MessageDriven(mappedName="jms/security_mdb_InQueue", description="mymessagedriven bean description")
43@RunAs("javaee")
44
45 public class MessageBean implements MessageListener {
46
47 @EJB private Hello1 hello1;
48 @EJB private Hello2 hello2;
49
50 @Resource(name="jms/MyQueueConnectionFactory",
51 mappedName="jms/security_mdb_QCF")
52 QueueConnectionFactory qcFactory;
53
54 @Resource(mappedName="jms/security_mdb_OutQueue") Queue clientQueue;
55
56 public void onMessage(Message message) {
57 System.out.println("Got message!!!");
58
59 QueueConnection connection = null;
60 try {
61
62 System.out.println("Calling hello1 stateless bean");
63 hello1.hello("local ejb3.0 stateless");
64
65 try {
66 System.out.println("Calling hello2 stateful bean");
67 hello2.hello("local ejb3.0 stateful");
68 throw new IllegalStateException("Illegal Access of hello2");
69 } catch(AccessLocalException ex) {
70 System.out.println("Expected Exception: " + ex);
71 }
72
73 hello2.removeMethod();
74
75 connection = qcFactory.createQueueConnection();
76 QueueSession session = connection.createQueueSession(false,
77 Session.AUTO_ACKNOWLEDGE);
78 QueueSender sender = session.createSender(clientQueue);
79 connection.start();
80
81 TextMessage tmessage = session.createTextMessage();
82 tmessage.setText("mdb() invoked");
83 System.out.println("Sending message");
84 sender.send(tmessage);
85 System.out.println("message sent");
86 connection.close();
87
88 } catch(Exception e) {
89 e.printStackTrace();
90 } finally {
91 try {
92 if(connection != null) {
93 connection.close();
94 }
95 } catch(Exception e) {
96 e.printStackTrace();
97 }
98 }
99
100 }
101
102}