KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   Copyright (C) 2001-2002 Renaud Pawlak.
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
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.Vector JavaDoc;
22 import org.aopalliance.intercept.Interceptor;
23
24 /**
25  * This special aspect component is used by the system to solve
26  * inter-aspect composition issues.
27  *
28  * <p>It is typically used to order the different wrappers at runtime
29  * (see <code>getWeaveTimeRank()</code>) or to check if two aspect
30  * components are incompatible of dependent (see
31  * <code>addIncompatibleACPairs()</code> and
32  * <code>addDependentACPair()</code>).
33  *
34  * @author <a HREF="mailto:pawlak@cnam.fr">Renaud Pawlak</a>
35  */

36
37 public class CompositionAspect extends AspectComponent {
38
39     /** The name of the wrapping order property in the prop file. */
40     //protected static String wrappingOrderProp = "org.objectweb.jac.comp.wrappingOrder";
41
/** The name of the incompatible property in the prop file. */
42     //protected static String incompatibleACsProp = "org.objectweb.jac.comp.imcompatibleACs";
43
/** The name of the dependent property in the prop file. */
44     //protected static String dependentACsProp = "org.objectweb.jac.comp.dependentACs";
45

46     /** Store the default wrapping order. */
47     protected Vector JavaDoc wrappingOrder = JacPropLoader.wrappingOrder;
48
49     /** Store the exclusive aspect component pairs. */
50     protected Vector JavaDoc incompatibleACs = JacPropLoader.incompatibleACs;
51
52     /** Store the dependent aspect component pairs. */
53     protected Vector JavaDoc dependentACs = JacPropLoader.dependentACs;
54
55     /**
56      * The default contructor (reads the jac.prop file to initialize
57      * the composition aspect). */

58
59     public CompositionAspect() {
60     }
61
62     /**
63      * When a wrappee method is beeing wrapped by a wrapper, this
64      * method is upcalled by the system to get the rank of the wrapper
65      * within the wrapping chain (the set of wrappers that allready
66      * wrap the wrappee method).
67      *
68      * @param wrappingChain the set of wrapping methods that allready
69      * wraps the wrappee method
70      * @param wrapper the wrapper that is going be added to the
71      * wrapping chain
72      *
73      * @see Wrapping#wrap(Wrappee,Wrapper,AbstractMethodItem)
74      */

75     public int getWeaveTimeRank(WrappingChain wrappingChain, Wrapper wrapper) {
76         int i = 0;
77         Interceptor[] chain = wrappingChain.chain;
78         int wrapperRank = wrappingOrder.indexOf(wrapper.getClass().getName());
79         for (; i < chain.length; i++) {
80             if (wrapperRank
81                 <= wrappingOrder.indexOf(chain[i].getClass().getName())) {
82                 return i;
83             }
84         }
85         /*Log.trace("composition",
86                   "getting weave time rank for "+wrapper+"."+wrappingMethod+
87                   "==>" + i + "/" + wrappingChain.size());*/

88         return i;
89     }
90
91     /**
92      * Returns true if wrapperType1 has to be run before
93      * wrapperType2. This method is used by
94      * <code>getWeaveTimeRank()</code>.
95      *
96      * @param wrapperType1 the first type to check
97      * @param wrapperType2 the second type to check
98      * @return true if (wrapperType1 < wrapperType2 )
99      * @see #getWeaveTimeRank(WrappingChain,Wrapper)
100      */

101
102     public final boolean areCorrectlyOrdered(
103         String JavaDoc wrapperType1,
104         String JavaDoc wrapperType2) {
105         /*
106         Log.trace("composition","areCorrectlyOrdered("+
107                   wrapperType1+"("+i1+"),"+wrapperType2+"("+i1+"))");
108         */

109         return (
110             wrappingOrder.indexOf(wrapperType1)
111                 <= wrappingOrder.indexOf(wrapperType2));
112         /*
113         Log.trace("composition","areCorrectlyOrdered("+
114                   wrapperType1+","+wrapperType2+") -> false");
115         */

116     }
117
118     /**
119      * The getter for the wrapping types order.
120      *
121      * @return a vector that contains the ordered wrapper classes */

122
123     public final Vector JavaDoc getWrappingTypesOrder() {
124         return wrappingOrder;
125     }
126
127     /**
128      * Add a new exlusive aspect component pair.
129      *
130      * <p>If ac1 and ac2 are incompatible, then ac1 cannot be
131      * registered in the Aspect Component Manager if ac2 is already
132      * registered (and reverse).
133      *
134      * <p>NOTE: this is a reflexive relation.
135      *
136      * @param ac1 the aspect component that is incompatible with ac2
137      * @param ac2 the aspect component that is incompatible with ac1
138      * @see ACManager#register(String,Object) */

139
140     public final void addIncompatibleACPair(
141         AspectComponent ac1,
142         AspectComponent ac2) {
143         incompatibleACs.add(ac1);
144         incompatibleACs.add(ac2);
145     }
146
147     /**
148      * Add a new dependent aspect component pair.
149      *
150      * <p>If ac1 depends on ac2, then ac1 cannot be registered in the
151      * Aspect Component Manager if ac2 is not already registered.
152      *
153      * <p>NOTE: this is a transitive relation.
154      *
155      * @param ac1 the aspect component that depends on ac2
156      * @param ac2 the aspect component on which ac1 depends
157      * @see ACManager#register(String,Object) */

158
159     public final void addDependentACPair(
160         AspectComponent ac1,
161         AspectComponent ac2) {
162         dependentACs.add(ac1);
163         dependentACs.add(ac2);
164     }
165
166     /**
167      * Returns true if the aspect components are incompatible.
168      *
169      * <p>NOTE: <code>areIncompatible(ac1,ac2)<code> equals
170      * <code>areIncompatible(ac2,ac1)<code>
171      *
172      * @param ac1 the first aspect component to check
173      * @param ac2 the second aspect component to check
174      * @return true if ac1 is incompatible with ac2
175      * @see #addIncompatibleACPair(AspectComponent,AspectComponent) */

176
177     public final boolean areIncompatible(
178         AspectComponent ac1,
179         AspectComponent ac2) {
180         for (int i = 0; i < incompatibleACs.size(); i += 2) {
181             if ((ac1.equals(incompatibleACs.get(i))
182                 && ac2.equals(incompatibleACs.get(i + 1)))
183                 || (ac2.equals(incompatibleACs.get(i))
184                     && ac1.equals(incompatibleACs.get(i + 1)))) {
185                 return true;
186             }
187         }
188         return false;
189     }
190
191     /**
192      * Returns true if the aspect components are dependent.
193      *
194      * <p>NOTE: <code>areDependent(ac1,ac2)<code> not equals
195      * <code>areDependent(ac2,ac1)<code>
196      *
197      * @param ac1 the first aspect component to check
198      * @param ac2 the second aspect component to check
199      * @return true if ac1 depends on ac2
200      * @see #addDependentACPair(AspectComponent,AspectComponent) */

201
202     public final boolean areDependent(
203         AspectComponent ac1,
204         AspectComponent ac2) {
205         for (int i = 0; i < dependentACs.size(); i += 2) {
206             if ((ac1.equals(dependentACs.get(i))
207                 && ac2.equals(dependentACs.get(i + 1)))
208                 || (ac2.equals(dependentACs.get(i))
209                     && ac1.equals(dependentACs.get(i + 1)))) {
210                 return true;
211             }
212         }
213         return false;
214     }
215
216 }
217
Popular Tags