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: JStatefulInputStream.java 9280 2006-08-01 10:15:24Z japaz $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas_ejb.container;
025:
026: import java.io.ByteArrayInputStream;
027: import java.io.IOException;
028: import java.io.InputStream;
029: import java.io.ObjectInputStream;
030: import java.io.ObjectStreamClass;
031: import java.rmi.server.RemoteStub;
032:
033: import javax.ejb.Handle;
034: import javax.ejb.HomeHandle;
035: import javax.naming.InitialContext;
036: import javax.naming.NamingException;
037:
038: import org.objectweb.util.monolog.api.BasicLevel;
039:
040: /**
041: * Extends ObjectInputStream because we want to enable resolveObject.
042: * This class is very related to JStatefulOutputStream
043: */
044: public class JStatefulInputStream extends ObjectInputStream {
045:
046: private JStatefulSwitch jss;
047:
048: /**
049: * Constructor.
050: * @throws IOException
051: */
052: public JStatefulInputStream(InputStream in, JStatefulSwitch jss)
053: throws IOException {
054: super (in);
055: TraceEjb.ssfpool.log(BasicLevel.DEBUG, "constructor");
056: enableResolveObject(true);
057: this .jss = jss;
058: }
059:
060: /**
061: * We must care about some object types. See EJB spec. 7.4.1
062: * @param obj serialized field
063: * @return resolved object
064: */
065: protected Object resolveObject(Object obj) throws IOException {
066: Object ret;
067:
068: if (obj instanceof HomeHandle) {
069: // HomeHandle -> EJBHome
070: TraceEjb.ssfpool.log(BasicLevel.DEBUG, "HomeHandle");
071: ret = ((HomeHandle) obj).getEJBHome();
072: } else if (obj instanceof Handle) {
073: // Handle -> EJBObject
074: TraceEjb.ssfpool.log(BasicLevel.DEBUG, "Handle");
075: ret = ((Handle) obj).getEJBObject();
076: } else if (obj instanceof JWrapper) {
077: int type = ((JWrapper) obj).getType();
078: switch (type) {
079: case JWrapper.LOCAL_ENTITY:
080: TraceEjb.ssfpool.log(BasicLevel.DEBUG, "JEntityLocal");
081: String jndiname = (String) ((JWrapper) obj).getObject();
082: Object pk = ((JWrapper) obj).getPK();
083: JEntityLocalHome lhome = (JEntityLocalHome) JLocalHome
084: .getLocalHome(jndiname);
085: ret = lhome.findLocalByPK(pk);
086: break;
087: case JWrapper.LOCAL_HOME:
088: TraceEjb.ssfpool.log(BasicLevel.DEBUG, "JLocalHome");
089: String jname = (String) ((JWrapper) obj).getObject();
090: ret = JLocalHome.getLocalHome(jname);
091: break;
092: case JWrapper.USER_TX:
093: // UserTransaction
094: // Should be outside transaction here.
095: TraceEjb.ssfpool.log(BasicLevel.DEBUG,
096: "UserTransaction");
097: ret = jss.getStatefulContext().getUserTransaction();
098: break;
099: case JWrapper.SESSION_CTX:
100: // SessionContext.
101: // Should be outside transaction here.
102: TraceEjb.ssfpool
103: .log(BasicLevel.DEBUG, "SessionContext");
104: ret = jss.getStatefulContext();
105: break;
106: case JWrapper.NAMING_CONTEXT:
107: // CompNamingContext
108: TraceEjb.ssfpool.log(BasicLevel.DEBUG,
109: "CompNamingContext");
110: String cname = (String) ((JWrapper) obj).getObject();
111: try {
112: InitialContext ictx = new InitialContext();
113: ret = ictx.lookup(cname);
114: } catch (NamingException e) {
115: TraceEjb.ssfpool.log(BasicLevel.ERROR,
116: "Cannot retrieve NamingContext" + e);
117: throw new IOException(
118: "Cannot retrieve NamingContext");
119: }
120: break;
121: case JWrapper.HANDLE:
122: TraceEjb.ssfpool.log(BasicLevel.DEBUG, "Handle");
123:
124: // Deserialize the Handle without JStatefulOutputStream
125: JWrapper theJWrapper = (JWrapper) obj;
126: byte ser[] = (byte[]) theJWrapper.getObject();
127:
128: ByteArrayInputStream bais = new ByteArrayInputStream(
129: ser);
130: ObjectInputStream ois = new ObjectInputStream(bais);
131: Handle theHandle;
132:
133: try {
134: theHandle = (Handle) ois.readObject();
135: } catch (ClassNotFoundException e) {
136: e.printStackTrace();
137: throw new IOException(e.getMessage());
138: }
139:
140: ret = theHandle;
141: break;
142: default:
143: TraceEjb.ssfpool.log(BasicLevel.DEBUG,
144: "Other wrapped object");
145: ret = ((JWrapper) obj).getObject();
146: break;
147: }
148: } else if (obj instanceof RemoteStub) {
149: // Remote Stub -> Remote
150: TraceEjb.ssfpool.log(BasicLevel.DEBUG, "RemoteStub");
151: // TODO
152: ret = obj;
153: } else {
154: // Default -> keep the object as is.
155: TraceEjb.ssfpool.log(BasicLevel.DEBUG, "Other");
156: ret = obj;
157: }
158: return ret;
159: }
160:
161: /**
162: * Use the thread context class loader to resolve the class
163: * @param v class to resolve
164: * @return class resolved
165: * @throws java.io.IOException thrown by the underlying InputStream.
166: * @throws ClassNotFoundException thrown by the underlying InputStream.
167: */
168: protected Class resolveClass(ObjectStreamClass v)
169: throws IOException, ClassNotFoundException {
170: TraceEjb.ssfpool.log(BasicLevel.DEBUG, v);
171: ClassLoader cl = Thread.currentThread().getContextClassLoader();
172: return cl.loadClass(v.getName());
173: }
174:
175: }
|