1 15 package org.apache.hivemind.lib.impl; 16 17 import java.util.Hashtable ; 18 19 import javax.naming.Context ; 20 import javax.naming.InitialContext ; 21 import javax.naming.NamingException ; 22 23 import org.apache.hivemind.ApplicationRuntimeException; 24 import org.apache.hivemind.HiveMind; 25 import org.apache.hivemind.lib.NameLookup; 26 import org.apache.hivemind.lib.RemoteExceptionCoordinator; 27 import org.apache.hivemind.lib.RemoteExceptionEvent; 28 import org.apache.hivemind.lib.RemoteExceptionListener; 29 30 36 public class NameLookupImpl implements NameLookup, RemoteExceptionListener 37 { 38 private RemoteExceptionCoordinator _coordinator; 39 private Context _initialContext; 40 private String _initialFactory; 41 private String _URLPackages; 42 private String _providerURL; 43 44 public Object lookup(String name, Class expected) 45 { 46 int i = 0; 47 48 while (true) 49 { 50 Context context = null; 51 Object raw = null; 52 53 try 54 { 55 context = getInitialContext(); 56 57 raw = context.lookup(name); 58 } 59 catch (NamingException ex) 60 { 61 if (i++ == 0) 62 _coordinator.fireRemoteExceptionDidOccur(this, ex); 63 else 64 throw new ApplicationRuntimeException( 65 ImplMessages.unableToLookup(name, context), 66 ex); 67 continue; 68 } 69 70 if (raw == null) 71 throw new ApplicationRuntimeException(ImplMessages.noObject(name, expected)); 72 73 if (!expected.isAssignableFrom(raw.getClass())) 74 throw new ApplicationRuntimeException(ImplMessages.wrongType(name, raw, expected)); 75 76 return raw; 77 } 78 } 79 80 private Context getInitialContext() throws NamingException 81 { 82 if (_initialContext == null) 83 { 84 85 Hashtable properties = new Hashtable (); 86 87 if (!HiveMind.isBlank(_initialFactory)) 88 properties.put(Context.INITIAL_CONTEXT_FACTORY, _initialFactory); 89 90 if (!HiveMind.isBlank(_providerURL)) 91 properties.put(Context.PROVIDER_URL, _providerURL); 92 93 if (!HiveMind.isBlank(_URLPackages)) 94 properties.put(Context.URL_PKG_PREFIXES, _URLPackages); 95 96 _initialContext = constructContext(properties); 97 } 98 99 return _initialContext; 100 } 101 102 106 protected Context constructContext(Hashtable properties) throws NamingException 107 { 108 return new InitialContext (properties); 109 } 110 111 114 public void remoteExceptionDidOccur(RemoteExceptionEvent event) 115 { 116 _initialContext = null; 117 } 118 119 123 public void setInitialFactory(String string) 124 { 125 _initialFactory = string; 126 } 127 128 132 public void setProviderURL(String string) 133 { 134 _providerURL = string; 135 } 136 137 142 143 public void setURLPackages(String string) 144 { 145 _URLPackages = string; 146 } 147 148 public void setCoordinator(RemoteExceptionCoordinator coordinator) 149 { 150 _coordinator = coordinator; 151 } 152 153 } 154 | Popular Tags |