001    /**
002     *
003     * Copyright 2003-2004 The Apache Software Foundation
004     *
005     *  Licensed under the Apache License, Version 2.0 (the "License");
006     *  you may not use this file except in compliance with the License.
007     *  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.naming.java;
019    
020    import javax.naming.Context;
021    
022    import org.apache.geronimo.interceptor.Interceptor;
023    import org.apache.geronimo.interceptor.Invocation;
024    import org.apache.geronimo.interceptor.InvocationResult;
025    
026    /**
027     * An interceptor that pushes the current component's java:comp context into
028     * the java: JNDI namespace
029     *
030     * @version $Rev: 417891 $ $Date: 2006-06-28 15:45:07 -0700 (Wed, 28 Jun 2006) $
031     */
032    public class ComponentContextInterceptor implements Interceptor {
033        private final Interceptor next;
034        private final Context compContext;
035    
036        /**
037         * Constructor specifying the components JNDI Context (java:comp)
038         * @param compContext the component's JNDI Context
039         */
040        public ComponentContextInterceptor(Interceptor next, Context compContext) {
041            assert next != null;
042            assert compContext != null;
043            this.next = next;
044            this.compContext = compContext;
045        }
046    
047        public InvocationResult invoke(Invocation invocation) throws Throwable {
048            Context oldContext = RootContext.getComponentContext();
049            try {
050                RootContext.setComponentContext(compContext);
051                return next.invoke(invocation);
052            } finally {
053                RootContext.setComponentContext(oldContext);
054            }
055        }
056    }