1 22 package org.jboss.ejb.plugins; 23 24 import java.io.InputStream ; 25 import java.io.ObjectInputStream ; 26 import java.io.ObjectStreamClass ; 27 import java.io.IOException ; 28 import java.lang.reflect.Proxy ; 29 import java.security.PrivilegedAction ; 30 import java.security.AccessController ; 31 32 import javax.ejb.Handle ; 33 import javax.ejb.HomeHandle ; 34 35 import org.jboss.ejb.StatefulSessionEnterpriseContext; 36 37 46 public class SessionObjectInputStream 47 extends ObjectInputStream 48 { 49 private StatefulSessionEnterpriseContext ctx; 50 private ClassLoader appCl; 51 52 public SessionObjectInputStream(StatefulSessionEnterpriseContext ctx, InputStream in) 54 throws IOException 55 { 56 super(in); 57 EnableResolveObjectAction.enableResolveObject(this); 58 59 this.ctx = ctx; 60 61 appCl = SecurityActions.getContextClassLoader(); 63 } 64 65 protected Object resolveObject(Object obj) 67 throws IOException 68 { 69 Object resolved = obj; 70 71 73 if (obj instanceof Handle ) 75 resolved = ((Handle )obj).getEJBObject(); 76 77 else if (obj instanceof HomeHandle ) 79 resolved = ((HomeHandle )obj).getEJBHome(); 80 81 83 else if( obj instanceof HandleWrapper ) 84 { 85 HandleWrapper wrapper = (HandleWrapper) obj; 86 try 87 { 88 resolved = wrapper.get(); 89 } 90 catch(ClassNotFoundException e) 91 { 92 throw new IOException ("Failed to find class: "+e.getMessage()); 93 } 94 } 95 96 else if (obj instanceof StatefulSessionBeanField) 97 { 98 byte type = ((StatefulSessionBeanField)obj).type; 99 100 if (type == StatefulSessionBeanField.SESSION_CONTEXT) 102 resolved = ctx.getSessionContext(); 103 104 else if (type == StatefulSessionBeanField.USER_TRANSACTION) 106 resolved = ctx.getSessionContext().getUserTransaction(); 107 } 108 return resolved; 109 } 110 111 113 protected Class resolveClass(ObjectStreamClass v) throws IOException , ClassNotFoundException 114 { 115 try 116 { 117 return appCl.loadClass(v.getName()); 119 120 } catch (ClassNotFoundException e) { 121 return super.resolveClass(v); 123 } 124 } 125 126 128 protected Class resolveProxyClass(String [] interfaces) throws IOException , ClassNotFoundException 129 { 130 Class clazz = null; 131 Class [] ifaceClasses = new Class [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 e) 139 { 140 throw new ClassNotFoundException ("Failed to resolve proxy class", e); 141 } 142 return clazz; 143 } 144 145 private static class EnableResolveObjectAction implements PrivilegedAction 146 { 147 SessionObjectInputStream is; 148 EnableResolveObjectAction(SessionObjectInputStream is) 149 { 150 this.is = is; 151 } 152 public Object 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 |