/*
* Copyright(C) 2001 Mika Riekkinen, Joni Suominen
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or(at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package alt.jiapi;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.log4j.Category;
import alt.jiapi.reflect.JiapiClass;
import alt.jiapi.reflect.Loader;
/**
* This class acts as a glue, that binds all the classes needed in
* instrumentation together. Mainly, it contains a List of
* InstrumentationDescriptors which define what classes will be
* instrumented and how.
*
* @author Mika Riekkinen
* @author Joni Suominen
* @version $Revision: 1.23 $ $Date: 2004/08/04 08:58:04 $
*/
public class InstrumentationContext {
private static Category log = Runtime.getLogCategory(InstrumentationContext.class);
private List descriptors = new ArrayList();
private Loader loader;
/**
* Creates a context.
*/
public InstrumentationContext() {
loader = new Loader();
}
/**
* Adds an instrumentation descriptor to this context.
* Descriptor contains all the items, that makes it possible to
* bind together inclusion rules and instrumentors. And exclusion rules.
* @param id InstrumentationDescriptor
* @see alt.jiapi.InstrumentationDescriptor
*/
public void addInstrumentationDescriptor(InstrumentationDescriptor id) {
descriptors.add(id);
}
/**
* Gets a Loader which is configured with this context.
*
* @return a Loader
*/
public Loader getLoader() {
return loader;
}
/**
* Get a List of <code>InstrumentationDescriptor</code>s added to
* this context.
*
* @return a List of descriptors. Returned List is never null.
*/
public List getDescriptors() {
return descriptors;
}
/**
* Manipulates the class by configured Instrumentors. All of the
* InstrumentorChains that are matched with inclusion rule for the
* given class, will be started.
*
* @param clazz JiapiClass to instrument
*/
public void instrument(JiapiClass clazz) {
List list = new ArrayList();
for (Iterator i = getDescriptors().iterator(); i.hasNext(); ) {
InstrumentationDescriptor id = (InstrumentationDescriptor)i.next();
if (id.match(clazz.getName())) {
list.addAll(id.getInstrumentors());
}
}
if (!list.isEmpty()) {
Iterator i = list.iterator();
long l1 = System.currentTimeMillis();
while(i.hasNext()) {
Instrumentor instrumentor = (Instrumentor)i.next();
instrumentor.instrument(clazz);
}
long l2 = System.currentTimeMillis();
log.debug("It took " + (l2-l1) + " ms to instrument " + clazz);
}
else {
log.debug("No inclusion rules match " + clazz.getName());
}
}
}