KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > j2ee > session > MarshallingInterceptor


1 // ========================================================================
2
// $Id: MarshallingInterceptor.java,v 1.4 2004/05/09 20:30:47 gregwilkins Exp $
3
// Copyright 2002-2004 Mort Bay Consulting Pty. Ltd.
4
// ------------------------------------------------------------------------
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
15

16 package org.mortbay.j2ee.session;
17
18 //----------------------------------------
19

20 import java.io.ByteArrayInputStream JavaDoc;
21 import java.io.ByteArrayOutputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.ObjectOutputStream JavaDoc;
25 import java.rmi.RemoteException JavaDoc;
26 import java.util.HashMap JavaDoc;
27
28 import org.jfox.ioc.logger.Logger;
29
30 //----------------------------------------
31

32 // If we distributed instances via tiers that knew nothing about their
33
// type (EJB/DB) they would not be able to handle them, so we send
34
// them all as Strings. This involves marshalling them on the way out
35
// and demarshalling them on the way back. The relevant
36
// activation/passivation notifications are made during this process.
37

38 // I should probably separate Marshalling from ActivationNotifying in
39
// case someone knows that they are only using classes that will be
40
// present in the e.g. ejb tier and can avoid the marshalling
41
// overhead.
42

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 JavaDoc
50   {
51     ObjectInputStream(InputStream JavaDoc is)
52       throws IOException JavaDoc
53     {
54       super(is);
55     }
56
57     private static final HashMap JavaDoc _primClasses = new HashMap JavaDoc(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     // is this really necessary ?
73
protected Class JavaDoc
74       resolveClass(java.io.ObjectStreamClass JavaDoc desc)
75       throws IOException JavaDoc, ClassNotFoundException JavaDoc
76     {
77       String JavaDoc name = desc.getName();
78       try
79       {
80     return Class.forName(name, false, Thread.currentThread().getContextClassLoader());
81       }
82       catch (ClassNotFoundException JavaDoc ex)
83       {
84     Class JavaDoc cl = (Class JavaDoc) _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 JavaDoc value)
95     throws IOException JavaDoc
96   {
97     if (value==null)
98       return null;
99
100     ByteArrayOutputStream JavaDoc baos=new ByteArrayOutputStream JavaDoc();
101     ObjectOutputStream JavaDoc oos =new ObjectOutputStream JavaDoc(baos);
102     oos.writeObject(value);
103     oos.flush();
104     return baos.toByteArray();
105   }
106
107   static public Object JavaDoc
108     demarshal(byte[] buffer)
109     throws IOException JavaDoc,ClassNotFoundException JavaDoc
110   {
111     if (buffer==null)
112       return buffer;
113
114     ByteArrayInputStream JavaDoc bais=new ByteArrayInputStream JavaDoc(buffer);
115     ObjectInputStream ois =new ObjectInputStream(bais);
116     return ois.readObject();
117   }
118
119   public Object JavaDoc
120     getAttribute(String JavaDoc name)
121     throws IllegalArgumentException JavaDoc, RemoteException JavaDoc
122   {
123     try
124     {
125       Object JavaDoc tmp=demarshal((byte[])super.getAttribute(name));
126 // if (tmp!=null && tmp instanceof HttpSessionActivationListener)
127
// ((HttpSessionActivationListener)tmp).sessionDidActivate(new HttpSessionEvent(_session));
128

129       return tmp;
130     }
131     catch (Exception JavaDoc e)
132     {
133       _log.error("could not get Attribute: "+name, e);
134       throw new IllegalArgumentException JavaDoc("could not get Attribute");
135     }
136   }
137
138   public Object JavaDoc
139     setAttribute(String JavaDoc name, Object JavaDoc value, boolean returnValue)
140     throws IllegalArgumentException JavaDoc
141   {
142     try
143     {
144       Object JavaDoc tmp=value;
145       if (tmp!=null)
146       {
147 // if (tmp instanceof HttpSessionActivationListener)
148
// ((HttpSessionActivationListener)tmp).sessionWillPassivate(new HttpSessionEvent(_session));
149
tmp=marshal(tmp);
150       }
151       return demarshal((byte[])super.setAttribute(name, tmp, returnValue));
152     }
153     catch (Exception JavaDoc e)
154     {
155       _log.error("could not set Attribute: "+name+":"+value, e);
156       throw new IllegalArgumentException JavaDoc("could not set Attribute");
157     }
158   }
159
160   // should an attribute be activated before it is removed ? How do we deal with the bind/unbind events... - TODO
161
public Object JavaDoc
162     removeAttribute(String JavaDoc name, boolean returnValue)
163     throws IllegalArgumentException JavaDoc
164   {
165     try
166     {
167       // should this be activated - probably
168
return demarshal((byte[])super.removeAttribute(name, returnValue));
169     }
170     catch (Exception JavaDoc e)
171     {
172       _log.error("could not remove Attribute: "+name, e);
173       throw new IllegalArgumentException JavaDoc("could not remove Attribute");
174     }
175   }
176
177   // public Object clone() { return this; } // Stateless
178
}
179
Popular Tags