1 45 package org.openejb.client; 46 47 import java.io.Serializable ; 48 import java.util.Hashtable ; 49 import java.net.URI ; 50 import java.net.URISyntaxException ; 51 52 import javax.naming.ConfigurationException ; 53 import javax.naming.Context ; 54 import javax.naming.InvalidNameException ; 55 import javax.naming.Name ; 56 import javax.naming.NameNotFoundException ; 57 import javax.naming.NameParser ; 58 import javax.naming.NamingEnumeration ; 59 import javax.naming.NamingException ; 60 import javax.naming.spi.InitialContextFactory ; 61 67 public class JNDIContext implements Serializable , InitialContextFactory , Context , RequestMethods, ResponseCodes { 68 69 private transient String tail = "/"; 70 private transient ServerMetaData server; 71 private transient ClientMetaData client; 72 private transient Hashtable env; 73 74 85 JNDIContext(Hashtable environment) throws NamingException { 86 init( environment ); 87 } 88 89 public JNDIContext(){ 90 } 91 92 95 public JNDIContext(JNDIContext that){ 96 this.tail = that.tail; 97 this.server = that.server; 98 this.client = that.client; 99 this.env = (Hashtable )that.env.clone(); 100 } 101 102 113 public void init(Hashtable environment) throws NamingException { 114 } 115 116 117 private JNDIResponse request(JNDIRequest req) throws Exception { 118 return (JNDIResponse) Client.request(req, new JNDIResponse(), server); 119 } 120 121 public static void print(String s){ 122 } 124 public static void println(String s){ 125 } 127 128 protected AuthenticationResponse requestAuthorization(AuthenticationRequest req) throws java.rmi.RemoteException { 130 return (AuthenticationResponse) Client.request(req, new AuthenticationResponse(), server); 131 } 132 133 137 153 public Context getInitialContext(Hashtable environment) throws NamingException { 154 if ( environment == null ) 155 throw new NamingException ("Invalid Argument, hashtable cannot be null."); 156 else 157 env = (Hashtable )environment.clone(); 158 159 String userID = (String ) env.get(Context.SECURITY_PRINCIPAL); 160 String psswrd = (String ) env.get(Context.SECURITY_CREDENTIALS); 161 Object serverURI = env.get(Context.PROVIDER_URL); 162 163 if (serverURI == null) serverURI = "foo://localhost:4201"; 164 if (userID == null) userID = "anonymous"; 165 if (psswrd == null) psswrd = "anon"; 166 167 String uriString = (String ) serverURI; 168 URI location = null; 169 try { 170 location = new URI (uriString); 171 } catch (Exception e) { 172 if (uriString.indexOf("://") == -1) { 173 try { 174 location = new URI ("foo://"+uriString); 175 } catch (URISyntaxException giveUp) { 176 throw (ConfigurationException )new ConfigurationException ("Context property value error for "+Context.PROVIDER_URL + " :"+e.getMessage()).initCause(e); 178 } 179 } 180 } 181 this.server = new ServerMetaData(location); 182 authenticate(userID, psswrd); 185 186 return this; 187 } 188 189 190 public void authenticate(String userID, String psswrd) throws javax.naming.AuthenticationException { 191 AuthenticationRequest req = new AuthenticationRequest(userID, psswrd); 194 AuthenticationResponse res = null; 195 196 try { 197 res = requestAuthorization(req); 198 } catch (java.rmi.RemoteException e) { 199 throw new javax.naming.AuthenticationException (e.getLocalizedMessage()); 200 } 201 202 switch (res.getResponseCode()) { 203 case AUTH_GRANTED: 204 client = res.getIdentity(); 205 break; 206 case AUTH_REDIRECT: 207 client = res.getIdentity(); 208 server = res.getServer(); 209 break; 210 case AUTH_DENIED: 211 throw new javax.naming.AuthenticationException ("This principle is not authorized."); 212 } 213 } 214 215 public EJBHomeProxy createEJBHomeProxy(EJBMetaDataImpl ejbData){ 217 218 EJBHomeHandler handler = EJBHomeHandler.createEJBHomeHandler(ejbData, server, client); 219 EJBHomeProxy proxy = handler.createEJBHomeProxy(); 220 handler.ejb.ejbHomeProxy = proxy; 221 222 return proxy; 223 224 } 225 226 230 234 public Object lookup(String name) throws NamingException { 235 236 if ( name == null ) throw new InvalidNameException ("The name cannot be null"); 237 else if ( name.equals("") ) return new JNDIContext(this); 238 else if ( !name.startsWith("/") ) name = tail+name; 239 240 JNDIRequest req = new JNDIRequest(); 241 req.setRequestMethod( JNDIRequest.JNDI_LOOKUP ); 242 req.setRequestString( name ); 243 244 JNDIResponse res = null; 245 try{ 246 res = request(req); 247 } catch (Exception e){ 248 throw new javax.naming.NamingException ("Cannot lookup "+name+": Received error: "+e.getMessage()); 250 } 251 252 switch ( res.getResponseCode() ) { 253 case JNDI_EJBHOME: 254 return createEJBHomeProxy( (EJBMetaDataImpl)res.getResult() ); 256 257 case JNDI_OK: 258 return res.getResult(); 259 260 case JNDI_CONTEXT: 261 JNDIContext subCtx = new JNDIContext(this); 262 if (!name.endsWith("/")) name += '/'; 263 subCtx.tail = name; 264 return subCtx; 265 266 case JNDI_NOT_FOUND: 267 throw new NameNotFoundException (name + " not found"); 268 269 case JNDI_NAMING_EXCEPTION: 270 throw (NamingException ) res.getResult(); 271 272 case JNDI_RUNTIME_EXCEPTION: 273 throw (RuntimeException ) res.getResult(); 274 275 case JNDI_ERROR: 276 throw (Error ) res.getResult(); 277 default: 278 throw new RuntimeException ("Invalid response from server :"+res.getResponseCode()); 279 } 280 } 281 282 public Object lookup(Name name) throws NamingException { 283 return lookup(name.toString()); 284 } 285 286 public NamingEnumeration list(String name) throws NamingException { 287 throw new javax.naming.NamingException ("TODO: Needs to be implemented"); 288 } 289 290 public NamingEnumeration list(Name name) throws NamingException { 291 return list(name.toString()); 292 } 293 294 public NamingEnumeration listBindings(String name) throws NamingException { 295 throw new javax.naming.NamingException ("TODO: Needs to be implemented"); 296 } 297 298 public NamingEnumeration listBindings(Name name) throws NamingException { 299 return listBindings(name.toString()); 300 } 301 302 303 public Object lookupLink(String name) throws NamingException { 304 return lookup(name); 305 } 306 307 public Object lookupLink(Name name) throws NamingException { 308 return lookupLink(name.toString()); 309 } 310 311 public NameParser getNameParser(String name) throws NamingException { 312 throw new javax.naming.NamingException ("TODO: Needs to be implemented"); 313 } 314 315 public NameParser getNameParser(Name name) throws NamingException { 316 return getNameParser(name.toString()); 317 } 318 319 public String composeName(String name, String prefix) throws NamingException { 320 throw new javax.naming.NamingException ("TODO: Needs to be implemented"); 321 } 322 323 public Name composeName(Name name, Name prefix) throws NamingException { 324 throw new javax.naming.NamingException ("TODO: Needs to be implemented"); 325 } 326 327 public Object addToEnvironment(String key, Object value) throws NamingException { 328 return env.put(key, value); 329 } 330 331 public Object removeFromEnvironment(String key) throws NamingException { 332 return env.remove(key); 333 } 334 335 public Hashtable getEnvironment() throws NamingException { 336 return (Hashtable )env.clone(); 337 } 338 339 public String getNameInNamespace() throws NamingException { 340 return ""; 341 } 342 343 public void close() throws NamingException { 344 } 345 346 350 359 public void bind(String name, Object obj) throws NamingException { 360 throw new javax.naming.OperationNotSupportedException (); 361 } 362 363 372 public void bind(Name name, Object obj) throws NamingException { 373 bind(name.toString(), obj); 374 } 375 376 385 public void rebind(String name, Object obj) throws NamingException { 386 throw new javax.naming.OperationNotSupportedException (); 387 388 } 389 390 399 public void rebind(Name name, Object obj) throws NamingException { 400 rebind(name.toString(), obj); 401 } 402 403 411 public void unbind(String name) throws NamingException { 412 throw new javax.naming.OperationNotSupportedException (); 413 414 } 415 416 424 public void unbind(Name name) throws NamingException { 425 unbind(name.toString()); 426 } 427 428 437 public void rename(String oldname, String newname) 438 throws NamingException { 439 throw new javax.naming.OperationNotSupportedException (); 440 } 441 442 451 public void rename(Name oldname, Name newname) 452 throws NamingException { 453 rename(oldname.toString(), newname.toString()); 454 } 455 456 464 public void destroySubcontext(String name) throws NamingException { 465 throw new javax.naming.OperationNotSupportedException (); 466 } 467 468 476 public void destroySubcontext(Name name) throws NamingException { 477 destroySubcontext(name.toString()); 478 } 479 480 489 public Context createSubcontext(String name) 490 throws NamingException { 491 throw new javax.naming.OperationNotSupportedException (); 492 } 493 494 503 public Context createSubcontext(Name name) throws NamingException { 504 return createSubcontext(name.toString()); 505 } 506 507 } 511 512 513 | Popular Tags |