Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 1 | /* |
Gaurav Gupta | 3655507 | 2020-03-26 14:10:23 +0530 | [diff] [blame] | 2 | * Copyright (c) 2017, 2020 Oracle and/or its affiliates. All rights reserved. |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 3 | * |
| 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 | |
| 17 | package com.sun.s1asdev.security.mdb; |
| 18 | |
Gaurav Gupta | ed5e731 | 2020-05-01 07:54:26 +0530 | [diff] [blame] | 19 | import jakarta.ejb.AccessLocalException; |
| 20 | import jakarta.ejb.MessageDriven; |
| 21 | import jakarta.ejb.EJBException; |
| 22 | import jakarta.ejb.NoSuchEJBException; |
| 23 | import jakarta.ejb.EJB; |
| 24 | import jakarta.ejb.TransactionManagement; |
| 25 | import jakarta.ejb.TransactionManagementType; |
| 26 | import jakarta.ejb.ActivationConfigProperty; |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 27 | |
Gaurav Gupta | 3655507 | 2020-03-26 14:10:23 +0530 | [diff] [blame] | 28 | import jakarta.jms.MessageListener; |
| 29 | import jakarta.jms.Message; |
| 30 | import jakarta.jms.Queue; |
| 31 | import jakarta.jms.QueueConnectionFactory; |
| 32 | import jakarta.jms.QueueConnection; |
| 33 | import jakarta.jms.QueueSession; |
| 34 | import jakarta.jms.QueueSender; |
| 35 | import jakarta.jms.TextMessage; |
| 36 | import jakarta.jms.Session; |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 37 | |
Lukas Jungmann | df23908 | 2020-04-29 13:37:31 +0200 | [diff] [blame] | 38 | import jakarta.annotation.Resource; |
| 39 | import jakarta.annotation.security.RunAs; |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 40 | |
| 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 | } |