KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > carol > cmi > jndi > FlatCtx


1 /*
2  * Copyright (C) 2002-2003, Simon Nieuviarts
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA
18  */

19 /*
20  * @(#)FlatCtx.java 1.4 99/10/15
21  *
22  * Copyright 1997, 1998, 1999 Sun Microsystems, Inc. All Rights
23  * Reserved.
24  *
25  * Sun grants you ("Licensee") a non-exclusive, royalty free,
26  * license to use, modify and redistribute this software in source and
27  * binary code form, provided that i) this copyright notice and license
28  * appear on all copies of the software; and ii) Licensee does not
29  * utilize the software in a manner which is disparaging to Sun.
30  *
31  * This software is provided "AS IS," without a warranty of any
32  * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
33  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
34  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
35  * HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE
36  * FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
37  * MODIFYING OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN
38  * NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
39  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
40  * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
41  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT
42  * OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS
43  * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
44  *
45  * This software is not designed or intended for use in on-line
46  * control of aircraft, air traffic, aircraft navigation or aircraft
47  * communications; or in the design, construction, operation or
48  * maintenance of any nuclear facility. Licensee represents and warrants
49  * that it will not use or redistribute the Software for such purposes.
50  */

51 package org.objectweb.carol.cmi.jndi;
52
53 import java.rmi.AccessException JavaDoc;
54 import java.rmi.RemoteException JavaDoc;
55 import java.util.Hashtable JavaDoc;
56
57 import javax.naming.CompositeName JavaDoc;
58 import javax.naming.Context JavaDoc;
59 import javax.naming.InvalidNameException JavaDoc;
60 import javax.naming.Name JavaDoc;
61 import javax.naming.NameAlreadyBoundException JavaDoc;
62 import javax.naming.NameClassPair JavaDoc;
63 import javax.naming.NameNotFoundException JavaDoc;
64 import javax.naming.NameParser JavaDoc;
65 import javax.naming.NamingEnumeration JavaDoc;
66 import javax.naming.NamingException JavaDoc;
67 import javax.naming.OperationNotSupportedException JavaDoc;
68 import javax.naming.Reference JavaDoc;
69 import javax.naming.Referenceable JavaDoc;
70 import javax.naming.spi.NamingManager JavaDoc;
71
72 import org.objectweb.carol.cmi.ClusterRegistry;
73 import org.objectweb.carol.util.configuration.TraceCarol;
74
75 /**
76  * A sample service provider that implements a flat namespace in memory.
77  */

78
79 class FlatCtx implements Context JavaDoc {
80     private Hashtable JavaDoc myEnv = null;
81     private String JavaDoc provider = null;
82     private static NameParser JavaDoc myParser = new FlatNameParser();
83     private ClusterRegistry reg;
84
85     FlatCtx(Hashtable JavaDoc environment) throws NamingException JavaDoc {
86         if (environment != null) {
87             myEnv = (Hashtable JavaDoc) (environment.clone());
88             provider = (String JavaDoc) myEnv.get(Context.PROVIDER_URL);
89         }
90         if (provider == null) {
91             provider = "cmi:";
92         }
93         org.objectweb.carol.cmi.NamingContext nc;
94         try {
95             nc = new org.objectweb.carol.cmi.NamingContext(provider);
96         } catch (java.net.MalformedURLException JavaDoc e) {
97             throw new NamingException JavaDoc(e.toString());
98         }
99         try {
100             reg = org.objectweb.carol.cmi.Naming.getRegistry(nc.hp);
101         } catch (java.rmi.RemoteException JavaDoc e) {
102             throw new NamingException JavaDoc(e.toString());
103         }
104     }
105
106     public Object JavaDoc lookup(String JavaDoc name) throws NamingException JavaDoc {
107         if (TraceCarol.isDebugJndiCarol())
108             TraceCarol.debugJndiCarol("lookup(" + name + ")");
109         if (name.equals("")) {
110             // Asking to look up this context itself. Create and return
111
// a new instance with its own independent environment.
112
return (new FlatCtx(myEnv));
113         }
114         try {
115             Object JavaDoc obj = reg.lookup(name);
116             if (obj instanceof RemoteReference) {
117                 return NamingManager.getObjectInstance(
118                     ((RemoteReference) obj).getReference(),
119                     null,
120                     this,
121                     this.myEnv);
122             }
123             if (TraceCarol.isDebugJndiCarol())
124                 TraceCarol.debugJndiCarol("lookup(" + name + ") returned");
125             return obj;
126         } catch (java.rmi.NotBoundException JavaDoc e) {
127             throw new NameNotFoundException JavaDoc(e.toString());
128         } catch (Exception JavaDoc e) {
129             throw new NamingException JavaDoc(e.toString());
130         }
131     }
132
133     public Object JavaDoc lookup(Name JavaDoc name) throws NamingException JavaDoc {
134         // Flat namespace; no federation; just call string version
135
return lookup(name.toString());
136     }
137
138     public void bind(String JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
139         if (TraceCarol.isDebugJndiCarol())
140             TraceCarol.debugJndiCarol("bind(" + name + ")");
141         if (name.equals("")) {
142             throw new InvalidNameException JavaDoc("Cannot bind empty name");
143         }
144         Object JavaDoc o = NamingManager.getStateToBind(obj, null, this, myEnv);
145         try {
146             if (o instanceof java.rmi.Remote JavaDoc) {
147                 reg.bind(name, (java.rmi.Remote JavaDoc) o);
148             } else if (o instanceof Reference JavaDoc) {
149                 reg.bind(name, new ReferenceImpl((Reference JavaDoc) o));
150             } else if (o instanceof Referenceable JavaDoc) {
151                 reg.bind(
152                     name,
153                     new ReferenceImpl(((Referenceable JavaDoc) o).getReference()));
154             } else
155                 throw new NamingException JavaDoc(
156                     "object to bind must be Remote : "
157                         + obj.getClass().getName());
158         } catch (java.rmi.RemoteException JavaDoc e) {
159             throw new NamingException JavaDoc(e.toString());
160         } catch (java.rmi.AlreadyBoundException JavaDoc e) {
161             throw new NameAlreadyBoundException JavaDoc(e.toString());
162         }
163     }
164
165     public void bind(Name JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
166         // Flat namespace; no federation; just call string version
167
bind(name.toString(), obj);
168     }
169
170     public void rebind(String JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
171         if (TraceCarol.isDebugJndiCarol())
172             TraceCarol.debugJndiCarol("rebind(" + name + ")");
173         if (name.equals("")) {
174             throw new InvalidNameException JavaDoc("Cannot bind empty name");
175         }
176         Object JavaDoc o = NamingManager.getStateToBind(obj, null, this, myEnv);
177         try {
178             if (o instanceof java.rmi.Remote JavaDoc) {
179                 reg.rebind(name, (java.rmi.Remote JavaDoc) o);
180             } else if (o instanceof Reference JavaDoc) {
181                 reg.rebind(name, new ReferenceImpl((Reference JavaDoc) o));
182             } else if (o instanceof Referenceable JavaDoc) {
183                 reg.rebind(
184                     name,
185                     new ReferenceImpl(((Referenceable JavaDoc) o).getReference()));
186             } else
187                 throw new NamingException JavaDoc(
188                     "object to bind must be Remote : "
189                         + obj.getClass().getName());
190         } catch (java.rmi.RemoteException JavaDoc e) {
191             throw new NamingException JavaDoc(e.toString());
192         }
193     }
194
195     public void rebind(Name JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
196         // Flat namespace; no federation; just call string version
197
rebind(name.toString(), obj);
198     }
199
200     public void unbind(String JavaDoc name) throws NamingException JavaDoc {
201         if (TraceCarol.isDebugJndiCarol())
202             TraceCarol.debugJndiCarol("unbind(" + name + ")");
203         if (name.equals("")) {
204             throw new InvalidNameException JavaDoc("Cannot unbind empty name");
205         }
206         try {
207             reg.unbind(name);
208         } catch (java.rmi.RemoteException JavaDoc e) {
209             throw new NamingException JavaDoc(e.toString());
210         } catch (java.rmi.NotBoundException JavaDoc e) {
211             throw new NameNotFoundException JavaDoc(e.toString());
212         }
213     }
214
215     public void unbind(Name JavaDoc name) throws NamingException JavaDoc {
216         // Flat namespace; no federation; just call string version
217
unbind(name.toString());
218     }
219
220     public void rename(String JavaDoc oldname, String JavaDoc newname) throws NamingException JavaDoc {
221         throw new NamingException JavaDoc("not supported");
222     }
223
224     public void rename(Name JavaDoc oldname, Name JavaDoc newname) throws NamingException JavaDoc {
225         // Flat namespace; no federation; just call string version
226
rename(oldname.toString(), newname.toString());
227     }
228
229     public NamingEnumeration JavaDoc list(String JavaDoc name) throws NamingException JavaDoc {
230         try {
231             return new CmiNames(reg.list());
232         } catch (AccessException JavaDoc e) {
233             throw new NamingException JavaDoc(e.toString());
234         } catch (RemoteException JavaDoc e) {
235             throw new NamingException JavaDoc(e.toString());
236         }
237     }
238
239     public NamingEnumeration JavaDoc list(Name JavaDoc name) throws NamingException JavaDoc {
240         // Flat namespace; no federation; just call string version
241
return list(name.toString());
242     }
243
244     public NamingEnumeration JavaDoc listBindings(String JavaDoc name) throws NamingException JavaDoc {
245         throw new NamingException JavaDoc("not supported");
246     }
247
248     public NamingEnumeration JavaDoc listBindings(Name JavaDoc name) throws NamingException JavaDoc {
249         // Flat namespace; no federation; just call string version
250
return listBindings(name.toString());
251     }
252
253     public void destroySubcontext(String JavaDoc name) throws NamingException JavaDoc {
254         throw new OperationNotSupportedException JavaDoc("FlatCtx does not support subcontexts");
255     }
256
257     public void destroySubcontext(Name JavaDoc name) throws NamingException JavaDoc {
258         // Flat namespace; no federation; just call string version
259
destroySubcontext(name.toString());
260     }
261
262     public Context JavaDoc createSubcontext(String JavaDoc name) throws NamingException JavaDoc {
263         throw new OperationNotSupportedException JavaDoc("FlatCtx does not support subcontexts");
264     }
265
266     public Context JavaDoc createSubcontext(Name JavaDoc name) throws NamingException JavaDoc {
267         // Flat namespace; no federation; just call string version
268
return createSubcontext(name.toString());
269     }
270
271     public Object JavaDoc lookupLink(String JavaDoc name) throws NamingException JavaDoc {
272         // This flat context does not treat links specially
273
return lookup(name);
274     }
275
276     public Object JavaDoc lookupLink(Name JavaDoc name) throws NamingException JavaDoc {
277         // Flat namespace; no federation; just call string version
278
return lookupLink(name.toString());
279     }
280
281     public NameParser JavaDoc getNameParser(String JavaDoc name) throws NamingException JavaDoc {
282         return myParser;
283     }
284
285     public NameParser JavaDoc getNameParser(Name JavaDoc name) throws NamingException JavaDoc {
286         // Flat namespace; no federation; just call string version
287
return getNameParser(name.toString());
288     }
289
290     public String JavaDoc composeName(String JavaDoc name, String JavaDoc prefix)
291         throws NamingException JavaDoc {
292         Name JavaDoc result =
293             composeName(new CompositeName JavaDoc(name), new CompositeName JavaDoc(prefix));
294         return result.toString();
295     }
296
297     public Name JavaDoc composeName(Name JavaDoc name, Name JavaDoc prefix) throws NamingException JavaDoc {
298         Name JavaDoc result = (Name JavaDoc) (prefix.clone());
299         result.addAll(name);
300         return result;
301     }
302
303     public Object JavaDoc addToEnvironment(String JavaDoc propName, Object JavaDoc propVal)
304         throws NamingException JavaDoc {
305         if (myEnv == null) {
306             myEnv = new Hashtable JavaDoc(5, 0.75f);
307         }
308         return myEnv.put(propName, propVal);
309     }
310
311     public Object JavaDoc removeFromEnvironment(String JavaDoc propName)
312         throws NamingException JavaDoc {
313         if (myEnv == null)
314             return null;
315
316         return myEnv.remove(propName);
317     }
318
319     public Hashtable JavaDoc getEnvironment() throws NamingException JavaDoc {
320         if (myEnv == null) {
321             // Must return non-null
322
return new Hashtable JavaDoc(3, 0.75f);
323         } else {
324             return (Hashtable JavaDoc) myEnv.clone();
325         }
326     }
327
328     public String JavaDoc getNameInNamespace() throws NamingException JavaDoc {
329         return "";
330     }
331
332     public void close() throws NamingException JavaDoc {
333         myEnv = null;
334         reg = null;
335     }
336
337     // Class for enumerating name/class pairs
338
class CmiNames implements NamingEnumeration JavaDoc {
339         String JavaDoc[] names;
340         int index = 0;
341
342         CmiNames (String JavaDoc[] names) {
343             this.names = names;
344         }
345
346         public boolean hasMoreElements() {
347             return index < names.length;
348         }
349
350         public boolean hasMore() throws NamingException JavaDoc {
351             return hasMoreElements();
352         }
353
354         public Object JavaDoc nextElement() {
355             String JavaDoc name = names[index++];
356             String JavaDoc className = "java.lang.Object";
357             return new NameClassPair JavaDoc(name, className);
358         }
359
360         public Object JavaDoc next() throws NamingException JavaDoc {
361             return nextElement();
362         }
363
364         public void close() throws NamingException JavaDoc {
365         names=null;
366         }
367     }
368 }
369
Popular Tags