KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joseki > server > Registry


1 /*
2  * (c) Copyright 2003, 2004 Hewlett-Packard Development Company, LP
3  * [See end of file]
4  */

5
6 /** Possible superclass for DispatcherRegistry to give a single "registry"
7  * implementation.
8  * A Map wrapper (impleemntation inheriticance, not interface inheritance)
9  * that restricts the keys to strings, and values to a declared object type.
10  *
11  * @author Andy Seaborne
12  * @version $Id: Registry.java,v 1.3 2004/04/30 14:13:13 andy_seaborne Exp $
13  */

14  
15 package org.joseki.server;
16
17 import java.util.* ;
18
19 public class Registry
20 {
21     static Registry registryRegistry =
22         createRegistry("Registry", Registry.class) ;
23     
24     public static Registry getRegistry(String JavaDoc name)
25     {
26         return (Registry)registryRegistry.get(name) ;
27     }
28     
29     public static Registry createRegistry(String JavaDoc regName, Class JavaDoc objClass)
30     {
31         Registry reg = new Registry(regName, objClass) ;
32         registryRegistry.add(regName, reg) ;
33         return reg ;
34     }
35     
36     Class JavaDoc objectClass ;
37     Map registry = new HashMap() ;
38     
39     Registry(String JavaDoc regName, Class JavaDoc objClass)
40     {
41         objectClass = objClass ;
42     }
43
44     public void add(String JavaDoc name, Object JavaDoc object)
45     {
46         if ( object.getClass().equals(objectClass) )
47         {
48             registry.put(name,object) ;
49             return ;
50         }
51         throw new RuntimeException JavaDoc("Registry: wrong type: "+object.getClass().getName()+" [expected "+objectClass.getName()+"]") ;
52     }
53     
54     // Generics, generics , ...
55
public Object JavaDoc /* objectClass */ get(String JavaDoc name)
56     {
57         return registry.get(name) ;
58     }
59     
60     public Iterator registeredNames()
61     {
62         return registry.keySet().iterator() ;
63     }
64
65     public Iterator registeredObjects()
66     {
67         return registry.values().iterator() ;
68     }
69
70     public void remove(String JavaDoc name)
71     {
72         registry.remove(name) ;
73     }
74 }
75
76
77 /*
78  * (c) Copyright 2003, 2004 Hewlett-Packard Development Company, LP
79  * All rights reserved.
80  *
81  * Redistribution and use in source and binary forms, with or without
82  * modification, are permitted provided that the following conditions
83  * are met:
84  * 1. Redistributions of source code must retain the above copyright
85  * notice, this list of conditions and the following disclaimer.
86  * 2. Redistributions in binary form must reproduce the above copyright
87  * notice, this list of conditions and the following disclaimer in the
88  * documentation and/or other materials provided with the distribution.
89  * 3. The name of the author may not be used to endorse or promote products
90  * derived from this software without specific prior written permission.
91  *
92  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
93  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
94  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
95  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
96  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
98  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
99  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
100  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
101  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
102  */

103  
104
Popular Tags