1 16 package org.mortbay.j2ee.session; 17 18 20 import java.io.ByteArrayInputStream ; 21 import java.io.ByteArrayOutputStream ; 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.io.ObjectOutputStream ; 25 import java.rmi.RemoteException ; 26 import java.util.HashMap ; 27 28 import org.jfox.ioc.logger.Logger; 29 30 32 38 43 public class MarshallingInterceptor 44 extends StateInterceptor 45 { 46 protected static final Logger _log=Logger.getLogger(MarshallingInterceptor.class); 47 48 static class ObjectInputStream 49 extends java.io.ObjectInputStream 50 { 51 ObjectInputStream(InputStream is) 52 throws IOException 53 { 54 super(is); 55 } 56 57 private static final HashMap _primClasses = new HashMap (8, 1.0F); 58 59 static 60 { 61 _primClasses.put("boolean" , boolean.class); 62 _primClasses.put("byte" , byte.class); 63 _primClasses.put("char" , char.class); 64 _primClasses.put("short" , short.class); 65 _primClasses.put("int" , int.class); 66 _primClasses.put("long" , long.class); 67 _primClasses.put("float" , float.class); 68 _primClasses.put("double" , double.class); 69 _primClasses.put("void" , void.class); 70 } 71 72 protected Class 74 resolveClass(java.io.ObjectStreamClass desc) 75 throws IOException , ClassNotFoundException 76 { 77 String name = desc.getName(); 78 try 79 { 80 return Class.forName(name, false, Thread.currentThread().getContextClassLoader()); 81 } 82 catch (ClassNotFoundException ex) 83 { 84 Class cl = (Class ) _primClasses.get(name); 85 if (cl != null) 86 return cl; 87 else 88 throw ex; 89 } 90 } 91 } 92 93 static public byte[] 94 marshal(Object value) 95 throws IOException 96 { 97 if (value==null) 98 return null; 99 100 ByteArrayOutputStream baos=new ByteArrayOutputStream (); 101 ObjectOutputStream oos =new ObjectOutputStream (baos); 102 oos.writeObject(value); 103 oos.flush(); 104 return baos.toByteArray(); 105 } 106 107 static public Object 108 demarshal(byte[] buffer) 109 throws IOException ,ClassNotFoundException 110 { 111 if (buffer==null) 112 return buffer; 113 114 ByteArrayInputStream bais=new ByteArrayInputStream (buffer); 115 ObjectInputStream ois =new ObjectInputStream(bais); 116 return ois.readObject(); 117 } 118 119 public Object 120 getAttribute(String name) 121 throws IllegalArgumentException , RemoteException 122 { 123 try 124 { 125 Object tmp=demarshal((byte[])super.getAttribute(name)); 126 129 return tmp; 130 } 131 catch (Exception e) 132 { 133 _log.error("could not get Attribute: "+name, e); 134 throw new IllegalArgumentException ("could not get Attribute"); 135 } 136 } 137 138 public Object 139 setAttribute(String name, Object value, boolean returnValue) 140 throws IllegalArgumentException 141 { 142 try 143 { 144 Object tmp=value; 145 if (tmp!=null) 146 { 147 tmp=marshal(tmp); 150 } 151 return demarshal((byte[])super.setAttribute(name, tmp, returnValue)); 152 } 153 catch (Exception e) 154 { 155 _log.error("could not set Attribute: "+name+":"+value, e); 156 throw new IllegalArgumentException ("could not set Attribute"); 157 } 158 } 159 160 public Object 162 removeAttribute(String name, boolean returnValue) 163 throws IllegalArgumentException 164 { 165 try 166 { 167 return demarshal((byte[])super.removeAttribute(name, returnValue)); 169 } 170 catch (Exception e) 171 { 172 _log.error("could not remove Attribute: "+name, e); 173 throw new IllegalArgumentException ("could not remove Attribute"); 174 } 175 } 176 177 } 179 | Popular Tags |