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 */
017package org.apache.xbean.naming.context;
018
019import javax.naming.Context;
020import javax.naming.Name;
021import javax.naming.NamingException;
022import javax.naming.NamingEnumeration;
023import javax.naming.NameParser;
024import javax.naming.Binding;
025import javax.naming.NameClassPair;
026
027import java.util.Hashtable;
028
029/**
030 * @version $Rev$ $Date$
031 */
032public class VirtualSubcontext extends ContextFlyweight {
033    private final Name nameInContext;
034    private final Context context;
035
036    public VirtualSubcontext(Name nameInContext, Context context) throws NamingException {
037        if (context instanceof VirtualSubcontext) {
038            VirtualSubcontext virtualSubcontext = (VirtualSubcontext) context;
039            this.nameInContext = virtualSubcontext.getName(nameInContext);
040            this.context = virtualSubcontext.context;
041        } else {
042            this.nameInContext = nameInContext;
043            this.context = context;
044        }
045    }
046
047    @Override
048    protected Context getContext() throws NamingException {
049        return context;
050    }
051
052    @Override
053    protected Name getName(Name name) throws NamingException {
054        return context.composeName(nameInContext, name);
055    }
056
057    @Override
058    protected String getName(String name) throws NamingException {
059        Name parsedName = context.getNameParser("").parse(name);
060        return context.composeName(nameInContext, parsedName).toString();
061    }
062
063    @Override
064    public boolean equals(Object o) {
065        if (this == o) return true;
066        if (o == null || getClass() != o.getClass()) return false;
067
068        VirtualSubcontext that = (VirtualSubcontext) o;
069
070        if (context != null ? !context.equals(that.context) : that.context != null) return false;
071        if (nameInContext != null ? !nameInContext.equals(that.nameInContext) : that.nameInContext != null) return false;
072
073        return true;
074    }
075
076    @Override
077    public int hashCode() {
078        int result = nameInContext != null ? nameInContext.hashCode() : 0;
079        result = 31 * result + (context != null ? context.hashCode() : 0);
080        return result;
081    }
082
083    public void close() throws NamingException {
084        context.close();
085    }
086
087    public String getNameInNamespace() throws NamingException {
088        Name parsedNameInNamespace = context.getNameParser("").parse(context.getNameInNamespace());
089        return context.composeName(parsedNameInNamespace, nameInContext).toString();
090    }
091}