KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > plugins > SessionObjectOutputStream


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.ejb.plugins;
23
24 import java.io.OutputStream JavaDoc;
25 import java.io.ObjectOutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.rmi.Remote JavaDoc;
28 import java.rmi.server.RemoteObject JavaDoc;
29 import java.rmi.server.RemoteStub JavaDoc;
30 import java.security.PrivilegedAction JavaDoc;
31 import java.security.AccessController JavaDoc;
32 import javax.ejb.EJBObject JavaDoc;
33 import javax.ejb.EJBHome JavaDoc;
34 import javax.ejb.Handle JavaDoc;
35 import javax.ejb.SessionContext JavaDoc;
36 import javax.transaction.UserTransaction JavaDoc;
37
38
39 /**
40  * The SessionObjectOutputStream is used to serialize stateful session beans
41  * when they are passivated
42  *
43  * @see org.jboss.ejb.plugins.SessionObjectInputStream
44  * @author <a HREF="mailto:rickard.oberg@telkel.com">Rickard berg</a>
45  * @author <a HREF="mailto:sebastien.alborini@m4x.org">Sebastien Alborini</a>
46  * @version $Revision: 37459 $
47  */

48 public class SessionObjectOutputStream
49    extends ObjectOutputStream JavaDoc
50 {
51    // Constructors -------------------------------------------------
52
public SessionObjectOutputStream(OutputStream JavaDoc out)
53       throws IOException JavaDoc
54    {
55       super(out);
56       EnableReplaceObjectAction.enableReplaceObject(this);
57    }
58
59    // ObjectOutputStream overrides ----------------------------------
60
protected Object JavaDoc replaceObject(Object JavaDoc obj)
61       throws IOException JavaDoc
62    {
63       Object JavaDoc replacement = obj;
64       // section 6.4.1 of the ejb1.1 specification states what must be taken care of
65

66       // ejb reference (remote interface) : store handle
67
if (obj instanceof EJBObject JavaDoc)
68          replacement = ((EJBObject JavaDoc)obj).getHandle();
69       
70       // ejb reference (home interface) : store handle
71
else if (obj instanceof EJBHome JavaDoc)
72          replacement = ((EJBHome JavaDoc)obj).getHomeHandle();
73       
74       // session context : store a typed dummy object
75
else if (obj instanceof SessionContext JavaDoc)
76          replacement = new StatefulSessionBeanField(StatefulSessionBeanField.SESSION_CONTEXT);
77
78       // naming context : the jnp implementation is serializable, do nothing
79

80       // user transaction : store a typed dummy object
81
else if (obj instanceof UserTransaction JavaDoc)
82          replacement = new StatefulSessionBeanField(StatefulSessionBeanField.USER_TRANSACTION);
83
84       else if( obj instanceof Handle JavaDoc )
85          replacement = new HandleWrapper((Handle JavaDoc)obj);
86
87       else if( (obj instanceof Remote JavaDoc) && !(obj instanceof RemoteStub JavaDoc) )
88       {
89          Remote JavaDoc remote = (Remote JavaDoc) obj;
90          try
91          {
92             replacement = RemoteObject.toStub(remote);
93          }
94          catch(IOException JavaDoc ignore)
95          {
96             // Let the Serialization layer try with original object
97
}
98       }
99
100       return replacement;
101    }
102
103    private static class EnableReplaceObjectAction implements PrivilegedAction JavaDoc
104    {
105       SessionObjectOutputStream os;
106       EnableReplaceObjectAction(SessionObjectOutputStream os)
107       {
108          this.os = os;
109       }
110       public Object JavaDoc run()
111       {
112          os.enableReplaceObject(true);
113          return null;
114       }
115       static void enableReplaceObject(SessionObjectOutputStream os)
116       {
117          EnableReplaceObjectAction action = new EnableReplaceObjectAction(os);
118          AccessController.doPrivileged(action);
119       }
120    }
121 }
122
123
Popular Tags