KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > interceptor > iiop > ClassLoaderInterceptor


1 /*
2  * CoadunationLib: The coaduntion implementation library.
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * ClassLoaderInterceptor.java
20  */

21
22 // package path
23
package com.rift.coad.lib.interceptor.iiop;
24
25 // java imports
26
import java.util.Stack JavaDoc;
27 import java.util.MissingResourceException JavaDoc;
28 import java.lang.ThreadLocal JavaDoc;
29 import org.omg.CORBA.TIMEOUT JavaDoc;
30 import org.omg.IOP.ServiceContext JavaDoc;
31 import org.omg.PortableInterceptor.ServerRequestInterceptor JavaDoc;
32 import org.omg.PortableInterceptor.ServerRequestInfo JavaDoc;
33 import org.omg.PortableInterceptor.ForwardRequest JavaDoc;
34 import org.omg.PortableInterceptor.ORBInitInfo JavaDoc;
35
36 // logging import
37
import org.apache.log4j.Logger;
38
39 // coadunation imports
40
import com.rift.coad.lib.common.ObjectSerializer;
41 import com.rift.coad.lib.naming.NamingDirector;
42 import com.rift.coad.lib.interceptor.credentials.Credential;
43 import com.rift.coad.lib.interceptor.InterceptorWrapper;
44 import com.rift.coad.lib.interceptor.ServerInterceptor;
45
46
47 /**
48  * This class is responsible for setting the class loader correctly for the in
49  * bound thread.
50  *
51  * @author Brett Chaldecott
52  */

53 public class ClassLoaderInterceptor extends InterceptorWrapper implements
54         ServerRequestInterceptor JavaDoc {
55     
56     // the class log variable
57
protected static Logger log =
58             Logger.getLogger(ClassLoaderInterceptor.class.getName());
59     
60     // private member variables
61
private ThreadLocal JavaDoc loaderStack = new ThreadLocal JavaDoc();
62     
63     
64     /** Creates a new instance of ClassLoaderInterceptor */
65     public ClassLoaderInterceptor(ORBInitInfo JavaDoc info) {
66         
67     }
68     
69     
70     /**
71      * This method returns the name of this interceptor.
72      *
73      * @return A string containing the name of this interceptor.
74      */

75     public String JavaDoc name() {
76         return "ClassLoaderInterceptor";
77     }
78     
79     
80     /**
81      * This method is called to distory this object.
82      */

83     public void destroy() {
84         // do nothing for time being
85
}
86     
87     
88     /**
89      * Allows the interceptor to process service context information.
90      *
91      * @param ri The reference to the request information
92      */

93     public void receive_request_service_contexts(ServerRequestInfo JavaDoc ri) {
94         
95     }
96     
97     
98     /**
99      * Allows an Interceptor to query request information after all the
100      * information, including operation parameters, are available.
101      */

102     public void receive_request(ServerRequestInfo JavaDoc ri) {
103         log.debug("receive request");
104         ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
105         try {
106             loader = NamingDirector.getInstance().getPOA().id_to_servant(
107                     ri.object_id()).getClass().getClassLoader();
108         } catch (Exception JavaDoc ex) {
109             // ignore exception
110
}
111         Stack JavaDoc stack = getLoaderStack();
112         stack.push(Thread.currentThread().getContextClassLoader());
113         Thread.currentThread().setContextClassLoader(loader);
114     }
115     
116     
117     /**
118      * Allows an Interceptor to query the exception information and modify the
119      * reply service context before the exception is thrown to the client.
120      */

121     public void send_exception(ServerRequestInfo JavaDoc ri) {
122         log.debug("send exception");
123         Stack JavaDoc stack = getLoaderStack();
124         Thread.currentThread().setContextClassLoader((ClassLoader JavaDoc)stack.pop());
125     }
126     
127     
128     /**
129      * Allows an Interceptor to query the information available when a request
130      * results in something other than a normal reply or an exception.
131      */

132     public void send_other(ServerRequestInfo JavaDoc ri) {
133         
134     }
135     
136     
137     /**
138      * Allows an Interceptor to query reply information and modify the reply
139      * service context after the target operation has been invoked and before
140      * the reply is returned to the client.
141      */

142     public void send_reply(ServerRequestInfo JavaDoc ri) {
143         log.debug("send reply");
144         Stack JavaDoc stack = getLoaderStack();
145         Thread.currentThread().setContextClassLoader((ClassLoader JavaDoc)stack.pop());
146     }
147     
148     
149     /**
150      * This method returns the stack for the given thread.
151      */

152     private Stack JavaDoc getLoaderStack() {
153         Stack JavaDoc stack = (Stack JavaDoc)loaderStack.get();
154         if (stack == null) {
155             stack = new Stack JavaDoc();
156             loaderStack.set(stack);
157         }
158         return stack;
159     }
160 }
161
Popular Tags