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: * --------------------------------------------------------------------------
022: * $Id: JStatefulOutputStream.java 9280 2006-08-01 10:15:24Z japaz $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas_ejb.container;
025:
026: import java.io.ByteArrayOutputStream;
027: import java.io.IOException;
028: import java.io.ObjectOutputStream;
029: import java.io.OutputStream;
030: import java.rmi.Remote;
031: import java.rmi.server.RemoteObject;
032: import java.rmi.server.RemoteStub;
033:
034: import javax.ejb.EJBHome;
035: import javax.ejb.EJBObject;
036: import javax.ejb.Handle;
037: import javax.ejb.SessionContext;
038: import javax.naming.Context;
039: import javax.naming.NamingException;
040: import javax.transaction.UserTransaction;
041:
042: import org.objectweb.jonas.naming.CompNamingContext;
043: import org.objectweb.util.monolog.api.BasicLevel;
044:
045: /**
046: * Extends ObjectOutputStream because we want to enable replaceObject.
047: * This class is very related to JStatefulInputStream
048: */
049: public class JStatefulOutputStream extends ObjectOutputStream {
050:
051: /**
052: * Constructor.
053: * @throws IOException
054: */
055: protected JStatefulOutputStream(OutputStream out)
056: throws IOException {
057: super (out);
058: TraceEjb.ssfpool.log(BasicLevel.DEBUG, "constructor");
059: enableReplaceObject(true);
060: }
061:
062: /**
063: * We must care about some object types. See EJB spec. 7.4.1
064: */
065: protected Object replaceObject(Object obj) throws IOException {
066: Object ret;
067:
068: if (obj instanceof EJBObject) {
069: // EJBObject -> its Handle
070: TraceEjb.ssfpool.log(BasicLevel.DEBUG, "EJBObject");
071: ret = ((EJBObject) obj).getHandle();
072: } else if (obj instanceof EJBHome) {
073: // EJBHome -> its HomeHandle
074: TraceEjb.ssfpool.log(BasicLevel.DEBUG, "EJBHome");
075: ret = ((EJBHome) obj).getHomeHandle();
076: } else if (obj instanceof JEntityLocal) {
077: // JEntityLocal -> Home jndi local name + PK
078: TraceEjb.ssfpool.log(BasicLevel.DEBUG, "JEntityLocal");
079: JEntityLocal local = (JEntityLocal) obj;
080: JEntityLocalHome lhome = (JEntityLocalHome) local
081: .getEJBLocalHome();
082: String jndiname = lhome.getJndiLocalName();
083: Object pk = local.getPrimaryKey();
084: ret = new JWrapper(JWrapper.LOCAL_ENTITY, jndiname, pk);
085: } else if (obj instanceof JLocalHome) {
086: // JLocalHome -> JNDI local name
087: TraceEjb.ssfpool.log(BasicLevel.DEBUG, "JLocalHome");
088: JLocalHome lhome = (JLocalHome) obj;
089: String jndiname = lhome.getJndiLocalName();
090: ret = new JWrapper(JWrapper.LOCAL_HOME, jndiname);
091: } else if (obj instanceof CompNamingContext) {
092: // ref to the env. naming context
093: try {
094: String cname = ((Context) obj).getNameInNamespace();
095: ret = new JWrapper(JWrapper.NAMING_CONTEXT, cname);
096: } catch (NamingException e) {
097: TraceEjb.ssfpool.log(BasicLevel.ERROR,
098: "Cannot convert Context: " + e);
099: ret = obj;
100: }
101: } else if (obj instanceof UserTransaction) {
102: // USerTransaction ref -> just mark it.
103: TraceEjb.ssfpool.log(BasicLevel.DEBUG, "UserTransaction");
104: ret = new JWrapper(JWrapper.USER_TX, null);
105: } else if (obj instanceof SessionContext) {
106: // SessionContext ref -> just mark it.
107: TraceEjb.ssfpool.log(BasicLevel.DEBUG, "SessionContext");
108: ret = new JWrapper(JWrapper.SESSION_CTX, null);
109: } else if (obj instanceof Handle) {
110: // Handle -> wrap it to avoid a confusion with EJBObject
111: // when the object will be deserialized.
112: TraceEjb.ssfpool.log(BasicLevel.DEBUG, "Handle");
113:
114: // Serialize the Handle without JStatefulOutputStream
115: Handle theHandle = (Handle) obj;
116: byte ser[];
117: ByteArrayOutputStream baos = new ByteArrayOutputStream();
118: ObjectOutputStream oos = new ObjectOutputStream(baos);
119: oos.writeObject(theHandle);
120: ser = baos.toByteArray();
121:
122: ret = new JWrapper(JWrapper.HANDLE, ser);
123: // ret = new JWrapper(JWrapper.HANDLE, (Handle) obj);
124: } else if ((obj instanceof Remote)
125: && !(obj instanceof RemoteStub)) {
126: // Remote ref -> its stub.
127: TraceEjb.ssfpool.log(BasicLevel.DEBUG, "Remote");
128: Remote remote = (Remote) obj;
129: try {
130: ret = RemoteObject.toStub(remote);
131: } catch (IOException ignore) {
132: TraceEjb.ssfpool.log(BasicLevel.DEBUG,
133: "Cannot convert to stub");
134: ret = obj;
135: }
136: } else {
137: // Default -> keep the object as is.
138: TraceEjb.ssfpool.log(BasicLevel.DEBUG, "Other:" + obj);
139: ret = obj;
140: }
141: return ret;
142: }
143: }
|