001: /*
002: * CoadunationLib: The coaduntion implementation library.
003: * Copyright (C) 2006 Rift IT Contracting
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
018: *
019: * ClassLoaderInterceptor.java
020: */
021:
022: // package path
023: package com.rift.coad.lib.interceptor.iiop;
024:
025: // java imports
026: import java.util.Stack;
027: import java.util.MissingResourceException;
028: import java.lang.ThreadLocal;
029: import org.omg.CORBA.TIMEOUT;
030: import org.omg.IOP.ServiceContext;
031: import org.omg.PortableInterceptor.ServerRequestInterceptor;
032: import org.omg.PortableInterceptor.ServerRequestInfo;
033: import org.omg.PortableInterceptor.ForwardRequest;
034: import org.omg.PortableInterceptor.ORBInitInfo;
035:
036: // logging import
037: import org.apache.log4j.Logger;
038:
039: // coadunation imports
040: import com.rift.coad.lib.common.ObjectSerializer;
041: import com.rift.coad.lib.naming.NamingDirector;
042: import com.rift.coad.lib.interceptor.credentials.Credential;
043: import com.rift.coad.lib.interceptor.InterceptorWrapper;
044: import com.rift.coad.lib.interceptor.ServerInterceptor;
045:
046: /**
047: * This class is responsible for setting the class loader correctly for the in
048: * bound thread.
049: *
050: * @author Brett Chaldecott
051: */
052: public class ClassLoaderInterceptor extends InterceptorWrapper
053: implements ServerRequestInterceptor {
054:
055: // the class log variable
056: protected static Logger log = Logger
057: .getLogger(ClassLoaderInterceptor.class.getName());
058:
059: // private member variables
060: private ThreadLocal loaderStack = new ThreadLocal();
061:
062: /** Creates a new instance of ClassLoaderInterceptor */
063: public ClassLoaderInterceptor(ORBInitInfo info) {
064:
065: }
066:
067: /**
068: * This method returns the name of this interceptor.
069: *
070: * @return A string containing the name of this interceptor.
071: */
072: public String name() {
073: return "ClassLoaderInterceptor";
074: }
075:
076: /**
077: * This method is called to distory this object.
078: */
079: public void destroy() {
080: // do nothing for time being
081: }
082:
083: /**
084: * Allows the interceptor to process service context information.
085: *
086: * @param ri The reference to the request information
087: */
088: public void receive_request_service_contexts(ServerRequestInfo ri) {
089:
090: }
091:
092: /**
093: * Allows an Interceptor to query request information after all the
094: * information, including operation parameters, are available.
095: */
096: public void receive_request(ServerRequestInfo ri) {
097: log.debug("receive request");
098: ClassLoader loader = Thread.currentThread()
099: .getContextClassLoader();
100: try {
101: loader = NamingDirector.getInstance().getPOA()
102: .id_to_servant(ri.object_id()).getClass()
103: .getClassLoader();
104: } catch (Exception ex) {
105: // ignore exception
106: }
107: Stack stack = getLoaderStack();
108: stack.push(Thread.currentThread().getContextClassLoader());
109: Thread.currentThread().setContextClassLoader(loader);
110: }
111:
112: /**
113: * Allows an Interceptor to query the exception information and modify the
114: * reply service context before the exception is thrown to the client.
115: */
116: public void send_exception(ServerRequestInfo ri) {
117: log.debug("send exception");
118: Stack stack = getLoaderStack();
119: Thread.currentThread().setContextClassLoader(
120: (ClassLoader) stack.pop());
121: }
122:
123: /**
124: * Allows an Interceptor to query the information available when a request
125: * results in something other than a normal reply or an exception.
126: */
127: public void send_other(ServerRequestInfo ri) {
128:
129: }
130:
131: /**
132: * Allows an Interceptor to query reply information and modify the reply
133: * service context after the target operation has been invoked and before
134: * the reply is returned to the client.
135: */
136: public void send_reply(ServerRequestInfo ri) {
137: log.debug("send reply");
138: Stack stack = getLoaderStack();
139: Thread.currentThread().setContextClassLoader(
140: (ClassLoader) stack.pop());
141: }
142:
143: /**
144: * This method returns the stack for the given thread.
145: */
146: private Stack getLoaderStack() {
147: Stack stack = (Stack) loaderStack.get();
148: if (stack == null) {
149: stack = new Stack();
150: loaderStack.set(stack);
151: }
152: return stack;
153: }
154: }
|