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 package org.apache.xbean.naming.context; 018 019 import javax.naming.Context; 020 import javax.naming.Name; 021 import javax.naming.NamingException; 022 import javax.naming.NamingEnumeration; 023 import javax.naming.NameParser; 024 import java.util.Hashtable; 025 026 /** 027 * @version $Rev$ $Date$ 028 */ 029 public class VirtualSubcontext implements Context { 030 private final Name nameInContext; 031 private final Context context; 032 033 public VirtualSubcontext(Name nameInContext, Context context) throws NamingException { 034 if (context instanceof VirtualSubcontext) { 035 VirtualSubcontext virtualSubcontext = (VirtualSubcontext) context; 036 this.nameInContext = virtualSubcontext.getNameInContext(nameInContext); 037 this.context = virtualSubcontext.context; 038 } else { 039 this.nameInContext = nameInContext; 040 this.context = context; 041 } 042 } 043 044 private Name getNameInContext(Name name) throws NamingException { 045 return context.composeName(nameInContext, name); 046 } 047 048 private Name getNameInContext(String name) throws NamingException { 049 Name parsedName = context.getNameParser("").parse(name); 050 return context.composeName(nameInContext, parsedName); 051 } 052 053 public Object lookup(Name name) throws NamingException { 054 return context.lookup(getNameInContext(name)); 055 } 056 057 public Object lookup(String name) throws NamingException { 058 return context.lookup(getNameInContext(name)); 059 } 060 061 public void bind(Name name, Object obj) throws NamingException { 062 context.bind(getNameInContext(name), obj); 063 } 064 065 public void bind(String name, Object obj) throws NamingException { 066 context.bind(getNameInContext(name), obj); 067 } 068 069 public void rebind(Name name, Object obj) throws NamingException { 070 context.rebind(getNameInContext(name), obj); 071 } 072 073 public void rebind(String name, Object obj) throws NamingException { 074 context.rebind(getNameInContext(name), obj); 075 } 076 077 public void unbind(Name name) throws NamingException { 078 context.unbind(getNameInContext(name)); 079 } 080 081 public void unbind(String name) throws NamingException { 082 context.unbind(getNameInContext(name)); 083 } 084 085 public void rename(Name oldName, Name newName) throws NamingException { 086 context.rename(getNameInContext(oldName), getNameInContext(newName)); 087 } 088 089 public void rename(String oldName, String newName) throws NamingException { 090 context.rename(getNameInContext(oldName), getNameInContext(newName)); 091 } 092 093 public NamingEnumeration list(Name name) throws NamingException { 094 return context.list(getNameInContext(name)); 095 } 096 097 public NamingEnumeration list(String name) throws NamingException { 098 return context.list(getNameInContext(name)); 099 } 100 101 public NamingEnumeration listBindings(Name name) throws NamingException { 102 return context.listBindings(getNameInContext(name)); 103 } 104 105 public NamingEnumeration listBindings(String name) throws NamingException { 106 return context.listBindings(getNameInContext(name)); 107 } 108 109 public void destroySubcontext(Name name) throws NamingException { 110 context.destroySubcontext(getNameInContext(name)); 111 } 112 113 public void destroySubcontext(String name) throws NamingException { 114 context.destroySubcontext(getNameInContext(name)); 115 } 116 117 public Context createSubcontext(Name name) throws NamingException { 118 return context.createSubcontext(getNameInContext(name)); 119 } 120 121 public Context createSubcontext(String name) throws NamingException { 122 return context.createSubcontext(getNameInContext(name)); 123 } 124 125 public Object lookupLink(Name name) throws NamingException { 126 return context.lookupLink(getNameInContext(name)); 127 } 128 129 public Object lookupLink(String name) throws NamingException { 130 return context.lookupLink(getNameInContext(name)); 131 } 132 133 public NameParser getNameParser(Name name) throws NamingException { 134 return context.getNameParser(getNameInContext(name)); 135 } 136 137 public NameParser getNameParser(String name) throws NamingException { 138 return context.getNameParser(getNameInContext(name)); 139 } 140 141 public Name composeName(Name name, Name prefix) throws NamingException { 142 return context.composeName(name, prefix); 143 } 144 145 public String composeName(String name, String prefix) throws NamingException { 146 return context.composeName(name, prefix); 147 } 148 149 public Object addToEnvironment(String propName, Object propVal) throws NamingException { 150 return context.addToEnvironment(propName, propVal); 151 } 152 153 public Object removeFromEnvironment(String propName) throws NamingException { 154 return context.removeFromEnvironment(propName); 155 } 156 157 public Hashtable getEnvironment() throws NamingException { 158 return context.getEnvironment(); 159 } 160 161 public void close() throws NamingException { 162 context.close(); 163 } 164 165 public String getNameInNamespace() throws NamingException { 166 Name parsedNameInNamespace = context.getNameParser("").parse(context.getNameInNamespace()); 167 return context.composeName(parsedNameInNamespace, nameInContext).toString(); 168 } 169 }