1 7 package org.jboss.jms.p2p.naming; 8 9 import java.io.InputStream ; 10 import java.io.Serializable ; 11 import java.util.Enumeration ; 12 import java.util.Hashtable ; 13 import java.util.Properties ; 14 import java.util.StringTokenizer ; 15 16 import javax.naming.Context ; 17 import javax.naming.Name ; 18 import javax.naming.NameParser ; 19 import javax.naming.NamingEnumeration ; 20 import javax.naming.NamingException ; 21 import javax.naming.OperationNotSupportedException ; 22 23 import org.jgroups.blocks.DistributedTree; 24 import org.jboss.jms.client.JBossConnectionFactory; 25 import org.jboss.jms.client.p2p.P2PImplementation; 26 import org.jboss.jms.destination.JBossDestination; 27 28 51 public class ContextImpl implements Context 52 { 53 private String contextName = null; 54 private Hashtable environment = null; 55 private DistributedTree jndiTree; 56 57 ContextImpl(Hashtable environment) throws Exception 58 { 59 this.environment = environment; 60 String properties = "UDP(mcast_addr=228.1.2.3;mcast_port=45566;ip_ttl=0):" + "PING(timeout=5000;num_initial_members=6):" + "FD_SOCK:" + "VERIFY_SUSPECT(timeout=1500):" + "pbcast.STABLE(desired_avg_gossip=10000):" + "pbcast.NAKACK(gc_lag=5;retransmit_timeout=3000):" + "UNICAST(timeout=5000):" + "FRAG(down_thread=false;up_thread=false):" + "pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;" + "shun=false;print_local_addr=true):" + "pbcast.STATE_TRANSFER"; 61 this.jndiTree = new DistributedTree("org.jboss.jms.p2p.naming", properties); 62 this.jndiTree.start(); 63 this.init(); 64 } 65 66 private ContextImpl(Hashtable environment, DistributedTree jndiTree, String contextName) 67 { 68 this.environment = environment; 69 this.jndiTree = jndiTree; 70 this.contextName = contextName; 71 } 72 73 public Object lookup(Name name) throws NamingException 74 { 75 throw new OperationNotSupportedException (); 76 } 77 78 public Object lookup(String name) throws NamingException 79 { 80 String fullyQualifiedName = this.createName(name); 81 Object object = this.jndiTree.get(fullyQualifiedName); 82 if (object == null) 83 { 84 throw new NamingException ("Name '" + fullyQualifiedName + "' not bound to JNDI Tree."); 85 } 86 return object; 87 } 88 89 public void bind(Name name, Object object) throws NamingException 90 { 91 throw new OperationNotSupportedException (); 92 } 93 94 public void bind(String name, Object object) throws NamingException 95 { 96 String fullyQualifiedName = this.createName(name); 97 if (this.jndiTree.get(fullyQualifiedName) != null) 98 { 99 throw new NamingException ("The name '" + fullyQualifiedName + "' is already bound to the tree."); 100 } 101 this.jndiTree.add(fullyQualifiedName, (Serializable ) object); 102 } 103 104 public void rebind(Name name, Object object) throws NamingException 105 { 106 throw new OperationNotSupportedException (); 107 } 108 109 public void rebind(String name, Object object) throws NamingException 110 { 111 String fullyQualifiedName = this.createName(name); 112 if (this.jndiTree.get(fullyQualifiedName) == null) 113 { 114 this.jndiTree.add(fullyQualifiedName, (Serializable ) object); 115 } 116 else 117 { 118 this.jndiTree.set(fullyQualifiedName, (Serializable ) object); 119 } 120 } 121 122 public void unbind(Name name) throws NamingException 123 { 124 throw new OperationNotSupportedException (); 125 } 126 127 public void unbind(String name) throws NamingException 128 { 129 String fullyQualifiedName = this.createName(name); 130 this.jndiTree.remove(fullyQualifiedName); 131 } 132 133 public void rename(Name oldName, Name newName) throws NamingException 134 { 135 throw new OperationNotSupportedException (); 136 } 137 138 public void rename(String oldName, String newName) throws NamingException 139 { 140 String fullyQualifiedName = this.createName(oldName); 141 Object object = this.lookup(fullyQualifiedName); 142 this.unbind(fullyQualifiedName); 143 fullyQualifiedName = this.createName(newName); 144 this.bind(fullyQualifiedName, object); 145 } 146 147 public NamingEnumeration list(Name name) throws NamingException 148 { 149 throw new OperationNotSupportedException (); 150 } 151 152 public NamingEnumeration list(String name) throws NamingException 153 { 154 String fullyQualifiedName = this.createName(name); 155 Enumeration enumeration = this.jndiTree.getChildrenNames(fullyQualifiedName).elements(); 156 return new NamingEnumerationImpl(enumeration); 157 } 158 159 public NamingEnumeration listBindings(Name name) throws NamingException 160 { 161 throw new OperationNotSupportedException (); 162 } 163 164 public NamingEnumeration listBindings(String name) throws NamingException 165 { 166 return this.list(name); 167 } 168 169 public void destroySubcontext(Name name) throws NamingException 170 { 171 throw new OperationNotSupportedException (); 172 } 173 174 public void destroySubcontext(String name) throws NamingException 175 { 176 String fullyQualifiedName = this.createName(name); 177 this.jndiTree.remove(fullyQualifiedName); 178 } 179 180 public Context createSubcontext(Name name) throws NamingException 181 { 182 throw new OperationNotSupportedException (); 183 } 184 185 public Context createSubcontext(String name) throws NamingException 186 { 187 String fullyQualifiedName = this.createName(name); 188 this.jndiTree.add(fullyQualifiedName); 189 return new ContextImpl(this.environment, this.jndiTree, fullyQualifiedName); 190 } 191 192 public Object lookupLink(Name name) throws NamingException 193 { 194 throw new OperationNotSupportedException (); 195 } 196 197 public Object lookupLink(String name) throws NamingException 198 { 199 throw new OperationNotSupportedException (); 200 } 201 202 public NameParser getNameParser(Name name) throws NamingException 203 { 204 throw new OperationNotSupportedException (); 205 } 206 207 public NameParser getNameParser(String name) throws NamingException 208 { 209 throw new OperationNotSupportedException (); 210 } 211 212 public Name composeName(Name name, Name prefix) throws NamingException 213 { 214 throw new OperationNotSupportedException (); 215 } 216 217 public String composeName(String name, String prefix) throws NamingException 218 { 219 throw new OperationNotSupportedException (); 220 } 221 222 public Object addToEnvironment(String name, Object value) throws NamingException 223 { 224 return this.environment.put(name, value); 225 } 226 227 public Object removeFromEnvironment(String name) throws NamingException 228 { 229 return this.environment.remove(name); 230 } 231 232 public Hashtable getEnvironment() throws NamingException 233 { 234 return this.environment; 235 } 236 237 public void close() throws NamingException 238 { 239 if (this.jndiTree != null && this.contextName == null) { 241 this.jndiTree.stop(); 242 InitialContextFactoryImpl.unregisterInitialContext(this); 243 } 244 } 245 246 public String getNameInNamespace() throws NamingException 247 { 248 throw new OperationNotSupportedException (); 249 } 250 251 public void finalize() throws Throwable 252 { 253 this.close(); 254 super.finalize(); 255 } 256 257 private String createName(String name) throws NamingException 258 { 259 if (name == null) 260 { 261 throw new NamingException ("The name supplied is null."); 262 } 263 if (this.contextName == null) 264 { 265 return name; 266 } 267 else 268 { 269 return this.contextName + "/" + name; 270 } 271 } 272 273 private void init() throws Exception 274 { 275 InputStream propertyFileInputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("p2p.properties"); 276 if (propertyFileInputStream == null) 277 { 278 propertyFileInputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("org/jboss/jms/p2p/p2p.properties"); 280 } 281 Properties properties = new Properties (); 282 properties.load(propertyFileInputStream); 283 284 this.rebind(properties.getProperty("org.jboss.jms.p2p.connectionfactory.name", "ConnectionFactory"), new JBossConnectionFactory(new P2PImplementation())); 286 287 String declaredTopics = properties.getProperty("org.jboss.jms.p2p.destinations.topics.names"); 289 if (declaredTopics != null) 290 { 291 StringTokenizer tokenizer = new StringTokenizer (declaredTopics, ",", false); 292 while (tokenizer.hasMoreTokens()) 293 { 294 String currentToken = tokenizer.nextToken(); 295 this.rebind(currentToken, new JBossDestination(currentToken)); 298 } 299 } 300 } 301 302 private class NamingEnumerationImpl implements NamingEnumeration 303 { 304 private Enumeration enumeration = null; 305 306 NamingEnumerationImpl(Enumeration enumeration) 307 { 308 this.enumeration = enumeration; 309 } 310 311 public Object next() throws NamingException 312 { 313 return this.nextElement(); 314 } 315 316 public boolean hasMore() throws NamingException 317 { 318 return this.hasMoreElements(); 319 } 320 321 public void close() throws NamingException 322 { 323 this.enumeration = null; 324 } 325 326 public boolean hasMoreElements() 327 { 328 return this.enumeration.hasMoreElements(); 329 } 330 331 public Object nextElement() 332 { 333 return this.enumeration.nextElement(); 334 } 335 } 336 } | Popular Tags |