1 22 package org.jboss.util.naming; 23 24 import javax.naming.Context ; 25 import javax.naming.InitialContext ; 26 import javax.naming.LinkRef ; 27 import javax.naming.Name ; 28 import javax.naming.NameNotFoundException ; 29 import javax.naming.NamingException ; 30 31 import org.jboss.logging.Logger; 32 33 39 public class Util 40 { 41 private static final Logger log = Logger.getLogger(Util.class); 42 43 49 public static Context createSubcontext(Context ctx, String name) throws NamingException 50 { 51 Name n = ctx.getNameParser("").parse(name); 52 return createSubcontext(ctx, n); 53 } 54 55 61 public static Context createSubcontext(Context ctx, Name name) throws NamingException 62 { 63 Context subctx = ctx; 64 for (int pos = 0; pos < name.size(); pos++) 65 { 66 String ctxName = name.get(pos); 67 try 68 { 69 subctx = (Context ) ctx.lookup(ctxName); 70 } 71 catch (NameNotFoundException e) 72 { 73 subctx = ctx.createSubcontext(ctxName); 74 } 75 ctx = subctx; 77 } 78 return subctx; 79 } 80 81 86 public static void bind(Context ctx, String name, Object value) throws NamingException 87 { 88 Name n = ctx.getNameParser("").parse(name); 89 bind(ctx, n, value); 90 } 91 92 97 public static void bind(Context ctx, Name name, Object value) throws NamingException 98 { 99 int size = name.size(); 100 String atom = name.get(size - 1); 101 Context parentCtx = createSubcontext(ctx, name.getPrefix(size - 1)); 102 parentCtx.bind(atom, value); 103 } 104 105 110 public static void rebind(Context ctx, String name, Object value) throws NamingException 111 { 112 Name n = ctx.getNameParser("").parse(name); 113 rebind(ctx, n, value); 114 } 115 116 121 public static void rebind(Context ctx, Name name, Object value) throws NamingException 122 { 123 int size = name.size(); 124 String atom = name.get(size - 1); 125 Context parentCtx = createSubcontext(ctx, name.getPrefix(size - 1)); 126 parentCtx.rebind(atom, value); 127 } 128 129 133 public static void unbind(Context ctx, String name) throws NamingException 134 { 135 unbind(ctx, ctx.getNameParser("").parse(name)); 136 } 137 138 142 public static void unbind(Context ctx, Name name) throws NamingException 143 { 144 ctx.unbind(name); int sz = name.size(); 146 while (--sz > 0) 148 { 149 Name pname = name.getPrefix(sz); 150 try 151 { 152 ctx.destroySubcontext(pname); 153 } 154 catch (NamingException e) 155 { 156 log.trace("Unable to remove context " + pname, e); 157 break; 158 } 159 } 160 } 161 162 170 public static Object lookup(String name, Class clazz) throws Exception 171 { 172 InitialContext ctx = new InitialContext (); 173 try 174 { 175 return lookup(ctx, name, clazz); 176 } 177 finally 178 { 179 ctx.close(); 180 } 181 } 182 183 191 public static Object lookup(Name name, Class clazz) throws Exception 192 { 193 InitialContext ctx = new InitialContext (); 194 try 195 { 196 return lookup(ctx, name, clazz); 197 } 198 finally 199 { 200 ctx.close(); 201 } 202 } 203 204 213 public static Object lookup(Context context, String name, Class clazz) throws Exception 214 { 215 Object result = context.lookup(name); 216 checkObject(context, name, result, clazz); 217 return result; 218 } 219 220 229 public static Object lookup(Context context, Name name, Class clazz) throws Exception 230 { 231 Object result = context.lookup(name); 232 checkObject(context, name.toString(), result, clazz); 233 return result; 234 } 235 236 243 public static void createLinkRef(String fromName, String toName) throws NamingException 244 { 245 InitialContext ctx = new InitialContext (); 246 createLinkRef(ctx, fromName, toName); 247 } 248 249 257 public static void createLinkRef(Context ctx, String fromName, String toName) throws NamingException 258 { 259 LinkRef link = new LinkRef (toName); 260 Context fromCtx = ctx; 261 Name name = ctx.getNameParser("").parse(fromName); 262 String atom = name.get(name.size()-1); 263 for(int n = 0; n < name.size()-1; n ++) 264 { 265 String comp = name.get(n); 266 try 267 { 268 fromCtx = (Context ) fromCtx.lookup(comp); 269 } 270 catch(NameNotFoundException e) 271 { 272 fromCtx = fromCtx.createSubcontext(comp); 273 } 274 } 275 276 log.debug("atom: " + atom); 277 log.debug("link: " + link); 278 279 fromCtx.rebind(atom, link); 280 281 log.debug("Bound link " + fromName + " to " + toName); 282 } 283 284 290 public static void removeLinkRef(String name) throws NamingException 291 { 292 InitialContext ctx = new InitialContext (); 293 removeLinkRef(ctx, name); 294 } 295 296 303 public static void removeLinkRef(Context ctx, String name) throws NamingException 304 { 305 log.debug("Unbinding link " + name); 306 ctx.unbind(name); 307 } 308 309 310 318 protected static void checkObject(Context context, String name, Object object, Class clazz) throws Exception 319 { 320 Class objectClass = object.getClass(); 321 if (clazz.isAssignableFrom(objectClass) == false) 322 { 323 StringBuffer buffer = new StringBuffer (100); 324 buffer.append("Object at '").append(name); 325 buffer.append("' in context ").append(context.getEnvironment()); 326 buffer.append(" is not an instance of "); 327 appendClassInfo(buffer, clazz); 328 buffer.append(" object class is "); 329 appendClassInfo(buffer, object.getClass()); 330 throw new ClassCastException (buffer.toString()); 331 } 332 } 333 334 340 protected static void appendClassInfo(StringBuffer buffer, Class clazz) 341 { 342 buffer.append("[class=").append(clazz.getName()); 343 buffer.append(" classloader=").append(clazz.getClassLoader()); 344 buffer.append(" interfaces={"); 345 Class [] interfaces = clazz.getInterfaces(); 346 for (int i=0; i<interfaces.length; ++i) 347 { 348 if (i > 0) 349 buffer.append(", "); 350 buffer.append("interface=").append(interfaces[i].getName()); 351 buffer.append(" classloader=").append(interfaces[i].getClassLoader()); 352 } 353 buffer.append("}]"); 354 } 355 } 356 | Popular Tags |