001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ejb.plugins;
023:
024: import java.io.OutputStream;
025: import java.io.ObjectOutputStream;
026: import java.io.IOException;
027: import java.rmi.Remote;
028: import java.rmi.server.RemoteObject;
029: import java.rmi.server.RemoteStub;
030: import java.security.PrivilegedAction;
031: import java.security.AccessController;
032: import javax.ejb.EJBObject;
033: import javax.ejb.EJBHome;
034: import javax.ejb.Handle;
035: import javax.ejb.SessionContext;
036: import javax.transaction.UserTransaction;
037:
038: /**
039: * The SessionObjectOutputStream is used to serialize stateful session beans
040: * when they are passivated
041: *
042: * @see org.jboss.ejb.plugins.SessionObjectInputStream
043: * @author <a href="mailto:rickard.oberg@telkel.com">Rickard berg</a>
044: * @author <a href="mailto:sebastien.alborini@m4x.org">Sebastien Alborini</a>
045: * @version $Revision: 57209 $
046: */
047: public class SessionObjectOutputStream extends ObjectOutputStream {
048: // Constructors -------------------------------------------------
049: public SessionObjectOutputStream(OutputStream out)
050: throws IOException {
051: super (out);
052: EnableReplaceObjectAction.enableReplaceObject(this );
053: }
054:
055: // ObjectOutputStream overrides ----------------------------------
056: protected Object replaceObject(Object obj) throws IOException {
057: Object replacement = obj;
058: // section 6.4.1 of the ejb1.1 specification states what must be taken care of
059:
060: // ejb reference (remote interface) : store handle
061: if (obj instanceof EJBObject)
062: replacement = ((EJBObject) obj).getHandle();
063:
064: // ejb reference (home interface) : store handle
065: else if (obj instanceof EJBHome)
066: replacement = ((EJBHome) obj).getHomeHandle();
067:
068: // session context : store a typed dummy object
069: else if (obj instanceof SessionContext)
070: replacement = new StatefulSessionBeanField(
071: StatefulSessionBeanField.SESSION_CONTEXT);
072:
073: // naming context : the jnp implementation is serializable, do nothing
074:
075: // user transaction : store a typed dummy object
076: else if (obj instanceof UserTransaction)
077: replacement = new StatefulSessionBeanField(
078: StatefulSessionBeanField.USER_TRANSACTION);
079:
080: else if (obj instanceof Handle)
081: replacement = new HandleWrapper((Handle) obj);
082:
083: else if ((obj instanceof Remote)
084: && !(obj instanceof RemoteStub)) {
085: Remote remote = (Remote) obj;
086: try {
087: replacement = RemoteObject.toStub(remote);
088: } catch (IOException ignore) {
089: // Let the Serialization layer try with original object
090: }
091: }
092:
093: return replacement;
094: }
095:
096: private static class EnableReplaceObjectAction implements
097: PrivilegedAction {
098: SessionObjectOutputStream os;
099:
100: EnableReplaceObjectAction(SessionObjectOutputStream os) {
101: this .os = os;
102: }
103:
104: public Object run() {
105: os.enableReplaceObject(true);
106: return null;
107: }
108:
109: static void enableReplaceObject(SessionObjectOutputStream os) {
110: EnableReplaceObjectAction action = new EnableReplaceObjectAction(
111: os);
112: AccessController.doPrivileged(action);
113: }
114: }
115: }
|