001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    
018    package org.apache.geronimo.security.remoting.jmx;
019    
020    import java.io.IOException;
021    import java.io.Serializable;
022    
023    import org.activeio.Packet;
024    import org.activeio.RequestListener;
025    import org.activeio.packet.EmptyPacket;
026    
027    import org.apache.geronimo.interceptor.Interceptor;
028    import org.apache.geronimo.interceptor.Invocation;
029    import org.apache.geronimo.interceptor.InvocationResult;
030    import org.apache.commons.logging.Log;
031    import org.apache.commons.logging.LogFactory;
032    
033    /**
034     * @version $Rev: 487175 $ $Date: 2006-12-14 03:10:31 -0800 (Thu, 14 Dec 2006) $
035     */
036    public class RequestChannelInterceptorInvoker implements RequestListener {
037    
038        private static final Log log = LogFactory.getLog(RequestChannelInterceptorInvoker.class);
039    
040        private ClassLoader classloader;
041        private Interceptor next;
042    
043        public RequestChannelInterceptorInvoker(Interceptor next, ClassLoader classloader) {
044            this.next = next;
045            this.classloader = classloader;
046        }
047    
048        public static class ThrowableWrapper implements Serializable {
049            private static final long serialVersionUID = 3905243428970182455L;
050            ThrowableWrapper(Throwable exception) {
051                this.exception = exception;
052            }
053            public Throwable exception;
054        }
055    
056        public ClassLoader getClassloader() {
057            return classloader;
058        }
059    
060        public void setClassloader(ClassLoader classloader) {
061            this.classloader = classloader;
062        }
063    
064        public Packet onRequest(Packet request) {
065            Thread currentThread = Thread.currentThread();
066            ClassLoader orig = currentThread.getContextClassLoader();
067            try {
068    
069                Invocation marshalledInvocation;
070    
071                try {
072                    currentThread.setContextClassLoader(classloader);
073                    marshalledInvocation = (Invocation) RequestChannelInterceptor.deserialize(request,classloader);
074                } catch (Throwable e) {
075                    // Could not deserialize the invocation...
076                    log.error("Could not deserialize the invocation", e);
077                    return RequestChannelInterceptor.serialize(new ThrowableWrapper(e));
078                }
079    
080                try {
081                    InvocationResult rc = next.invoke(marshalledInvocation);
082                    return RequestChannelInterceptor.serialize(rc);
083                } catch (Throwable e) {
084                    return RequestChannelInterceptor.serialize(new ThrowableWrapper(e));
085                }
086    
087    
088            } catch (IOException e) {
089                // TODO: handle this.
090                return EmptyPacket.EMPTY_PACKET;
091            } finally {
092                currentThread.setContextClassLoader(orig);
093            }
094        }
095    
096        public void onRquestError(IOException error) {
097            log.error("Request Error", error);
098        }
099    
100    }