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    import java.io.ObjectStreamException;
020    import java.io.Serializable;
021    import java.net.URI;
022    
023    import org.apache.geronimo.interceptor.Invocation;
024    import org.apache.geronimo.interceptor.InvocationKey;
025    
026    /**
027     * @version $Rev: 487175 $ $Date: 2006-12-14 03:10:31 -0800 (Thu, 14 Dec 2006) $
028     */
029    public final class InvocationSupport implements Serializable, InvocationKey {
030    
031        private static final long serialVersionUID = 3690191053600796981L;
032    
033        // Be careful here.  If you change the ordinals, this class must be changed on evey client.
034        private static int MAX_ORDINAL = 2;
035        private static final InvocationSupport[] values = new InvocationSupport[MAX_ORDINAL + 1];
036        private static final InvocationSupport REMOTE_URI = new InvocationSupport("REMOTE_URI", 0);
037        private static final InvocationSupport INVOCATION_TYPE = new InvocationSupport("INVOCATION_TYPE", 1);
038    
039        public static URI getRemoteURI(Invocation invocation) {
040            return (URI) invocation.get(REMOTE_URI);
041        }
042        public static void putRemoteURI(Invocation invocation, URI remoteURI) {
043            invocation.put(REMOTE_URI, remoteURI);
044        }
045        public static InvocationType getInvocationType(Invocation invocation) {
046            return (InvocationType) invocation.get(INVOCATION_TYPE);
047        }
048        public static void putInvocationType(Invocation invocation, InvocationType type) {
049            invocation.put(INVOCATION_TYPE, type);
050        }
051    
052        private final transient String name;
053        private final int ordinal;
054    
055        private InvocationSupport(String name, int ordinal) {
056            assert ordinal < MAX_ORDINAL;
057            assert values[ordinal] == null;
058            this.name = name;
059            this.ordinal = ordinal;
060            values[ordinal] = this;
061        }
062    
063        public String toString() {
064            return name;
065        }
066    
067        Object readResolve() throws ObjectStreamException {
068            return values[ordinal];
069        }
070    
071        static public boolean isAncestor(ClassLoader parent, ClassLoader child) {
072            // Root child? ancestor must be root too.
073            if (child == null)
074                return parent == null;
075            // Root parent is the ancestor of all classloaders.
076            if (parent == null)
077                return true;
078    
079            while (child != null) {
080                if (child.equals(parent))
081                    return true;
082                child = child.getParent();
083            }
084            return false;
085        }
086    
087        /**
088         * @see org.apache.geronimo.interceptor.InvocationKey#isTransient()
089         */
090        public boolean isTransient() {
091            return true;
092        }
093    
094    }