KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > core > ObjectRepository


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, but
10   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 program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA */

18
19 package org.objectweb.jac.core;
20
21 import java.util.Arrays JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Vector JavaDoc;
27 import org.apache.log4j.Logger;
28 import org.objectweb.jac.core.rtti.ClassItem;
29 import org.objectweb.jac.core.rtti.ClassRepository;
30 import org.objectweb.jac.core.rtti.CollectionItem;
31 import org.objectweb.jac.core.rtti.FieldItem;
32 import org.objectweb.jac.core.rtti.RttiAC;
33 import org.objectweb.jac.util.ExtArrays;
34 import org.objectweb.jac.util.Predicate;
35 import org.objectweb.jac.util.Strings;
36
37 /**
38  * This class allows the JAC applications to access objects that are
39  * managed within the current VM or that can be defined outside of it
40  * by the currently woven aspects.
41  *
42  * <p>By default, the object repository methods only return the JAC
43  * objects that are accessible through the
44  * <code>JacObject.getObject</code> methods. However, since it upcalls
45  * the <code>ACManager.whenGetObjects()</code> method, all the aspect
46  * components can change the returned object set.
47  *
48  * <p>As typical examples:
49  *
50  * <ul><li>the persistence aspects may load objects that correspond to
51  * the get filter so that the application can see the objects stored
52  * in the database
53  *
54  * <li>the owning aspect can remove some objects from the returned
55  * collection if the current user is not allowed to access them
56  *
57  * <li>the distribution aspect can initiate a distributed seek over
58  * all the containers of the topology to add some objects that
59  * correspond to the filter but that may have not been declared yet in
60  * this VM</ul>
61  *
62  * @see ACManager#whenGetObjects(Collection,ClassItem)
63  * @see AspectComponent#whenGetObjects(Collection,ClassItem)
64  */

65
66 public class ObjectRepository {
67     static Logger logger = Logger.getLogger("repository");
68     static Logger loggerGet = Logger.getLogger("repository.getobjects");
69
70     /** Memorize the JAC object instances of the system. */
71     transient static org.objectweb.jac.util.WeakHashMap instances = new org.objectweb.jac.util.WeakHashMap();
72
73     /** Fast access for the JAC objects. Integer -> Object */
74     transient static java.util.WeakHashMap JavaDoc reverseInstances = new java.util.WeakHashMap JavaDoc();
75
76     /** The number of created JAC objects. */
77     transient static int instancecount = 0;
78
79     public static void register(Object JavaDoc wrappee) {
80         // This test is not useful anymore since register should be
81
// called only once
82
//if(instances.contains(wrappee)) return;
83
// do not register collections
84
if (wrappee instanceof org.objectweb.jac.lib.java.util.Vector ||
85             wrappee instanceof org.objectweb.jac.lib.java.util.HashSet ||
86             wrappee instanceof org.objectweb.jac.lib.java.util.Hashtable ||
87             wrappee instanceof org.objectweb.jac.lib.java.util.HashMap) {
88             return;
89         }
90         //System.out.println("registering "+wrappee);
91
Integer JavaDoc inst = new Integer JavaDoc(instancecount++);
92         instances.put(inst,wrappee);
93         logger.debug(wrappee+"->"+inst);
94         reverseInstances.put(wrappee,inst);
95     }
96
97     /**
98      * This method deletes the given JAC object by removing it from
99      * the different collections it has been inserted into by the
100      * system.
101      *
102      * <p>The JAVA GC, will then be able to free it from memory on its
103      * next run.
104      */

105     public static void delete(Wrappee object) {
106         logger.debug("delete "+object);
107         // remove from system list
108
instances.remove(reverseInstances.get(object));
109         reverseInstances.remove(object);
110         // remove from name repository
111
NameRepository.get().unregisterObject(object);
112         ACManager.getACM().whenDeleted(object);
113     }
114
115     public static void free(Wrappee object) {
116         // remove from system list
117
instances.remove(object);
118         reverseInstances.remove(object);
119         // remove from name repository
120
NameRepository.get().unregisterObject(object);
121         ACManager.getACM().whenFree(object);
122     }
123
124     /**
125      * This method returns the nth object that has been created
126      * in the Jac system.
127      *
128      * @param nth the object index
129      * @return the requested object
130      */

131     public static Object JavaDoc getMemoryObject(int nth) {
132         return instances.get(new Integer JavaDoc(nth));
133     }
134
135
136     /**
137      * This method returns the index of anobject that has been created
138      * in the Jac system.
139      *
140      * @param obj the object
141      * @return the index of the object
142      */

143     public static int getMemoryObjectIndex(Object JavaDoc obj) {
144         return ((Integer JavaDoc)reverseInstances.get(obj)).intValue();
145     }
146
147
148     /**
149      * This method returns the object counter of the Jac system (last
150      * created object is <code>getObject(objectCount()-1)</code>).
151      *
152      * @return the number of created objects
153      */

154     public static int memoryObjectCount() {
155         return instances.size(); //instancecount;
156
}
157     
158     
159     /**
160      * This method returns all the JAC objects that are in the current
161      * JVM memory (use <code>getObjects()</code> to get all the objects
162      * handled by the woven aspects -- such as distribution or
163      * persistence).
164      *
165      * @return all the JAC objects in memory
166      *
167      * @see #getObjects()
168      * @see #getObjects(ClassItem)
169      * @see #getMemoryObjects(ClassItem)
170      */

171     public static Collection JavaDoc getMemoryObjects() {
172         return instances.values();
173     }
174
175
176     /**
177      * This method returns all the Jac objects of a given type as an
178      * array.
179      *
180      * @param type the type to get
181      * @return all the Jac objects of a given type
182      *
183      * @see #getObjects()
184      * @see #getObjects(ClassItem)
185      * @see #getMemoryObjects(ClassItem)
186      */

187     public static Object JavaDoc[] getMemoryObjects(String JavaDoc type) {
188         ClassItem cl = null;
189         try {
190             cl = ClassRepository.get().getClass(type);
191         } catch( Exception JavaDoc e ) {
192             e.printStackTrace();
193             return ExtArrays.emptyObjectArray;
194         }
195         return getMemoryObjects(cl);
196     }
197
198
199     /**
200      * This method returns all the JAC objects of a given type as an
201      * array.
202      *
203      * @param cl the type to get
204      * @return all the JAC objects of a given type
205      *
206      * @see #getObjects()
207      * @see #getObjects(ClassItem)
208      * @see #getMemoryObjects()
209      */

210     public static Object JavaDoc[] getMemoryObjects(ClassItem cl) {
211         Vector JavaDoc objects = new Vector JavaDoc();
212
213         Class JavaDoc type = cl.getActualClass();
214         Iterator JavaDoc it = getMemoryObjects().iterator();
215         while(it.hasNext()) {
216             Object JavaDoc cur = it.next();
217             if (type.isAssignableFrom(cur.getClass())) {
218                 objects.add(cur);
219             }
220         }
221
222         logger.debug("getMemoryObjects("+cl+") -> "+objects);
223         return objects.toArray();
224     }
225
226     /**
227      * This method returns all the JAC objects that match any of the
228      * given types.
229      *
230      * @param types the types to get
231      * @return all the JAC objects that match any of the given types
232      */

233     public static Object JavaDoc[] getMemoryObjects(String JavaDoc[] types) {
234         Vector JavaDoc objects = new Vector JavaDoc();
235         for (int i=0; i<types.length; i++) {
236             try {
237                 objects.addAll(
238                     Arrays.asList(
239                         getMemoryObjects(ClassRepository.get().getClass(types[i]))) );
240             } catch( Exception JavaDoc e ) {
241                 e.printStackTrace();
242             }
243         }
244         return objects.toArray();
245     }
246
247     /**
248      * This method returns all the Jac objects that match any of the
249      * given types.
250      *
251      * @param types the types to get
252      * @return all the Jac objects that match any of the given types
253      */

254     public static Object JavaDoc[] getMemoryObjects(ClassItem[] types) {
255         Vector JavaDoc objects = new Vector JavaDoc();
256         for (int i=0; i<types.length; i++) {
257             objects.addAll( Arrays.asList(getMemoryObjects(types[i])) );
258         }
259         return objects.toArray();
260     }
261
262     /**
263      * Gets all the instantiated JAC objects on the current VM and on
264      * all the external objects sources known by the aspects (maybe
265      * resticed by some aspects).
266      *
267      * @return a collection of acessible objects
268      *
269      * @see #getObjects(ClassItem)
270      * @see #getMemoryObjects()
271      */

272     public static Collection JavaDoc getObjects() {
273         HashSet JavaDoc objects = new HashSet JavaDoc(instances.values());
274         ((ACManager)ACManager.get()).whenGetObjects(objects,null);
275         return objects;
276     }
277
278     /**
279      * Gets all the JAC objects instances of a given class on the
280      * current VM and on all the external objects sources known by the
281      * aspects (maybe resticed by some aspects).
282      *
283      * @return a collection of acessible instances of <code>cl</code>
284      *
285      * @see #getObjects(Class)
286      */

287     public static Collection JavaDoc getObjects(ClassItem cl) {
288         loggerGet.debug("getObjects "+cl);
289         String JavaDoc repName = (String JavaDoc)cl.getAttribute(RttiAC.REPOSITORY_NAME);
290         CollectionItem repCollection =
291             (CollectionItem)cl.getAttribute(RttiAC.REPOSITORY_COLLECTION);
292         Object JavaDoc repository = null;
293         if (repName!=null) {
294             repository = NameRepository.get().getObject(repName);
295             if (repository==null)
296                 loggerGet.error(cl+": no such repository object "+repName);
297         }
298         if (repository!=null && repCollection!=null) {
299             loggerGet.debug("Using repository "+repName+"."+repCollection.getName());
300             return FieldItem.getPathLeaves(repCollection,repository);
301         } else {
302             List JavaDoc objects = new Vector JavaDoc(Arrays.asList(getMemoryObjects(cl)));
303             ((ACManager)ACManager.get()).whenGetObjects(objects,cl);
304             return objects;
305         }
306     }
307
308     /**
309      * Gets all the JAC objects instances of a given class on the
310      * current VM and on all the external objects sources known by the
311      * aspects (maybe resticed by some aspects).
312      *
313      * @return a collection of acessible instances of <code>cl</code>
314      * @see #getObjects(ClassItem) */

315     public static Collection JavaDoc getObjects(Class JavaDoc cl) {
316         return getObjects(ClassRepository.get().getClass(cl));
317     }
318
319     /**
320      * Get all instances of a class whose field relation contains the
321      * given value. If a repository has been defined for the class,
322      * only objects belonging to the repository are returned.
323      *
324      * @param cl the class
325      * @param relation the relation
326      * @param value the value that the relation must contain
327      *
328      * @see org.objectweb.jac.core.rtti.RttiConf#defineRepository(ClassItem,String,CollectionItem)
329      */

330     public static Collection JavaDoc getObjectsWhere(ClassItem cl,
331                                              FieldItem relation, Object JavaDoc value) {
332         loggerGet.debug("getObjectsWhere "+cl+","+relation+","+value);
333         Collection JavaDoc objects = getObjects(cl);
334         Vector JavaDoc result = new Vector JavaDoc();
335         FieldItem field = relation.getField();
336         Iterator JavaDoc it = objects.iterator();
337         while (it.hasNext()) {
338             Object JavaDoc object = it.next();
339             for (Iterator JavaDoc j=relation.getSubstances(object).iterator();
340                  j.hasNext();) {
341                 Object JavaDoc substance = j.next();
342                 if (field instanceof CollectionItem) {
343                     if (((CollectionItem)field).
344                         getActualCollectionThroughAccessor(substance).contains(value)) {
345                         result.add(object);
346                         break;
347                     }
348                 } else if (field.isReference()) {
349                     if (field.getThroughAccessor(substance)==value) {
350                         result.add(object);
351                         break;
352                     }
353                 } else {
354                     Object JavaDoc testedValue = field.getThroughAccessor(substance);
355                     if ((value==null && testedValue==null) || (value!=null && value.equals(testedValue))) {
356                         result.add(object);
357                         break;
358                     }
359                 }
360             }
361         }
362         return result;
363     }
364
365
366     /**
367      * Get all instances of class cl which match a predicate
368      * @param cl the class
369      * @param filter the predicate to be matched
370      * @return a collection whose all items are such that filter(item)==true
371      */

372     public static Collection JavaDoc getObjectsWhere(ClassItem cl, Predicate filter) {
373         loggerGet.debug("getObjectsWhere "+cl+","+filter.getClass().getName());
374         Collection JavaDoc objects = getObjects(cl);
375         Vector JavaDoc result = new Vector JavaDoc();
376         Iterator JavaDoc it = objects.iterator();
377         while (it.hasNext()) {
378             Object JavaDoc object = it.next();
379             if (filter.apply(object)) {
380                 result.add(object);
381             }
382         }
383         return result;
384     }
385
386     public void dump() {
387     }
388
389 }
390
Popular Tags