refactor
This commit is contained in:
coderfengyun 2014-03-24 14:29:40 +08:00
parent 3483b72229
commit fed4cf7dd4
3 changed files with 127 additions and 77 deletions

View File

@ -8,18 +8,20 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.locks.ReentrantReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.apache.log4j.Logger;
import org.bench4q.agent.Main;
import org.bench4q.agent.parameterization.SessionObject; import org.bench4q.agent.parameterization.SessionObject;
public class ParamCoordinator implements SessionObject { public class ParamCoordinator implements SessionObject {
private String userDefineParameterFolderPath = "/home/yxsh/git/Bench4Q-Agent/parameterClass/";
private UUID testRunId; private UUID testRunId;
private UUID id = java.util.UUID.randomUUID(); private UUID id;
private Set<String> usedClassName = new HashSet<String>(); private Set<String> usedClassName = new HashSet<String>();
private Map<String, Object> objMap = new HashMap<String, Object>(); private Map<String, Object> objMap = new HashMap<String, Object>();
private Map<String, String> runtimeParamMap = new HashMap<String, String>(); private Map<String, String> runtimeParamMap = new HashMap<String, String>();
private Map<String, Object> cacheObjMap = new HashMap<String, Object>(); private Map<String, Object> cacheObjMap = new HashMap<String, Object>();
private ReentrantReadWriteLock mapRWLock = new ReentrantReadWriteLock(); private ReentrantReadWriteLock mapRWLock = new ReentrantReadWriteLock();
private Logger logger = Logger.getLogger(ParamCoordinator.class);
private ParamCoordinator() { private ParamCoordinator() {
} }
@ -41,15 +43,13 @@ public class ParamCoordinator implements SessionObject {
return result; return result;
String[] argsArray = paraFormat.getArgsArray(); String[] argsArray = paraFormat.getArgsArray();
if (paraFormat.getParamType().equals("crossThread")) { if (paraFormat.getParamType().equals("crossThread")) {
result = this.getParameter( result = this.getParameterCrossThread(paraFormat.getName(),
paraFormat.getName(), buildFullPath(paraFormat.getClassName()),
getCurrentPackageFullName() + "."
+ paraFormat.getClassName(),
paraFormat.getMethodName(), argsArray); paraFormat.getMethodName(), argsArray);
} else if (paraFormat.getParamType().equals("inThread")) { } else if (paraFormat.getParamType().equals("inThread")) {
result = this.instanceLevelGetParameter(paraFormat.getName(), result = this.getParameterInThread(paraFormat.getName(),
paraFormat.getClassName(), paraFormat.getMethodName(), buildFullPath(paraFormat.getClassName()),
argsArray); paraFormat.getMethodName(), argsArray);
} }
return result; return result;
} }
@ -59,7 +59,11 @@ public class ParamCoordinator implements SessionObject {
ParamFormatParser.class.getName().lastIndexOf('.')); 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) { String functionName, Object[] args) {
boolean hasThisClass = false; boolean hasThisClass = false;
@ -73,15 +77,7 @@ public class ParamCoordinator implements SessionObject {
Object result = null; Object result = null;
try { try {
Class<?>[] argTypeArr = new Class<?>[args.length + 2]; Class<?>[] argTypeArr = new Class<?>[args.length + 2];
argTypeArr[0] = id.getClass(); Object[] totalArgs = prepareTypeArrayAndArgArray(args, argTypeArr);
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];
}
Method m = instance.getClass().getMethod(functionName, argTypeArr); Method m = instance.getClass().getMethod(functionName, argTypeArr);
result = m.invoke(instance, totalArgs); result = m.invoke(instance, totalArgs);
} catch (Exception ex) { } catch (Exception ex) {
@ -105,7 +101,7 @@ public class ParamCoordinator implements SessionObject {
private boolean createObj(String className) { private boolean createObj(String className) {
try { try {
MyFileClassLoader classLoader = new MyFileClassLoader( MyFileClassLoader classLoader = new MyFileClassLoader(
userDefineParameterFolderPath); Main.USER_DEFINED_PARAMS_FOLDER);
Class<?> cls = classLoader.loadClass(className); Class<?> cls = classLoader.loadClass(className);
Object instance = cls.newInstance(); Object instance = cls.newInstance();
mapRWLock.writeLock().lock(); mapRWLock.writeLock().lock();
@ -125,42 +121,44 @@ public class ParamCoordinator implements SessionObject {
return result; return result;
} }
private String getParameter(String name, String className, private String getParameterCrossThread(String name, String className,
String functionName, Object[] args) { String functionName, Object[] args) {
ParametersFactory paramFactor = ParametersFactory.getInstance(); try {
boolean hasThisClass = paramFactor.containObj(className); Object instance = ParametersFactory.getInstance()
if (false == hasThisClass) { .getObjWithGuardExist(className);
paramFactor.createObj(className); Class<?>[] argTypeArr = new Class<?>[args.length + 2];
Object[] totalArgs = prepareTypeArrayAndArgArray(args, argTypeArr);
Method m = instance.getClass().getMethod(functionName, argTypeArr);
Object result = m.invoke(instance, totalArgs);
fillToContext(name, className, result);
return (String) result;
} catch (Exception ex) {
logger.error(ex, ex);
return null;
}
} }
Object instance = paramFactor.getObj(className); private void fillToContext(String name, String className, Object result) {
Object result = null; usedClassName.add(className);
try { runtimeParamMap.put(name, (String) result);
Class<?>[] argTypeArr = new Class<?>[args.length + 2]; }
argTypeArr[0] = id.getClass();
private Object[] prepareTypeArrayAndArgArray(Object[] args,
Class<?>[] argTypeArr) {
argTypeArr[0] = this.id.getClass();
argTypeArr[1] = this.cacheObjMap.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]; Object[] totalArgs = new Object[args.length + 2];
totalArgs[0] = id; totalArgs[0] = id;
totalArgs[1] = this.cacheObjMap; totalArgs[1] = this.cacheObjMap;
for (int i = 2; i < args.length + 2; i++) { for (int i = 2; i < args.length + 2; i++) {
argTypeArr[i] = args[i - 2].getClass();
totalArgs[i] = args[i - 2]; totalArgs[i] = args[i - 2];
} }
return totalArgs;
Method m = instance.getClass().getMethod(functionName, argTypeArr);
result = m.invoke(instance, totalArgs);
} catch (Exception ex) {
System.out.println(ex.getMessage() + ex.getStackTrace());
System.out.println(((InvocationTargetException) ex)
.getTargetException().getMessage());
return null;
}
usedClassName.add(className);
runtimeParamMap.put(name, (String) result);
return (String) result;
} }
/** /**
@ -193,27 +191,25 @@ public class ParamCoordinator implements SessionObject {
for (String xx : usedClassName) { for (String xx : usedClassName) {
release(xx); 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) { 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 { try {
Object instance = ParametersFactory.getInstance()
.getObjWithGuardExist(className);
Method m = instance.getClass().getMethod("unreg", id.getClass()); Method m = instance.getClass().getMethod("unreg", id.getClass());
m.invoke(instance, id); m.invoke(instance, id);
} catch (Exception ex) { } catch (Exception ex) {
System.out.println("realse failed"); System.out.println("realse failed");
} }
} }
} }

View File

@ -4,20 +4,39 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.locks.ReentrantReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.bench4q.agent.Main;
public class ParametersFactory { public class ParametersFactory {
private static final ParametersFactory instance = new ParametersFactory(); private static final ParametersFactory instance = new ParametersFactory();
private Map<String, Object> objMap = new HashMap<String, Object>(); private Map<String, Object> objMap = new HashMap<String, Object>();
private ReentrantReadWriteLock mapRWLock = new ReentrantReadWriteLock(); private ReentrantReadWriteLock mapRWLock = new ReentrantReadWriteLock();
private String rootFilePath = "/home/yxsh/git/Bench4Q-Agent/parameterClass/";
private ParametersFactory() { private ParametersFactory() {
} }
static public ParametersFactory getInstance() { public static ParametersFactory getInstance() {
return instance; 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; boolean ret = false;
mapRWLock.readLock().lock(); mapRWLock.readLock().lock();
ret = objMap.containsKey(className); ret = objMap.containsKey(className);
@ -25,27 +44,21 @@ public class ParametersFactory {
return ret; return ret;
} }
public Object getObj(String className) { private boolean createObj(String className) {
Object result = null;
mapRWLock.readLock().lock();
result = objMap.get(className);
mapRWLock.readLock().unlock();
return result;
}
public boolean createObj(String className) {
try { try {
MyFileClassLoader cl = new MyFileClassLoader(rootFilePath); MyFileClassLoader cl = new MyFileClassLoader(
Main.USER_DEFINED_PARAMS_FOLDER);
Class<?> cls = cl.loadClass(className); Class<?> cls = cl.loadClass(className);
Object instance = cls.newInstance(); Object instance = cls.newInstance();
mapRWLock.writeLock().lock(); mapRWLock.writeLock().lock();
objMap.put(className, instance); objMap.put(className, instance);
return true;
} catch (Exception ex) { } catch (Exception ex) {
System.out.println(ex.getMessage()); System.out.println(ex.getMessage());
} return false;
} finally {
mapRWLock.writeLock().unlock(); mapRWLock.writeLock().unlock();
return true; }
} }
} }

View File

@ -7,7 +7,9 @@ import java.util.UUID;
import org.bench4q.agent.parameterization.impl.ParamCoordinator; import org.bench4q.agent.parameterization.impl.ParamCoordinator;
import org.bench4q.agent.parameterization.impl.Para_Table; import org.bench4q.agent.parameterization.impl.Para_Table;
import org.bench4q.agent.parameterization.impl.ParamFormatParser;
import org.bench4q.share.helper.TestHelper; import org.bench4q.share.helper.TestHelper;
import org.junit.After;
import org.junit.Test; import org.junit.Test;
public class Test_InstanceCotroller { 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\" />"; // "<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\" />"; 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 @Test
public void testGetParam() throws Exception { public void testGetParam() throws Exception {
Para_Table table = new Para_Table(); Para_Table table = new Para_Table();
@ -45,6 +52,40 @@ public class Test_InstanceCotroller {
assertNotNull(password2); assertNotNull(password2);
assertEquals(Integer.parseInt(passwordName) + 10, assertEquals(Integer.parseInt(passwordName) + 10,
Integer.parseInt(password2)); 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();
} }
} }