1 43 package org.exolab.jms.messagemgr; 44 45 import javax.naming.CompositeName ; 46 import javax.naming.Context ; 47 import javax.naming.InvalidNameException ; 48 import javax.naming.NameAlreadyBoundException ; 49 import javax.naming.NameNotFoundException ; 50 import javax.naming.NamingException ; 51 52 import org.exolab.jms.client.JmsDestination; 53 54 55 61 class ContextHelper { 62 63 75 public static void rebind(Context context, String name, 76 JmsDestination destination) 77 throws NamingException { 78 CompositeName composite = getCompositeName(name); 79 Context subcontext = context; 80 String component = null; 81 82 for (int i = 0; i < composite.size() - 1; i++) { 83 component = composite.get(i); 84 if (component.length() == 0) { 85 throw new InvalidNameException ("'" + name 86 + "' is not a valid name"); 87 } 88 Object object = null; 89 try { 90 object = subcontext.lookup(component); 91 if (!(object instanceof Context )) { 92 String subname = ""; 93 for (int j = 0; j <= i; j++) { 94 if (j > 0) { 95 subname += "."; 96 } 97 subname += composite.get(j); 98 } 99 throw new NameAlreadyBoundException ( 100 "'" + subname + "' is already bound"); 101 } else { 102 subcontext = (Context ) object; 103 } 104 } catch (NameNotFoundException exception) { 105 subcontext = subcontext.createSubcontext(component); 106 } 107 } 108 109 component = composite.get(composite.size() - 1); 110 if (component.length() == 0) { 111 throw new InvalidNameException ("'" + name 112 + "' is not a valid name"); 113 } 114 115 try { 116 Object object = subcontext.lookup(component); 117 if (object instanceof Context ) { 118 throw new NameAlreadyBoundException ("'" + name + 119 "' is already bound"); 120 } 121 } catch (NameNotFoundException ignore) { 122 } catch (NamingException exception) { 123 } 124 125 subcontext.rebind(component, destination); 126 } 127 128 138 public static void unbind(Context context, String name) 139 throws NamingException { 140 try { 141 Object object = context.lookup(name); 142 if (!(object instanceof JmsDestination)) { 143 throw new NamingException ( 144 "Cannot unbind name='" + name 145 + "': it does not refer to a Destination"); 146 } 147 context.unbind(name); 148 } catch (NameNotFoundException ignore) { 149 } 150 } 151 152 private static CompositeName getCompositeName(String name) 153 throws NamingException { 154 CompositeName composite = new CompositeName (name.replace('.', '/')); 157 if (composite.size() == 0) { 158 throw new InvalidNameException ("'" + name 159 + "' is not a valid name"); 160 } 161 return composite; 162 } 163 } 164 | Popular Tags |