KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > jms > p2p > naming > ContextImpl


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.jms.p2p.naming;
8
9 import java.io.InputStream JavaDoc;
10 import java.io.Serializable JavaDoc;
11 import java.util.Enumeration JavaDoc;
12 import java.util.Hashtable JavaDoc;
13 import java.util.Properties JavaDoc;
14 import java.util.StringTokenizer JavaDoc;
15
16 import javax.naming.Context JavaDoc;
17 import javax.naming.Name JavaDoc;
18 import javax.naming.NameParser JavaDoc;
19 import javax.naming.NamingEnumeration JavaDoc;
20 import javax.naming.NamingException JavaDoc;
21 import javax.naming.OperationNotSupportedException JavaDoc;
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 /**
29  * Simple {@link Context} implementation to enable using Pure P2P JMS/JBoss in a vendor neutral
30  * way. This uses {@link DistributedTree} to create a single namespace shared by all Pure P2P JMS/
31  * JBoss clients defined in the same group. This is a minimal implementation only built to support
32  * the very basic needs of JMS clients--namely looking up instances of
33  * {@link javax.jms.ConnectionFactory} and {@link javax.jms.Destination} by thier <code>String</code>
34  * names. In fact, all the operations that requre use of {@link Name} will throw
35  * {@link OperationNotSupportedException} because I didn't want to get into dealing with the syntax
36  * properties, etc. If it is implemented here, it is either required to support the JMS clients, it
37  * was easy so I did it, or partial implementation was easy so I did it. An example of partial
38  * implementation is the {@link NamingEnumeration} returned by the {@link #list} methods. The
39  * returned <code>NamingEnumeration</code> is be expected to provide more than the <code>String</code>
40  * values of the keys which is what it returns now.<br/>
41  * <br/>
42  * Additionally, this should be refactored to use a single channel, as right now it creates its
43  * own. That means that right now for a JMS client that uses JNDI, two distinct channels will be
44  * opened. This is currently required becuase of the use of the JavaGroup blocks which would step
45  * one one another ({@link org.jgroups.blocks.PullPushAdapter} taking messages meeded buy the
46  * {@link DistributedTree}, etc.).
47  *
48  * @author <a HREF="mailto:nathan@jboss.org">Nathan Phelps</a>
49  * @version $Revision: 1.4 $ $Date: 2003/09/23 15:46:12 $
50  */

51 public class ContextImpl implements Context JavaDoc
52 {
53     private String JavaDoc contextName = null;
54     private Hashtable JavaDoc environment = null;
55     private DistributedTree jndiTree;
56
57     ContextImpl(Hashtable JavaDoc environment) throws Exception JavaDoc
58     {
59         this.environment = environment;
60         String JavaDoc 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 JavaDoc environment, DistributedTree jndiTree, String JavaDoc contextName)
67     {
68         this.environment = environment;
69         this.jndiTree = jndiTree;
70         this.contextName = contextName;
71     }
72
73     public Object JavaDoc lookup(Name JavaDoc name) throws NamingException JavaDoc
74     {
75         throw new OperationNotSupportedException JavaDoc();
76     }
77
78     public Object JavaDoc lookup(String JavaDoc name) throws NamingException JavaDoc
79     {
80         String JavaDoc fullyQualifiedName = this.createName(name);
81         Object JavaDoc object = this.jndiTree.get(fullyQualifiedName);
82         if (object == null)
83         {
84             throw new NamingException JavaDoc("Name '" + fullyQualifiedName + "' not bound to JNDI Tree.");
85         }
86         return object;
87     }
88
89     public void bind(Name JavaDoc name, Object JavaDoc object) throws NamingException JavaDoc
90     {
91         throw new OperationNotSupportedException JavaDoc();
92     }
93
94     public void bind(String JavaDoc name, Object JavaDoc object) throws NamingException JavaDoc
95     {
96         String JavaDoc fullyQualifiedName = this.createName(name);
97         if (this.jndiTree.get(fullyQualifiedName) != null)
98         {
99             throw new NamingException JavaDoc("The name '" + fullyQualifiedName + "' is already bound to the tree.");
100         }
101         this.jndiTree.add(fullyQualifiedName, (Serializable JavaDoc) object);
102     }
103
104     public void rebind(Name JavaDoc name, Object JavaDoc object) throws NamingException JavaDoc
105     {
106         throw new OperationNotSupportedException JavaDoc();
107     }
108
109     public void rebind(String JavaDoc name, Object JavaDoc object) throws NamingException JavaDoc
110     {
111         String JavaDoc fullyQualifiedName = this.createName(name);
112         if (this.jndiTree.get(fullyQualifiedName) == null)
113         {
114             this.jndiTree.add(fullyQualifiedName, (Serializable JavaDoc) object);
115         }
116         else
117         {
118             this.jndiTree.set(fullyQualifiedName, (Serializable JavaDoc) object);
119         }
120     }
121
122     public void unbind(Name JavaDoc name) throws NamingException JavaDoc
123     {
124         throw new OperationNotSupportedException JavaDoc();
125     }
126
127     public void unbind(String JavaDoc name) throws NamingException JavaDoc
128     {
129         String JavaDoc fullyQualifiedName = this.createName(name);
130         this.jndiTree.remove(fullyQualifiedName);
131     }
132
133     public void rename(Name JavaDoc oldName, Name JavaDoc newName) throws NamingException JavaDoc
134     {
135         throw new OperationNotSupportedException JavaDoc();
136     }
137
138     public void rename(String JavaDoc oldName, String JavaDoc newName) throws NamingException JavaDoc
139     {
140         String JavaDoc fullyQualifiedName = this.createName(oldName);
141         Object JavaDoc object = this.lookup(fullyQualifiedName);
142         this.unbind(fullyQualifiedName);
143         fullyQualifiedName = this.createName(newName);
144         this.bind(fullyQualifiedName, object);
145     }
146
147     public NamingEnumeration JavaDoc list(Name JavaDoc name) throws NamingException JavaDoc
148     {
149         throw new OperationNotSupportedException JavaDoc();
150     }
151
152     public NamingEnumeration JavaDoc list(String JavaDoc name) throws NamingException JavaDoc
153     {
154         String JavaDoc fullyQualifiedName = this.createName(name);
155         Enumeration JavaDoc enumeration = this.jndiTree.getChildrenNames(fullyQualifiedName).elements();
156         return new NamingEnumerationImpl(enumeration);
157     }
158
159     public NamingEnumeration JavaDoc listBindings(Name JavaDoc name) throws NamingException JavaDoc
160     {
161         throw new OperationNotSupportedException JavaDoc();
162     }
163
164     public NamingEnumeration JavaDoc listBindings(String JavaDoc name) throws NamingException JavaDoc
165     {
166         return this.list(name);
167     }
168
169     public void destroySubcontext(Name JavaDoc name) throws NamingException JavaDoc
170     {
171         throw new OperationNotSupportedException JavaDoc();
172     }
173
174     public void destroySubcontext(String JavaDoc name) throws NamingException JavaDoc
175     {
176         String JavaDoc fullyQualifiedName = this.createName(name);
177         this.jndiTree.remove(fullyQualifiedName);
178     }
179
180     public Context JavaDoc createSubcontext(Name JavaDoc name) throws NamingException JavaDoc
181     {
182         throw new OperationNotSupportedException JavaDoc();
183     }
184
185     public Context JavaDoc createSubcontext(String JavaDoc name) throws NamingException JavaDoc
186     {
187         String JavaDoc fullyQualifiedName = this.createName(name);
188         this.jndiTree.add(fullyQualifiedName);
189         return new ContextImpl(this.environment, this.jndiTree, fullyQualifiedName);
190     }
191
192     public Object JavaDoc lookupLink(Name JavaDoc name) throws NamingException JavaDoc
193     {
194         throw new OperationNotSupportedException JavaDoc();
195     }
196
197     public Object JavaDoc lookupLink(String JavaDoc name) throws NamingException JavaDoc
198     {
199         throw new OperationNotSupportedException JavaDoc();
200     }
201
202     public NameParser JavaDoc getNameParser(Name JavaDoc name) throws NamingException JavaDoc
203     {
204         throw new OperationNotSupportedException JavaDoc();
205     }
206
207     public NameParser JavaDoc getNameParser(String JavaDoc name) throws NamingException JavaDoc
208     {
209         throw new OperationNotSupportedException JavaDoc();
210     }
211
212     public Name JavaDoc composeName(Name JavaDoc name, Name JavaDoc prefix) throws NamingException JavaDoc
213     {
214         throw new OperationNotSupportedException JavaDoc();
215     }
216
217     public String JavaDoc composeName(String JavaDoc name, String JavaDoc prefix) throws NamingException JavaDoc
218     {
219         throw new OperationNotSupportedException JavaDoc();
220     }
221
222     public Object JavaDoc addToEnvironment(String JavaDoc name, Object JavaDoc value) throws NamingException JavaDoc
223     {
224         return this.environment.put(name, value);
225     }
226
227     public Object JavaDoc removeFromEnvironment(String JavaDoc name) throws NamingException JavaDoc
228     {
229         return this.environment.remove(name);
230     }
231
232     public Hashtable JavaDoc getEnvironment() throws NamingException JavaDoc
233     {
234         return this.environment;
235     }
236
237     public void close() throws NamingException JavaDoc
238     {
239         if (this.jndiTree != null && this.contextName == null) // Don't stop unless this is the InitialContext.
240
{
241             this.jndiTree.stop();
242             InitialContextFactoryImpl.unregisterInitialContext(this);
243         }
244     }
245
246     public String JavaDoc getNameInNamespace() throws NamingException JavaDoc
247     {
248         throw new OperationNotSupportedException JavaDoc();
249     }
250
251     public void finalize() throws Throwable JavaDoc
252     {
253         this.close();
254         super.finalize();
255     }
256
257     private String JavaDoc createName(String JavaDoc name) throws NamingException JavaDoc
258     {
259         if (name == null)
260         {
261             throw new NamingException JavaDoc("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 JavaDoc
274     {
275         InputStream JavaDoc propertyFileInputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("p2p.properties");
276         if (propertyFileInputStream == null)
277         {
278             // load default
279
propertyFileInputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("org/jboss/jms/p2p/p2p.properties");
280         }
281         Properties JavaDoc properties = new Properties JavaDoc();
282         properties.load(propertyFileInputStream);
283
284         // Since each client may declare a connection factory we'll just do a rebind.
285
this.rebind(properties.getProperty("org.jboss.jms.p2p.connectionfactory.name", "ConnectionFactory"), new JBossConnectionFactory(new P2PImplementation()));
286
287         // Now load any declared destinations, right now just do topics
288
String JavaDoc declaredTopics = properties.getProperty("org.jboss.jms.p2p.destinations.topics.names");
289         if (declaredTopics != null)
290         {
291             StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(declaredTopics, ",", false);
292             while (tokenizer.hasMoreTokens())
293             {
294                 String JavaDoc currentToken = tokenizer.nextToken();
295                 // TOD: Need to validate that the name includes no illegal characters
296
// TOD: Should I be doing a rebind for destinations?
297
this.rebind(currentToken, new JBossDestination(currentToken));
298             }
299         }
300     }
301
302     private class NamingEnumerationImpl implements NamingEnumeration JavaDoc
303     {
304         private Enumeration JavaDoc enumeration = null;
305
306         NamingEnumerationImpl(Enumeration JavaDoc enumeration)
307         {
308             this.enumeration = enumeration;
309         }
310
311         public Object JavaDoc next() throws NamingException JavaDoc
312         {
313             return this.nextElement();
314         }
315
316         public boolean hasMore() throws NamingException JavaDoc
317         {
318             return this.hasMoreElements();
319         }
320
321         public void close() throws NamingException JavaDoc
322         {
323             this.enumeration = null;
324         }
325
326         public boolean hasMoreElements()
327         {
328             return this.enumeration.hasMoreElements();
329         }
330
331         public Object JavaDoc nextElement()
332         {
333             return this.enumeration.nextElement();
334         }
335     }
336 }
Popular Tags