1 18 package org.apache.geronimo.interop.naming; 19 20 import java.util.HashMap ; 21 import javax.naming.NameNotFoundException ; 22 import javax.naming.NamingException ; 23 import javax.naming.Context ; 24 25 import org.apache.geronimo.interop.adapter.Adapter; 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 29 public class NamingContext { 30 31 private final Log log = LogFactory.getLog(NamingContext.class); 32 33 public static final NamingContext getInstance(Class baseClass) { 34 NamingContext context; 35 synchronized (contextMap) { 36 context = (NamingContext) contextMap.get(baseClass); 37 if (context == null) { 38 context = new NamingContext(); 39 contextMap.put(baseClass, context); 40 context.init(baseClass); 41 } 42 } 43 return context; 44 } 45 46 private static ThreadLocal current = new ThreadLocal (); 47 private static HashMap contextMap = new HashMap (); 48 private static boolean quiet = false; private static boolean verbose = true; private String logContext; 51 private HashMap map = new HashMap (); 52 private HashMap failedBindings = new HashMap (); 53 54 public static final NamingContext getCurrent() { 55 return (NamingContext) current.get(); 56 } 57 58 public static final NamingContext push(NamingContext that) { 59 NamingContext restore = getCurrent(); 60 current.set(that); 61 return restore; 62 } 63 64 public static void pop(NamingContext restore) { 65 current.set(restore); 66 } 67 68 public HashMap getMap() { 69 return map; 70 } 71 72 public Object lookup(String name, String prefix) throws NamingException { 73 74 log.debug( "NameContext.lookup(): name = " + name + ", prefix = " + prefix ); 75 76 if (prefix != null) { 77 name += prefix + "/" + name; 78 } 79 80 86 Object value = map.get(name); 87 88 if (value == null) { 89 value = dynamicLookup(name); 90 if (value != null) { 91 map.put(name, value); } 93 } 94 95 98 if (value == null) 99 { 100 value = tryBindCorbaName(name); 101 } 102 103 if (value == null) { 104 NameNotFoundException notFound = new NameNotFoundException (name.length() == 0 ? formatEmptyName() : name); 105 if (!quiet) { 106 NameServiceLog.getInstance().warnNameNotFound(logContext, notFound); 107 } 108 throw notFound; 109 } else { 110 return value; 111 } 112 } 113 114 public Object lookupReturnNullIfNotFound(String name, String prefix) { 115 if (prefix != null) { 116 name += prefix + "/" + name; 117 } 118 return map.get(name); 119 } 120 121 protected void init(Class baseClass) { 122 } 125 126 protected synchronized void bindAdapter(Adapter adp) { 127 String names[] = adp.getBindNames(); 128 for( int i=0; i<names.length; i++ ) { 129 log.debug( "NameContext.bindAdapter(): name[" + i + "] = " + names[i] + ", adp = " + adp ); 130 map.put(names[i], adp); 131 } 132 } 133 134 protected synchronized void unbindAdapter( Adapter adp ) { 135 String names[] = adp.getBindNames(); 136 for( int i=0; i<names.length; i++ ) 137 { 138 log.debug( "NameContext.bindAdapter(): name[" + i + "] = " + names[i] + ", adp = " + adp ); 139 map.remove( names[i] ); 140 } 141 } 142 143 protected boolean adapterExists(String name) { 144 System.out.println("TODO: NamingComponent.componentExists(): name = " + name); 145 return false; 146 } 147 148 153 protected Object bindCorbaName(String name, String value) 154 { 155 String url = value.substring("lookup=".length()); 156 157 167 168 java.util.Properties p = new java.util.Properties (); 169 170 173 p.put(Context.INITIAL_CONTEXT_FACTORY, "" ); p.put(Context.PROVIDER_URL, url); 175 176 Context initialContext = null; 177 Object object = null; 178 try 179 { 180 initialContext = new javax.naming.InitialContext (p); 181 object = initialContext.lookup(""); 182 } 183 catch (javax.naming.NamingException ne) 184 { 185 failedBindings(name, value); 186 NameServiceLog.getInstance().warnBindFailed(logContext, name, url, ne); 187 return null; 188 } 189 catch (java.lang.IllegalArgumentException ie) 190 { 191 NameServiceLog.getInstance().warnBindFailed(logContext, name, url, ie); 192 return null; 193 } 194 catch (Exception ex) 195 { 196 failedBindings(name, value); 197 NameServiceLog.getInstance().warnBindFailed(logContext, name, url, ex); 198 return null; 199 } 200 201 if (object == null) 202 { 203 NameServiceLog.getInstance().warnIllegalBindValue(logContext, Object .class, name, url); 204 return null; 205 } 206 207 map.put(name, object); 208 209 return object; 210 } 211 212 protected Object dynamicLookup(String name) { 213 return null; 214 } 215 216 protected String formatEmptyName() { 217 return "formatEmptyName:"; 218 } 219 220 private Object tryBindCorbaName(String name) 223 { 224 Object obj = null; 225 Object val = failedBindings.get(name); 226 if( val != null) 227 { 228 obj = bindCorbaName(name, (String )val); 229 } 230 return obj; 231 } 232 233 237 private void failedBindings(String name, String value) 238 { 239 Object val = failedBindings.get(name); 240 if( val == null) 241 { 242 failedBindings.put(name, value); 243 } 244 else 245 { 246 failedBindings.remove(name); 249 } 250 } 251 252 } 253 | Popular Tags |