KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > beans > secured > Listener


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: Listener.java,v 1.4 2004/12/17 15:09:45 joaninh Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jtests.beans.secured;
27
28 import javax.ejb.CreateException JavaDoc;
29 import javax.ejb.EJBException JavaDoc;
30 import javax.ejb.MessageDrivenBean JavaDoc;
31 import javax.ejb.MessageDrivenContext JavaDoc;
32 import javax.jms.Message JavaDoc;
33 import javax.jms.MessageListener JavaDoc;
34 import javax.jms.QueueConnectionFactory JavaDoc;
35 import javax.jms.Queue JavaDoc;
36 import javax.jms.QueueConnection JavaDoc;
37 import javax.jms.QueueSession JavaDoc;
38 import javax.jms.QueueSender JavaDoc;
39 import javax.jms.Session JavaDoc;
40 import javax.naming.InitialContext JavaDoc;
41 import javax.naming.NamingException JavaDoc;
42 import javax.rmi.PortableRemoteObject JavaDoc;
43
44 import org.objectweb.jonas.common.Log;
45 import org.objectweb.util.monolog.api.Logger;
46 import org.objectweb.util.monolog.api.BasicLevel;
47
48 /**
49  * Come from the mdb tests
50  * @author Philippe Coq & Philippe Durieux
51  * @author Florent Benoit (run-as test)
52  */

53 public class Listener implements MessageDrivenBean JavaDoc, MessageListener JavaDoc {
54
55     /**
56      * Logger
57      */

58     private static Logger logger = null;
59
60     /**
61      * Context
62      */

63     private transient MessageDrivenContext JavaDoc mdbContext;
64
65     /**
66      * Class of the other bean
67      */

68     private transient DerivedLocalHome dh = null;
69
70     
71     /**
72      * Default constructor
73      */

74     public Listener() {
75     }
76
77     // ------------------------------------------------------------------
78
// MessageDrivenBean implementation
79
// ------------------------------------------------------------------
80

81     /**
82      * Set the associated context. The container call this method
83      * after the instance creation.
84      * The enterprise Bean instance should store the reference to the context
85      * object in an instance variable.
86      * This method is called with no transaction context.
87      *
88      * @param ctx MessageDrivenContext A MessageDrivenContext interface for the instance.
89      */

90
91     public void setMessageDrivenContext(MessageDrivenContext JavaDoc ctx) {
92         if (logger == null) {
93             logger = Log.getLogger(Log.JONAS_TESTS_PREFIX);
94         }
95         logger.log(BasicLevel.DEBUG, "");
96         mdbContext = ctx;
97     }
98
99     /**
100      * A container invokes this method before it ends the life of the message-driven object.
101      * This happens when a container decides to terminate the message-driven object.
102      *
103      * This method is called with no transaction context.
104      *
105      */

106     public void ejbRemove() {
107         logger.log(BasicLevel.DEBUG, "");
108     }
109
110     /**
111      * The Message driven bean must define an ejbCreate methods with no args.
112      */

113     public void ejbCreate() {
114         logger.log(BasicLevel.DEBUG, "");
115     }
116
117     /**
118      * onMessage method
119      * @param message receive message
120      */

121     public void onMessage(Message JavaDoc message) {
122         logger.log(BasicLevel.DEBUG, "");
123
124         String JavaDoc messageTest = "ok";
125
126         try {
127             // Don't test message but test run-as bean by calling another bean
128
try {
129                 InitialContext JavaDoc ictx = new InitialContext JavaDoc();
130                 dh = (DerivedLocalHome) ictx.lookup("java:comp/env/ejb/derivednorunaslocal");
131             } catch (NamingException JavaDoc e) {
132                 logger.log(BasicLevel.ERROR, "Listener : Cannot get DerivedHome:" + e);
133                 messageTest = "fail : Cannot get DerivedHome: " + e;
134             }
135             
136             // Create a new bean for this message (with right run-as)
137
try {
138                 dh.create();
139             } catch (EJBException JavaDoc ejbe) {
140                 messageTest = "fail : EJBException : " + ejbe.getMessage();
141                 logger.log(BasicLevel.ERROR, "EJBException: " + ejbe.getMessage());
142             } catch (CreateException JavaDoc e) {
143                 messageTest = "fail : Can not create the bean";
144                 logger.log(BasicLevel.ERROR, "Listener exception: " + e);
145             }
146         } catch (Exception JavaDoc ee) {
147             logger.log(BasicLevel.ERROR, "exception: " + ee);
148              messageTest = "fail :" + ee.getMessage();
149         }
150
151         QueueConnectionFactory JavaDoc qcf = null;
152         Queue JavaDoc queue = null;
153         try {
154             // Send the message (ok or fail)
155
try {
156                 InitialContext JavaDoc ictx = new InitialContext JavaDoc();
157                 qcf = (QueueConnectionFactory JavaDoc) ictx.lookup("java:comp/env/jms/RunAsFactory");
158                 queue = (Queue JavaDoc) ictx.lookup("java:comp/env/jms/testRunAs");
159             } catch (NamingException JavaDoc e) {
160                 logger.log(BasicLevel.ERROR, "Listener : Cannot get DerivedHome:" + e);
161             }
162             
163             
164             QueueConnection JavaDoc qc = qcf.createQueueConnection();
165             QueueSession JavaDoc qs = qc.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
166             Message JavaDoc m = qs.createMessage();
167             m.setStringProperty("testRunAsJms", messageTest);
168             QueueSender JavaDoc qsender = qs.createSender(queue);
169             qsender.send(m);
170             
171             // close
172
qsender.close();
173             qs.close();
174             qc.close();
175         } catch (Exception JavaDoc e) {
176             logger.log(BasicLevel.ERROR, "exception:" + e);
177         }
178     }
179 }
180
Popular Tags