refactor and add Constructor annotation

refactor and  add Constructor annotation
This commit is contained in:
coderfengyun 2014-04-09 21:24:43 +08:00
parent c588495dd9
commit b0aa80c6a8
16 changed files with 406 additions and 8 deletions

View File

@ -0,0 +1,5 @@
package org.bench4q.agent.parameterization;
public interface Parameterization {
public String getCurrentValue();
}

View File

@ -6,11 +6,16 @@ import java.util.UUID;
import org.bench4q.agent.parameterization.ParameterizedClass;
import org.bench4q.agent.parameterization.PickOrder;
import org.bench4q.agent.parameterization.UpdateStrategy;
import org.bench4q.agent.plugin.Constructor;
@ParameterizedClass(name = "Para_IteratorNumber", permittedPickOrders = { PickOrder.SEQUENTIAL }, permittedUpdateStrategies = { UpdateStrategy.EACH_ITERATION })
public class Para_IteratorNumber {
public Long iteratorNum = new Long(0);
@Constructor
public Para_IteratorNumber() {
}
/**
* For all methods, they will has these two params
*

View File

@ -8,6 +8,7 @@ import org.apache.commons.io.FileUtils;
import org.bench4q.agent.parameterization.ParameterizedClass;
import org.bench4q.agent.parameterization.PickOrder;
import org.bench4q.agent.parameterization.UpdateStrategy;
import org.bench4q.agent.plugin.Constructor;
import org.bench4q.agent.utils.ParameterParser;
@ParameterizedClass(name = "Para_List", permittedPickOrders = {
@ -28,6 +29,7 @@ public class Para_List extends BasePara {
this.currentValue = currentValue;
}
@Constructor
public Para_List(UUID testId, PickOrder pickOrder,
UpdateStrategy updateStrategy, String paramName, int currentIndex) {
super(testId, paramName, updateStrategy, pickOrder, currentIndex);

View File

@ -6,12 +6,17 @@ import java.util.UUID;
import org.bench4q.agent.parameterization.ParameterizedClass;
import org.bench4q.agent.parameterization.PickOrder;
import org.bench4q.agent.parameterization.UpdateStrategy;
import org.bench4q.agent.plugin.Constructor;
@ParameterizedClass(name = "Para_RandomNumber", permittedPickOrders = { PickOrder.RANDOM }, permittedUpdateStrategies = {
UpdateStrategy.EACH_ITERATION, UpdateStrategy.EACH_OCCURRENCE,
UpdateStrategy.ONCE })
public class Para_RandomNumber {
@Constructor
public Para_RandomNumber() {
}
public String getRandomIntegerNumber(UUID id, String begin, String end,
String stringformat) {

View File

@ -15,6 +15,7 @@ import java.util.concurrent.ConcurrentHashMap;
import org.bench4q.agent.parameterization.ParameterizedClass;
import org.bench4q.agent.parameterization.PickOrder;
import org.bench4q.agent.parameterization.UpdateStrategy;
import org.bench4q.agent.plugin.Constructor;
@ParameterizedClass(name = "Para_Table", permittedPickOrders = {
PickOrder.RANDOM, PickOrder.SEQUENTIAL, PickOrder.UNIQUE }, permittedUpdateStrategies = {
@ -24,6 +25,7 @@ public class Para_Table extends BasePara {
public final int cacheSize = 1000;// 1 sequence 2 random
public ConcurrentHashMap<String, Reader> readerMap = new ConcurrentHashMap<String, Para_Table.Reader>();
@Constructor
public Para_Table(UUID testId, PickOrder pickOrder,
UpdateStrategy updateStrategy, String parameterName, int lastIndex) {
super(testId, parameterName, updateStrategy, pickOrder, lastIndex);

View File

@ -5,6 +5,7 @@ import java.util.UUID;
import org.bench4q.agent.parameterization.ParameterizedClass;
import org.bench4q.agent.parameterization.PickOrder;
import org.bench4q.agent.parameterization.UpdateStrategy;
import org.bench4q.agent.plugin.Constructor;
@ParameterizedClass(name = "Para_UniqueNumber", permittedPickOrders = { PickOrder.UNIQUE }, permittedUpdateStrategies = {
UpdateStrategy.EACH_ITERATION, UpdateStrategy.EACH_OCCURRENCE,
@ -12,6 +13,10 @@ import org.bench4q.agent.parameterization.UpdateStrategy;
public class Para_UniqueNumber {
int currentNumber = 0;
@Constructor
public Para_UniqueNumber() {
}
synchronized String getNumber(UUID id) {
return String.valueOf(currentNumber++);
}

View File

@ -0,0 +1,28 @@
package org.bench4q.agent.plugin;
import java.util.UUID;
import org.bench4q.agent.Main;
public abstract class BasePlugin {
private UUID testId;
protected UUID getTestId() {
return testId;
}
private void setTestId(UUID testId) {
this.testId = testId;
}
protected BasePlugin(String testId) {
this.setTestId(UUID.fromString(testId));
}
protected String getFileFullPath(String simpleFileName) {
String FILE_SEPARATOR = System.getProperty("file.separator");
return Main.SCENARIO_PARAMETERS_FOLDER + FILE_SEPARATOR
+ this.getTestId().toString() + FILE_SEPARATOR + simpleFileName
+ ".txt";
}
}

View File

@ -0,0 +1,12 @@
package org.bench4q.agent.plugin;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.CONSTRUCTOR)
@Retention(RetentionPolicy.RUNTIME)
public @interface Constructor {
}

View File

@ -9,7 +9,4 @@ import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
public @interface Plugin {
String value();
public static @interface Constructor {
}
}

View File

@ -71,8 +71,9 @@ public class PluginManager {
PluginInfo pluginInfo = new PluginInfo();
pluginInfo
.setName((plugin.getAnnotation(Plugin.class)).value());
pluginInfo.setParameters(this.getParameters(plugin
.getConstructors()[0]));
pluginInfo
.setParameters(this.getParameters(getConstructor(plugin
.getConstructors())));
Method[] behaviors = this.getBehaviors(plugin);
pluginInfo.setBehaviors(new BehaviorInfo[behaviors.length]);
int i = 0;
@ -174,15 +175,25 @@ public class PluginManager {
if (ctConstructors.length != 1) {
return null;
}
Constructor<?> constructor = ctConstructors[0];
Constructor<?> constructor = getConstructor(ctConstructors);
Object[] params = prepareParameters(constructor, parameters);
return plugin.getConstructors()[0].newInstance(params);
return constructor.newInstance(params);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private Constructor<?> getConstructor(Constructor<?>[] ctConstructors) {
for (Constructor<?> constructor2 : ctConstructors) {
if (constructor2
.isAnnotationPresent(org.bench4q.agent.plugin.Constructor.class)) {
return constructor2;
}
}
return null;
}
private Object[] prepareParameters(Constructor<?> behavior,
Map<String, String> parameters) {
try {

View File

@ -4,6 +4,7 @@ import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.bench4q.agent.plugin.Behavior;
import org.bench4q.agent.plugin.Constructor;
import org.bench4q.agent.plugin.Parameter;
import org.bench4q.agent.plugin.Plugin;
import org.bench4q.agent.utils.Type.SupportTypes;
@ -29,6 +30,7 @@ public class CommandLinePlugin {
this.standardError = standardError;
}
@Constructor
public CommandLinePlugin() {
}

View File

@ -0,0 +1,317 @@
package org.bench4q.agent.plugin.basic.csvProvider;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Pattern;
import org.bench4q.agent.parameterization.Parameterization;
import org.bench4q.agent.plugin.BasePlugin;
import org.bench4q.agent.plugin.Constructor;
import org.bench4q.agent.plugin.Parameter;
import org.bench4q.agent.plugin.Plugin;
import org.bench4q.agent.utils.ParameterParser;
import org.bench4q.agent.utils.Type.SupportTypes;
@Plugin(value = "CsvProvider")
public class CsvProvider extends BasePlugin implements Parameterization {
private static final String ENABLE_CheckBox = "enable";
public static final char DEFAULT_SEPARATOR = ',';
private static final Pattern pattern = Pattern.compile("\\[(\\d+)\\]");
private String filename;
private boolean macintoshLineSeparator;
private char separator;
private List<String> commentPrefixes;
private List<String[]> allValues;
private String[] currentValues;
private String[] fieldNames;
private int position = 0;
private Boolean shared;
private boolean loop;
private boolean bigFile;
private Reader input;
private CsvProvider parent;
@Constructor
public CsvProvider(
String testId,
@Parameter(type = SupportTypes.Field, value = "fileName") String fileName,
@Parameter(type = SupportTypes.Field, value = "separator") String separator,
@Parameter(type = SupportTypes.NField, value = "fieldNames") String fieldNames,
@Parameter(type = SupportTypes.CheckBox, value = "shared") String shared,
@Parameter(type = SupportTypes.CheckBox, value = "loop") String loop,
@Parameter(type = SupportTypes.CheckBox, value = "loadAtRunTime") String loadAtRunTime,
@Parameter(type = SupportTypes.NField, value = "commentPrefixes") String commentPrefixes) {
super(testId);
this.filename = fileName;
if (fieldNames == null) {
this.fieldNames = null;
return;
}
this.separator = separator != null && separator.length() >= 1 ? separator
.charAt(0) : DEFAULT_SEPARATOR;
try {
this.fieldNames = fieldNames != null ? split(new StringReader(
fieldNames), null, this.separator, false) : null;
} catch (IOException e) {
throw new RuntimeException("Incorrect field names in CsvProvider: "
+ fieldNames, e);
}
this.shared = ParameterParser.buildCheckBox(shared).contains(
ENABLE_CheckBox);
this.loop = ParameterParser.buildCheckBox(loop).contains(
ENABLE_CheckBox);
this.bigFile = ParameterParser.buildCheckBox(loadAtRunTime).contains(
ENABLE_CheckBox);
this.commentPrefixes = ParameterParser.buildNField(commentPrefixes);
try {
if (this.shared || !this.bigFile) {
this.input = new InputStreamReader(new FileInputStream(
this.getFileFullPath(this.filename)));
}
if (!bigFile) {
allValues = new ArrayList<String[]>();
String[] line;
while ((line = split(input, this.commentPrefixes,
this.separator, macintoshLineSeparator)) != null) {
allValues.add(line);
}
input.close();
}
position = -1;
} catch (Exception ex) {
throw new RuntimeException("CsvProvider can't parse file "
+ this.filename, ex);
}
}
public Object createNewCsvProvider() {
return new CsvProvider(this);
}
private CsvProvider(CsvProvider csvProvider) {
super(csvProvider.getTestId().toString());
allValues = csvProvider.allValues;
filename = csvProvider.filename;
fieldNames = csvProvider.fieldNames;
macintoshLineSeparator = csvProvider.macintoshLineSeparator;
commentPrefixes = csvProvider.commentPrefixes;
separator = csvProvider.separator;
shared = csvProvider.shared;
loop = csvProvider.loop;
bigFile = csvProvider.bigFile;
if (shared) {
parent = csvProvider;
input = csvProvider.input;
}
reset();
}
public void reset() {
position = -1;
currentValues = null;
if (bigFile && !shared) {
try {
if (input != null) {
input.close();
}
input = new InputStreamReader(new FileInputStream(
this.getFileFullPath(this.filename)));
} catch (Exception ex) {
input = null;
throw new RuntimeException(
"Can't (re)set CsvProvider session object with file \""
+ filename + "\".", ex);
}
}
}
public void doNext() {
if (shared) {
position = parent.position;
input = parent.input;
}
++position;
if (bigFile) {
try {
currentValues = split(input, commentPrefixes, separator,
macintoshLineSeparator);
} catch (IOException ex) {
throw new RuntimeException("CsvProvider can't read file \""
+ filename + "\".", ex);
}
} else if (position < allValues.size()) {
currentValues = allValues.get(position);
} else {
currentValues = null;
}
// if there is no next line
if (currentValues == null) {
if (loop) {
position = -1;
if (shared) {
parent.position = -1;
}
if (bigFile) {
try {
if (input != null) {
input.close();
}
input = new InputStreamReader(new FileInputStream(
this.getFileFullPath(this.filename)));
} catch (Exception ex) {
input = null;
throw new RuntimeException(
"CsvProvider session object can't access file \""
+ filename + "\".", ex);
}
if (shared) {
parent.input = input;
}
}
doNext();
}
} else if (shared) {
parent.position = position;
}
}
static private String[] split(Reader enumeration,
List<String> commentPrefixes, char separator,
boolean macintoshLineSeparator) throws IOException {
final List<String> l = new LinkedList<String>();
final StringBuilder s = new StringBuilder();
int c;
int state = 0;
boolean again = true;
/* CSV parser automaton */
while (again) {
c = enumeration.read();
switch (state) {
case 0: // normal character reading
switch (c) {
case -1:
l.add(new String(s));
again = false;
break;
case '\r':
if (macintoshLineSeparator) {
l.add(new String(s));
again = false;
}
break;
case '\n':
if (!macintoshLineSeparator) {
l.add(new String(s));
again = false;
}
break;
case '"':
state = 2;
break;
default:
if (isComment(s.toString() + (char) c, l, commentPrefixes)) {
s.setLength(0);
state = 1;
} else if (c == separator) {
l.add(new String(s));
s.setLength(0);
} else {
s.append((char) c);
}
}
break;
case 1: // discard comment line
switch (c) {
case -1:
again = false;
break;
case '\r':
if (macintoshLineSeparator) {
state = 0;
}
break;
case '\n':
if (!macintoshLineSeparator) {
state = 0;
}
break;
}
break;
case 2: // reading a "-delimited sequence
switch (c) {
case -1:
throw new IOException(
"Ending \" character is missing at end of file.");
case '"':
state = 3;
break;
default:
s.append((char) c);
}
break;
case 3: // found a " character in a "-delimited sequence
switch (c) {
case -1:
l.add(new String(s));
again = false;
break;
case '"': // "" is the escape sequence for "
s.append('"');
state = 2;
break;
case '\r':
state = 0;
if (macintoshLineSeparator) {
l.add(new String(s));
again = false;
}
break;
case '\n':
state = 0;
if (!macintoshLineSeparator) {
l.add(new String(s));
again = false;
}
break;
default:
state = 0;
if (c == separator) {
l.add(new String(s));
s.setLength(0);
} else {
s.append((char) c);
}
}
}
}
return (l.size() == 1 && l.get(0).length() == 0) ? null : l
.toArray(new String[l.size()]);
}
private static boolean isComment(String field, List<String> l,
List<String> commentPrefixes) {
if (commentPrefixes != null && l.isEmpty()) {
for (String prefix : commentPrefixes) {
if (field.startsWith(prefix)) {
return true;
}
}
}
return false;
}
public String getCurrentValue() {
return null;
}
}

View File

@ -7,6 +7,7 @@ import java.util.GregorianCalendar;
import java.util.Random;
import org.bench4q.agent.plugin.Behavior;
import org.bench4q.agent.plugin.Constructor;
import org.bench4q.agent.plugin.Parameter;
import org.bench4q.agent.plugin.Plugin;
import org.bench4q.agent.utils.Type.SupportTypes;
@ -14,6 +15,7 @@ import org.bench4q.agent.utils.Type.SupportTypes;
@Plugin("Hbase")
public class HBasePlugin {
@Constructor
public HBasePlugin() {
}

View File

@ -30,9 +30,9 @@ import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.bench4q.agent.plugin.Behavior;
import org.bench4q.agent.plugin.Constructor;
import org.bench4q.agent.plugin.Parameter;
import org.bench4q.agent.plugin.Plugin;
import org.bench4q.agent.plugin.Plugin.Constructor;
import org.bench4q.agent.utils.ParameterParser;
import org.bench4q.agent.utils.Type.SupportTypes;
import org.bench4q.agent.utils.types.Table;

View File

@ -1,12 +1,15 @@
package org.bench4q.agent.plugin.basic.log;
import org.bench4q.agent.plugin.Behavior;
import org.bench4q.agent.plugin.Constructor;
import org.bench4q.agent.plugin.Parameter;
import org.bench4q.agent.plugin.Plugin;
import org.bench4q.agent.utils.Type.SupportTypes;
@Plugin("Log")
public class LogPlugin {
@Constructor
public LogPlugin() {
}

View File

@ -2,6 +2,7 @@ package org.bench4q.agent.plugin.basic.timer;
import org.apache.log4j.Logger;
import org.bench4q.agent.plugin.Behavior;
import org.bench4q.agent.plugin.Constructor;
import org.bench4q.agent.plugin.Parameter;
import org.bench4q.agent.plugin.Plugin;
import org.bench4q.agent.utils.Type.SupportTypes;
@ -10,6 +11,7 @@ import org.bench4q.agent.utils.Type.SupportTypes;
public class ConstantTimerPlugin {
private Logger logger = Logger.getLogger(ConstantTimerPlugin.class);
@Constructor
public ConstantTimerPlugin() {
}