1 8 package org.codehaus.spice.jndikit; 9 10 import java.util.Hashtable ; 11 import javax.naming.CompositeName ; 12 import javax.naming.Context ; 13 import javax.naming.InvalidNameException ; 14 import javax.naming.Name ; 15 import javax.naming.NameParser ; 16 import javax.naming.NamingEnumeration ; 17 import javax.naming.NamingException ; 18 import javax.naming.spi.ResolveResult ; 19 20 27 public abstract class AbstractURLContext 28 extends AbstractContext 29 implements NameParser 30 { 31 private final String m_scheme; 32 33 public AbstractURLContext( final String scheme, final Hashtable environment ) 34 { 35 super( environment ); 36 m_scheme = scheme; 37 } 38 39 public Name parse( final String name ) 40 throws NamingException 41 { 42 return ( new CompositeName ().add( name ) ); 43 } 44 45 protected NameParser getNameParser() 46 throws NamingException 47 { 48 return this; 49 } 50 51 54 protected void bind( final Name name, final Object object, final boolean rebind ) 55 throws NamingException 56 { 57 final ResolveResult resolveResult = getBaseURLContext( name, getRawEnvironment() ); 58 final Context context = (Context )resolveResult.getResolvedObj(); 59 60 try 61 { 62 if( rebind ) 63 { 64 context.rebind( resolveResult.getRemainingName(), object ); 65 } 66 else 67 { 68 context.bind( resolveResult.getRemainingName(), object ); 69 } 70 } 71 finally 72 { 73 context.close(); 74 } 75 } 76 77 85 public Context createSubcontext( final Name name ) 86 throws NamingException 87 { 88 final ResolveResult resolveResult = getBaseURLContext( name, getRawEnvironment() ); 89 final Context context = (Context )resolveResult.getResolvedObj(); 90 91 try 92 { 93 return context.createSubcontext( resolveResult.getRemainingName() ); 94 } 95 finally 96 { 97 context.close(); 98 } 99 } 100 101 public void destroySubcontext( final Name name ) 102 throws NamingException 103 { 104 final ResolveResult resolveResult = getBaseURLContext( name, getRawEnvironment() ); 105 final Context context = (Context )resolveResult.getResolvedObj(); 106 107 try 108 { 109 context.destroySubcontext( resolveResult.getRemainingName() ); 110 } 111 finally 112 { 113 context.close(); 114 } 115 } 116 117 public String getNameInNamespace() 118 throws NamingException 119 { 120 return ""; 121 } 122 123 130 public NamingEnumeration list( final Name name ) 131 throws NamingException 132 { 133 final ResolveResult resolveResult = getBaseURLContext( name, getRawEnvironment() ); 134 final Context context = (Context )resolveResult.getResolvedObj(); 135 136 try 137 { 138 return context.list( resolveResult.getRemainingName() ); 139 } 140 finally 141 { 142 context.close(); 143 } 144 } 145 146 153 public NamingEnumeration listBindings( final Name name ) 154 throws NamingException 155 { 156 final ResolveResult resolveResult = getBaseURLContext( name, getRawEnvironment() ); 157 final Context context = (Context )resolveResult.getResolvedObj(); 158 159 try 160 { 161 return context.listBindings( resolveResult.getRemainingName() ); 162 } 163 finally 164 { 165 context.close(); 166 } 167 } 168 169 177 public Object lookup( final Name name ) 178 throws NamingException 179 { 180 final ResolveResult resolveResult = getBaseURLContext( name, getRawEnvironment() ); 181 final Context context = (Context )resolveResult.getResolvedObj(); 182 183 try 184 { 185 return context.lookup( resolveResult.getRemainingName() ); 186 } 187 finally 188 { 189 context.close(); 190 } 191 } 192 193 199 public void unbind( final Name name ) 200 throws NamingException 201 { 202 final ResolveResult resolveResult = getBaseURLContext( name, getRawEnvironment() ); 203 final Context context = (Context )resolveResult.getResolvedObj(); 204 205 try 206 { 207 context.unbind( resolveResult.getRemainingName() ); 208 } 209 finally 210 { 211 context.close(); 212 } 213 } 214 215 protected ResolveResult getBaseURLContext( final Name name, final Hashtable environment ) 216 throws NamingException 217 { 218 if( name.isEmpty() ) 219 { 220 throw new InvalidNameException ( "Unable to locate URLContext will empty name" ); 221 } 222 223 final String nameString = name.get(0).toString(); 224 int index = nameString.indexOf( ':' ); 225 226 if( -1 == index ) 227 { 228 final String explanation = 229 "Unable to build URLContext as it does not specify scheme"; 230 throw new InvalidNameException ( explanation ); 231 } 232 233 final String scheme = nameString.substring( 0, index ); 234 final int end = getEndIndexOfURLPart( nameString, index + 1 ); 235 final String urlPart = nameString.substring( index + 1, end ); 236 final String namePart = nameString.substring( end ); 237 238 if( !m_scheme.equals( scheme ) ) 239 { 240 final String explanation = 241 "Bad Scheme use to build URLContext (" + 242 scheme + "). " + "Expected " + m_scheme; 243 throw new InvalidNameException ( explanation ); 244 } 245 246 final Context context = newContext( urlPart ); 247 248 return new ResolveResult ( context, new CompositeName ( namePart ) ); 249 } 250 251 264 protected int getEndIndexOfURLPart( final String name, final int index ) 265 throws NamingException 266 { 267 int result = 0; 268 269 if( name.startsWith( "//", index ) ) 271 { 272 int end = name.indexOf( "/", index + 2 ); 274 275 if( -1 != end ) 276 { 277 result = end; 278 } 279 else 280 { 281 result = name.length(); 282 } 283 } 284 285 return result; 286 } 287 288 296 protected abstract Context newContext( String urlPart ) 297 throws NamingException ; 298 } 299 | Popular Tags |