KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_ejb > container > JStatefulOutputStream


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  * --------------------------------------------------------------------------
22  * $Id: JStatefulOutputStream.java,v 1.1 2005/07/13 06:29:45 durieuxp Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas_ejb.container;
27
28 import java.io.IOException JavaDoc;
29 import java.io.ObjectOutputStream JavaDoc;
30 import java.io.OutputStream JavaDoc;
31 import java.rmi.Remote JavaDoc;
32 import java.rmi.server.RemoteObject JavaDoc;
33 import java.rmi.server.RemoteStub JavaDoc;
34
35 import javax.ejb.EJBHome JavaDoc;
36 import javax.ejb.EJBObject JavaDoc;
37 import javax.ejb.Handle JavaDoc;
38 import javax.ejb.SessionContext JavaDoc;
39 import javax.transaction.UserTransaction JavaDoc;
40
41 import org.objectweb.util.monolog.api.BasicLevel;
42
43 /**
44  * Extends ObjectOutputStream because we want to enable replaceObject.
45  * This class is very related to JStatefulInputStream
46  */

47 public class JStatefulOutputStream extends ObjectOutputStream JavaDoc {
48
49     /**
50      * Constructor.
51      * @throws IOException
52      */

53     protected JStatefulOutputStream(OutputStream JavaDoc out) throws IOException JavaDoc {
54         super(out);
55         TraceEjb.ssfpool.log(BasicLevel.DEBUG, "constructor");
56         enableReplaceObject(true);
57     }
58
59     /**
60      * We must care about some object types. See EJB spec. 7.4.1
61      */

62     protected Object JavaDoc replaceObject(Object JavaDoc obj) throws IOException JavaDoc {
63         Object JavaDoc ret;
64
65         if (obj instanceof EJBObject JavaDoc) {
66             // EJBObject -> its Handle
67
TraceEjb.ssfpool.log(BasicLevel.DEBUG, "EJBObject");
68             ret = ((EJBObject JavaDoc) obj).getHandle();
69         } else if (obj instanceof EJBHome JavaDoc) {
70             // EJBHome -> its HomeHandle
71
TraceEjb.ssfpool.log(BasicLevel.DEBUG, "EJBHome");
72             ret = ((EJBHome JavaDoc) obj).getHomeHandle();
73         } else if (obj instanceof UserTransaction JavaDoc) {
74             // USerTransaction ref -> just mark it.
75
TraceEjb.ssfpool.log(BasicLevel.DEBUG, "UserTransaction");
76             ret = new JWrapper(JWrapper.USER_TX, null);
77         } else if (obj instanceof SessionContext JavaDoc) {
78             // SessionContext ref -> just mark it.
79
TraceEjb.ssfpool.log(BasicLevel.DEBUG, "SessionContext");
80             ret = new JWrapper(JWrapper.SESSION_CTX, null);
81         } else if (obj instanceof Handle JavaDoc) {
82             // Handle -> wrap it to avoid a confusion with EJBObject
83
// when the object will be deserialized.
84
TraceEjb.ssfpool.log(BasicLevel.DEBUG, "Handle");
85             ret = new JWrapper(JWrapper.HANDLE, (Handle JavaDoc) obj);
86         } else if ((obj instanceof Remote JavaDoc) && !(obj instanceof RemoteStub JavaDoc)) {
87             // Remote ref -> its stub.
88
TraceEjb.ssfpool.log(BasicLevel.DEBUG, "Remote");
89             Remote JavaDoc remote = (Remote JavaDoc) obj;
90             try {
91                 ret = RemoteObject.toStub(remote);
92             } catch (IOException JavaDoc ignore) {
93                 TraceEjb.ssfpool.log(BasicLevel.DEBUG, "Cannot convert to stub");
94                 ret = obj;
95             }
96         } else {
97             // Default -> keep the object as is.
98
TraceEjb.ssfpool.log(BasicLevel.DEBUG, "Other:" + obj);
99             ret = obj;
100         }
101         return ret;
102     }
103 }
104
Popular Tags