KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > aop > jdk15 > dynamic > common > scenario > ScenarioRunner


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.aop.jdk15.dynamic.common.scenario;
23
24
25 import java.util.Map JavaDoc;
26
27 import javassist.CtClass;
28 import javassist.CtField;
29 import javassist.CtMethod;
30 import javassist.NotFoundException;
31
32 import org.jboss.aop.Advised;
33 import org.jboss.aop.AspectManager;
34 import org.jboss.aop.ClassAdvisor;
35 import org.jboss.aop.advice.AdviceBinding;
36 import org.jboss.aop.classpool.AOPClassPool;
37 import org.jboss.aop.pointcut.ast.ParseException;
38 import org.jboss.test.aop.jdk15.dynamic.common.BindingInterceptor;
39 import org.jboss.test.aop.jdk15.dynamic.common.InstanceInterceptor;
40 import org.jboss.test.aop.jdk15.dynamic.common.POJOWrappingInfo;
41
42 /**
43  * Class responsible for running dynamic scenarios that are common
44  * to all test cases contained in the
45  * org.jboss.test.aop.jdk15.dynamic package.
46  *
47  * @author Flavia Rainone
48  *
49  */

50 public class ScenarioRunner
51 {
52    // Bindings used in the tests
53
private static final AdviceBinding constructorExecution;
54    private static final AdviceBinding methodExecution;
55    private static final AdviceBinding fieldRead;
56    private static final AdviceBinding fieldWrite;
57    // javassist objects to allow P
58
private static CtClass POJO_CLASS;
59    private static CtField FIELD;
60    private static CtMethod METHOD;
61    private static String JavaDoc NOT_ADVISED_METHOD;
62    private static CtClass POJO_CLIENT;
63
64    static
65    {
66       try
67       {
68          constructorExecution = new AdviceBinding("execution(*.POJO->new(..))", null);
69          constructorExecution.addInterceptor(BindingInterceptor.class);
70          fieldRead = new AdviceBinding("get(public int *.POJO->counter)", null);
71          fieldRead.addInterceptor(BindingInterceptor.class);
72          fieldWrite = new AdviceBinding("set(public int *.POJO->counter)", null);
73          fieldWrite.addInterceptor(BindingInterceptor.class);
74          methodExecution = new AdviceBinding("execution(public void *.POJO->someMethod())", null);
75          methodExecution.addInterceptor(BindingInterceptor.class);
76       }
77       catch(ParseException e)
78       {
79          throw new RuntimeException JavaDoc("Unexpected exception", e);
80       }
81    }
82    
83    
84    /**
85     * Constructor. Resets interceptor counts.
86     */

87    public ScenarioRunner()
88    {
89       InstanceInterceptor.resetCounts();
90       BindingInterceptor.resetCounts();
91    }
92
93
94    /**
95     * Adds bidings so that all joinpoints of POJO class become intercepted
96     * by BindingInterceptor. This is done after having loaded POJO class.
97     * As soon as the POJO joinponts become intercepted, all of them are
98     * executed once, before the bindings get removed.
99     * @throws NotFoundException
100     * @throws Exception
101     */

102    public POJOWrappingInfo[] interceptPerClassLoadBefore() throws Exception JavaDoc
103    {
104       POJOWrappingInfo[] wrappingInfos = new POJOWrappingInfo[3];
105       this.executeJoinpoints(false);
106       wrappingInfos[0] = getCurrentWrappingInfo();
107       this.addBindings();
108       wrappingInfos[1] = getCurrentWrappingInfo();
109       this.executeJoinpoints(false);
110       this.removeBindings();
111       wrappingInfos[2] = getCurrentWrappingInfo();
112       this.executeJoinpoints(false);
113       return wrappingInfos;
114    }
115    
116    private static POJOWrappingInfo getCurrentWrappingInfo() throws Exception JavaDoc
117    {
118       return new POJOWrappingInfo(POJO_CLASS, FIELD, METHOD, NOT_ADVISED_METHOD, POJO_CLIENT);
119    }
120
121    /**
122     * Adds bidings so that all joinpoints of POJO class become intercepted
123     * by BindingInterceptor. This is done before the POJO class gets loaded.
124     * As soon as the POJO joinponts become intercepted, all of them are
125     * executed once, before the bindings get removed.
126     * @return the pojo wrapping infos obtained in the key points of the scenario execution.
127     */

128    public POJOWrappingInfo[] interceptPerClassLoadAfter() throws Exception JavaDoc
129    {
130       POJOWrappingInfo[] wrappingInfos = new POJOWrappingInfo[2];
131       this.addBindings();
132       this.executeJoinpoints(false);
133       this.executeJoinpoints(false);
134       wrappingInfos[0] = getCurrentWrappingInfo();
135       this.removeBindings();
136       wrappingInfos[1] = getCurrentWrappingInfo();
137       this.executeJoinpoints(false);
138       return wrappingInfos;
139    }
140
141    /**
142     * Adds bindings so that all joinpoints of POJO class become intercepted
143     * by BindingInterceptor. After this, executes the POJO joinpoints and
144     * remove the bindings. Repeat the previous steps.
145     * @return the pojo wrapping infos obtained in the key points of the scenario execution.
146     */

147    public POJOWrappingInfo[] addAndRemoveBindingTwice() throws Exception JavaDoc
148    {
149       POJOWrappingInfo[] wrappingInfos = new POJOWrappingInfo[5];
150       this.executeJoinpoints(false);
151       wrappingInfos[0] = getCurrentWrappingInfo();
152       this.addBindings();
153       wrappingInfos[1] = getCurrentWrappingInfo();
154       this.executeJoinpoints(false);
155       this.removeBindings();
156       wrappingInfos[2] = getCurrentWrappingInfo();
157       this.executeJoinpoints(false);
158       this.addBindings();
159       wrappingInfos[3] = getCurrentWrappingInfo();
160       this.executeJoinpoints(false);
161       this.removeBindings();
162       wrappingInfos[4] = getCurrentWrappingInfo();
163       this.executeJoinpoints(false);
164       return wrappingInfos;
165    }
166    
167    /**
168     * Adds bindings so that all joinpoints of POJO class become intercepted
169     * by BindingInterceptor. As soon as the bindings are added, they are
170     * removed before POJO joinponts are executed, which happens once.
171     * The goal of this use case is to test how the code instrumentation
172     * (in load time) behaves, hopefully removing completely the added bindings.
173     * @return the pojo wrapping infos obtained in the key points of the scenario execution.
174     */

175    public POJOWrappingInfo[] executeAfterBindingRemoval() throws Exception JavaDoc
176    {
177       this.addBindings();
178       this.removeBindings();
179       this.executeJoinpoints(false);
180       return new POJOWrappingInfo[]{getCurrentWrappingInfo()};
181    }
182    
183    /**
184     * This method does the following step sequence twice: adding a InstanceInterceptor
185     * to the ClassInstanceAdvisor interceptor chain associated with a POJO instance
186     * and executing all POJO instance joinpoints.
187     * @return the pojo wrapping infos obtained in the key points of the scenario execution.
188     */

189    public POJOWrappingInfo[] interceptPerInstance() throws Exception JavaDoc {
190       executeJoinpoints(false);
191       POJOWrappingInfo[] wrappingInfos = new POJOWrappingInfo[2];
192       wrappingInfos[0] = getCurrentWrappingInfo();
193       executeJoinpoints(true);
194       wrappingInfos[1] = getCurrentWrappingInfo();
195       executeJoinpoints(true);
196       return wrappingInfos;
197    }
198    
199    /**
200     * This method does the same that interceptPerInstance, except that it
201     * tries to force the garbage collector to run, on a best effort basis.
202     * The goal of this scenario is to check if the joinpoints are unwrapped
203     * as soon as jboss aop realizes the ClassInstanceAdvisors were garbage
204     * collected and the joinpoints are no more intercepted.
205     * @return the pojo wrapping infos obtained in the key points of the scenario execution.
206     */

207    public POJOWrappingInfo[] interceptPerInstanceGC() throws Exception JavaDoc
208    {
209       this.instancesCollected = 0;
210       this.executeJoinpoints(false);
211       POJOWrappingInfo[] wrappingInfos = new POJOWrappingInfo[3];
212       wrappingInfos[0] = getCurrentWrappingInfo();
213       this.executeJoinpoints(true);
214       wrappingInfos[1] = getCurrentWrappingInfo();
215       this.executeJoinpoints(true);
216       while(this.instancesCollected < 3) {
217          for (int i = 0; i < 10000; i++)
218          {
219             String JavaDoc string = new String JavaDoc("any string to fill memory space...");
220          }
221          System.gc();
222          try
223          {
224             Thread.sleep(10000);
225          }
226          catch (InterruptedException JavaDoc e)
227          {
228             throw new RuntimeException JavaDoc("Unexpected exception, e");
229          }
230       }
231       this.executeJoinpoints(false);
232       this.executeJoinpoints(false);
233       wrappingInfos[2] = getCurrentWrappingInfo();
234       return wrappingInfos;
235    }
236    
237    /**
238     * Adds bidings so that all joinpoints of POJO class become intercepted
239     * by BindingInterceptor. As soon as the POJO joinponts become intercepted,
240     * a POJO instance is created and a InstanceInterceptor is added to its
241     * ClassInstanceAdvisor interceptor chain. All of the POJO instance joinpoints
242     * are executed, after which the bindings are removed.
243     * @return the pojo wrapping infos obtained in the key points of the scenario execution.
244     */

245    public POJOWrappingInfo[] interceptPerClassPerInstance() throws Exception JavaDoc
246    {
247       this.executeJoinpoints(false);
248       POJOWrappingInfo[] wrappingInfos = new POJOWrappingInfo[3];
249       wrappingInfos[0] = getCurrentWrappingInfo();
250       this.addBindings();
251       wrappingInfos[1] = getCurrentWrappingInfo();
252       this.executeJoinpoints(true);
253       wrappingInfos[2] = getCurrentWrappingInfo();
254       this.removeBindings();
255       this.executeJoinpoints(false);
256       return wrappingInfos;
257    }
258    
259   /**
260    * Adds a InstanceInterceptor
261     * to the ClassInstanceAdvisor interceptor chain associated with a POJO
262     * instance and executes all POJO instance joinpoints.
263     * After this, this method adds bidings so that all joinpoints of POJO class
264     * become intercepted by BindingInterceptor. As soon as the POJO joinponts
265     * become intercepted, a POJO instance is created and a InstanceInterceptor
266     * is added to its ClassInstanceAdvisor interceptor chain. All of the POJO
267     * instance joinpoints are executed, after which the bindings are removed.
268     * @return the pojo wrapping infos obtained in the key points of the scenario execution.
269     */

270    public POJOWrappingInfo[] interceptPerInstancePerClass() throws Exception JavaDoc
271    {
272       this.executeJoinpoints(true);
273       this.executeJoinpoints(true);
274       POJOWrappingInfo[] wrappingInfos = new POJOWrappingInfo[3];
275       wrappingInfos[0] = getCurrentWrappingInfo();
276       this.addBindings();
277       wrappingInfos[1] = getCurrentWrappingInfo();
278       this.executeJoinpoints(true);
279       wrappingInfos[2] = getCurrentWrappingInfo();
280       this.removeBindings();
281       this.executeJoinpoints(false);
282       return wrappingInfos;
283    }
284    
285    /** Keeps track of the number of POJO instances that were collected. */
286    private int instancesCollected = 0;
287    
288    /**
289     * Tells scenario runner that a pojo instance is being collected.
290     */

291    void pojoCollected()
292    {
293       instancesCollected++;
294    }
295    
296    /**
297     * Executes all joinpoints of POJO class.
298     * @param interceptInstance if <code>true</code>, a InstanceInterceptor
299     * will be added to the interceptor chain of the ClassInstanceAdvisor
300     * associated with a POJO instance.
301     */

302    private void executeJoinpoints(boolean interceptInstance)
303    {
304       POJO pojo = new POJO(this);
305       InstanceInterceptor instanceInterceptor = null;
306       if (interceptInstance)
307       {
308          instanceInterceptor = new InstanceInterceptor();
309          Advised advised = (Advised) pojo;
310          advised._getInstanceAdvisor().insertInterceptor(instanceInterceptor);
311       }
312       pojo.counter++;
313       pojo.someMethod();
314       //after loading pojo into the class loader
315
if (POJO_CLASS == null)
316       {
317          loadPOJOData();
318       }
319    }
320    
321    /**
322     * Call after pojo has been loaded, to not invalidade tests results.
323     */

324    private static void loadPOJOData()
325    {
326       Map JavaDoc cls = AspectManager.instance().getRegisteredCLs();
327       AOPClassPool classPool = (AOPClassPool) cls.get(POJO.class.getClassLoader());
328       try
329       {
330          POJO_CLASS = classPool.getLocally(POJO.class.getName());
331          FIELD = POJO_CLASS.getDeclaredField("counter");
332          METHOD = POJO_CLASS.getDeclaredMethod("someMethod");
333          NOT_ADVISED_METHOD = ClassAdvisor.notAdvisedMethodName(POJO.class.getName(), METHOD.getName());
334          POJO_CLIENT= classPool.getLocally(ScenarioRunner.class.getName());
335       }
336       catch (NotFoundException e)
337       {
338          throw new RuntimeException JavaDoc("Unexpected exception", e);
339       }
340    }
341    
342    /**
343     * Adds bindings to AspectManager so that all joinpoints of POJO class become intercepted
344     * by BindingInterceptor.
345     */

346    void addBindings()
347    {
348       AspectManager aspectManager = AspectManager.instance();
349       aspectManager.addBinding(constructorExecution);
350       aspectManager.addBinding(fieldRead);
351       aspectManager.addBinding(fieldWrite);
352       aspectManager.addBinding(methodExecution);
353    }
354    
355    /**
356     * Removes the bindings added through the addBindings method.
357     */

358    private void removeBindings()
359    {
360       AspectManager aspectManager = AspectManager.instance();
361       aspectManager.removeBinding(constructorExecution.getName());
362       aspectManager.removeBinding(fieldRead.getName());
363       aspectManager.removeBinding(fieldWrite.getName());
364       aspectManager.removeBinding(methodExecution.getName());
365    }
366 }
Popular Tags