KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > util > Repository


1 /*
2   Copyright (C) 2001-2003 Renaud Pawlak <renaud@aopsys.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program 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
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.util;
19
20 import java.util.*;
21 import org.apache.log4j.Logger;
22
23 /**
24  * @author <a HREF="http://cedric.cnam.fr/~pawlak/index-english.html">Renaud Pawlak</a>
25  */

26
27 /**
28  * This class can be subclassed to create specific repositories.
29  *
30  * <p>A repository class should be a singleton (a sole instance
31  * class). Thus, the repository subclasses should define a static
32  * field and a static method 'get' that returns the unique repository
33  * for the class and that creates it if it does not exist.
34  */

35 public class Repository {
36     static Logger logger = Logger.getLogger("repository");
37
38     /**
39      * Get the sole repository instance for this class. Creates it if
40      * it does not exist yet.
41      *
42      * <p>NOTE: this method MUST be defined by all subclasses.
43      */

44     public static Repository get () {
45         if (repository == null)
46             repository = new Repository();
47         return repository;
48     }
49    
50     /**
51      * Store the sole instance of repository.
52      *
53      * <p>NOTE: this field MUST be defined by all subclasses.
54      *
55      * @see #get()
56      */

57     protected static Repository repository = null;
58
59     /**
60      * Link JAC objects to the names (String -> Object) */

61     public Map objects;
62
63     /**
64      * Reverse hashtable to find an objet from its name (Object -> String)*/

65     public Map names;
66
67     public Repository() {
68         init();
69     }
70
71     protected void init() {
72         objects = new Hashtable();
73         names = new Hashtable();
74     }
75
76     /**
77      * Register a new object into the repository.
78      *
79      * @param logicalName the key that allows to find the object
80      * @param object the object to register
81      * @return true if the object registered, false if already
82      * registered
83      *
84      * @see #unregister(String)
85      */

86     public boolean register(String JavaDoc logicalName, Object JavaDoc object) {
87         logger.debug("register("+logicalName+","+object.getClass().getName()+")");
88         Object JavaDoc old = objects.put(logicalName, object);
89         if (old!=null) {
90             logger.warn("overriding "+logicalName+" -> "+old+" with "+object);
91             names.remove(old);
92         }
93         names.put(object, logicalName);
94         return true;
95     }
96
97     /**
98      * Unregister a JacObject from the repository.
99      *
100      * @param logicalName the key that allows to find the object
101      *
102      * @see #register(String,Object)
103      * @see #unregisterObject(Object)
104      */

105     public void unregister(String JavaDoc logicalName) {
106         logger.debug("unregister("+logicalName+")");
107         Object JavaDoc object = objects.remove(logicalName);
108         if (object != null) {
109             names.remove(object);
110         }
111     }
112
113     /**
114      * Unregister a JacObject from the repository.
115      *
116      * @param object the object to unregister
117      *
118      * @see #register(String,Object)
119      * @see #unregister(String)
120      */

121     public void unregisterObject(Object JavaDoc object) {
122         logger.debug("unregisterObject("+object+")");
123         Object JavaDoc logicalName = names.remove(object);
124         if (logicalName != null) {
125             objects.remove(logicalName);
126         }
127     }
128
129     /**
130      * Returns true if an object is registered with this name.
131      *
132      * @param logicalName the key that allows to find the object
133      *
134      * @see #register(String,Object) */

135
136     public boolean isRegistered(String JavaDoc logicalName) {
137         if (objects.containsKey(logicalName)) {
138             return true;
139         }
140         return false;
141     }
142
143     /**
144      * Return all the registered objects as an array.
145      *
146      * <p>Reverse operation is <code>getNames()</code>.
147      *
148      * @return the registered objects in this repository
149      *
150      * @see #register(String,Object)
151      * @see #getNames()
152      */

153     public Object JavaDoc[] getObjects() {
154         return objects.values().toArray();
155     }
156
157     /**
158      * Return the names of the registered objects as an array.
159      *
160      * <p>Reverse operation is <code>getObjects()</code>.
161      *
162      * @return the registered object names in this repository
163      *
164      * @see #register(String,Object)
165      * @see #getObjects()
166      */

167     public String JavaDoc[] getNames() {
168         return (String JavaDoc[])names.values().toArray(ExtArrays.emptyStringArray);
169     }
170
171     /**
172      * Return a registered object for a given logical name.
173      *
174      * <p>Return <code>null</code> if the name does not correspond to
175      * any registered object or if <code>logicalName</code> is null.
176      *
177      * <p>Reverse operation is <code>getName(Object)</code>.
178      *
179      * @param logicalName the key that allows to find the object
180      * @return the corresponding object, null if not registered
181      *
182      * @see #register(String,Object)
183      * @see #getName(Object)
184      */

185     public Object JavaDoc getObject(String JavaDoc logicalName) {
186         if (logicalName == null)
187             return null;
188         Object JavaDoc ret = objects.get(logicalName);
189         logger.debug("getObject("+logicalName+") -> "+
190                      (ret==null?"null":ret.getClass().getName()));
191         return ret;
192     }
193
194     /**
195      * Returns the name of a registered object. Null if not
196      * registered.
197      *
198      * <p>Reverse operation is <code>getObject(String)</code>.
199      *
200      * @param object the object to get the name of
201      * @return the object name, null if not registered
202      *
203      * @see #register(String,Object)
204      * @see #getObject(String)
205      */

206     public String JavaDoc getName(Object JavaDoc object) {
207         if (object == null) {
208             return null;
209         }
210         return (String JavaDoc)names.get(object);
211     }
212
213     /**
214      * Dump all the registered objects.
215      */

216     public void dump() {
217         System.out.println(this + " dump:");
218         System.out.println(getPrintableString());
219     }
220
221     public String JavaDoc getPrintableString() {
222         return ""+objects;
223     }
224
225 }
226
Popular Tags