finish plugin api

This commit is contained in:
fanfuxiaoran 2014-03-11 17:25:26 +08:00
parent c9d020cffa
commit 7548eb9faf
6 changed files with 120 additions and 34 deletions

View File

@ -6,7 +6,6 @@ import org.apache.log4j.Logger;
import org.bench4q.master.domain.entity.User;
import org.bench4q.master.domain.service.auth.AuthenticationManager;
import org.bench4q.master.exception.Bench4QException;
import org.bench4q.master.exception.ExceptionLog;
import org.bench4q.share.models.ErrorResponseModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
@ -44,6 +43,7 @@ public abstract class BaseController {
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
@ResponseBody
public ErrorResponseModel handleException(Bench4QException e) {
logger.error(e,e.fillInStackTrace());
return ErrorResponseModel.buildErrorResponse(e.getCode(),
e.getMessage(), e.getResource());
}
@ -52,7 +52,7 @@ public abstract class BaseController {
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
@ResponseBody
public ErrorResponseModel handleException(RuntimeException e) {
logger.info(ExceptionLog.getStackTrace(e));
logger.error(e,e.fillInStackTrace());
return ErrorResponseModel.buildErrorResponse("RUNTIME_EXCEPTION",
e.getMessage(), "");
}

View File

@ -17,7 +17,7 @@ import javax.persistence.Table;
public class Plugin {
private int id;
private String name;
private Set<Method> methods;
private Set<Method> pluginMethods;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@ -39,13 +39,14 @@ public class Plugin {
this.name = name;
}
@OneToMany(mappedBy = "plugin", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
public Set<Method> getMethods() {
return methods;
public Set<Method> getPluginMethods() {
return pluginMethods;
}
public void setMethods(Set<Method> methods) {
this.methods = methods;
public void setPluginMethods(Set<Method> pluginMethods) {
this.pluginMethods = pluginMethods;
}
}

View File

@ -1,5 +1,8 @@
package org.bench4q.master.domain.factory;
import java.util.ArrayList;
import java.util.HashSet;
import org.bench4q.master.domain.entity.plugin.Method;
import org.bench4q.master.domain.entity.plugin.MethodParam;
import org.bench4q.master.domain.entity.plugin.ParamProperty;
@ -13,10 +16,11 @@ public class PluginFactory {
public static Plugin createPluginEntity(PluginModel pluginModel) {
Plugin plugin = new Plugin();
if (plugin.getMethods() != null) {
plugin.setPluginMethods(new HashSet<Method>());
if (pluginModel.getMethods() != null) {
for (MethodModel methodModel : pluginModel.getMethods()) {
plugin.getMethods()
.add(createMethodEntity(methodModel, plugin));
plugin.getPluginMethods().add(
createMethodEntity(methodModel, plugin));
}
}
@ -28,6 +32,7 @@ public class PluginFactory {
private static Method createMethodEntity(MethodModel methodModel,
Plugin plugin) {
Method method = new Method();
method.setMethodParams(new HashSet<MethodParam>());
method.setName(methodModel.getName());
if (methodModel.getMethodParams() != null) {
for (MethodParamModel methodParamModel : methodModel
@ -48,6 +53,7 @@ public class PluginFactory {
methodParam.setLable(methodParamModel.getLable());
methodParam.setName(methodParamModel.getName());
methodParam.setSeperator(methodParamModel.getSeperator());
methodParam.setParamProperties(new ArrayList<ParamProperty>());
if (methodParamModel.getParamProperties() != null) {
for (ParamPropertyModel paramPropertyModel : methodParamModel
.getParamProperties()) {
@ -69,4 +75,45 @@ public class PluginFactory {
paramProperty.setMethodParam(methodParam);
return paramProperty;
}
public static MethodModel extractMethodModel(Method method) {
MethodModel methodModel = new MethodModel();
methodModel.setName(method.getName());
if (method.getMethodParams() != null) {
methodModel.setMethodParams(new HashSet<MethodParamModel>());
for (MethodParam methodParam : method.getMethodParams()) {
methodModel.getMethodParams().add(
extractMethodParamModel(methodParam));
}
}
return methodModel;
}
private static MethodParamModel extractMethodParamModel(
MethodParam methodParam) {
MethodParamModel methodParamModel = new MethodParamModel();
methodParamModel.setEditorType(methodParam.getEditorType());
methodParamModel.setLable(methodParam.getLable());
methodParamModel.setName(methodParam.getName());
methodParamModel.setSeperator(methodParam.getSeperator());
if (methodParam.getParamProperties() != null) {
methodParamModel
.setParamProperties(new ArrayList<ParamPropertyModel>());
for (ParamProperty paramProperty : methodParam.getParamProperties()) {
methodParamModel.getParamProperties().add(
extractParamPropertyModel(paramProperty));
}
}
return methodParamModel;
}
private static ParamPropertyModel extractParamPropertyModel(
ParamProperty paramProperty) {
ParamPropertyModel paramPropertyModel = new ParamPropertyModel();
paramPropertyModel.setKey(paramProperty.getPropertyName());
paramPropertyModel.setValue(paramProperty.getPropertyValue());
paramPropertyModel.setValueSeperator(paramProperty.getValueSeperator());
return paramPropertyModel;
}
}

View File

@ -13,6 +13,7 @@ import org.springframework.stereotype.Component;
@Component
public class PluginRepository extends AbstractRepositoty {
public boolean attatch(Plugin plugin) throws Bench4QException {
Session session = this.getSessionHelper().openSession();
Transaction transaction = session.beginTransaction();
Plugin pluginExist = null;
@ -23,6 +24,7 @@ public class PluginRepository extends AbstractRepositoty {
;
if (pluginExist != null) {
logger.info("the plugin exist");
return false;
}
session.merge(plugin);
@ -55,6 +57,7 @@ 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);

View File

@ -9,6 +9,9 @@ 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.MethodParamModel;
import org.bench4q.share.models.master.plugin.PluginModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@ -33,8 +36,9 @@ public class PluginService {
return this.plugins;
}
public boolean addPlugin(Plugin plugin) throws Bench4QException {
return this.getPluginRepository().attatch(plugin);
public boolean addPlugin(PluginModel pluginModel) throws Bench4QException {
return this.getPluginRepository().attatch(
PluginFactory.createPluginEntity(pluginModel));
}
public boolean deletePlugin(String pluginName) throws Bench4QException {
@ -58,7 +62,8 @@ public class PluginService {
public List<String> getMethodNameInPlugin(String pluginName) {
List<String> methodNameList = new ArrayList<String>();
Set<Method> methods = this.getPluginByName(pluginName).getMethods();
Set<Method> methods = this.getPluginByName(pluginName)
.getPluginMethods();
if (methods != null) {
for (Method method : methods) {
methodNameList.add(method.getName());
@ -82,15 +87,22 @@ public class PluginService {
}
private Set<Method> getMethodInPlugin(String pluginName) {
return this.getPluginByName(pluginName).getMethods();
public Set<MethodParamModel> getMethodParamModelsInPlugin(
String pluginName, String methodName) throws Bench4QException {
return PluginFactory.extractMethodModel(
this.getMethodInPlugin(pluginName, methodName))
.getMethodParams();
}
public Set<MethodParam> getMethodParams(String pluginName,
String methodName) throws Bench4QException {
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();
}
}
}

View File

@ -8,12 +8,11 @@ import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import javax.xml.bind.JAXBException;
import org.apache.commons.io.FileUtils;
import org.bench4q.master.domain.entity.plugin.Plugin;
import org.bench4q.master.domain.factory.PluginFactory;
import org.bench4q.master.domain.service.PluginService;
import org.bench4q.master.exception.Bench4QException;
import org.bench4q.share.helper.MarshalHelper;
@ -21,7 +20,6 @@ import org.bench4q.share.models.master.plugin.MethodModel;
import org.bench4q.share.models.master.plugin.MethodParamModel;
import org.bench4q.share.models.master.plugin.ParamPropertyModel;
import org.bench4q.share.models.master.plugin.PluginModel;
import org.bench4q.share.models.master.plugin.PluginGUI;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
@ -65,9 +63,41 @@ public class Test_PluginService {
@Test
public void testAddPlugin() throws JAXBException, Bench4QException {
Plugin plugin=PluginFactory.createPluginEntity(createOnePlugin());
Assert.assertTrue(this.getPluginService().addPlugin(createOnePlugin()));
}
@Test
public void testDeletePlugin() throws Bench4QException {
PluginModel plugin = createOnePlugin();
plugin.setName("ppp");
Assert.assertTrue(this.getPluginService().addPlugin(plugin));
Assert.assertTrue(this.getPluginService()
.deletePlugin(plugin.getName()));
}
@Test
public void testLoadPluginNameList() throws Bench4QException {
PluginModel plugin = createOnePlugin();
plugin.setName("ppp");
Assert.assertTrue(this.getPluginService().addPlugin(plugin));
List<String> pluginNameList = this.getPluginService()
.getPluginNameList();
Assert.assertTrue(pluginNameList.size() >0);
Assert.assertTrue(this.getPluginService()
.deletePlugin(plugin.getName()));
}
@Test
public void testGetMethodNameList() throws Bench4QException {
PluginModel plugin = createOnePlugin();
plugin.setName("ppp");
Assert.assertTrue(this.getPluginService().addPlugin(plugin));
List<String> methodNameList = this.getPluginService()
.getMethodNameInPlugin(plugin.getName());
Assert.assertTrue(methodNameList.size() == 1);
Assert.assertTrue(this.getPluginService()
.deletePlugin(plugin.getName()));
}
private PluginModel createOnePlugin() {
@ -85,28 +115,21 @@ public class Test_PluginService {
methodParamParam.setSeperator("&");
methodParamParam.setParamProperties(generateParamProperties());
List<MethodParamModel> methodParams = new ArrayList<MethodParamModel>();
Set<MethodParamModel> methodParams = new HashSet<MethodParamModel>();
methodParams.add(methodParamParam);
methodParams.add(methodParamParam);
method.setName("get");
method.setParams(methodParams);
method.setMethodParams(methodParams);
Set<MethodModel> methods = new HashSet<MethodModel>();
methods.add(method);
plugin.setMethods(methods);
plugin.setName("http");
plugin.setName("http" + UUID.randomUUID());
return plugin;
}
private PluginGUI generateOnePluginGUI() {
PluginGUI pluginGUI = new PluginGUI();
pluginGUI.setPlugin(createOnePlugin());
return pluginGUI;
}
private List<ParamPropertyModel> generateUrlProperties() {
ParamPropertyModel paramPropertyUrl = new ParamPropertyModel();
paramPropertyUrl.setKey("size");