/*
* 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.instrumentor;
import org.apache.log4j.Category;
import alt.jiapi.Rule;
import alt.jiapi.Runtime;
/**
* Base class for Strategies.
*
* @author Mika Riekkinen
* @author Joni Suominen
* @version $Revision: 1.5 $ $Date: 2004/03/15 14:47:53 $
*/
public abstract class AbstractStrategy implements Strategy {
private Instrumentation instrumentation;
private static Category log = Runtime.getLogCategory(AbstractStrategy.class);
/**
* An array of String that represents matchers used.
*/
protected String[] resolutions;
private boolean reverseMatch;
private Rule[] rules;
/**
* Constructor with now resolutions defined.
* This constructor may be used by Strategies, that make no use of the
* resolutions.
*/
protected AbstractStrategy() {
}
/**
* Constructor with a single resolution.
* @param resolution Resolution to be used.
*/
protected AbstractStrategy(String resolution) {
this(new String [] {resolution}, false);
}
/**
* Constructor with an array of resolutions.
* @param resolutions Resolutions to be used.
*/
protected AbstractStrategy(String[] resolutions) {
this(resolutions, false);
}
/**
*
*
* @param matchers An array of matchers, that is used in comparing
* whether or not a found hot spot is matched.
* @param reverseMatch If true, matched known spots are blocked from
* further instrumentation. Instead unmatched spots are
* forwarded to next Instrumentor in chain.
*/
protected AbstractStrategy(String[] resolutions, boolean reverseMatch) {
this.reverseMatch = reverseMatch;
setResolutions(resolutions);
}
/**
* Sets the matchers of this Strategy.
* @param matchers Matchers to set.
*/
public void setResolutions(String[] resolutions) {
this.resolutions = resolutions;
this.reverseMatch = false;
createRules(resolutions);
}
/**
* Get the matchers of this strategy.
* @return matchers, or null, if this strategy does not have any
* matchers.
*/
public String[] getResolutions() {
return resolutions;
}
/**
* Compares given String to resolutions of this Strategy.
*
* @return true, if match is found in any of the resolutions.
*/
protected boolean match(String name) {
if (name == null || rules == null) {
return false;
}
boolean b = false;
for (int i = 0; i < rules.length; i++) {
if (rules[i].match(name)) {
b = true;
break;
}
}
// If reverseMatch, toggle b
b = b ^ reverseMatch;
return b;
}
void setInstrumentation(Instrumentation i) {
this.instrumentation = i;
}
protected Instrumentation getInstrumentation() {
return instrumentation;
}
// Converts resolutions to rules.
private void createRules(String[] resolutions) {
if(resolutions == null) {
return;
}
this.rules = new Rule[resolutions.length];
for (int i = 0; i < resolutions.length; i++) {
try {
rules[i] = new Rule(resolutions[i]);
}
catch(Exception e) {
log.error(e.getMessage());
}
}
}
}