KickJava   Java API By Example, From Geeks To Geeks.

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


1 // ========================================================================
2
// $Id: TypeCheckingInterceptor.java,v 1.4 2004/05/09 20:30:48 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.rmi.RemoteException JavaDoc;
21
22 import org.jfox.ioc.logger.Logger;
23
24 //----------------------------------------
25
// every time an attribute is added to the underlying HttpSession this
26
// interceptor ensures that the attribute type/value is one of :
27

28 // java.io.Serializable
29
// javax.ejb.EJBObject
30
// javax.ejb.EJBHome
31
// javax.transaction.UserTransaction - TODO - how/why ?
32
// javax.naming.Context - java:comp/env - we allow any Context
33

34 // TODO - How do we serialize Contexts and UserTransactions
35

36 public
37   class TypeCheckingInterceptor
38   extends StateInterceptor
39 {
40   protected static final Logger _log=Logger.getLogger(TypeCheckingInterceptor.class);
41
42   public Object JavaDoc
43     setAttribute(String JavaDoc name, Object JavaDoc value, boolean returnValue)
44     throws IllegalArgumentException JavaDoc, RemoteException JavaDoc
45   {
46     // SRV.7.7.2
47

48     Object JavaDoc tmp=value;
49
50     if (tmp!=null)
51     {
52       // The container may choose to support storage of other
53
// designated objects in the HttpSession, such as references
54
// to Enterprise JavaBean components and transactions.
55

56       if (tmp instanceof javax.ejb.EJBObject JavaDoc)
57     tmp=new SerializableEJBObject((javax.ejb.EJBObject JavaDoc)tmp);
58       else if (tmp instanceof javax.ejb.EJBHome JavaDoc)
59     tmp=new SerializableEJBHome((javax.ejb.EJBHome JavaDoc)tmp);
60       else if (tmp instanceof javax.naming.Context JavaDoc)
61     tmp=new SerializableContext((javax.naming.Context JavaDoc)tmp);
62       else if (tmp instanceof javax.transaction.UserTransaction JavaDoc)
63     tmp=new SerializableUserTransaction((javax.transaction.UserTransaction JavaDoc)tmp);
64     }
65
66     // The container must accept objects that implement the
67
// Serializable interface
68
if (tmp instanceof java.io.Serializable JavaDoc)
69     {
70       try
71       {
72     return super.setAttribute(name, tmp, returnValue);
73       }
74       catch (RemoteException JavaDoc e)
75       {
76     _log.error("could not set attribute", e);
77     return null;
78       }
79     }
80     else
81     {
82       // The servlet container may throw an IllegalArgumentException
83
// if an object is placed into the session that is not
84
// Serializable or for which specific support has not been made
85
// available.
86
throw new IllegalArgumentException JavaDoc("distributed attribute value must be Serializable,EJBObject,EJBHome,UserTransaction or Context: "+tmp);
87     }
88   }
89
90   public Object JavaDoc
91     getAttribute(String JavaDoc name)
92     throws IllegalArgumentException JavaDoc, RemoteException JavaDoc
93   {
94     Object JavaDoc tmp=super.getAttribute(name);
95
96     if (tmp!=null)
97     {
98       if (tmp instanceof org.mortbay.j2ee.session.SerializableEJBObject)
99     return ((SerializableEJBObject)tmp).toEJBObject();
100       else if (tmp instanceof org.mortbay.j2ee.session.SerializableEJBHome)
101     return ((SerializableEJBHome)tmp).toEJBHome();
102       else if (tmp instanceof org.mortbay.j2ee.session.SerializableContext)
103     return ((SerializableContext)tmp).toContext();
104       else if (tmp instanceof org.mortbay.j2ee.session.SerializableUserTransaction)
105     return ((SerializableUserTransaction)tmp).toUserTransaction();
106     }
107
108     return tmp;
109   }
110
111   // public Object clone() { return this; } // Stateless
112
}
113
Popular Tags