KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ch > ethz > prose > query > QueryManager


1 //
2
// This file is part of the prose package.
3
//
4
// The contents of this file are subject to the Mozilla Public License
5
// Version 1.1 (the "License"); you may not use this file except in
6
// compliance with the License. You may obtain a copy of the License at
7
// http://www.mozilla.org/MPL/
8
//
9
// Software distributed under the License is distributed on an "AS IS" basis,
10
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11
// for the specific language governing rights and limitations under the
12
// License.
13
//
14
// The Original Code is prose.
15
//
16
// The Initial Developer of the Original Code is Andrei Popovici. Portions
17
// created by Andrei Popovici are Copyright (C) 2002 Andrei Popovici.
18
// All Rights Reserved.
19
//
20
// Contributor(s):
21
// $Id: QueryManager.java,v 1.1.1.1 2003/07/02 15:30:52 apopovic Exp $
22
// =====================================================================
23
//
24
// (history at end)
25
//
26

27 package ch.ethz.prose.query;
28
29 // used packages
30
import ch.ethz.jvmai.JVMAspectInterface;
31 import ch.ethz.prose.AspectManager;
32 import ch.ethz.prose.Aspect;
33 import ch.ethz.prose.crosscut.Crosscut;
34
35 import java.util.List JavaDoc;
36 import java.util.Vector JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.Comparator JavaDoc;
39 import java.util.Collections JavaDoc;
40 import java.util.Set JavaDoc;
41 import java.util.HashSet JavaDoc;
42
43 import ch.ethz.prose.engine.ExceptionThrowRequest;
44 import ch.ethz.prose.engine.FieldAccessRequest;
45 import ch.ethz.prose.engine.FieldModificationRequest;
46 import ch.ethz.prose.engine.JoinPointRequest;
47 import ch.ethz.prose.engine.MethodEntryRequest;
48 import ch.ethz.prose.engine.MethodExitRequest;
49 import ch.ethz.prose.engine.JoinPointManager;
50
51 import ch.ethz.jvmai.MethodEntryJoinPoint;
52 import ch.ethz.jvmai.MethodExitJoinPoint;
53 import ch.ethz.jvmai.FieldAccessJoinPoint;
54 import ch.ethz.jvmai.FieldModificationJoinPoint;
55 import ch.ethz.jvmai.ExceptionJoinPoint;
56
57
58
59 /**
60  * Class
61  *
62  * @version $Revision: 1.1.1.1 $
63  * @author Andrei Popovici
64  */

65 public class QueryManager
66 {
67
68
69     public static final int SELECT_ASPECT = 0x01; // must be the minimum value of the 'SELECT' constants
70
public static final int SELECT_CROSSCUT = 0x02;
71     public static final int SELECT_JOINPOINT = 0x04;
72     public static final int GROUP_BY_ASPECT = 0x01;
73     public static final int GROUP_BY_CROSSCUT = 0x02;
74     public static final int GROUP_BY_JOINPOINT = 0x04;
75     public static final int SELECT_ALL = SELECT_ASPECT | SELECT_CROSSCUT | SELECT_JOINPOINT;
76
77     AspectManager aspectMgr = null;
78     JoinPointManager jpMgr = null;
79     public QueryManager(AspectManager am)
80     {
81     if (am == null)
82         throw new IllegalArgumentException JavaDoc("QueryManager.init: null am");
83     aspectMgr = am;
84     jpMgr = am.getJoinPointManager();
85     }
86
87     /**
88      * This method processes the system (of its <code>AspectManager</code>) for finding a duplicate free
89      * result <code>List</code> consisting of three columns:
90      * <ul>
91      * <li><code>Aspect</code>
92      * <li><code>Crosscut</code>
93      * <li><code>JoinPointRequest</code>
94      * </ul>
95      * As input it takes a <code>List</code> of <code>Aspect</code> and two <code>int</code>'s indicating
96      * which columns are selected and by which column the result is grouped.
97      *
98      * Through the logical <code>OR</code> combination of the three member constants <code>SELECT_ASPECT</code>,
99      * <code>SELECT_CROSSCUT</code> and <code>SELECT_JOINPOINT</code> every possible opportunity of selecting the columns is
100      * reached.
101      *
102      * The value of the <code>groupBy</code> variable should be in accordance with one of the codes <code>GROUP_BY_ASPECT</code>,
103      * <code>GROUP_BY_CROSSCUT</code> or <code>GROUP_BY_JOINPOINT</code>. If the value differs from one of these member constants, the
104      * default constant <code>GROUP_BY_ASPECT</code> is assigned.
105      *
106      * @param aspectList The list that is processed for searching all corresponding crosscuts and their joinpoints.
107      * @param selectFields The constant that indicates which columns are selected in the result. The non-selected
108      * columns are set to <code>null</code>. An input value other than explained above raises an Exception.
109      * @param groupBy Indicates if the result table is grouped by aspects, crosscuts or joinpoints. By default
110    * they are grouped by aspects.
111    */

112     public List JavaDoc queryAspects(List JavaDoc aspectList, int selectFields, int groupBy)
113     {
114     if (selectFields == 0)
115         return new Vector JavaDoc();
116
117     HashSet JavaDoc result = new HashSet JavaDoc();
118     Iterator JavaDoc i = aspectList.iterator();
119     while (i.hasNext())
120         {
121         AspectSurrogate as = (AspectSurrogate)i.next();
122         Aspect crtAspect = reconstructAspect(as);
123         if (crtAspect != null)
124           {
125             Iterator JavaDoc j = crtAspect.getCrosscuts().iterator();
126             if (!j.hasNext()) result.add(createTuple(as,null,null,selectFields));
127             while (j.hasNext())
128               {
129             Crosscut crtCrosscut = (Crosscut)j.next();
130             Iterator JavaDoc k = jpMgr.getJoinpoints(crtCrosscut).iterator();
131             CrosscutSurrogate cs = new CrosscutSurrogate(as,crtCrosscut);
132             if (!k.hasNext()) result.add(createTuple(as,cs,null,selectFields));
133             while (k.hasNext())
134               {
135                 JoinPointRequest crtJpr = (JoinPointRequest)k.next();
136                 result.add(createTuple(as,cs,crtJpr,selectFields));
137               }
138
139               }
140           }
141         }
142     return sortTupleList(new Vector JavaDoc(result),groupBy);
143     }
144
145     public List JavaDoc queryCrosscuts(List JavaDoc crosscutList, int selectFields, int groupBy)
146     {
147     if (selectFields == 0)
148         return new Vector JavaDoc();
149
150     HashSet JavaDoc result = new HashSet JavaDoc();
151     Iterator JavaDoc i = crosscutList.iterator();
152     while (i.hasNext())
153         {
154         try
155             {
156             CrosscutSurrogate cs = (CrosscutSurrogate)i.next();
157             AspectSurrogate as = cs.getOwnerSurrogate();
158
159             Crosscut crtCrosscut = reconstructCrosscut(cs);
160             Iterator JavaDoc k = jpMgr.getJoinpoints(crtCrosscut).iterator();
161             if (!k.hasNext()) result.add(createTuple(as,cs,null,selectFields));
162             while (k.hasNext())
163                 {
164                 JoinPointRequest crtJpr = (JoinPointRequest)k.next();
165                 result.add(createTuple(as,cs,crtJpr,selectFields));
166                 }
167             }
168         catch (ClassNotFoundException JavaDoc e)
169             {
170             // The following exception are CAUGHT and not propagated. The
171
// reason is that a surrogate may come from a remote machine
172
// and may attempt to instantiate a method or a class (etc)
173
// which does not exist on our VM. This being the case,
174
// we skip this iteration
175
}
176
177         }
178     return sortTupleList(new Vector JavaDoc(result),groupBy);
179     }
180
181     public List JavaDoc queryJoinpoints(List JavaDoc jpList, int selectFields, int groupBy)
182     {
183     if (selectFields == 0)
184         return new Vector JavaDoc();
185
186     HashSet JavaDoc result = new HashSet JavaDoc();
187     Iterator JavaDoc i = new HashSet JavaDoc(jpList).iterator();
188     while (i.hasNext())
189         {
190         try
191             {
192             JoinPointRequestSurrogate jprs = (JoinPointRequestSurrogate)i.next();
193             JoinPointRequest jpr = reconstructRequest(jprs);
194             Iterator JavaDoc j = jpMgr.getCrosscuts(jpr).iterator();
195             while (j.hasNext())
196                 {
197                 Crosscut crtCrosscut = (Crosscut)j.next();
198                 Aspect crtAspect = crtCrosscut.getOwner();
199                 result.add(createTuple(crtAspect,crtCrosscut,jprs,selectFields));
200                 }
201             }
202
203         // The following exception are CAUGHT and not propagated. The
204
// reason is that a surrogate may come from a remote machine
205
// and may attempt to instantiate a method or a class (etc)
206
// which does not exist on our VM. This being the case,
207
// we skip this iteration
208
catch (ClassNotFoundException JavaDoc e)
209             {}
210         catch (NoSuchMethodException JavaDoc e)
211             {}
212         catch (NoSuchFieldException JavaDoc e)
213             {}
214
215         }
216     return sortTupleList(new Vector JavaDoc(result),groupBy);
217     }
218
219
220     public List JavaDoc queryAllJoinpoints()
221     {
222     Set JavaDoc jpSur = new HashSet JavaDoc();
223     Iterator JavaDoc i = jpMgr.allJoinpoints().iterator();
224     while (i.hasNext())
225         jpSur.add(new JoinPointRequestSurrogate((JoinPointRequest)i.next()));
226
227     List JavaDoc jpSurList = new Vector JavaDoc(jpSur);
228     return jpSurList;
229     }
230
231     public List JavaDoc queryAllAspects()
232     {
233     Set JavaDoc apSur = new HashSet JavaDoc();
234     Iterator JavaDoc i = aspectMgr.getAllAspects().iterator();
235     while (i.hasNext())
236         apSur.add(new AspectSurrogate((Aspect)i.next()));
237
238     return new Vector JavaDoc(apSur);
239     }
240
241
242
243     private List JavaDoc sortTupleList(List JavaDoc lst, int groupBy)
244     {
245     if (groupBy != 0)
246         Collections.sort(lst,new TupleComparator(groupBy));
247
248     return lst;
249     }
250
251     static class TupleComparator implements Comparator JavaDoc
252     {
253     int groupBy;
254     TupleComparator(int groupBy)
255     {
256         this.groupBy=groupBy;
257     }
258
259     private int doHashCode(Object JavaDoc o)
260     {
261         if (o == null)
262         return 0;
263         else
264         return o.hashCode();
265     }
266
267     private int doEquals(Object JavaDoc o1, Object JavaDoc o2)
268     {
269         int compareVal = doHashCode(o1) - doHashCode(o2);
270         if (compareVal != 0)
271         return compareVal;
272
273         if (o1 != null && o1.equals(o2))
274         return 0;
275         else
276         return 1;
277     }
278
279
280     public int compare(Object JavaDoc o1, Object JavaDoc o2)
281     {
282         int hvalCompare;
283         Tuple t1 = (Tuple)o1;
284         Tuple t2 = (Tuple)o2;
285         switch (groupBy)
286         {
287         case GROUP_BY_ASPECT:
288             hvalCompare =
289             doEquals(t1.getAspectSurrogate(),t2.getAspectSurrogate());
290             break;
291         case GROUP_BY_JOINPOINT:
292             hvalCompare =
293             doEquals(t1.getRequestSurrogate(),t2.getRequestSurrogate());
294             break;
295         case GROUP_BY_CROSSCUT:
296             hvalCompare =
297             doEquals(t1.getAspectSurrogate(),t2.getAspectSurrogate());
298             if (hvalCompare == 0)
299             hvalCompare = doEquals(t1.getCrosscutSurrogate(),t2.getCrosscutSurrogate());
300             break;
301         default:
302             throw new IllegalArgumentException JavaDoc("Illegal groupByValue");
303         }
304         // System.err.println("\nt1: " + t1 + "\n" + t2 + " ->RESULT: " + hvalCompare);
305
return hvalCompare;
306     }
307     }
308
309     protected Tuple createTuple(AspectSurrogate as, CrosscutSurrogate cs, JoinPointRequest jpr, int selectFields)
310     {
311     Tuple result = new Tuple();
312     if ( (selectFields & SELECT_ASPECT) != 0 )
313         result.setAspectSurrogate(as);
314     if ( (selectFields & SELECT_CROSSCUT) != 0 )
315         result.setCrosscutSurrogate(cs);
316     if ( (selectFields & SELECT_JOINPOINT) != 0)
317         {
318         if (jpr == null)
319             result.setRequestSurrogate(null);
320         else
321             result.setRequestSurrogate(new JoinPointRequestSurrogate(jpr));
322         }
323
324     return result;
325     }
326
327     protected Tuple createTuple(Aspect a, Crosscut c, JoinPointRequestSurrogate jprs, int selectFields)
328     {
329     Tuple result = new Tuple();
330     AspectSurrogate as = null;
331     if ( (selectFields & SELECT_ASPECT) != 0 )
332         {
333         if (a!= null)
334             as = new AspectSurrogate(a);
335         else
336             as = null;
337         result.setAspectSurrogate(as);
338         }
339
340     if ( (selectFields & SELECT_CROSSCUT) != 0 )
341         {
342         if (as == null)
343             {
344             if (a != null)
345                 as = new AspectSurrogate(a);
346             else
347                 as = null;
348             }
349         result.setCrosscutSurrogate( new CrosscutSurrogate(as,c));
350         }
351
352     if ( (selectFields & SELECT_JOINPOINT) != 0)
353         result.setRequestSurrogate(jprs);
354     return result;
355     }
356
357
358
359
360
361
362     public Aspect reconstructAspect(AspectSurrogate as)
363     {
364     List JavaDoc lst = aspectMgr.getAllAspects();
365     Iterator JavaDoc i = lst.iterator();
366     while (i.hasNext())
367         {
368         Aspect crtAsp = (Aspect)(i.next());
369         if (crtAsp.getAssociatedObject().equals(as.getAssociatedObject()) &&
370             crtAsp.getClass().getName().equals(as.getAspectClassName()))
371              return crtAsp;
372         }
373     return null;
374     }
375
376     protected Crosscut reconstructCrosscut(CrosscutSurrogate cs) throws ClassNotFoundException JavaDoc
377     {
378     Aspect a = reconstructAspect(cs.getOwnerSurrogate());
379     if (a == null)
380         return null;
381     return (Crosscut)a.getCrosscuts().get(cs.getIndex());
382     }
383
384
385   /**
386    * Returns a <code>JoinPointRequest</code> Object that is represented by this
387    * <code>JoinPointRequestSurrogate</code>.
388    *
389    * @exception ClassNotFoundException can be thrown if the class of a required name or type or is not available.
390    * @exception NoSuchMethodException can be thrown while the creation of a <code>MethodEntryRequest</code> or a
391    * <code>MethodExitRequest</code>.
392    * @exception NoSuchFieldException can be thrown while the creation of a <code>FieldAccessRequest</code> or a
393    * <code>FieldModificationRequest</code>.
394    */

395   protected JoinPointRequest reconstructRequest(JoinPointRequestSurrogate jprs)
396       throws ClassNotFoundException JavaDoc, NoSuchMethodException JavaDoc, NoSuchFieldException JavaDoc
397     {
398       return jpMgr.createJoinPointRequest(jprs.getKind(),jprs.getMember().toRealInstance());
399     }
400
401
402 }
403
404
405 //======================================================================
406
//
407
// $Log: QueryManager.java,v $
408
// Revision 1.1.1.1 2003/07/02 15:30:52 apopovic
409
// Imported from ETH Zurich
410
//
411
// Revision 1.2 2003/05/25 11:37:13 popovici
412
// Redefinitions: Aspects surrogates do not need to have the class to
413
// be recreated; aspects surrogates can now be created without a real aspect
414
//
415
// Revision 1.1 2003/05/20 16:05:09 popovici
416
//
417
// New QueryManager replaces functionality in AspectManager (better Soc)
418
// New 'Surrogate' classes for usage in the QueryManager
419
// The 'RemoteAspectManager' and tools modified to use the Surrogates and the QueryManager
420
//
421
//
422
Popular Tags