add plugin service
This commit is contained in:
parent
d89569ac17
commit
3c62882c5a
|
@ -21,6 +21,9 @@ public class Main {
|
|||
try {
|
||||
MasterServer masterServer = new MasterServer(getPortToServe());
|
||||
masterServer.start();
|
||||
|
||||
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return;
|
||||
|
|
|
@ -13,6 +13,8 @@ import javax.persistence.JoinColumn;
|
|||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
import javax.xml.bind.annotation.XmlElement;
|
||||
import javax.xml.bind.annotation.XmlElementWrapper;
|
||||
|
||||
@Entity
|
||||
@Table(name = "method")
|
||||
|
@ -33,6 +35,7 @@ public class Method {
|
|||
this.id = id;
|
||||
}
|
||||
|
||||
@XmlElement(name="name")
|
||||
@Column(name = "method", nullable = false)
|
||||
public String getName() {
|
||||
return name;
|
||||
|
@ -52,6 +55,8 @@ public class Method {
|
|||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@XmlElementWrapper(name="params")
|
||||
@XmlElement(name="param",type=MethodParam.class)
|
||||
@OneToMany(mappedBy = "method", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
|
||||
public Set<MethodParam> getMethodParams() {
|
||||
return methodParams;
|
||||
|
|
|
@ -1,22 +1,34 @@
|
|||
package org.bench4q.master.domain.entity.plugin;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
import javax.xml.bind.annotation.XmlElement;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
|
||||
@XmlRootElement
|
||||
@Entity
|
||||
@Table(name = "methodParam")
|
||||
public class MethodParam {
|
||||
public static final int FIELD = 1;
|
||||
public static final int TABLE = 1;
|
||||
public static final int CHECKBOX = 1;
|
||||
private int id;
|
||||
private String name;
|
||||
private ParamType type;
|
||||
private Method method;
|
||||
private boolean nullable;
|
||||
private String lable;
|
||||
private String name;
|
||||
private int editorType;
|
||||
private List<ParamProperty> properties;
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
|
@ -29,6 +41,7 @@ public class MethodParam {
|
|||
this.id = id;
|
||||
}
|
||||
|
||||
@XmlElement(name = "name")
|
||||
@Column(name = "name", nullable = false)
|
||||
public String getName() {
|
||||
return name;
|
||||
|
@ -38,16 +51,6 @@ public class MethodParam {
|
|||
this.name = name;
|
||||
}
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "typeId", nullable = false)
|
||||
public ParamType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(ParamType type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "methodId", nullable = false)
|
||||
public Method getMethod() {
|
||||
|
@ -58,13 +61,32 @@ public class MethodParam {
|
|||
this.method = method;
|
||||
}
|
||||
|
||||
@Column(name = "nullable", nullable = false)
|
||||
public boolean isNullable() {
|
||||
return nullable;
|
||||
@XmlElement(name = "lable")
|
||||
@Column(name = "lable")
|
||||
public String getLable() {
|
||||
return lable;
|
||||
}
|
||||
|
||||
public void setNullable(boolean nullable) {
|
||||
this.nullable = nullable;
|
||||
public void setLable(String lable) {
|
||||
this.lable = lable;
|
||||
}
|
||||
|
||||
@Column(name = "editorType")
|
||||
public int getEditorType() {
|
||||
return editorType;
|
||||
}
|
||||
|
||||
public void setEditorType(int editorType) {
|
||||
this.editorType = editorType;
|
||||
}
|
||||
|
||||
@OneToMany(mappedBy = "method", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
|
||||
public List<ParamProperty> getProperties() {
|
||||
return properties;
|
||||
}
|
||||
|
||||
public void setProperties(List<ParamProperty> properties) {
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,75 @@
|
|||
package org.bench4q.master.domain.entity.plugin;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.Table;
|
||||
import javax.xml.bind.annotation.XmlElement;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
|
||||
@XmlRootElement(name = "pararmProperty")
|
||||
@Entity
|
||||
@Table(name = "paramProperties")
|
||||
public class ParamProperty {
|
||||
private int id;
|
||||
private String key;
|
||||
private String value;
|
||||
private MethodParam param;
|
||||
private String valueSeperator;
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@Column(name = "id", nullable = false)
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@XmlElement
|
||||
@Column(name = "key")
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public void setKey(String key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
@XmlElement
|
||||
@Column(name = "value")
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "methodParamId")
|
||||
public MethodParam getParam() {
|
||||
return param;
|
||||
}
|
||||
|
||||
public void setParam(MethodParam param) {
|
||||
this.param = param;
|
||||
}
|
||||
|
||||
@XmlElement
|
||||
@Column(name = "valueSeperator")
|
||||
public String getValueSeperator() {
|
||||
return valueSeperator;
|
||||
}
|
||||
|
||||
public void setValueSeperator(String valueSeperator) {
|
||||
this.valueSeperator = valueSeperator;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,66 +0,0 @@
|
|||
package org.bench4q.master.domain.entity.plugin;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "parameType")
|
||||
public class ParamType {
|
||||
private int id;
|
||||
private String name;
|
||||
private String splitBy;
|
||||
private String entryKeyStart;
|
||||
private String entryValueStart;
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@Column(name = "id", nullable = false)
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@Column(name = "name", nullable = false)
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Column(name = "splitBy")
|
||||
public String getSplitBy() {
|
||||
return splitBy;
|
||||
}
|
||||
|
||||
public void setSplitBy(String splitBy) {
|
||||
this.splitBy = splitBy;
|
||||
}
|
||||
|
||||
@Column(name = "entryKeyStart")
|
||||
public String getEntryKeyStart() {
|
||||
return entryKeyStart;
|
||||
}
|
||||
|
||||
public void setEntryKeyStart(String entryKeyStart) {
|
||||
this.entryKeyStart = entryKeyStart;
|
||||
}
|
||||
|
||||
@Column(name = "entryValueStart")
|
||||
public String getEntryValueStart() {
|
||||
return entryValueStart;
|
||||
}
|
||||
|
||||
public void setEntryValueStart(String entryValueStart) {
|
||||
this.entryValueStart = entryValueStart;
|
||||
}
|
||||
|
||||
}
|
|
@ -11,7 +11,11 @@ import javax.persistence.GenerationType;
|
|||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
import javax.xml.bind.annotation.XmlElement;
|
||||
import javax.xml.bind.annotation.XmlElementWrapper;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
|
||||
@XmlRootElement(name = "plugin")
|
||||
@Entity
|
||||
@Table(name = "plugin")
|
||||
public class Plugin {
|
||||
|
@ -30,6 +34,7 @@ public class Plugin {
|
|||
this.id = id;
|
||||
}
|
||||
|
||||
@XmlElement(name = "name")
|
||||
@Column(name = "name", nullable = false)
|
||||
public String getName() {
|
||||
return name;
|
||||
|
@ -39,6 +44,8 @@ public class Plugin {
|
|||
this.name = name;
|
||||
}
|
||||
|
||||
@XmlElementWrapper(name = "methods")
|
||||
@XmlElement(name = "method", type = Method.class)
|
||||
@OneToMany(mappedBy = "plugin", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
|
||||
public Set<Method> getMethods() {
|
||||
return methods;
|
||||
|
|
|
@ -0,0 +1,115 @@
|
|||
package org.bench4q.master.domain.repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.bench4q.master.domain.entity.plugin.Plugin;
|
||||
import org.bench4q.master.exception.Bench4QException;
|
||||
import org.bench4q.master.exception.ExceptionUtils.EntityUniqueAlReadyExistException;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.Transaction;
|
||||
import org.hibernate.criterion.Restrictions;
|
||||
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;
|
||||
try {
|
||||
pluginExist = (Plugin) session.createCriteria(Plugin.class).add(
|
||||
Restrictions.eq("name", plugin.getName()));
|
||||
if (pluginExist != null) {
|
||||
logger.info("the plugin exist");
|
||||
return false;
|
||||
}
|
||||
session.merge(plugin);
|
||||
transaction.commit();
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
transaction.rollback();
|
||||
logger.error(e, e.fillInStackTrace());
|
||||
throw new Bench4QException("", "add plugin fail", "");
|
||||
} finally {
|
||||
releaseSession(session);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void guardOtherUniqueConditionForEntity(
|
||||
String uniquePropertyName, String value)
|
||||
throws EntityUniqueAlReadyExistException {
|
||||
if (getPlugin(value) != null) {
|
||||
throw new EntityUniqueAlReadyExistException("User with the name "
|
||||
+ value + "already exists");
|
||||
}
|
||||
}
|
||||
|
||||
public boolean detach(String pluginName) throws Bench4QException {
|
||||
Session session = this.getSessionHelper().openSession();
|
||||
Transaction transaction = session.beginTransaction();
|
||||
try {
|
||||
Plugin pluginExist = (Plugin) session.createCriteria(Plugin.class)
|
||||
.add(Restrictions.eq("name", pluginName)).uniqueResult();
|
||||
if (pluginExist == null) {
|
||||
logger.info("plugin not exist");
|
||||
return false;
|
||||
}
|
||||
session.delete(pluginExist);
|
||||
transaction.commit();
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
logger.error(e, e.fillInStackTrace());
|
||||
transaction.rollback();
|
||||
throw new Bench4QException("", "delete plugin fail", "");
|
||||
} finally {
|
||||
releaseSession(session);
|
||||
}
|
||||
}
|
||||
|
||||
public List<Plugin> loadPlugins() throws Bench4QException {
|
||||
Session session = this.getSessionHelper().openSession();
|
||||
try {
|
||||
@SuppressWarnings("unchecked")
|
||||
List<Plugin> ret = session.createCriteria(Plugin.class).list();
|
||||
return ret;
|
||||
} catch (Exception e) {
|
||||
logger.error("load Plugins ");
|
||||
logger.error(e, e.fillInStackTrace());
|
||||
throw new Bench4QException("", "load plugin failed", "");
|
||||
} finally {
|
||||
releaseSession(session);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isExist(Plugin plugin) {
|
||||
Session session = this.getSessionHelper().openSession();
|
||||
Plugin pluginAlreadyExist = (Plugin) session
|
||||
.createCriteria(Plugin.class)
|
||||
.add(Restrictions.eq("userName", plugin.getName()))
|
||||
.uniqueResult();
|
||||
releaseSession(session);
|
||||
return pluginAlreadyExist != null;
|
||||
}
|
||||
|
||||
public Plugin getEntity(int id) {
|
||||
Plugin result = null;
|
||||
Session session = this.getSessionHelper().openSession();
|
||||
result = (Plugin) session.get(Plugin.class, id);
|
||||
releaseSession(session);
|
||||
return result;
|
||||
}
|
||||
|
||||
public Plugin getPlugin(String pluginName) {
|
||||
Plugin plugin = null;
|
||||
Session session = this.getSessionHelper().openSession();
|
||||
plugin = deGetPlugin(pluginName, session);
|
||||
releaseSession(session);
|
||||
return plugin;
|
||||
}
|
||||
|
||||
private Plugin deGetPlugin(String pluginName, Session session) {
|
||||
return (Plugin) session.createCriteria(Plugin.class)
|
||||
.add(Restrictions.eq("name", pluginName)).uniqueResult();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,96 @@
|
|||
package org.bench4q.master.domain.service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
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.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class PluginService {
|
||||
private List<Plugin> plugins;
|
||||
private List<String> pluginNameList;
|
||||
private PluginRepository pluginRepository;
|
||||
|
||||
public PluginRepository getPluginRepository() {
|
||||
return pluginRepository;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
public void setPluginRepository(PluginRepository pluginRepository) {
|
||||
this.pluginRepository = pluginRepository;
|
||||
}
|
||||
|
||||
private List<Plugin> getPlugins() throws Bench4QException {
|
||||
this.plugins = this.getPluginRepository().loadPlugins();
|
||||
|
||||
return this.plugins;
|
||||
}
|
||||
|
||||
public boolean addPlugin(Plugin plugin) throws Bench4QException {
|
||||
return this.getPluginRepository().attatch(plugin);
|
||||
}
|
||||
|
||||
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 {
|
||||
this.pluginNameList = new ArrayList<String>();
|
||||
this.getPlugins();
|
||||
if (this.plugins != null) {
|
||||
for (Plugin plugin : this.plugins) {
|
||||
this.pluginNameList.add(plugin.getName());
|
||||
}
|
||||
}
|
||||
return this.pluginNameList;
|
||||
}
|
||||
|
||||
public List<String> getMethodNameInPlugin(String pluginName) {
|
||||
List<String> methodNameList = new ArrayList<String>();
|
||||
Set<Method> methods = this.getPluginByName(pluginName).getMethods();
|
||||
if (methods != null) {
|
||||
for (Method method : methods) {
|
||||
methodNameList.add(method.getName());
|
||||
}
|
||||
}
|
||||
return methodNameList;
|
||||
}
|
||||
|
||||
private Method getMethodInPlugin(String pluginName, String methodName)
|
||||
throws Bench4QException {
|
||||
Set<Method> methods = this.getMethodInPlugin(pluginName);
|
||||
if (methods != null) {
|
||||
for (Method method : methods) {
|
||||
if (method.getName().equals(methodName))
|
||||
return method;
|
||||
}
|
||||
}
|
||||
|
||||
throw new Bench4QException("", "no such method:" + methodName
|
||||
+ "in plugin:" + pluginName, "");
|
||||
|
||||
}
|
||||
|
||||
private Set<Method> getMethodInPlugin(String pluginName) {
|
||||
|
||||
return this.getPluginByName(pluginName).getMethods();
|
||||
}
|
||||
|
||||
public Set<MethodParam> getMethodParams(String pluginName,
|
||||
String methodName) throws Bench4QException {
|
||||
Method method = this.getMethodInPlugin(pluginName, methodName);
|
||||
return method.getMethodParams();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,110 @@
|
|||
package org.bench4q.master.test.service;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.xml.bind.JAXBException;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.bench4q.master.domain.service.PluginService;
|
||||
import org.bench4q.share.helper.MarshalHelper;
|
||||
import org.bench4q.share.models.master.plugin.Method;
|
||||
import org.bench4q.share.models.master.plugin.MethodParam;
|
||||
import org.bench4q.share.models.master.plugin.ParamProperty;
|
||||
import org.bench4q.share.models.master.plugin.Plugin;
|
||||
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;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = { "classpath:service-test-context.xml" })
|
||||
public class Test_PluginService {
|
||||
|
||||
private PluginService pluginService;
|
||||
|
||||
public PluginService getPluginService() {
|
||||
return pluginService;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
public void setPluginService(PluginService pluginService) {
|
||||
this.pluginService = pluginService;
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPluginModel() throws JAXBException, IOException {
|
||||
File file = new File(buildSavePath()+System.getProperty("file.separator")+"plugin.xml");
|
||||
Plugin plugin = new Plugin();
|
||||
Method method = new Method();
|
||||
MethodParam methodParamUrl = new MethodParam();
|
||||
methodParamUrl.setEditorType(MethodParam.FIELD);
|
||||
methodParamUrl.setName("url");
|
||||
methodParamUrl.setSplitBy("");
|
||||
methodParamUrl.setParamProperties(generateUrlProperties());
|
||||
|
||||
MethodParam methodParamParam = new MethodParam();
|
||||
methodParamParam.setEditorType(MethodParam.TABLE);
|
||||
methodParamParam.setName("Param");
|
||||
methodParamParam.setSplitBy("&");
|
||||
methodParamParam.setParamProperties(generateParamProperties());
|
||||
|
||||
List<MethodParam> methodParams = new ArrayList<MethodParam>();
|
||||
methodParams.add(methodParamParam);
|
||||
methodParams.add(methodParamParam);
|
||||
|
||||
method.setName("get");
|
||||
method.setParams(methodParams);
|
||||
|
||||
Set<Method> methods = new HashSet<Method>();
|
||||
methods.add(method);
|
||||
plugin.setMethods(methods);
|
||||
FileUtils.writeStringToFile(file,
|
||||
MarshalHelper.marshal(Plugin.class, plugin));
|
||||
}
|
||||
|
||||
private List<ParamProperty> generateUrlProperties() {
|
||||
ParamProperty paramPropertyUrl = new ParamProperty();
|
||||
paramPropertyUrl.setKey("size");
|
||||
paramPropertyUrl.setValue("20");
|
||||
List<ParamProperty> paramProperties = new ArrayList<ParamProperty>();
|
||||
paramProperties.add(paramPropertyUrl);
|
||||
return paramProperties;
|
||||
}
|
||||
|
||||
private List<ParamProperty> generateParamProperties() {
|
||||
ParamProperty paramProperty = new ParamProperty();
|
||||
paramProperty.setKey("cols");
|
||||
paramProperty.setValue("key,value");
|
||||
paramProperty.setValueSeperator(",");
|
||||
List<ParamProperty> paramProperties = new ArrayList<ParamProperty>();
|
||||
paramProperties.add(paramProperty);
|
||||
return paramProperties;
|
||||
}
|
||||
|
||||
private String buildSavePath() {
|
||||
String dirString = "GUI" + System.getProperty("file.separator")
|
||||
+ System.getProperty("file.separator")
|
||||
+ new SimpleDateFormat("yyyyMMdd").format(new Date())
|
||||
+ "plugin.xml";
|
||||
File dirFile = new File(dirString);
|
||||
if (!dirFile.exists()) {
|
||||
dirFile.mkdirs();
|
||||
}
|
||||
return dirString;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue