KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sampleappli > OrderBean


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 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  * Initial developer(s): ____________________________________.
22  * Contributor(s): ______________________________________.
23  *
24  * --------------------------------------------------------------------------
25  * $Id: OrderBean.java,v 1.6 2004/04/19 06:39:30 benoitf Exp $
26  * --------------------------------------------------------------------------
27  */

28
29 // OrderBean.java
30
// Message Driven bean
31
package sampleappli;
32
33 import javax.ejb.MessageDrivenBean;
34 import javax.ejb.MessageDrivenContext;
35 import javax.jms.Message;
36 import javax.jms.MessageListener;
37 import javax.jms.TextMessage;
38
39 import javax.naming.Context;
40 import javax.naming.InitialContext;
41
42 import java.io.DataOutputStream;
43 import java.io.BufferedOutputStream;
44 import java.io.File;
45 import java.io.FileOutputStream;
46
47 /**
48  *
49  */

50 public class OrderBean implements MessageDrivenBean, MessageListener {
51
52     private transient MessageDrivenContext mdbContext;
53
54     String filename = null;
55
56     // ------------------------------------------------------------------
57
// MessageDrivenBean implementation
58
// ------------------------------------------------------------------
59

60     /**
61      * Default constructor
62      */

63     public OrderBean() {
64     }
65
66     /**
67      * Set the associated context. The container call this method after the
68      * instance creation. The enterprise Bean instance should store the
69      * reference to the context object in an instance variable. This method is
70      * called with no transaction context.
71      * @param MessageDrivenContext A MessageDrivenContext interface for the
72      * instance.
73      * @throws EJBException Thrown by the method to indicate a failure caused by
74      * a system-level error.
75      */

76
77     public void setMessageDrivenContext(MessageDrivenContext ctx) {
78         mdbContext = ctx;
79         Context initialContext = null;
80         if (filename == null) {
81             try {
82                 initialContext = new InitialContext();
83                 // Check that the SessionContext is the good one.
84
filename = (String) initialContext.lookup("java:comp/env/orderfilename");
85             } catch (Exception e) {
86                 System.err.println("cannot lookup environment");
87             }
88         }
89     }
90
91     /**
92      * A container invokes this method before it ends the life of the
93      * message-driven object. This happens when a container decides to terminate
94      * the message-driven object. This method is called with no transaction
95      * context.
96      * @throws EJBException Thrown by the method to indicate a failure caused by
97      * a system-level error.
98      */

99     public void ejbRemove() {
100     }
101
102     /**
103      * The Message driven bean must define an ejbCreate methods with no args.
104      */

105     public void ejbCreate() {
106     }
107
108     /**
109      * onMessage method
110      */

111     public void onMessage(Message message) {
112         TextMessage msg = (TextMessage) message;
113         try {
114             File f = new File(System.getProperty("java.io.tmpdir") + File.separator + System.getProperty("user.name")
115                     + "_" + filename);
116             DataOutputStream fileout = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(f
117                     .getCanonicalPath(), true)));
118             fileout.writeBytes(msg.getText() + System.getProperty("line.separator"));
119             fileout.close();
120         } catch (Exception e) {
121             System.err.println("OrderBean onMessage Exception caught: " + e);
122         }
123     }
124 }
Popular Tags