/* * Copyright (c) 2002-2005 Universidade Federal de Campina Grande * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ package org.ourgrid.threadServicesAspects; import org.ourgrid.test.threadServices.*; import org.ourgrid.common.logger.MGLogWriter4j; import org.ourgrid.common.xml.XMLLogWriter4j; import java.util.TimerTask; privileged public aspect FunctionalThreadsMonitor { /** * Constant used to measure the execution time of a method. */ private long time = 0; /* * The object responsible for storing lists of threads according to their states. */ private ExtendedThreadLists tLists= new ExtendedThreadLists(); /* * The following advice replace the methods from ThreadServices by calls to tLists operations. */ boolean around(): execution(public static boolean areAllThreadsWaiting()){ return tLists.areAllRunningThreadsWaiting(); } void around(): execution(public static void org.ourgrid.test.threadServices.ThreadServices.waitUntilWorkIsDone()){ tLists.waitUntilWorkIsDoneNotifying(); } void around(): execution(public static void org.ourgrid.test.threadServices.ThreadServices.waitThreadsDie()){ tLists.waitUntilAllThreadsDie(); } void around(Thread t): execution(public static void org.ourgrid.test.threadServices.ThreadServices.waitUntilThreadHasStartedRunning(Thread))&& args(t){ tLists.waitUntilThreadHasStartedRunning(t); } void around(Thread t): execution(public static void org.ourgrid.test.threadServices.ThreadServices.waitUntilThreadHasFinished(Thread))&&args(t){ tLists.waitUntilThreadHasFinished(t); } void around(Thread t): execution(public static void org.ourgrid.test.threadServices.ThreadServices.waitUntilThreadIsWaiting(Thread))&& args(t){ tLists.waitUntilThreadIsWaiting(t); } void around(): execution(public static void org.ourgrid.test.threadServices.ThreadServices.printWaitingThreads()) { tLists.printWaitingThreads(); } void around(): execution(public static void org.ourgrid.test.threadServices.ThreadServices.printStartedThreads()) { tLists.printStartedThreads(); } void around(): execution(public static void org.ourgrid.test.threadServices.ThreadServices.printRunningThreads()) { tLists.printRunningThreads(); } boolean around(): execution(public static boolean org.ourgrid.test.threadServices.ExtendedThreadServices.stillIdle()) { return tLists.stillIdle(); } /* * POINTCUT DEFINITIONS */ pointcut excludedEntities(): !within(ThreadLists) && !within(MGLogWriter4j) && !within(XMLLogWriter4j) && !within(TimerTask+) && !within(org.ourgrid.common.fd.*); pointcut threadStartCalls(Thread t):call(public void start())&& target(t) && excludedEntities(); pointcut waitCalls(Object o):call(public void wait())&& target(o) && excludedEntities(); pointcut runnableRunExecutions():execution(public void Runnable+.run()) && excludedEntities(); /* * ADVICE */ before(Thread t): threadStartCalls(t) { tLists.includeInStartedThreads(t); } before(): runnableRunExecutions() { tLists.includeInRunningThreads(Thread.currentThread()); } after(): runnableRunExecutions() { tLists.removeRunnableThread(); } before(Object o):waitCalls(o) && excludedEntities() { tLists.addWaiting(o); } before(Object o):(call(public void notifyAll()))&& target(o) && excludedEntities() { tLists.notifyAllWaitingThreads(o); } before(Object o):(call(public void notify()))&& target(o) && excludedEntities() { tLists.notifyOneWaitingThread(o); } before(): if (ThreadLists.DEBUG) && execution(* *..*Test.test*(..)) { time = System.currentTimeMillis(); System.out.println("Executing method from test:"+thisJoinPoint); } after(): if (ThreadLists.DEBUG) && execution (* *..*Test.test*(..)) { System.out.println("End of Executing method from s test:"+thisJoinPoint); System.out.println("Time elapsed: "+(System.currentTimeMillis() - time)/1000 + " seconds in test "+thisJoinPoint); } }