KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > kernel > registry > RegistryUtil


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
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 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: RegistryUtil.java 10:30:18 AM ddesjardins $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.kernel.registry;
23
24 import java.util.Hashtable JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.TreeMap JavaDoc;
27 import java.util.Map.Entry;
28
29 import javax.naming.Binding JavaDoc;
30 import javax.naming.InvalidNameException JavaDoc;
31 import javax.naming.LinkRef JavaDoc;
32 import javax.naming.NameAlreadyBoundException JavaDoc;
33 import javax.naming.NameClassPair JavaDoc;
34 import javax.naming.NameNotFoundException JavaDoc;
35 import javax.naming.NamingException JavaDoc;
36 import javax.naming.NotContextException JavaDoc;
37
38 import org.objectweb.petals.kernel.registry.jndi.JNDIConnection;
39 import org.objectweb.petals.kernel.registry.jndi.NamingContextImpl;
40 import org.objectweb.petals.util.LoggingUtil;
41 import org.objectweb.petals.util.SystemUtil;
42
43 /**
44  * Util which acts on the data
45  *
46  * @author ddesjardins - eBMWebsourcing
47  */

48 public class RegistryUtil {
49
50     /**
51      * Logger
52      */

53     protected LoggingUtil log;
54
55     /**
56      * Debug boolean
57      */

58     protected boolean superDebug;
59
60     /**
61      * Registry Server
62      */

63     protected RegistryServer server;
64
65     public RegistryUtil(RegistryServer server) {
66         super();
67         this.server = server;
68     }
69
70     /**
71      * Bind an object with a key in the specified context
72      *
73      * @param contextName
74      * name of the context
75      * @param key
76      * key to bind with
77      * @param value
78      * value to bind to the key
79      * @throws NamingException
80      * if a problem occurs
81      */

82     public void bind(Object JavaDoc contextName, Object JavaDoc key, Object JavaDoc value)
83         throws NamingException JavaDoc {
84         if (superDebug) {
85             log.start();
86         }
87         if (!(key instanceof String JavaDoc)) {
88             throw new InvalidNameException JavaDoc("Key must be non null and a String");
89         }
90         if (!(contextName instanceof String JavaDoc)) {
91             throw new InvalidNameException JavaDoc(
92                 "Context name must be non null and a String");
93         }
94         if (((String JavaDoc) key).length() == 0) {
95             throw new InvalidNameException JavaDoc("Key must not be an empty String");
96         }
97         String JavaDoc path = ""
98             + contextName + key;
99         String JavaDoc fullContextName = path.substring(0, path.lastIndexOf("/") + 1);
100         if (path.lastIndexOf("/") == 0) {
101             fullContextName = (String JavaDoc) contextName;
102         }
103         String JavaDoc lastKey = path.substring(path.lastIndexOf("/") + 1);
104         if (server.getData().containsKey(fullContextName)) {
105             Map JavaDoc<String JavaDoc, Object JavaDoc> subData = server.getData().get(fullContextName);
106             if (subData.containsKey(lastKey)) {
107                 if (superDebug) {
108                     log.debug(lastKey
109                         + " is already bound in context " + fullContextName);
110                 }
111                 throw new NameAlreadyBoundException JavaDoc(lastKey
112                     + " is already bound in context " + fullContextName);
113             } else {
114                 subData.put(lastKey, value);
115             }
116         } else {
117             throw new NotContextException JavaDoc("Context "
118                 + fullContextName + " does not exists");
119         }
120         if (superDebug) {
121             log.end();
122         }
123     }
124
125     /**
126      * Create and bind a new context in the specified context
127      *
128      * @param contextName
129      * name of the context in which we want to create a new context
130      * @param key
131      * name of the context that we want to create
132      * @return newly created context
133      * @throws NamingException
134      * if the context name does not exist or if the key is already
135      * bound
136      */

137     public String JavaDoc createSubcontext(Object JavaDoc contextName, Object JavaDoc key)
138         throws NamingException JavaDoc {
139         if (superDebug) {
140             log.start();
141         }
142         if (!(key instanceof String JavaDoc)) {
143             throw new InvalidNameException JavaDoc("Key must be non null and a String");
144         }
145         if (!(contextName instanceof String JavaDoc)) {
146             throw new InvalidNameException JavaDoc(
147                 "Context name must be non null and a String");
148         }
149         if (((String JavaDoc) key).length() == 0) {
150             throw new InvalidNameException JavaDoc("Key must not be an empty String");
151         }
152         String JavaDoc path = ""
153             + contextName + key;
154         String JavaDoc fullContextName = path.substring(0, path.lastIndexOf("/") + 1);
155         if (path.lastIndexOf("/") == 0) {
156             fullContextName = (String JavaDoc) contextName;
157         }
158         String JavaDoc lastKey = path.substring(path.lastIndexOf("/") + 1);
159         if (!server.getData().containsKey(fullContextName)) {
160             throw new NotContextException JavaDoc("Context "
161                 + fullContextName + " does not exists");
162         } else {
163             Map JavaDoc<String JavaDoc, Object JavaDoc> newMap = new TreeMap JavaDoc<String JavaDoc, Object JavaDoc>();
164             server.getData().put(path
165                 + "/", newMap);
166             bind(fullContextName, lastKey, newMap);
167         }
168         if (superDebug) {
169             log.end();
170         }
171         return ""
172             + contextName + key + "/";
173     }
174
175     /**
176      * Destroy a subcontext in the specified context
177      *
178      * @param contextName
179      * name of the context in which we want to create a new context
180      * @param key
181      * name of the context that we want to create
182      * @return newly created context
183      * @throws NamingException
184      * if the context name does not exist or if the key is already
185      * bound
186      */

187     public void destroySubcontext(Object JavaDoc contextName, Object JavaDoc key)
188         throws NamingException JavaDoc {
189         if (superDebug) {
190             log.start();
191         }
192         if (!(key instanceof String JavaDoc)) {
193             throw new InvalidNameException JavaDoc("Key must be non null and a String");
194         }
195         if (!(contextName instanceof String JavaDoc)) {
196             throw new InvalidNameException JavaDoc(
197                 "Context name must be non null and a String");
198         }
199         if (((String JavaDoc) key).length() == 0) {
200             throw new InvalidNameException JavaDoc("Key must not be an empty String");
201         }
202         String JavaDoc path = ""
203             + contextName + key;
204         String JavaDoc fullContextName = path.substring(0, path.lastIndexOf("/") + 1);
205         if (path.lastIndexOf("/") == 0) {
206             fullContextName = (String JavaDoc) contextName;
207         }
208         String JavaDoc lastKey = path.substring(path.lastIndexOf("/") + 1);
209         if (!server.getData().containsKey(fullContextName)) {
210             throw new NotContextException JavaDoc("Context "
211                 + fullContextName + " does not exists");
212         } else {
213             unbind(fullContextName, lastKey);
214             server.getData().remove(path
215                 + "/");
216             for (String JavaDoc contextKey : server.getData().keySet()) {
217                 if (contextKey.startsWith(path
218                     + "/")) {
219                     server.getData().remove(contextKey);
220                 }
221             }
222         }
223         if (superDebug) {
224             log.end();
225         }
226     }
227
228     /**
229      * List the content of a context
230      *
231      * @param contextName
232      * name of the context
233      * @param contextToList
234      * name of the context to list the content
235      * @return array of NameClassPair
236      * @throws NamingException
237      */

238     public NameClassPair JavaDoc[] list(Object JavaDoc contextName, Object JavaDoc contextToList)
239         throws NamingException JavaDoc {
240         if (superDebug) {
241             log.start();
242         }
243         if (!(contextToList instanceof String JavaDoc)) {
244             throw new InvalidNameException JavaDoc(
245                 "The name of the context to list must be non null and a String");
246         }
247         if (!(contextName instanceof String JavaDoc)) {
248             throw new InvalidNameException JavaDoc(
249                 "Context name must be non null and a String");
250         }
251         if (((String JavaDoc) contextToList).length() == 0) {
252             throw new InvalidNameException JavaDoc("You should retrieve this context "
253                 + contextName + " by another way");
254         }
255         NameClassPair JavaDoc[] names = null;
256         String JavaDoc path = ""
257             + contextName + contextToList + "/";
258         if ("/".equals(contextToList)) {
259             path = (String JavaDoc) contextToList;
260         }
261         if (!server.getData().containsKey(path)) {
262             throw new NotContextException JavaDoc("Context "
263                 + path + " does not exists");
264         } else {
265             Map JavaDoc<String JavaDoc, Object JavaDoc> mapToList = server.getData().get(path);
266             names = new NameClassPair JavaDoc[mapToList.keySet().size()];
267             int i = 0;
268             for (Entry<String JavaDoc, ?> entry : mapToList.entrySet()) {
269                 if (entry.getValue() != null) {
270                     String JavaDoc className = entry.getValue().getClass().getName();
271                     if (server.getData().containsKey(path
272                         + entry.getKey() + "/")) {
273                         className = NamingContextImpl.class.getName();
274                     }
275                     names[i] = new NameClassPair JavaDoc(entry.getKey(), className);
276                 } else {
277                     names[i] = new NameClassPair JavaDoc(entry.getKey(), null);
278                 }
279                 i++;
280             }
281         }
282         if (superDebug) {
283             log.end();
284         }
285         return names;
286     }
287
288     /**
289      * List the bindings of a context
290      *
291      * @param contextName
292      * name of the context
293      * @param contextToList
294      * name of the context to list the content
295      * @return array of NameClassPair
296      * @throws NamingException
297      */

298     public Binding JavaDoc[] listBindings(Object JavaDoc contextName, Object JavaDoc contextToList)
299         throws NamingException JavaDoc {
300         if (superDebug) {
301             log.start();
302         }
303         if (!(contextToList instanceof String JavaDoc)) {
304             throw new InvalidNameException JavaDoc(
305                 "The name of the context to list must be non null and a String");
306         }
307         if (!(contextName instanceof String JavaDoc)) {
308             throw new InvalidNameException JavaDoc(
309                 "Context name must be non null and a String");
310         }
311         if (((String JavaDoc) contextToList).length() == 0) {
312             throw new InvalidNameException JavaDoc("You should retrieve this context "
313                 + contextName + " by another way");
314         }
315         Binding JavaDoc[] bindings = null;
316         Map JavaDoc<String JavaDoc, ?> mapToList = null;
317         String JavaDoc path = ""
318             + contextName + contextToList + "/";
319         if ("/".equals(contextToList)) {
320             path = (String JavaDoc) contextToList;
321         }
322         if (!server.getData().containsKey(path)) {
323             throw new NotContextException JavaDoc("Context "
324                 + path + " does not exists");
325         } else {
326             mapToList = server.getData().get(path);
327             bindings = new Binding JavaDoc[mapToList.keySet().size()];
328             int i = 0;
329             for (Entry<String JavaDoc, ?> entry : mapToList.entrySet()) {
330                 if (entry.getValue() != null) {
331                     String JavaDoc className = entry.getValue().getClass().getName();
332                     Object JavaDoc value = entry.getValue();
333                     if (server.getData().containsKey(path
334                         + entry.getKey() + "/")) {
335                         className = NamingContextImpl.class.getName();
336                         value = new NamingContextImpl(
337                             new Hashtable JavaDoc<String JavaDoc, Object JavaDoc>(),
338                             new JNDIConnection(Integer.parseInt(SystemUtil
339                                 .getJndiPort()), SystemUtil.getHost()), path
340                                 + entry.getKey() + "/");
341                     }
342                     bindings[i] = new Binding JavaDoc(entry.getKey(), className, value);
343                 } else {
344                     bindings[i] = new Binding JavaDoc(entry.getKey(), null, null);
345                 }
346                 i++;
347             }
348         }
349         if (superDebug) {
350             log.end();
351         }
352         return bindings;
353     }
354
355     /**
356      * Lookup for an object
357      *
358      * @param contextName
359      * name of the context
360      * @param key
361      * key to look for
362      * @return value of the object bound to the key
363      * @throws NamingException
364      */

365     public Object JavaDoc lookup(Object JavaDoc contextName, Object JavaDoc key) throws NamingException JavaDoc {
366         if (superDebug) {
367             log.start();
368         }
369         if (!(key instanceof String JavaDoc)) {
370             throw new InvalidNameException JavaDoc("Key must be non null and a String");
371         }
372         if (!(contextName instanceof String JavaDoc)) {
373             throw new InvalidNameException JavaDoc(
374                 "Context name must be non null and a String");
375         }
376         if (((String JavaDoc) key).length() == 0) {
377             throw new InvalidNameException JavaDoc("You should retrieve this context "
378                 + contextName + " by another way");
379         }
380         Object JavaDoc out = null;
381         String JavaDoc path = ""
382             + contextName + key;
383         if (((String JavaDoc) key).startsWith("/")) {
384             // The key is an absolute path (linkref)
385
path = (String JavaDoc) key;
386         } else if (((String JavaDoc) key).startsWith(".")) {
387             // The key is a relative linkref
388
path = ""
389                 + contextName + ((String JavaDoc) key).substring(1);
390         }
391         String JavaDoc fullContextName = path.substring(0, path.lastIndexOf("/") + 1);
392         if (path.lastIndexOf("/") == 0) {
393             fullContextName = (String JavaDoc) contextName;
394         }
395         String JavaDoc lastKey = path.substring(path.lastIndexOf("/") + 1);
396         if (!server.getData().containsKey(fullContextName)) {
397             throw new NotContextException JavaDoc("Context "
398                 + fullContextName + " does not exists");
399         } else {
400             if (server.getData().get(fullContextName).containsKey(lastKey)) {
401                 if (server.getData().containsKey(fullContextName
402                     + lastKey + "/")) {
403                     out = new NamingContextImpl(
404                         new Hashtable JavaDoc<String JavaDoc, Object JavaDoc>(), new JNDIConnection(
405                             Integer.parseInt(SystemUtil.getJndiPort()),
406                             SystemUtil.getHost()), ""
407                             + contextName + key + "/");
408                 } else {
409                     out = server.getData().get(fullContextName).get(lastKey);
410                 }
411             } else {
412                 throw new NameNotFoundException JavaDoc(lastKey
413                     + " is not bound in the context " + fullContextName);
414             }
415         }
416         if (superDebug) {
417             log.end();
418         }
419         return out;
420     }
421
422     /**
423      * Lookup for an object go through the links if the object found is a link
424      *
425      * @param contextName
426      * name of the context
427      * @param key
428      * key to look for
429      * @return value of the object bound to the key
430      * @throws NamingException
431      */

432     public Object JavaDoc lookupLink(Object JavaDoc contextName, Object JavaDoc key)
433         throws NamingException JavaDoc {
434         if (superDebug) {
435             log.start();
436         }
437         if (!(key instanceof String JavaDoc)) {
438             throw new InvalidNameException JavaDoc("Key must be non null and a String");
439         }
440         if (!(contextName instanceof String JavaDoc)) {
441             throw new InvalidNameException JavaDoc(
442                 "Context name must be non null and a String");
443         }
444         if (((String JavaDoc) key).length() == 0) {
445             throw new InvalidNameException JavaDoc("You should retrieve this context "
446                 + contextName + " by another way");
447         }
448         Object JavaDoc out = null;
449         boolean test = true;
450         while (test) {
451             out = lookup(contextName, key);
452             if (out instanceof LinkRef JavaDoc) {
453                 LinkRef JavaDoc linkRef = (LinkRef JavaDoc) out;
454                 contextName = "/";
455                 key = linkRef.getLinkName().substring(1);
456             } else {
457                 test = false;
458             }
459         }
460         if (superDebug) {
461             log.end();
462         }
463         return out;
464     }
465
466     /**
467      * Rebind an object with a key in the specified context
468      *
469      * @param data
470      * data
471      * @param contextName
472      * name of the context
473      * @param key
474      * key to bind with
475      * @param value
476      * value to bind to the key
477      * @throws NamingException
478      * if a problem occurs
479      */

480     public void rebind(Object JavaDoc contextName, Object JavaDoc key, Object JavaDoc value)
481         throws NamingException JavaDoc {
482         if (superDebug) {
483             log.start();
484         }
485         if (!(key instanceof String JavaDoc)) {
486             throw new InvalidNameException JavaDoc("Key must be non null and a String");
487         }
488         if (!(contextName instanceof String JavaDoc)) {
489             throw new InvalidNameException JavaDoc(
490                 "Context name must be non null and a String");
491         }
492         if (((String JavaDoc) key).length() == 0) {
493             throw new InvalidNameException JavaDoc("Key must not be an empty String");
494         }
495         String JavaDoc path = ""
496             + contextName + key;
497         String JavaDoc fullContextName = path.substring(0, path.lastIndexOf("/") + 1);
498         if (path.lastIndexOf("/") == 0) {
499             fullContextName = (String JavaDoc) contextName;
500         }
501         String JavaDoc lastKey = path.substring(path.lastIndexOf("/") + 1);
502         if (!server.getData().containsKey(fullContextName)) {
503             throw new NotContextException JavaDoc("Context "
504                 + fullContextName + " does not exists");
505         } else {
506             if (server.getData().get(fullContextName).containsKey(lastKey)) {
507                 server.getData().get(fullContextName).put(lastKey, value);
508             } else {
509                 throw new NameNotFoundException JavaDoc(lastKey
510                     + " is not bound in the context " + fullContextName);
511             }
512         }
513         if (superDebug) {
514             log.end();
515         }
516     }
517
518     /**
519      * Rebind an object with a key in the specified context
520      *
521      * @param contextName
522      * name of the context
523      * @param oldKey
524      * old key
525      * @param newKey
526      * new key to bind to
527      * @throws NamingException
528      * if a problem occurs
529      */

530     public void rename(Object JavaDoc contextName, Object JavaDoc oldKey, Object JavaDoc newKey)
531         throws NamingException JavaDoc {
532         if (superDebug) {
533             log.start();
534         }
535         if (!(oldKey instanceof String JavaDoc)) {
536             throw new InvalidNameException JavaDoc(
537                 "Old key must be non null and a String");
538         }
539         if (!(newKey instanceof String JavaDoc)) {
540             throw new InvalidNameException JavaDoc(
541                 "New key must be non null and a String");
542         }
543         if (!(contextName instanceof String JavaDoc)) {
544             throw new InvalidNameException JavaDoc(
545                 "Context name must be non null and a String");
546         }
547         if (((String JavaDoc) oldKey).length() == 0) {
548             throw new InvalidNameException JavaDoc(
549                 "Old key must not be an empty String");
550         }
551         if (((String JavaDoc) newKey).length() == 0) {
552             throw new InvalidNameException JavaDoc(
553                 "New key must not be an empty String");
554         }
555         Object JavaDoc value = lookup(contextName, oldKey);
556         unbind(contextName, oldKey);
557         bind(contextName, newKey, value);
558         if (superDebug) {
559             log.end();
560         }
561     }
562
563     public void setLog(LoggingUtil log) {
564         this.log = log;
565     }
566
567     /**
568      * Unbind an an object
569      *
570      * @param contextName
571      * name of the context in which we work
572      * @param key
573      * key to unbind
574      * @throws NamingException
575      */

576     public void unbind(Object JavaDoc contextName, Object JavaDoc key) throws NamingException JavaDoc {
577         if (superDebug) {
578             log.start();
579         }
580         if (!(key instanceof String JavaDoc)) {
581             throw new InvalidNameException JavaDoc("Key must be non null and a String");
582         }
583         if (!(contextName instanceof String JavaDoc)) {
584             throw new InvalidNameException JavaDoc(
585                 "Context name must be non null and a String");
586         }
587         if (((String JavaDoc) key).length() == 0) {
588             throw new InvalidNameException JavaDoc("You should retrieve this context "
589                 + contextName + " by another way");
590         }
591         String JavaDoc path = ""
592             + contextName + key;
593         String JavaDoc fullContextName = path.substring(0, path.lastIndexOf("/") + 1);
594         if (path.lastIndexOf("/") == 0) {
595             fullContextName = (String JavaDoc) contextName;
596         }
597         String JavaDoc lastKey = path.substring(path.lastIndexOf("/") + 1);
598         if (!server.getData().containsKey(fullContextName)) {
599             throw new NotContextException JavaDoc("Context "
600                 + fullContextName + " does not exists");
601         } else {
602             if (server.getData().get(fullContextName).containsKey(lastKey)) {
603                 server.getData().get(fullContextName).remove(lastKey);
604             } else {
605                 throw new NameNotFoundException JavaDoc(lastKey
606                     + " is not bound in the context " + fullContextName);
607             }
608         }
609         if (superDebug) {
610             log.end();
611         }
612     }
613
614     public boolean isSuperDebug() {
615         return superDebug;
616     }
617
618     public void setSuperDebug(boolean superDebug) {
619         this.superDebug = superDebug;
620     }
621
622 }
623
Popular Tags