refactor the plugin ,change eager get method to lazy get method ,

and change get the interface of get method list
This commit is contained in:
fanfuxiaoran 2014-04-03 13:49:49 +08:00
parent b50e910726
commit e25a35d20b
10 changed files with 151 additions and 77 deletions

View File

@ -1,17 +1,16 @@
<html>
<head>
<title>Bench4Q Test Case</title>
<link href="style/bootstrap-cerulean.css" />
<link href="style/bootstrap-classic.css" />
<link href="style/bootstrap-cerulean.css" />
</head>
<body>
<img src="images/1.jpg" alt="No this one" />
<img src="images/2.jpg" alt="No this one" />
<img src="images/3.jpg" alt="No this one" />
<script src="script/agentTable.js" type="text/javascript"></script>
<script src="script/base.js" type="text/javascript"></script>
</body>
</html
<html>
<head>
<title>Bench4Q Test Case</title>
<link href="style/bootstrap-cerulean.css" />
<link href="style/bootstrap-classic.css" />
<link href="style/bootstrap-cerulean.css" />
</head>
<body>
<img src="images/1.jpg" alt="No this one" />
<img src="images/2.jpg" alt="No this one" />
<img src="images/3.jpg" alt="No this one" />
<script src="script/agentTable.js" type="text/javascript"></script>
<script src="script/base.js" type="text/javascript"></script>
</body>
</html>

View File

@ -45,6 +45,31 @@ public class PluginController extends BaseController {
return pluginResponseModel;
}
@RequestMapping(value = "loadMethods/{pluginName}", method = {
RequestMethod.GET, RequestMethod.POST })
@ResponseBody
public PluginResponseModel getMethods(
@PathVariable("pluginName") String pluginName)
throws Bench4QException {
if (!this.checkScope(UserService.NORAML_AUTHENTICATION)) {
throw new Bench4QException(400 + "", "not permitted",
"/loadMethods/{pluginName}");
}
PluginResponseModel pluginResponseModel = new PluginResponseModel();
try {
pluginResponseModel.setMethodModels(this.getPluginService()
.getMethodsInPlugin(pluginName));
pluginResponseModel.setSuccess(true);
return pluginResponseModel;
} catch (Exception e) {
logger.error(ExceptionLog.getStackTrace(e));
pluginResponseModel.setSuccess(false);
return pluginResponseModel;
}
}
@RequestMapping(value = "loadMethodList/{pluginName}", method = {
RequestMethod.GET, RequestMethod.POST })
@ResponseBody
@ -55,15 +80,17 @@ public class PluginController extends BaseController {
throw new Bench4QException(400 + "", "not permitted",
"/loadMethodList/{pluginName}");
}
PluginResponseModel pluginResponseModel = new PluginResponseModel();
try {
PluginResponseModel pluginResponseModel = new PluginResponseModel();
pluginResponseModel.setMethodList(this.getPluginService()
.getMethodNameInPlugin(pluginName));
pluginResponseModel.setSuccess(true);
return pluginResponseModel;
} catch (Exception e) {
logger.error(ExceptionLog.getStackTrace(e));
return null;
pluginResponseModel.setSuccess(false);
return pluginResponseModel;
}
}
@ -85,7 +112,6 @@ public class PluginController extends BaseController {
return pluginResponseModel;
}
//need to get info from master,then insert to dataBase
@RequestMapping(value = "/addPlugin", method = { RequestMethod.PUT })
@ResponseBody
public PluginResponseModel addPlugin(@RequestBody PluginGUI pluginGUI)
@ -94,13 +120,12 @@ public class PluginController extends BaseController {
throw new Bench4QException(400 + "", "not permitted",
"addPlugin/{pluginName}/{methodName}");
}
if (pluginGUI.getPlugin() == null) {
throw new Bench4QException(400 + "", "plugin is null", "addPlugin");
PluginResponseModel pluginResponseModel = validatePlugin(pluginGUI);
if (pluginResponseModel.isSuccess()) {
pluginResponseModel.setSuccess(this.getPluginService().addPlugin(
pluginGUI.getPlugin()));
}
PluginResponseModel pluginResponseModel = new PluginResponseModel();
pluginResponseModel.setSuccess(this.getPluginService().addPlugin(
pluginGUI.getPlugin()));
return pluginResponseModel;
}
@ -122,4 +147,19 @@ public class PluginController extends BaseController {
}
private PluginResponseModel validatePlugin(PluginGUI pluginGUI) {
PluginResponseModel pluginResponseModel = new PluginResponseModel();
pluginResponseModel.setSuccess(true);
if (pluginGUI.getPlugin() == null) {
pluginResponseModel.setSuccess(false);
pluginResponseModel.setFailMessage("plugin is null");
}
if (pluginGUI.getPlugin().getMethods() == null
|| pluginGUI.getPlugin().getMethods().size() == 0) {
pluginResponseModel.setSuccess(false);
pluginResponseModel.setFailMessage("method is empty");
}
return pluginResponseModel;
}
}

View File

@ -39,7 +39,7 @@ public class Plugin {
this.name = name;
}
@OneToMany(mappedBy = "plugin", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
@OneToMany(mappedBy = "plugin", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
public Set<Method> getPluginMethods() {
return pluginMethods;
}

View File

@ -2,6 +2,7 @@ package org.bench4q.master.domain.repository;
import java.util.List;
import org.bench4q.master.domain.entity.plugin.Method;
import org.bench4q.master.domain.entity.plugin.ParamType;
import org.bench4q.master.domain.entity.plugin.Plugin;
import org.bench4q.master.exception.Bench4QException;
@ -40,8 +41,6 @@ public class PluginRepository extends AbstractRepositoty {
ParamType paramType = null;
Session session = this.getSessionHelper().openSession();
Session session2=this.getSessionHelper().openSession();
System.out.println(session==session2);
@SuppressWarnings("unchecked")
List<ParamType> ret = session.createCriteria(ParamType.class).list();
if (ret.size() == 0) {
@ -66,7 +65,6 @@ public class PluginRepository extends AbstractRepositoty {
.uniqueResult();
if (pluginExist != null) {
logger.info("the plugin exist");
return false;
}
session.merge(plugin);
@ -99,7 +97,6 @@ public class PluginRepository extends AbstractRepositoty {
.add(Restrictions.eq("name", pluginName)).uniqueResult();
if (pluginExist == null) {
logger.info("plugin not exist");
System.out.println("not exist");
return false;
}
session.delete(pluginExist);
@ -153,4 +150,15 @@ public class PluginRepository extends AbstractRepositoty {
return (Plugin) session.createCriteria(Plugin.class)
.add(Restrictions.eq("name", pluginName)).uniqueResult();
}
public List<Method> loadMethodsInPlugin(Plugin plugin) {
Session session = this.getSessionHelper().openSession();
@SuppressWarnings("unchecked")
List<Method> methods = (List<Method>) session
.createCriteria(Method.class)
.add(Restrictions.eq("plugin", plugin)).list();
releaseSession(session);
return methods;
}
}

View File

@ -6,12 +6,12 @@ import java.util.List;
import java.util.Set;
import org.apache.log4j.Logger;
import org.bench4q.master.domain.entity.plugin.MethodParam;
import org.bench4q.master.domain.entity.plugin.Plugin;
import org.bench4q.master.domain.repository.PluginRepository;
import org.bench4q.master.exception.Bench4QException;
import org.bench4q.master.domain.entity.plugin.Method;
import org.bench4q.master.domain.factory.PluginFactory;
import org.bench4q.share.models.master.plugin.MethodModel;
import org.bench4q.share.models.master.plugin.MethodParamModel;
import org.bench4q.share.models.master.plugin.PluginModel;
import org.springframework.beans.factory.annotation.Autowired;
@ -41,41 +41,16 @@ public class PluginService {
this.pluginFactory = pluginFactory;
}
private Set<Plugin> getPlugins() throws Bench4QException {
List<Plugin> loadPluginList = this.getPluginRepository().loadPlugins();
Set<Plugin> plugins = new HashSet<Plugin>();
if (loadPluginList != null) {
for (Plugin plugin : loadPluginList) {
plugins.add(plugin);
}
}
return plugins;
}
public boolean addPlugin(PluginModel pluginModel) throws Bench4QException {
if (validatePlugin(pluginModel))
return this.getPluginRepository().attatch(
this.getPluginFactory().createPluginEntity(pluginModel));
else {
throw new Bench4QException("", "invalidate plugin file", "");
}
}
return this.getPluginRepository().attatch(
private boolean validatePlugin(PluginModel pluginModel) {
return true;
this.getPluginFactory().createPluginEntity(pluginModel));
}
public boolean deletePlugin(String pluginName) throws Bench4QException {
return this.getPluginRepository().detach(pluginName);
}
private Plugin getPluginByName(String pluginName) {
return this.getPluginRepository().getPlugin(pluginName);
}
public List<String> getPluginNameList() throws Bench4QException {
List<String> pluginNameList = new ArrayList<String>();
Set<Plugin> plugins = getPlugins();
@ -87,10 +62,24 @@ public class PluginService {
return pluginNameList;
}
private Set<Plugin> getPlugins() throws Bench4QException {
List<Plugin> loadPluginList = this.getPluginRepository().loadPlugins();
Set<Plugin> plugins = new HashSet<Plugin>();
if (loadPluginList != null) {
for (Plugin plugin : loadPluginList) {
plugins.add(plugin);
}
}
return plugins;
}
public List<String> getMethodNameInPlugin(String pluginName) {
List<String> methodNameList = new ArrayList<String>();
Set<Method> methods = this.getPluginByName(pluginName)
.getPluginMethods();
Plugin plugin = this.getPluginRepository().getPlugin(pluginName);
List<Method> methods = this.getPluginRepository().loadMethodsInPlugin(
plugin);
if (methods != null) {
for (Method method : methods) {
@ -102,7 +91,9 @@ public class PluginService {
private Method getMethodInPlugin(String pluginName, String methodName)
throws Bench4QException {
Set<Method> methods = this.getMethodInPlugin(pluginName);
Plugin plugin = this.getPluginRepository().getPlugin(pluginName);
List<Method> methods = this.getPluginRepository().loadMethodsInPlugin(
plugin);
if (methods != null) {
for (Method method : methods) {
if (method.getName().equals(methodName))
@ -123,15 +114,14 @@ public class PluginService {
.getMethodParams();
}
private Set<Method> getMethodInPlugin(String pluginName) {
return this.getPluginByName(pluginName).getPluginMethods();
}
public Set<MethodParam> getMethodParams(String pluginName, String methodName)
throws Bench4QException {
Method method = this.getMethodInPlugin(pluginName, methodName);
return method.getMethodParams();
public Set<MethodModel> getMethodsInPlugin(String pluginName) {
Plugin plugin = this.getPluginRepository().getPlugin(pluginName);
Set<MethodModel> methodModels = new HashSet<MethodModel>();
for (Method method : this.getPluginRepository().loadMethodsInPlugin(
plugin)) {
methodModels.add(PluginFactory.extractMethodModel(method));
}
return methodModels;
}
}

View File

@ -13,7 +13,6 @@ public class CollectionHelper {
*/
public static <T extends EntityBase, ID> T tryGetItemById(
List<T> container, ID id) {
System.out.println(id.toString());
for (T item : container) {
if (id.toString().equals(item.extractIdentifier().toString())) {
return item;

View File

@ -143,9 +143,7 @@ public class HighAvailablePool extends CurrentLoadSubject {
this.setCurrentAvailableLoad(0);
this.setMaxAvailableLoad((long) 0);
for (Agent agent : this.agentService.loadAgentPoolFromDB()) {
System.out.println(System.currentTimeMillis());
checkHeartBeat(agent);
System.out.println(System.currentTimeMillis());
}
}

View File

@ -6,6 +6,7 @@ import java.io.IOException;
import javax.xml.bind.JAXBException;
import org.apache.log4j.Logger;
import org.bench4q.share.communication.HttpRequester.HttpResponse;
import org.bench4q.share.helper.MarshalHelper;
import org.bench4q.share.models.master.plugin.PluginGUI;
@ -28,7 +29,7 @@ public class PluginControllerTest extends TestBase {
@Before
public void setUp() throws IOException, JAXBException {
testAddPlugin();
testAddPlugin();
}
@Test
@ -40,12 +41,17 @@ public class PluginControllerTest extends TestBase {
pluginName = pluginGUI.getPlugin().getName();
String pluginContentString = MarshalHelper.marshal(PluginGUI.class,
pluginGUI);
HttpResponse httpResponse=this.httpRequester.sendPutXml(url, pluginContentString,
HttpResponse httpResponse = this.httpRequester.sendPutXml(url,
pluginContentString,
makeAccessTockenMap(this.getAccessTocken()));
PluginResponseModel pluginResponseModel = (PluginResponseModel) MarshalHelper
.tryUnmarshal(PluginResponseModel.class,
httpResponse.getContent());
Assert.assertNotNull(pluginResponseModel);
Logger.getLogger(PluginControllerTest.class).info(
"add:"
+ MarshalHelper.marshal(PluginResponseModel.class,
pluginResponseModel));
Assert.assertTrue(pluginResponseModel.isSuccess());
}
@ -95,7 +101,7 @@ public class PluginControllerTest extends TestBase {
.unmarshal(PluginResponseModel.class, httpResponse.getContent());
assertNotNull(pluginResponseModel);
Assert.assertTrue(pluginResponseModel.isSuccess());
assertEquals(1, pluginResponseModel.getMethodList().size());
assertEquals(2, pluginResponseModel.getMethodList().size());
}
@Test
@ -117,6 +123,23 @@ public class PluginControllerTest extends TestBase {
Assert.assertTrue(pluginResponseModel.isSuccess());
}
@Test
public void testGetMethodInPlugin() throws IOException {
String url = URLSTRING + "/loadMethods/" + pluginName;
HttpResponse httpResponse = this.httpRequester.sendPost(url, null,
makeAccessTockenMap(this.getAccessTocken()));
Logger.getLogger(PluginControllerTest.class).info(
httpResponse.getContent());
PluginResponseModel pluginResponseModel = (PluginResponseModel) MarshalHelper
.tryUnmarshal(PluginResponseModel.class,
httpResponse.getContent());
assertNotNull(pluginResponseModel);
assertTrue(pluginResponseModel.isSuccess());
assertTrue(pluginResponseModel.getMethodModels().size() == 2);
}
@After
public void clear() throws IOException {
String url = URLSTRING + "/deletePlugin" + "/" + pluginName;

View File

@ -2,6 +2,9 @@ package org.bench4q.master.test.repository;
import static org.junit.Assert.*;
import java.util.List;
import org.bench4q.master.domain.entity.plugin.Method;
import org.bench4q.master.domain.entity.plugin.MethodParam;
import org.bench4q.master.domain.entity.plugin.ParamType;
import org.bench4q.master.domain.entity.plugin.Plugin;
@ -91,6 +94,14 @@ public class Test_PluginRepository {
}
@Test
public void testLoadMethodInPlugin(){
Plugin plugin=this.getPluginRepository().getPlugin(this.pluginName);
List<Method> methods=this.getPluginRepository().loadMethodsInPlugin(plugin);
assertNotNull(methods);
assertTrue(methods.size()>0);
}
@After
public void clear() throws Bench4QException {
this.getPluginRepository().detach(pluginName);

View File

@ -66,7 +66,7 @@ public class Test_PluginService {
assertTrue(this.getPluginService().getPluginNameList().size() > 0);
int methodCount = this.getPluginService()
.getMethodNameInPlugin(nameForPlugin).size();
assertEquals(1, methodCount);
assertEquals(2, methodCount);
}
@Test
@ -96,6 +96,12 @@ public class Test_PluginService {
}
@Test
public void testGetMethodInPlugin() {
assertTrue(this.getPluginService().getMethodsInPlugin(nameForPlugin)
.size() > 0);
}
private String addPlugin() throws Bench4QException {
PluginModel plugin = Test_PlunginHelper.createOnePlugin();
plugin.setName("test" + UUID.randomUUID());