1 /**
2 *
3 * Copyright 2003-2004 The Apache Software Foundation
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 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.geronimo.security.remoting.jmx;
19
20 import java.io.IOException;
21 import java.io.Serializable;
22
23 import org.activeio.Packet;
24 import org.activeio.RequestListener;
25 import org.activeio.packet.EmptyPacket;
26
27 import org.apache.geronimo.interceptor.Interceptor;
28 import org.apache.geronimo.interceptor.Invocation;
29 import org.apache.geronimo.interceptor.InvocationResult;
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32
33 /**
34 * @version $Rev: 437623 $ $Date: 2006-08-28 02:48:23 -0700 (Mon, 28 Aug 2006) $
35 */
36 public class RequestChannelInterceptorInvoker implements RequestListener {
37
38 private static final Log log = LogFactory.getLog(RequestChannelInterceptorInvoker.class);
39
40 private ClassLoader classloader;
41 private Interceptor next;
42
43 public RequestChannelInterceptorInvoker(Interceptor next, ClassLoader classloader) {
44 this.next = next;
45 this.classloader = classloader;
46 }
47
48 public static class ThrowableWrapper implements Serializable {
49 private static final long serialVersionUID = 3905243428970182455L;
50 ThrowableWrapper(Throwable exception) {
51 this.exception = exception;
52 }
53 public Throwable exception;
54 }
55
56 public ClassLoader getClassloader() {
57 return classloader;
58 }
59
60 public void setClassloader(ClassLoader classloader) {
61 this.classloader = classloader;
62 }
63
64 public Packet onRequest(Packet request) {
65 Thread currentThread = Thread.currentThread();
66 ClassLoader orig = currentThread.getContextClassLoader();
67 try {
68
69 Invocation marshalledInvocation;
70
71 try {
72 currentThread.setContextClassLoader(classloader);
73 marshalledInvocation = (Invocation) RequestChannelInterceptor.deserialize(request,classloader);
74 } catch (Throwable e) {
75
76 log.error("Could not deserialize the invocation", e);
77 return RequestChannelInterceptor.serialize(new ThrowableWrapper(e));
78 }
79
80 try {
81 InvocationResult rc = next.invoke(marshalledInvocation);
82 return RequestChannelInterceptor.serialize(rc);
83 } catch (Throwable e) {
84 return RequestChannelInterceptor.serialize(new ThrowableWrapper(e));
85 }
86
87
88 } catch (IOException e) {
89
90 return EmptyPacket.EMPTY_PACKET;
91 } finally {
92 currentThread.setContextClassLoader(orig);
93 }
94 }
95
96 public void onRquestError(IOException error) {
97 log.error("Request Error", error);
98 }
99
100 }