add plugin service and controller

This commit is contained in:
fanfuxiaoran 2014-04-16 17:09:29 +08:00
parent 3190963c2d
commit 22ef97bd37
12 changed files with 222 additions and 44 deletions

View File

@ -1,21 +1,21 @@
package org.bench4q.master.api;
import java.util.List;
import org.apache.log4j.Logger;
import org.bench4q.master.domain.service.PluginService;
import org.bench4q.master.domain.service.UserService;
import org.bench4q.master.exception.Bench4QException;
import org.bench4q.master.exception.ExceptionLog;
import org.bench4q.share.models.master.plugin.BehaviorInfoModel;
import org.bench4q.share.models.master.plugin.PluginResponseModel;
import org.bench4q.share.models.master.plugin.PluginUIModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
@Controller
@RequestMapping("/plugin")
public class PluginController extends BaseController {
@ -31,12 +31,39 @@ public class PluginController extends BaseController {
this.pluginService = pluginService;
}
@RequestMapping("/loadPluginList")
@RequestMapping(value = "loadPluginUI", method = { RequestMethod.GET,
RequestMethod.POST })
@ResponseBody
public PluginResponseModel loadPluginUIModels() throws Bench4QException {
if (!this.checkScope(UserService.NORAML_AUTHENTICATION)) {
throw new Bench4QException(400 + "", "not permitted",
"/loadPluginUI");
}
PluginResponseModel pluginResponseModel = new PluginResponseModel();
try {
List<PluginUIModel> pluginUIModels = this.getPluginService()
.loadPluginUIs();
if (pluginUIModels == null) {
pluginResponseModel.setSuccess(false);
} else {
pluginResponseModel.setSuccess(true);
pluginResponseModel.setPluginUIModels(pluginUIModels);
}
return pluginResponseModel;
} catch (Exception e) {
logger.info(ExceptionLog.getStackTrace(e));
pluginResponseModel.setSuccess(false);
return pluginResponseModel;
}
}
@RequestMapping("/loadPluginNameList")
@ResponseBody
public PluginResponseModel loadPluginList() throws Bench4QException {
if (!this.checkScope(UserService.NORAML_AUTHENTICATION)) {
throw new Bench4QException(400 + "", "not permitted",
"/loadPluginList");
"/loadPluginNameList");
}
PluginResponseModel pluginResponseModel = new PluginResponseModel();
pluginResponseModel.setPluginList(this.getPluginService()
@ -46,7 +73,7 @@ public class PluginController extends BaseController {
return pluginResponseModel;
}
@RequestMapping(value = "loadMethods/{pluginName}", method = {
@RequestMapping(value = "loadBehaviors/{pluginName}", method = {
RequestMethod.GET, RequestMethod.POST })
@ResponseBody
public PluginResponseModel getMethods(
@ -55,14 +82,19 @@ public class PluginController extends BaseController {
if (!this.checkScope(UserService.NORAML_AUTHENTICATION)) {
throw new Bench4QException(400 + "", "not permitted",
"/loadMethods/{pluginName}");
"/loadBehaviors/{pluginName}");
}
PluginResponseModel pluginResponseModel = new PluginResponseModel();
try {
List<BehaviorInfoModel> behaviorInfoModels = this
.getPluginService().loadBehaviorInfoModels(pluginName);
if (behaviorInfoModels == null)
pluginResponseModel.setSuccess(false);
else {
pluginResponseModel.setSuccess(true);
pluginResponseModel.setBehaviorInfoModels(behaviorInfoModels);
}
// pluginResponseModel.setMethodModels(this.getPluginService()
// .getMethodsInPlugin(pluginName));
pluginResponseModel.setSuccess(true);
return pluginResponseModel;
} catch (Exception e) {
logger.error(ExceptionLog.getStackTrace(e));
@ -73,8 +105,7 @@ public class PluginController extends BaseController {
@RequestMapping(value = "/addPlugin", method = { RequestMethod.PUT })
@ResponseBody
public PluginResponseModel addPlugin(
@RequestParam("script") CommonsMultipartFile plugin)
public PluginResponseModel addPlugin(@RequestBody String plugin)
throws Bench4QException {
if (!this.checkScope(UserService.SUPER_AUTHENTICATION)) {
throw new Bench4QException(400 + "", "not permitted",
@ -82,7 +113,7 @@ public class PluginController extends BaseController {
}
String pluginUIContent = new String(plugin.getBytes());
PluginResponseModel pluginResponseModel = new PluginResponseModel();
pluginResponseModel.setSuccess(false);
if (pluginResponseModel.isSuccess()) {
pluginResponseModel.setSuccess(this.getPluginService().addPlugin(
pluginUIContent));

View File

@ -40,5 +40,4 @@ public class ChoiceType extends ParamType {
public void setParentContainer(ParamType parentContainer) {
this.parentContainer = parentContainer;
}
}

View File

@ -33,14 +33,14 @@ public class PluginInfo {
@Column(name = "name", nullable = false)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@OneToMany(mappedBy = "pluginInfo", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
@OneToMany(mappedBy = "pluginInfo", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
public Set<PluginParamInfo> getPluginParamInfos() {
return pluginParamInfos;
}

View File

@ -1,5 +1,6 @@
package org.bench4q.master.domain.entity.plugin;
import java.util.Date;
import java.util.Set;
import javax.persistence.CascadeType;
@ -21,6 +22,7 @@ public class PluginUI {
private int id;
private PluginInfo pluginInfo;
private Set<BehaviorInfo> behaviorInfos;
private Date createDateTime;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@ -52,4 +54,13 @@ public class PluginUI {
this.behaviorInfos = behaviorInfos;
}
@Column(name = "createTime")
public Date getCreateDateTime() {
return createDateTime;
}
public void setCreateDateTime(Date createDateTime) {
this.createDateTime = createDateTime;
}
}

View File

@ -44,13 +44,12 @@ public class PluginDomainFactory {
this.paramTypeModelFactory = paramTypeModelFactory;
}
public PluginUIModel createPluginUIModel(PluginUI pluginUI) {
public PluginUIModel createPluginUIModelwithOutBehaviors(PluginUI pluginUI) {
PluginUIModel pluginUIModel = new PluginUIModel();
pluginUIModel.setPluginInfoModel(createPluginInfoModel(pluginUI
.getPluginInfo()));
pluginUIModel.setBehaviorInfos(createBehaviorInfoModels(pluginUI.getBehaviorInfos()));
pluginUIModel.setCreateTimeDate(pluginUI.getCreateDateTime());
return pluginUIModel;
}
private PluginInfoModel createPluginInfoModel(PluginInfo pluginInfo) {
@ -61,7 +60,7 @@ public class PluginDomainFactory {
return pluginInfoModel;
}
private List<BehaviorInfoModel> createBehaviorInfoModels(
public List<BehaviorInfoModel> createBehaviorInfoModels(
Set<BehaviorInfo> behaviorInfos) {
List<BehaviorInfoModel> behaviorInfoModels = new LinkedList<BehaviorInfoModel>();
for (BehaviorInfo behaviorInfo : behaviorInfos) {

View File

@ -1,5 +1,6 @@
package org.bench4q.master.domain.factory;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
@ -39,6 +40,7 @@ public class PluginEntityFactory {
pluginUI.setBehaviorInfos(createBehaviorInfosWithOutId(
XmlParseHelper.getChildElementsByName(root, "behavior"),
pluginUI));
pluginUI.setCreateDateTime(new Date());
return pluginUI;
}

View File

@ -89,11 +89,9 @@ public class PluginRepository extends AbstractRepositoty {
public Set<BehaviorInfo> loadBehaviorInfos(String pluginName) {
Session session = this.getSessionHelper().openSession();
try {
PluginUI pluginUI = dogetPluginUI(pluginName, session);
Set<BehaviorInfo> behaviorInfos = pluginUI.getBehaviorInfos();
logger.info(behaviorInfos.size());
return behaviorInfos;
} catch (Exception e) {
logger.info(ExceptionLog.getStackTrace(e));

View File

@ -2,9 +2,15 @@ package org.bench4q.master.domain.service;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import org.bench4q.master.domain.repository.PluginRepository;
import org.bench4q.master.domain.entity.plugin.BehaviorInfo;
import org.bench4q.master.domain.entity.plugin.PluginUI;
import org.bench4q.master.domain.factory.PluginDomainFactory;
import org.bench4q.master.domain.factory.PluginEntityFactory;
import org.bench4q.share.models.master.plugin.BehaviorInfoModel;
import org.bench4q.share.models.master.plugin.PluginUIModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@ -12,6 +18,7 @@ import org.springframework.stereotype.Component;
public class PluginService {
private PluginRepository pluginRepository;
private PluginEntityFactory pluginEntityFactory;
private PluginDomainFactory pluginDomainFactory;
public PluginRepository getPluginRepository() {
return pluginRepository;
@ -31,6 +38,15 @@ public class PluginService {
this.pluginEntityFactory = pluginEntityFactory;
}
public PluginDomainFactory getPluginDomainFactory() {
return pluginDomainFactory;
}
@Autowired
public void setPluginDomainFactory(PluginDomainFactory pluginDomainFactory) {
this.pluginDomainFactory = pluginDomainFactory;
}
public boolean addPlugin(String pluginUIContent) {
return this.getPluginRepository().attatch(
this.getPluginEntityFactory().createPluginUIWithOutId(
@ -42,9 +58,39 @@ public class PluginService {
}
public List<String> getPluginNameList() {
List<PluginUIModel> pluginUIModels = this.loadPluginUIs();
if (pluginUIModels == null)
return null;
List<String> pluginNameList = new LinkedList<String>();
for (PluginUIModel pluginUIModel : this.loadPluginUIs()) {
pluginNameList.add(pluginUIModel.getPluginInfoModel().getName());
}
return pluginNameList;
}
public List<PluginUIModel> loadPluginUIs() {
List<PluginUI> pluginUIs = this.getPluginRepository().loadPlugins();
if (pluginUIs == null)
return null;
List<PluginUIModel> pluginUIModels = new LinkedList<PluginUIModel>();
for (PluginUI pluginUI : pluginUIs) {
pluginUIModels.add(this.getPluginDomainFactory()
.createPluginUIModelwithOutBehaviors(pluginUI));
}
return pluginUIModels;
}
public List<BehaviorInfoModel> loadBehaviorInfoModels(String pluginName) {
Set<BehaviorInfo> behaviorInfos = this.getPluginRepository()
.loadBehaviorInfos(pluginName);
if (behaviorInfos == null)
return null;
return this.getPluginDomainFactory().createBehaviorInfoModels(
this.getPluginRepository().loadBehaviorInfos(pluginName));
}
}

View File

@ -6,8 +6,23 @@ import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.bench4q.master.domain.entity.plugin.PluginUI;
import org.bench4q.master.domain.factory.PluginEntityFactory;
public class Test_PluginHelper{
public static PluginUI createPluginUI(String fileName) throws IOException {
import org.bench4q.master.domain.repository.PluginRepository;
import org.springframework.beans.factory.annotation.Autowired;
public class Test_PluginHelper {
private PluginRepository pluginRepository;
public PluginRepository getPluginRepository() {
return pluginRepository;
}
@Autowired
public void setPluginRepository(PluginRepository pluginRepository) {
this.pluginRepository = pluginRepository;
}
public static PluginUI createPluginUI(String fileName) throws IOException {
PluginEntityFactory pluginEntityFactory = new PluginEntityFactory();
String fileSeparator = System.getProperty("file.separator");
String filePath = System.getProperty("user.dir") + fileSeparator
@ -18,4 +33,17 @@ public class Test_PluginHelper{
.createPluginUIWithOutId(uiContent);
return pluginUI;
}
protected String addPlugin(String fileName,
PluginRepository pluginRepository) {
try {
PluginUI pluginUI = createPluginUI(fileName);
pluginRepository.attatch(pluginUI);
return pluginUI.getPluginInfo().getName();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}

View File

@ -7,9 +7,7 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import javax.xml.bind.JAXBException;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.bench4q.master.domain.entity.Agent;

View File

@ -0,0 +1,79 @@
package org.bench4q.master.test.factory;
import static org.junit.Assert.*;
import java.util.Set;
import org.bench4q.master.domain.entity.plugin.BehaviorInfo;
import org.bench4q.master.domain.entity.plugin.PluginUI;
import org.bench4q.master.domain.factory.PluginDomainFactory;
import org.bench4q.master.domain.repository.PluginRepository;
import org.bench4q.share.models.master.plugin.PluginUIModel;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import TestHelper.Test_PluginHelper;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:service-test-context.xml" })
public class Test_PluginDomainFactory extends Test_PluginHelper {
private PluginDomainFactory pluginDomainFactory;
private PluginRepository pluginRepository;
private String pluginName;
public PluginDomainFactory getPluginDomainFactory() {
return pluginDomainFactory;
}
@Autowired
public void setPluginDomainFactory(PluginDomainFactory pluginDomainFactory) {
this.pluginDomainFactory = pluginDomainFactory;
}
public PluginRepository getPluginRepository() {
return pluginRepository;
}
@Autowired
public void setPluginRepository(PluginRepository pluginRepository) {
this.pluginRepository = pluginRepository;
}
@Before
public void setUp() {
this.pluginName = addPlugin("ui.xml", this.getPluginRepository());
}
@After
public void clear() {
}
@Test
public void test_createPluginUIModel() {
assertNotNull(pluginName);
PluginUI pluginUI = this.getPluginRepository().getPluginUI(pluginName);
PluginUIModel pluginUIModel = this.getPluginDomainFactory()
.createPluginUIModelwithOutBehaviors(pluginUI);
assertNotNull(pluginUIModel);
assertNull(pluginUIModel.getBehaviorInfos());
assertEquals(7, pluginUIModel.getPluginInfoModel().getParamInfoModels()
.size());
}
@Test
public void test_CreateBehaviorModels() {
assertNotNull(pluginName);
Set<BehaviorInfo> behaviorInfos = this.getPluginRepository()
.loadBehaviorInfos(pluginName);
assertNotNull(behaviorInfos);
assertTrue(behaviorInfos.size() > 0);
assertEquals(2, behaviorInfos.size());
}
}

View File

@ -2,7 +2,6 @@ package org.bench4q.master.test.repository;
import static org.junit.Assert.*;
import org.bench4q.master.domain.entity.plugin.PluginUI;
import org.bench4q.master.domain.factory.PluginEntityFactory;
import org.bench4q.master.domain.repository.PluginRepository;
import org.bench4q.master.exception.Bench4QException;
@ -17,7 +16,7 @@ import TestHelper.Test_PluginHelper;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:service-test-context.xml" })
public class Test_PluginRepository {
public class Test_PluginRepository extends Test_PluginHelper {
private PluginRepository pluginRepository;
private String pluginName;
private PluginEntityFactory pluginEntityFactory;
@ -42,7 +41,7 @@ public class Test_PluginRepository {
@Before
public void setUp() throws Bench4QException {
this.pluginName = addPlugin("ui.xml");
this.pluginName = addPlugin("ui.xml", this.getPluginRepository());
}
@Test
@ -54,7 +53,7 @@ public class Test_PluginRepository {
@Test
public void testLoadPlugins() throws Bench4QException {
int countBeforeInsert = this.getPluginRepository().loadPlugins().size();
String name = addPlugin("test.xml");
String name = addPlugin("test.xml", this.getPluginRepository());
assertEquals(countBeforeInsert + 1, this.getPluginRepository()
.loadPlugins().size());
@ -76,7 +75,7 @@ public class Test_PluginRepository {
@Test
public void testDeletePlugin() {
String name = addPlugin("ui.xml");
String name = addPlugin("ui.xml", this.getPluginRepository());
assertTrue(this.getPluginRepository().detach(name));
}
@ -85,16 +84,4 @@ public class Test_PluginRepository {
this.getPluginRepository().detach(pluginName);
}
private String addPlugin(String fileName) {
try {
PluginUI pluginUI = Test_PluginHelper.createPluginUI(fileName);
boolean add = this.getPluginRepository().attatch(pluginUI);
System.out.println(add);
return pluginUI.getPluginInfo().getName();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}