parent
3483b72229
commit
fed4cf7dd4
|
@ -8,18 +8,20 @@ import java.util.Map;
|
|||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.bench4q.agent.Main;
|
||||
import org.bench4q.agent.parameterization.SessionObject;
|
||||
|
||||
public class ParamCoordinator implements SessionObject {
|
||||
private String userDefineParameterFolderPath = "/home/yxsh/git/Bench4Q-Agent/parameterClass/";
|
||||
private UUID testRunId;
|
||||
private UUID id = java.util.UUID.randomUUID();
|
||||
private UUID id;
|
||||
private Set<String> usedClassName = new HashSet<String>();
|
||||
private Map<String, Object> objMap = new HashMap<String, Object>();
|
||||
private Map<String, String> runtimeParamMap = new HashMap<String, String>();
|
||||
private Map<String, Object> cacheObjMap = new HashMap<String, Object>();
|
||||
|
||||
private ReentrantReadWriteLock mapRWLock = new ReentrantReadWriteLock();
|
||||
private Logger logger = Logger.getLogger(ParamCoordinator.class);
|
||||
|
||||
private ParamCoordinator() {
|
||||
}
|
||||
|
@ -41,15 +43,13 @@ public class ParamCoordinator implements SessionObject {
|
|||
return result;
|
||||
String[] argsArray = paraFormat.getArgsArray();
|
||||
if (paraFormat.getParamType().equals("crossThread")) {
|
||||
result = this.getParameter(
|
||||
paraFormat.getName(),
|
||||
getCurrentPackageFullName() + "."
|
||||
+ paraFormat.getClassName(),
|
||||
result = this.getParameterCrossThread(paraFormat.getName(),
|
||||
buildFullPath(paraFormat.getClassName()),
|
||||
paraFormat.getMethodName(), argsArray);
|
||||
} else if (paraFormat.getParamType().equals("inThread")) {
|
||||
result = this.instanceLevelGetParameter(paraFormat.getName(),
|
||||
paraFormat.getClassName(), paraFormat.getMethodName(),
|
||||
argsArray);
|
||||
result = this.getParameterInThread(paraFormat.getName(),
|
||||
buildFullPath(paraFormat.getClassName()),
|
||||
paraFormat.getMethodName(), argsArray);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
@ -59,7 +59,11 @@ public class ParamCoordinator implements SessionObject {
|
|||
ParamFormatParser.class.getName().lastIndexOf('.'));
|
||||
}
|
||||
|
||||
private String instanceLevelGetParameter(String name, String className,
|
||||
private static String buildFullPath(String simpleClassName) {
|
||||
return getCurrentPackageFullName() + "." + simpleClassName;
|
||||
}
|
||||
|
||||
private String getParameterInThread(String name, String className,
|
||||
String functionName, Object[] args) {
|
||||
|
||||
boolean hasThisClass = false;
|
||||
|
@ -73,15 +77,7 @@ public class ParamCoordinator implements SessionObject {
|
|||
Object result = null;
|
||||
try {
|
||||
Class<?>[] argTypeArr = new Class<?>[args.length + 2];
|
||||
argTypeArr[0] = id.getClass();
|
||||
argTypeArr[1] = this.cacheObjMap.getClass();
|
||||
Object[] totalArgs = new Object[args.length + 2];
|
||||
totalArgs[0] = id;
|
||||
totalArgs[1] = this.cacheObjMap;
|
||||
for (int i = 2; i < args.length + 2; i++) {
|
||||
argTypeArr[i] = args[i - 2].getClass();
|
||||
totalArgs[i] = args[i - 2];
|
||||
}
|
||||
Object[] totalArgs = prepareTypeArrayAndArgArray(args, argTypeArr);
|
||||
Method m = instance.getClass().getMethod(functionName, argTypeArr);
|
||||
result = m.invoke(instance, totalArgs);
|
||||
} catch (Exception ex) {
|
||||
|
@ -105,7 +101,7 @@ public class ParamCoordinator implements SessionObject {
|
|||
private boolean createObj(String className) {
|
||||
try {
|
||||
MyFileClassLoader classLoader = new MyFileClassLoader(
|
||||
userDefineParameterFolderPath);
|
||||
Main.USER_DEFINED_PARAMS_FOLDER);
|
||||
Class<?> cls = classLoader.loadClass(className);
|
||||
Object instance = cls.newInstance();
|
||||
mapRWLock.writeLock().lock();
|
||||
|
@ -125,42 +121,44 @@ public class ParamCoordinator implements SessionObject {
|
|||
return result;
|
||||
}
|
||||
|
||||
private String getParameter(String name, String className,
|
||||
private String getParameterCrossThread(String name, String className,
|
||||
String functionName, Object[] args) {
|
||||
ParametersFactory paramFactor = ParametersFactory.getInstance();
|
||||
boolean hasThisClass = paramFactor.containObj(className);
|
||||
if (false == hasThisClass) {
|
||||
paramFactor.createObj(className);
|
||||
}
|
||||
|
||||
Object instance = paramFactor.getObj(className);
|
||||
Object result = null;
|
||||
try {
|
||||
Object instance = ParametersFactory.getInstance()
|
||||
.getObjWithGuardExist(className);
|
||||
Class<?>[] argTypeArr = new Class<?>[args.length + 2];
|
||||
argTypeArr[0] = id.getClass();
|
||||
argTypeArr[1] = this.cacheObjMap.getClass();
|
||||
Object[] totalArgs = new Object[args.length + 2];
|
||||
totalArgs[0] = id;
|
||||
totalArgs[1] = this.cacheObjMap;
|
||||
for (int i = 2; i < args.length + 2; i++) {
|
||||
argTypeArr[i] = args[i - 2].getClass();
|
||||
totalArgs[i] = args[i - 2];
|
||||
}
|
||||
|
||||
Object[] totalArgs = prepareTypeArrayAndArgArray(args, argTypeArr);
|
||||
Method m = instance.getClass().getMethod(functionName, argTypeArr);
|
||||
|
||||
result = m.invoke(instance, totalArgs);
|
||||
|
||||
Object result = m.invoke(instance, totalArgs);
|
||||
fillToContext(name, className, result);
|
||||
return (String) result;
|
||||
} catch (Exception ex) {
|
||||
System.out.println(ex.getMessage() + ex.getStackTrace());
|
||||
System.out.println(((InvocationTargetException) ex)
|
||||
.getTargetException().getMessage());
|
||||
logger.error(ex, ex);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private void fillToContext(String name, String className, Object result) {
|
||||
usedClassName.add(className);
|
||||
runtimeParamMap.put(name, (String) result);
|
||||
return (String) result;
|
||||
}
|
||||
|
||||
private Object[] prepareTypeArrayAndArgArray(Object[] args,
|
||||
Class<?>[] argTypeArr) {
|
||||
argTypeArr[0] = this.id.getClass();
|
||||
argTypeArr[1] = this.cacheObjMap.getClass();
|
||||
|
||||
for (int i = 2; i < args.length + 2; i++) {
|
||||
argTypeArr[i] = args[i - 2].getClass();
|
||||
}
|
||||
|
||||
Object[] totalArgs = new Object[args.length + 2];
|
||||
totalArgs[0] = id;
|
||||
totalArgs[1] = this.cacheObjMap;
|
||||
for (int i = 2; i < args.length + 2; i++) {
|
||||
totalArgs[i] = args[i - 2];
|
||||
}
|
||||
return totalArgs;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -193,27 +191,25 @@ public class ParamCoordinator implements SessionObject {
|
|||
for (String xx : usedClassName) {
|
||||
release(xx);
|
||||
}
|
||||
ParametersFactory.getInstance().cleanUp();
|
||||
cleanUp();
|
||||
}
|
||||
|
||||
private void cleanUp() {
|
||||
this.cacheObjMap.clear();
|
||||
this.objMap.clear();
|
||||
this.runtimeParamMap.clear();
|
||||
this.usedClassName.clear();
|
||||
}
|
||||
|
||||
private void release(String className) {
|
||||
ParametersFactory pf = ParametersFactory.getInstance();
|
||||
boolean hasThisClass = pf.containObj(className);
|
||||
if (false == hasThisClass) {
|
||||
pf.createObj(className);
|
||||
}
|
||||
|
||||
Object instance = pf.getObj(className);
|
||||
|
||||
try {
|
||||
|
||||
Object instance = ParametersFactory.getInstance()
|
||||
.getObjWithGuardExist(className);
|
||||
Method m = instance.getClass().getMethod("unreg", id.getClass());
|
||||
|
||||
m.invoke(instance, id);
|
||||
|
||||
} catch (Exception ex) {
|
||||
System.out.println("realse failed");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -4,20 +4,39 @@ import java.util.HashMap;
|
|||
import java.util.Map;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||
|
||||
import org.bench4q.agent.Main;
|
||||
|
||||
public class ParametersFactory {
|
||||
private static final ParametersFactory instance = new ParametersFactory();
|
||||
private Map<String, Object> objMap = new HashMap<String, Object>();
|
||||
private ReentrantReadWriteLock mapRWLock = new ReentrantReadWriteLock();
|
||||
private String rootFilePath = "/home/yxsh/git/Bench4Q-Agent/parameterClass/";
|
||||
|
||||
private ParametersFactory() {
|
||||
}
|
||||
|
||||
static public ParametersFactory getInstance() {
|
||||
public static ParametersFactory getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public boolean containObj(String className) {
|
||||
public void cleanUp() {
|
||||
this.objMap.clear();
|
||||
}
|
||||
|
||||
public Object getObjWithGuardExist(String className) {
|
||||
Object result = null;
|
||||
if (!guardClassExist(className))
|
||||
throw new NullPointerException("Not find " + className);
|
||||
mapRWLock.readLock().lock();
|
||||
result = objMap.get(className);
|
||||
mapRWLock.readLock().unlock();
|
||||
return result;
|
||||
}
|
||||
|
||||
private boolean guardClassExist(String className) {
|
||||
return containObj(className) ? true : createObj(className);
|
||||
}
|
||||
|
||||
private boolean containObj(String className) {
|
||||
boolean ret = false;
|
||||
mapRWLock.readLock().lock();
|
||||
ret = objMap.containsKey(className);
|
||||
|
@ -25,27 +44,21 @@ public class ParametersFactory {
|
|||
return ret;
|
||||
}
|
||||
|
||||
public Object getObj(String className) {
|
||||
Object result = null;
|
||||
mapRWLock.readLock().lock();
|
||||
result = objMap.get(className);
|
||||
mapRWLock.readLock().unlock();
|
||||
return result;
|
||||
}
|
||||
|
||||
public boolean createObj(String className) {
|
||||
private boolean createObj(String className) {
|
||||
try {
|
||||
MyFileClassLoader cl = new MyFileClassLoader(rootFilePath);
|
||||
MyFileClassLoader cl = new MyFileClassLoader(
|
||||
Main.USER_DEFINED_PARAMS_FOLDER);
|
||||
Class<?> cls = cl.loadClass(className);
|
||||
Object instance = cls.newInstance();
|
||||
mapRWLock.writeLock().lock();
|
||||
objMap.put(className, instance);
|
||||
|
||||
return true;
|
||||
} catch (Exception ex) {
|
||||
System.out.println(ex.getMessage());
|
||||
return false;
|
||||
} finally {
|
||||
mapRWLock.writeLock().unlock();
|
||||
}
|
||||
mapRWLock.writeLock().unlock();
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -7,7 +7,9 @@ import java.util.UUID;
|
|||
|
||||
import org.bench4q.agent.parameterization.impl.ParamCoordinator;
|
||||
import org.bench4q.agent.parameterization.impl.Para_Table;
|
||||
import org.bench4q.agent.parameterization.impl.ParamFormatParser;
|
||||
import org.bench4q.share.helper.TestHelper;
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
|
||||
public class Test_InstanceCotroller {
|
||||
|
@ -19,6 +21,11 @@ public class Test_InstanceCotroller {
|
|||
// "<parameters name=\"useNamePassword\" class=\"Para_Table\" type=\"crossThread\" method=\"getTableColumnValue\" args=\"file,ScenarioParameters\\param1.txt,0,sequence,;,~,2\" />";
|
||||
private static final String NEW_TEST_CASE = "<parameters name=\"useNamePassword\" class=\"Para_Table\" type=\"crossThread\" method=\"getTableColumnValue\" args=\"file;ScenarioParameters\\param1.txt;0;sequence;\\;;~;2\" />";
|
||||
|
||||
@After
|
||||
public void cleanUp() {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetParam() throws Exception {
|
||||
Para_Table table = new Para_Table();
|
||||
|
@ -45,6 +52,40 @@ public class Test_InstanceCotroller {
|
|||
assertNotNull(password2);
|
||||
assertEquals(Integer.parseInt(passwordName) + 10,
|
||||
Integer.parseInt(password2));
|
||||
TestHelper.invokePrivate(ic, "releaseAll", null, null);
|
||||
ic.doCleanUp();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetParameter() throws Exception {
|
||||
ParamCoordinator paracCoor = new ParamCoordinator(UUID.randomUUID(),
|
||||
UUID.randomUUID());
|
||||
ParamFormatParser paramFormatParser = ParamFormatParser
|
||||
.parseInputParameter(NEW_TEST_CASE);
|
||||
String result = (String) TestHelper.invokePrivate(paracCoor,
|
||||
"getParameter", new Class[] { ParamFormatParser.class },
|
||||
new Object[] { paramFormatParser });
|
||||
assertEquals("11", result);
|
||||
paracCoor.doCleanUp();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetParameterCrossThread() throws Exception {
|
||||
ParamCoordinator paracCoor = new ParamCoordinator(UUID.randomUUID(),
|
||||
UUID.randomUUID());
|
||||
ParamFormatParser paramFormatParser = ParamFormatParser
|
||||
.parseInputParameter(NEW_TEST_CASE);
|
||||
String result = (String) TestHelper.invokePrivate(
|
||||
paracCoor,
|
||||
"getParameterCrossThread",
|
||||
new Class[] { String.class, String.class, String.class,
|
||||
Object[].class },
|
||||
new Object[] {
|
||||
paramFormatParser.getName(),
|
||||
"org.bench4q.agent.parameterization.impl."
|
||||
+ paramFormatParser.getClassName(),
|
||||
paramFormatParser.getMethodName(),
|
||||
paramFormatParser.getArgsArray() });
|
||||
assertEquals("11", result);
|
||||
paracCoor.doCleanUp();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue