1 50 package org.apache.avalon.excalibur.naming; 51 52 import java.util.Hashtable ; 53 54 import javax.naming.CompositeName ; 55 import javax.naming.Context ; 56 import javax.naming.InvalidNameException ; 57 import javax.naming.Name ; 58 import javax.naming.NameParser ; 59 import javax.naming.NamingEnumeration ; 60 import javax.naming.NamingException ; 61 import javax.naming.spi.ResolveResult ; 62 63 71 public abstract class AbstractURLContext 72 extends AbstractContext 73 implements NameParser 74 { 75 protected final String m_scheme; 76 77 public AbstractURLContext( final String scheme, final Hashtable environment ) 78 { 79 super( environment ); 80 m_scheme = scheme; 81 } 82 83 public Name parse( final String name ) 84 throws NamingException 85 { 86 return ( new CompositeName ().add( name ) ); 87 } 88 89 protected NameParser getNameParser() 90 throws NamingException 91 { 92 return this; 93 } 94 95 98 protected void bind( final Name name, final Object object, final boolean rebind ) 99 throws NamingException 100 { 101 final ResolveResult resolveResult = getBaseURLContext( name, getRawEnvironment() ); 102 final Context context = (Context )resolveResult.getResolvedObj(); 103 104 try 105 { 106 if( rebind ) 107 { 108 context.rebind( resolveResult.getRemainingName(), object ); 109 } 110 else 111 { 112 context.bind( resolveResult.getRemainingName(), object ); 113 } 114 } 115 finally 116 { 117 context.close(); 118 } 119 } 120 121 128 public Context createSubcontext( final Name name ) 129 throws NamingException 130 { 131 final ResolveResult resolveResult = getBaseURLContext( name, getRawEnvironment() ); 132 final Context context = (Context )resolveResult.getResolvedObj(); 133 134 try 135 { 136 return context.createSubcontext( resolveResult.getRemainingName() ); 137 } 138 finally 139 { 140 context.close(); 141 } 142 } 143 144 public void destroySubcontext( final Name name ) 145 throws NamingException 146 { 147 final ResolveResult resolveResult = getBaseURLContext( name, getRawEnvironment() ); 148 final Context context = (Context )resolveResult.getResolvedObj(); 149 150 try 151 { 152 context.destroySubcontext( resolveResult.getRemainingName() ); 153 } 154 finally 155 { 156 context.close(); 157 } 158 } 159 160 public String getNameInNamespace() 161 throws NamingException 162 { 163 return ""; 164 } 165 166 173 public NamingEnumeration list( final Name name ) 174 throws NamingException 175 { 176 final ResolveResult resolveResult = getBaseURLContext( name, getRawEnvironment() ); 177 final Context context = (Context )resolveResult.getResolvedObj(); 178 179 try 180 { 181 return context.list( resolveResult.getRemainingName() ); 182 } 183 finally 184 { 185 context.close(); 186 } 187 } 188 189 196 public NamingEnumeration listBindings( final Name name ) 197 throws NamingException 198 { 199 final ResolveResult resolveResult = getBaseURLContext( name, getRawEnvironment() ); 200 final Context context = (Context )resolveResult.getResolvedObj(); 201 202 try 203 { 204 return context.listBindings( resolveResult.getRemainingName() ); 205 } 206 finally 207 { 208 context.close(); 209 } 210 } 211 212 219 public Object lookup( final Name name ) 220 throws NamingException 221 { 222 final ResolveResult resolveResult = getBaseURLContext( name, getRawEnvironment() ); 223 final Context context = (Context )resolveResult.getResolvedObj(); 224 225 try 226 { 227 return context.lookup( resolveResult.getRemainingName() ); 228 } 229 finally 230 { 231 context.close(); 232 } 233 } 234 235 241 public void unbind( final Name name ) 242 throws NamingException 243 { 244 final ResolveResult resolveResult = getBaseURLContext( name, getRawEnvironment() ); 245 final Context context = (Context )resolveResult.getResolvedObj(); 246 247 try 248 { 249 context.unbind( resolveResult.getRemainingName() ); 250 } 251 finally 252 { 253 context.close(); 254 } 255 } 256 257 protected ResolveResult getBaseURLContext( final Name name, final Hashtable environment ) 258 throws NamingException 259 { 260 if( name.isEmpty() ) 261 { 262 throw new InvalidNameException ( "Unable to locate URLContext will empty name" ); 263 } 264 265 final String nameString = name.toString(); 266 int index = nameString.indexOf( ':' ); 267 268 if( -1 == index ) 269 { 270 throw new InvalidNameException ( "Unable to build URLContext as it does not specify scheme" ); 271 } 272 273 final String scheme = nameString.substring( 0, index ); 274 final int end = getEndIndexOfURLPart( nameString, index + 1 ); 275 final String urlPart = nameString.substring( index + 1, end ); 276 final String namePart = nameString.substring( end ); 277 278 if( !m_scheme.equals( scheme ) ) 279 { 280 throw new InvalidNameException ( "Bad Scheme use to build URLContext (" + scheme + "). " + 281 "Expected " + m_scheme ); 282 } 283 284 final Context context = newContext( urlPart ); 285 286 return new ResolveResult ( context, new CompositeName ( namePart ) ); 287 } 288 289 302 protected int getEndIndexOfURLPart( final String name, final int index ) 303 throws NamingException 304 { 305 int result = 0; 306 307 if( name.startsWith( "//", index ) ) 309 { 310 int end = name.indexOf( "/", index + 2 ); 312 313 if( -1 != end ) 314 { 315 result = end; 316 } 317 else 318 { 319 result = name.length(); 320 } 321 } 322 323 return result; 324 } 325 326 334 protected abstract Context newContext( String urlPart ) 335 throws NamingException ; 336 } 337 | Popular Tags |