001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2004 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * Initial developer(s): ____________________________________.
022: * Contributor(s): ______________________________________.
023: *
024: * --------------------------------------------------------------------------
025: * $Id: OrderBean.java 4618 2004-04-19 06:39:30Z benoitf $
026: * --------------------------------------------------------------------------
027: */package sampleappli;
028:
029: import javax.ejb.MessageDrivenBean;
030: import javax.ejb.MessageDrivenContext;
031: import javax.jms.Message;
032: import javax.jms.MessageListener;
033: import javax.jms.TextMessage;
034:
035: import javax.naming.Context;
036: import javax.naming.InitialContext;
037:
038: import java.io.DataOutputStream;
039: import java.io.BufferedOutputStream;
040: import java.io.File;
041: import java.io.FileOutputStream;
042:
043: /**
044: *
045: */
046: public class OrderBean implements MessageDrivenBean, MessageListener {
047:
048: private transient MessageDrivenContext mdbContext;
049:
050: String filename = null;
051:
052: // ------------------------------------------------------------------
053: // MessageDrivenBean implementation
054: // ------------------------------------------------------------------
055:
056: /**
057: * Default constructor
058: */
059: public OrderBean() {
060: }
061:
062: /**
063: * Set the associated context. The container call this method after the
064: * instance creation. The enterprise Bean instance should store the
065: * reference to the context object in an instance variable. This method is
066: * called with no transaction context.
067: * @param MessageDrivenContext A MessageDrivenContext interface for the
068: * instance.
069: * @throws EJBException Thrown by the method to indicate a failure caused by
070: * a system-level error.
071: */
072:
073: public void setMessageDrivenContext(MessageDrivenContext ctx) {
074: mdbContext = ctx;
075: Context initialContext = null;
076: if (filename == null) {
077: try {
078: initialContext = new InitialContext();
079: // Check that the SessionContext is the good one.
080: filename = (String) initialContext
081: .lookup("java:comp/env/orderfilename");
082: } catch (Exception e) {
083: System.err.println("cannot lookup environment");
084: }
085: }
086: }
087:
088: /**
089: * A container invokes this method before it ends the life of the
090: * message-driven object. This happens when a container decides to terminate
091: * the message-driven object. This method is called with no transaction
092: * context.
093: * @throws EJBException Thrown by the method to indicate a failure caused by
094: * a system-level error.
095: */
096: public void ejbRemove() {
097: }
098:
099: /**
100: * The Message driven bean must define an ejbCreate methods with no args.
101: */
102: public void ejbCreate() {
103: }
104:
105: /**
106: * onMessage method
107: */
108: public void onMessage(Message message) {
109: TextMessage msg = (TextMessage) message;
110: try {
111: File f = new File(System.getProperty("java.io.tmpdir")
112: + File.separator + System.getProperty("user.name")
113: + "_" + filename);
114: DataOutputStream fileout = new DataOutputStream(
115: new BufferedOutputStream(new FileOutputStream(f
116: .getCanonicalPath(), true)));
117: fileout.writeBytes(msg.getText()
118: + System.getProperty("line.separator"));
119: fileout.close();
120: } catch (Exception e) {
121: System.err.println("OrderBean onMessage Exception caught: "
122: + e);
123: }
124: }
125: }
|