KickJava   Java API By Example, From Geeks To Geeks.

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


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.InputStream JavaDoc;
25 import java.io.ObjectInputStream JavaDoc;
26 import java.io.ObjectStreamClass JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.lang.reflect.Proxy JavaDoc;
29 import java.security.PrivilegedAction JavaDoc;
30 import java.security.AccessController JavaDoc;
31
32 import javax.ejb.Handle JavaDoc;
33 import javax.ejb.HomeHandle JavaDoc;
34
35 import org.jboss.ejb.StatefulSessionEnterpriseContext;
36
37 /**
38  * The SessionObjectInputStream is used to deserialize stateful session beans when they are activated
39  *
40  * @see org.jboss.ejb.plugins.SessionObjectOutputStream
41  * @author <a HREF="mailto:rickard.oberg@telkel.com">Rickard berg</a>
42  * @author <a HREF="mailto:sebastien.alborini@m4x.org">Sebastien Alborini</a>
43  * @author <a HREF="mailto:scott.stark@jboss.org">Scott Stark</a>
44  * @version $Revision: 37459 $
45  */

46 public class SessionObjectInputStream
47    extends ObjectInputStream JavaDoc
48 {
49    private StatefulSessionEnterpriseContext ctx;
50    private ClassLoader JavaDoc appCl;
51
52    // Constructors -------------------------------------------------
53
public SessionObjectInputStream(StatefulSessionEnterpriseContext ctx, InputStream JavaDoc in)
54       throws IOException JavaDoc
55    {
56       super(in);
57       EnableResolveObjectAction.enableResolveObject(this);
58
59       this.ctx = ctx;
60       
61       // cache the application classloader
62
appCl = SecurityActions.getContextClassLoader();
63    }
64
65    // ObjectInputStream overrides -----------------------------------
66
protected Object JavaDoc resolveObject(Object JavaDoc obj)
67       throws IOException JavaDoc
68    {
69       Object JavaDoc resolved = obj;
70
71       // section 6.4.1 of the ejb1.1 specification states what must be taken care of
72

73       // ejb reference (remote interface) : resolve handle to EJB
74
if (obj instanceof Handle JavaDoc)
75          resolved = ((Handle JavaDoc)obj).getEJBObject();
76       
77       // ejb reference (home interface) : resolve handle to EJB Home
78
else if (obj instanceof HomeHandle JavaDoc)
79          resolved = ((HomeHandle JavaDoc)obj).getEJBHome();
80       
81       // naming context: the jnp implementation of contexts is serializable, do nothing
82

83       else if( obj instanceof HandleWrapper )
84       {
85          HandleWrapper wrapper = (HandleWrapper) obj;
86          try
87          {
88             resolved = wrapper.get();
89          }
90          catch(ClassNotFoundException JavaDoc e)
91          {
92             throw new IOException JavaDoc("Failed to find class: "+e.getMessage());
93          }
94       }
95
96       else if (obj instanceof StatefulSessionBeanField)
97       {
98          byte type = ((StatefulSessionBeanField)obj).type;
99        
100          // session context: recreate it
101
if (type == StatefulSessionBeanField.SESSION_CONTEXT)
102             resolved = ctx.getSessionContext();
103
104          // user transaction: restore it
105
else if (type == StatefulSessionBeanField.USER_TRANSACTION)
106             resolved = ctx.getSessionContext().getUserTransaction();
107       }
108       return resolved;
109    }
110
111    /** Override the ObjectInputStream implementation to use the application class loader
112     */

113    protected Class JavaDoc resolveClass(ObjectStreamClass JavaDoc v) throws IOException JavaDoc, ClassNotFoundException JavaDoc
114    {
115       try
116       {
117          // use the application classloader to resolve the class
118
return appCl.loadClass(v.getName());
119          
120       } catch (ClassNotFoundException JavaDoc e) {
121          // we should probably never get here
122
return super.resolveClass(v);
123       }
124    }
125
126    /** Override the ObjectInputStream implementation to use the application class loader
127     */

128    protected Class JavaDoc resolveProxyClass(String JavaDoc[] interfaces) throws IOException JavaDoc, ClassNotFoundException JavaDoc
129    {
130        Class JavaDoc clazz = null;
131        Class JavaDoc[] ifaceClasses = new Class JavaDoc[interfaces.length];
132        for(int i = 0; i < interfaces.length; i ++)
133            ifaceClasses[i] = Class.forName(interfaces[i], false, appCl);
134        try
135        {
136            clazz = Proxy.getProxyClass(appCl, ifaceClasses);
137        }
138        catch(IllegalArgumentException JavaDoc e)
139        {
140            throw new ClassNotFoundException JavaDoc("Failed to resolve proxy class", e);
141        }
142        return clazz;
143    }
144
145    private static class EnableResolveObjectAction implements PrivilegedAction JavaDoc
146    {
147       SessionObjectInputStream is;
148       EnableResolveObjectAction(SessionObjectInputStream is)
149       {
150          this.is = is;
151       }
152       public Object JavaDoc run()
153       {
154          is.enableResolveObject(true);
155          return null;
156       }
157       static void enableResolveObject(SessionObjectInputStream is)
158       {
159          EnableResolveObjectAction action = new EnableResolveObjectAction(is);
160          AccessController.doPrivileged(action);
161       }
162    }
163 }
164
165
Popular Tags