Merge pull request #9 from lostcharlie/master

module service changed
This commit is contained in:
Zhen Tang 2013-06-03 01:52:05 -07:00
commit ca9c29570c
27 changed files with 347 additions and 914 deletions

View File

@ -1,58 +0,0 @@
package haflow.entity;
import java.util.UUID;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name = "configuration")
public class Configuration {
private UUID id;
private Module module;
private String key;
private String displayName;
@Id
@Column(name = "id", length = 16)
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
@ManyToOne
@JoinColumn(name = "moduleId")
public Module getModule() {
return module;
}
public void setModule(Module module) {
this.module = module;
}
@Column(name = "configurationKey")
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
@Column(name = "displayName")
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
}

View File

@ -3,23 +3,11 @@ package haflow.entity;
import java.util.Set;
import java.util.UUID;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name = "module")
public class Module {
private UUID id;
private String name;
private Set<Configuration> configurations;
private Set<ModuleConfiguration> configurations;
@Id
@Column(name = "id", length = 16)
public UUID getId() {
return id;
}
@ -28,7 +16,6 @@ public class Module {
this.id = id;
}
@Column(name = "name")
public String getName() {
return name;
}
@ -37,12 +24,11 @@ public class Module {
this.name = name;
}
@OneToMany(mappedBy = "module", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
public Set<Configuration> getConfigurations() {
public Set<ModuleConfiguration> getConfigurations() {
return configurations;
}
public void setConfigurations(Set<Configuration> configurations) {
public void setConfigurations(Set<ModuleConfiguration> configurations) {
this.configurations = configurations;
}

View File

@ -0,0 +1,23 @@
package haflow.entity;
public class ModuleConfiguration {
private String key;
private String displayName;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
}

View File

@ -14,7 +14,7 @@ import javax.persistence.Table;
public class Node {
private UUID id;
private Flow flow;
private Module module;
private UUID moduleId;
private String name;
@Id
@ -37,14 +37,13 @@ public class Node {
this.flow = flow;
}
@ManyToOne
@JoinColumn(name = "moduleId")
public Module getModule() {
return module;
@Column(name = "moduleId")
public UUID getModuleId() {
return moduleId;
}
public void setModule(Module module) {
this.module = module;
public void setModuleId(UUID moduleId) {
this.moduleId = moduleId;
}
@Column(name = "name")

View File

@ -0,0 +1,74 @@
package haflow.module;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import haflow.entity.Module;
import haflow.entity.ModuleConfiguration;
import haflow.utility.ClassHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class ModuleLoader {
private ClassHelper classHelper;
public ClassHelper getClassHelper() {
return classHelper;
}
@Autowired
public void setClassHelper(ClassHelper classHelper) {
this.classHelper = classHelper;
}
public Map<Module, ModuleMetadata> searchForModules() {
try {
Map<Module, ModuleMetadata> modules = new HashMap<Module, ModuleMetadata>();
List<String> classNames = this.getClassHelper().getClassNames(
"haflow", true);
for (String className : classNames) {
Class<?> moduleClass = Class.forName(className);
if (moduleClass
.isAnnotationPresent(haflow.module.annotation.Module.class)) {
Object obj = moduleClass.newInstance();
if (obj instanceof ModuleMetadata) {
ModuleMetadata metadata = (ModuleMetadata) obj;
Module module = new Module();
module.setId(UUID.fromString(moduleClass.getAnnotation(
haflow.module.annotation.Module.class).id()));
module.setName(moduleClass.getAnnotation(
haflow.module.annotation.Module.class).name());
module.setConfigurations(new HashSet<ModuleConfiguration>());
if (moduleClass
.isAnnotationPresent(haflow.module.annotation.ModuleConfiguration.class)) {
haflow.module.annotation.ModuleConfiguration configuration = moduleClass
.getAnnotation(haflow.module.annotation.ModuleConfiguration.class);
int i;
for (i = 0; i < configuration.configurationKeys().length; i++) {
ModuleConfiguration moduleConfiguration = new ModuleConfiguration();
moduleConfiguration.setKey(configuration
.configurationKeys()[i]);
moduleConfiguration
.setDisplayName(configuration
.configurationDisplayNames()[i]);
module.getConfigurations().add(
moduleConfiguration);
}
}
modules.put(module, metadata);
}
}
}
return modules;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}

View File

@ -0,0 +1,9 @@
package haflow.module;
import java.util.Map;
import org.w3c.dom.Document;
public interface ModuleMetadata {
public Document generate(Map<String, String> configurations);
}

View File

@ -0,0 +1,14 @@
package haflow.module.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Module {
String id();
String name();
}

View File

@ -0,0 +1,14 @@
package haflow.module.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface ModuleConfiguration {
String[] configurationKeys();
String[] configurationDisplayNames();
}

View File

@ -0,0 +1,20 @@
package haflow.module.basic;
import java.util.Map;
import org.w3c.dom.Document;
import haflow.module.ModuleMetadata;
import haflow.module.annotation.Module;
import haflow.module.annotation.ModuleConfiguration;
@Module(id = "70d027c3-a4bd-61b5-5063-134ff71f8122", name = "End")
@ModuleConfiguration(configurationKeys = { "aaa" }, configurationDisplayNames = { "bbb" })
public class EndModule implements ModuleMetadata {
public Document generate(Map<String, String> configurations) {
// TODO Auto-generated method stub
return null;
}
}

View File

@ -0,0 +1,20 @@
package haflow.module.basic;
import java.util.Map;
import org.w3c.dom.Document;
import haflow.module.ModuleMetadata;
import haflow.module.annotation.Module;
import haflow.module.annotation.ModuleConfiguration;
@Module(id = "9208d7d2-a8ff-2493-64c2-36f50bc95752", name = "Start")
@ModuleConfiguration(configurationKeys = { "aaa" }, configurationDisplayNames = { "bbb" })
public class StartModule implements ModuleMetadata {
public Document generate(Map<String, String> configurations) {
// TODO Auto-generated method stub
return null;
}
}

View File

@ -1,132 +0,0 @@
package haflow.service;
import haflow.entity.Configuration;
import haflow.entity.Module;
import haflow.utility.SessionHelper;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class ModuleService {
private SessionHelper sessionHelper;
public SessionHelper getSessionHelper() {
return sessionHelper;
}
@Autowired
public void setSessionHelper(SessionHelper sessionHelper) {
this.sessionHelper = sessionHelper;
}
public UUID addModule(String name, Set<Configuration> configurations) {
Session session = this.getSessionHelper().openSession();
Transaction transaction = session.beginTransaction();
try {
Module module = new Module();
module.setId(UUID.randomUUID());
module.setName(name);
module.setConfigurations(new HashSet<Configuration>());
for (Configuration configuration : configurations) {
configuration.setModule(module);
module.getConfigurations().add(configuration);
}
session.merge(module);
transaction.commit();
session.close();
return module.getId();
} catch (Exception e) {
e.printStackTrace();
transaction.rollback();
session.close();
return null;
}
}
public boolean modifyModule(UUID moduleId, String newName,
Set<Configuration> newConfigurations) {
Session session = this.getSessionHelper().openSession();
Transaction transaction = session.beginTransaction();
Module module = (Module) session.get(Module.class, moduleId);
if (module == null) {
return false;
}
module.setName(newName);
module.getConfigurations().clear();
for (Configuration configuration : newConfigurations) {
configuration.setModule(module);
module.getConfigurations().add(configuration);
}
try {
session.merge(module);
transaction.commit();
session.close();
return true;
} catch (Exception e) {
e.printStackTrace();
transaction.rollback();
session.close();
return false;
}
}
public boolean removeModule(UUID moduleId) {
Session session = this.getSessionHelper().openSession();
Transaction transaction = session.beginTransaction();
Module module = (Module) session.get(Module.class, moduleId);
if (module == null) {
return false;
}
try {
session.delete(module);
transaction.commit();
session.close();
return true;
} catch (Exception e) {
e.printStackTrace();
transaction.rollback();
session.close();
return false;
}
}
@SuppressWarnings("unchecked")
public List<Module> getModuleList() {
Session session = this.getSessionHelper().openSession();
try {
Query query = session.createQuery("from Module module");
List<Module> modules = (List<Module>) query.list();
session.close();
return modules;
} catch (Exception e) {
e.printStackTrace();
session.close();
return null;
}
}
public Module getModule(UUID moduleId) {
Session session = this.getSessionHelper().openSession();
try {
Module module = (Module) session.get(Module.class, moduleId);
session.close();
return module;
} catch (Exception e) {
e.printStackTrace();
session.close();
return null;
}
}
}

View File

@ -11,8 +11,4 @@ public class HomeController {
return new ModelAndView("main");
}
@RequestMapping("/admin")
public ModelAndView admin() {
return new ModelAndView("admin");
}
}

View File

@ -1,21 +1,10 @@
package haflow.ui.controller;
import haflow.ui.helper.ModuleHelper;
import haflow.ui.model.AddModuleModel;
import haflow.ui.model.AddModuleResultModel;
import haflow.ui.model.ModifyModuleModel;
import haflow.ui.model.ModifyModuleResultModel;
import haflow.ui.model.ModuleListModel;
import haflow.ui.model.ModuleModel;
import haflow.ui.model.RemoveModuleModel;
import haflow.ui.model.RemoveModuleResultModel;
import java.util.UUID;
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.ResponseBody;
@ -39,30 +28,4 @@ public class ModuleController {
public ModuleListModel get() {
return this.getModuleHelper().getModuleList();
}
@RequestMapping(value = "/{moduleId}", method = RequestMethod.GET)
@ResponseBody
public ModuleModel get(@PathVariable UUID moduleId) {
return this.getModuleHelper().getModule(moduleId);
}
@RequestMapping(method = RequestMethod.POST)
@ResponseBody
public AddModuleResultModel post(@RequestBody AddModuleModel model) {
return this.getModuleHelper().addModule(model);
}
@RequestMapping(value = "/{moduleId}", method = RequestMethod.PUT)
@ResponseBody
public ModifyModuleResultModel put(@PathVariable UUID moduleId,
@RequestBody ModifyModuleModel model) {
return this.getModuleHelper().modifyModule(moduleId, model);
}
@RequestMapping(value = "/{moduleId}", method = RequestMethod.DELETE)
@ResponseBody
public RemoveModuleResultModel delete(@PathVariable UUID moduleId,
@RequestBody RemoveModuleModel model) {
return this.getModuleHelper().removeModule(moduleId, model);
}
}

View File

@ -12,11 +12,9 @@ import org.springframework.stereotype.Component;
import haflow.entity.Edge;
import haflow.entity.Flow;
import haflow.entity.Node;
import haflow.entity.Module;
import haflow.profile.NodeAppearanceProfile;
import haflow.profile.NodeConfigurationProfile;
import haflow.service.FlowService;
import haflow.service.ModuleService;
import haflow.service.NodeAppearanceProfileService;
import haflow.service.NodeConfigurationProfileService;
import haflow.ui.model.ConfigurationItemModel;
@ -35,7 +33,6 @@ import haflow.ui.model.RemoveFlowResultModel;
public class FlowHelper {
private FlowService flowService;
private ModuleService moduleService;
private NodeAppearanceProfileService nodeAppearanceProfileService;
private NodeConfigurationProfileService nodeConfigurationProfileService;
@ -48,15 +45,6 @@ public class FlowHelper {
this.flowService = flowService;
}
public ModuleService getModuleService() {
return moduleService;
}
@Autowired
public void setModuleService(ModuleService moduleService) {
this.moduleService = moduleService;
}
public NodeAppearanceProfileService getNodeAppearanceProfileService() {
return nodeAppearanceProfileService;
}
@ -109,7 +97,7 @@ public class FlowHelper {
.getNodeConfigurationProfile(node.getId());
nodeModel.setFlowId(node.getFlow().getId());
nodeModel.setId(node.getId());
nodeModel.setModuleId(node.getModule().getId());
nodeModel.setModuleId(node.getModuleId());
nodeModel.setName(node.getName());
nodeModel.setPosition(new PositionModel());
nodeModel.getPosition().setLeft(
@ -157,15 +145,10 @@ public class FlowHelper {
if (!nodeModel.getFlowId().equals(flowId)) {
return false;
}
Module module = this.getModuleService().getModule(
nodeModel.getModuleId());
if (module == null) {
return false;
}
Node node = new Node();
node.setFlow(null);
node.setId(nodeModel.getId());
node.setModule(module);
node.setModuleId(nodeModel.getModuleId());
node.setName(nodeModel.getName());
this.getNodeAppearanceProfileService().mergeNodeAppearanceProfile(
nodeModel.getId(), nodeModel.getPosition().getLeft(),

View File

@ -1,43 +1,35 @@
package haflow.ui.helper;
import haflow.entity.Configuration;
import haflow.entity.ModuleConfiguration;
import haflow.entity.Module;
import haflow.service.ModuleService;
import haflow.ui.model.AddModuleModel;
import haflow.ui.model.AddModuleResultModel;
import haflow.module.ModuleLoader;
import haflow.ui.model.ConfigurationModel;
import haflow.ui.model.ModifyModuleModel;
import haflow.ui.model.ModifyModuleResultModel;
import haflow.ui.model.ModuleBriefModel;
import haflow.ui.model.ModuleListModel;
import haflow.ui.model.ModuleModel;
import haflow.ui.model.RemoveModuleModel;
import haflow.ui.model.RemoveModuleResultModel;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class ModuleHelper {
private ModuleService moduleService;
private ModuleLoader moduleLoader;
public ModuleService getModuleService() {
return moduleService;
public ModuleLoader getModuleLoader() {
return moduleLoader;
}
@Autowired
public void setModuleService(ModuleService moduleService) {
this.moduleService = moduleService;
public void setModuleLoader(ModuleLoader moduleLoader) {
this.moduleLoader = moduleLoader;
}
public ModuleListModel getModuleList() {
List<Module> moduleList = this.getModuleService().getModuleList();
Set<Module> moduleList = this.getModuleLoader().searchForModules()
.keySet();
ModuleListModel moduleListModel = new ModuleListModel();
moduleListModel.setModules(new ArrayList<ModuleBriefModel>());
for (Module module : moduleList) {
@ -46,10 +38,9 @@ public class ModuleHelper {
moduleBriefModel.setName(module.getName());
moduleBriefModel
.setConfigurations(new HashSet<ConfigurationModel>());
for (Configuration configuration : module.getConfigurations()) {
for (ModuleConfiguration configuration : module.getConfigurations()) {
ConfigurationModel model = new ConfigurationModel();
model.setDisplayName(configuration.getDisplayName());
model.setId(configuration.getId());
model.setKey(configuration.getKey());
moduleBriefModel.getConfigurations().add(model);
}
@ -57,88 +48,4 @@ public class ModuleHelper {
}
return moduleListModel;
}
public ModuleModel getModule(UUID moduleId) {
Module module = this.getModuleService().getModule(moduleId);
if (module == null) {
return null;
}
ModuleModel moduleModel = new ModuleModel();
moduleModel.setId(module.getId());
moduleModel.setName(module.getName());
moduleModel.setConfigurations(new HashSet<ConfigurationModel>());
for (Configuration configuration : module.getConfigurations()) {
ConfigurationModel model = new ConfigurationModel();
model.setDisplayName(configuration.getDisplayName());
model.setId(configuration.getId());
model.setKey(configuration.getKey());
moduleModel.getConfigurations().add(model);
}
return moduleModel;
}
public AddModuleResultModel addModule(AddModuleModel model) {
String name = model.getName();
Set<Configuration> configurations = new HashSet<Configuration>();
for (ConfigurationModel configurationModel : model.getConfigurations()) {
Configuration configuration = new Configuration();
configuration.setId(configurationModel.getId());
configuration.setDisplayName(configurationModel.getDisplayName());
configuration.setKey(configurationModel.getKey());
configurations.add(configuration);
}
UUID moduleId = this.getModuleService().addModule(name, configurations);
if (moduleId == null) {
AddModuleResultModel result = new AddModuleResultModel();
result.setMessage("fail");
result.setModuleId(null);
result.setSuccess(false);
return result;
} else {
AddModuleResultModel result = new AddModuleResultModel();
result.setMessage("success");
result.setModuleId(moduleId);
result.setSuccess(true);
return result;
}
}
public ModifyModuleResultModel modifyModule(UUID moduleId,
ModifyModuleModel model) {
String newName = model.getNewName();
Set<Configuration> newConfigurations = new HashSet<Configuration>();
for (ConfigurationModel configurationModel : model
.getNewConfigurations()) {
Configuration configuration = new Configuration();
configuration.setId(configurationModel.getId());
configuration.setDisplayName(configurationModel.getDisplayName());
configuration.setKey(configurationModel.getKey());
newConfigurations.add(configuration);
}
boolean success = this.getModuleService().modifyModule(moduleId,
newName, newConfigurations);
ModifyModuleResultModel result = new ModifyModuleResultModel();
result.setModuleId(moduleId);
result.setSuccess(success);
if (success) {
result.setMessage("success");
} else {
result.setMessage("fail");
}
return result;
}
public RemoveModuleResultModel removeModule(UUID moduleId,
RemoveModuleModel model) {
boolean success = this.getModuleService().removeModule(moduleId);
RemoveModuleResultModel result = new RemoveModuleResultModel();
result.setModuleId(moduleId);
result.setSuccess(success);
if (success) {
result.setMessage("success");
} else {
result.setMessage("fail");
}
return result;
}
}

View File

@ -1,33 +0,0 @@
package haflow.ui.model;
import java.util.Set;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlElement;
@XmlRootElement(name = "addModule")
public class AddModuleModel {
private String name;
private Set<ConfigurationModel> configurations;
@XmlElement
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlElementWrapper(name = "configurations")
@XmlElement(name = "configuration")
public Set<ConfigurationModel> getConfigurations() {
return configurations;
}
public void setConfigurations(Set<ConfigurationModel> configurations) {
this.configurations = configurations;
}
}

View File

@ -1,40 +0,0 @@
package haflow.ui.model;
import java.util.UUID;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "addModuleResult")
public class AddModuleResultModel {
private boolean success;
private UUID moduleId;
private String message;
@XmlElement
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
@XmlElement
public UUID getModuleId() {
return moduleId;
}
public void setModuleId(UUID moduleId) {
this.moduleId = moduleId;
}
@XmlElement
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}

View File

@ -1,25 +1,13 @@
package haflow.ui.model;
import java.util.UUID;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "configuration")
public class ConfigurationModel {
private UUID id;
private String key;
private String displayName;
@XmlElement
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
@XmlElement
public String getKey() {
return key;

View File

@ -1,44 +0,0 @@
package haflow.ui.model;
import java.util.Set;
import java.util.UUID;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "modifyModule")
public class ModifyModuleModel {
private UUID id;
private String newName;
private Set<ConfigurationModel> newConfigurations;
@XmlElement
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
@XmlElement
public String getNewName() {
return newName;
}
public void setNewName(String newName) {
this.newName = newName;
}
@XmlElementWrapper(name = "newConfigurations")
@XmlElement(name = "newConfiguration")
public Set<ConfigurationModel> getNewConfigurations() {
return newConfigurations;
}
public void setNewConfigurations(Set<ConfigurationModel> newConfigurations) {
this.newConfigurations = newConfigurations;
}
}

View File

@ -1,40 +0,0 @@
package haflow.ui.model;
import java.util.UUID;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "modifyModuleResult")
public class ModifyModuleResultModel {
private boolean success;
private UUID moduleId;
private String message;
@XmlElement
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
@XmlElement
public UUID getModuleId() {
return moduleId;
}
public void setModuleId(UUID moduleId) {
this.moduleId = moduleId;
}
@XmlElement
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}

View File

@ -1,8 +0,0 @@
package haflow.ui.model;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "removeModule")
public class RemoveModuleModel {
}

View File

@ -1,40 +0,0 @@
package haflow.ui.model;
import java.util.UUID;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "removeModuleResult")
public class RemoveModuleResultModel {
private boolean success;
private UUID moduleId;
private String message;
@XmlElement
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
@XmlElement
public UUID getModuleId() {
return moduleId;
}
public void setModuleId(UUID moduleId) {
this.moduleId = moduleId;
}
@XmlElement
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}

View File

@ -0,0 +1,152 @@
package haflow.utility;
import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import org.springframework.stereotype.Component;
@Component
public class ClassHelper {
private List<String> getClassNameByFile(String packageName,
String filePath, List<String> className,
boolean searchInChildPackage) throws Exception {
List<String> classNames = new ArrayList<String>();
String decodedFilePath = URLDecoder.decode(filePath, "utf-8");
File file = new File(decodedFilePath);
File[] classFiles = file.listFiles();
for (File classFile : classFiles) {
if (classFile.isDirectory()) {
if (searchInChildPackage) {
classNames.addAll(getClassNameByFile(packageName,
classFile.getPath(), classNames,
searchInChildPackage));
}
} else {
String classFilePath = classFile.getPath();
String base = Thread.currentThread().getContextClassLoader()
.getResource("").getPath();
classFilePath = classFilePath.replace(base, "");
if (classFilePath.endsWith(".class")) {
classFilePath = classFilePath.replace("\\", ".");
classFilePath = classFilePath.replace("/", ".");
classFilePath = classFilePath.substring(0,
classFilePath.lastIndexOf("."));
if (packageName.isEmpty()) {
while (true) {
try {
Class.forName(classFilePath);
break;
} catch (ClassNotFoundException ex) {
classFilePath = classFilePath
.substring(classFilePath.indexOf(".") + 1);
}
}
} else {
classFilePath = classFilePath.substring(classFilePath
.indexOf(packageName));
}
classNames.add(classFilePath);
}
}
}
return classNames;
}
private List<String> getClassNameByJar(String jarPath,
boolean searchInChildPackage) throws Exception {
List<String> classNames = new ArrayList<String>();
String[] jarInfo = jarPath.split("!");
String jarFilePath = jarInfo[0].substring(jarInfo[0].indexOf("/"));
String decodedJarFilePath = URLDecoder.decode(jarFilePath, "utf-8");
String packagePath = jarInfo[1].substring(1);
JarFile jarFile = new JarFile(decodedJarFilePath);
Enumeration<JarEntry> entrys = jarFile.entries();
while (entrys.hasMoreElements()) {
JarEntry jarEntry = entrys.nextElement();
String entryName = jarEntry.getName();
if (entryName.endsWith(".class")) {
if (searchInChildPackage) {
if (entryName.startsWith(packagePath)) {
entryName = entryName.replace("/", ".").substring(0,
entryName.lastIndexOf("."));
classNames.add(entryName);
}
} else {
int index = entryName.lastIndexOf("/");
String entryPath;
if (index != -1) {
entryPath = entryName.substring(0, index);
} else {
entryPath = entryName;
}
if (entryPath.equals(packagePath)) {
entryName = entryName.replace("/", ".").substring(0,
entryName.lastIndexOf("."));
classNames.add(entryName);
}
}
}
}
jarFile.close();
return classNames;
}
private List<String> getClassNameByJars(URL[] jarFiles, String packagePath,
boolean searchInChildPackage) throws Exception {
List<String> classNames = new ArrayList<String>();
if (jarFiles != null) {
for (int i = 0; i < jarFiles.length; i++) {
URL jarFileUrl = jarFiles[i];
String jarFilePath = jarFileUrl.getPath();
if (jarFilePath.endsWith("classes/")) {
continue;
}
String jarPath = jarFilePath + "!/" + packagePath;
classNames.addAll(this.getClassNameByJar(jarPath,
searchInChildPackage));
}
}
return classNames;
}
public List<String> getClassNames(String packageName,
boolean searchInchildPackage) {
try {
List<String> classNames = null;
ClassLoader classLoader = Thread.currentThread()
.getContextClassLoader();
String packagePath = packageName.replace(".", "/");
URL packageUrl = classLoader.getResource(packagePath);
if (packageUrl != null) {
String protocol = packageUrl.getProtocol();
if (protocol.equals("file")) {
classNames = this.getClassNameByFile(packageName,
packageUrl.getPath(), null, searchInchildPackage);
} else if (protocol.equals("jar")) {
classNames = this.getClassNameByJar(packageUrl.getPath(),
searchInchildPackage);
} else {
classNames = null;
}
} else {
classNames = this.getClassNameByJars(
((URLClassLoader) classLoader).getURLs(), packagePath,
searchInchildPackage);
}
return classNames;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}

View File

@ -15,10 +15,8 @@
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect </property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping class="haflow.entity.Configuration" />
<mapping class="haflow.entity.Edge" />
<mapping class="haflow.entity.Flow" />
<mapping class="haflow.entity.Module" />
<mapping class="haflow.entity.Node" />
<mapping class="haflow.profile.NodeAppearanceProfile" />

View File

@ -1,39 +0,0 @@
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>HA Flow Administration</title>
<link rel="stylesheet"
href="http://ajax.googleapis.com/ajax/libs/dojo/1.9.0/dijit/themes/claro/claro.css">
<link rel="stylesheet"
href="http://ajax.googleapis.com/ajax/libs/dojo/1.9.0/dojox/layout/resources/ScrollPane.css">
<link rel="stylesheet" href="<%=basePath%>/style/haflow.css">
<link rel="stylesheet" href="<%=basePath%>/style/haflow.ui.css">
<link rel="stylesheet" href="<%=basePath%>/style/haflow.admin.css">
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
<script type="text/javascript"
src="http://jsplumbtoolkit.com/js/jquery.ui.touch-punch.min.js"></script>
<script type="text/javascript"
src="http://jsplumbtoolkit.com/js/jquery.jsPlumb-1.4.1-all-min.js"></script>
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/dojo/1.9.0/dojo/dojo.js"></script>
<script type="text/javascript" src="<%=basePath%>/script/haflow.js"></script>
<script type="text/javascript" src="<%=basePath%>/script/haflow.ui.js"></script>
<script type="text/javascript" src="<%=basePath%>/script/haflow.admin.js"></script>
</head>
<body class="claro">
<input type="hidden" value="<%=basePath%>" id="basePath" />
</body>
</html>

View File

@ -1,278 +0,0 @@
dojo.require("dijit.TitlePane");
dojo.require("dijit.layout.BorderContainer");
dojo.require("dijit.layout.TabContainer");
dojo.require("dijit.layout.ContentPane");
dojo.require("dijit.Menu");
dojo.require("dijit.MenuItem");
dojo.require("dijit.MenuBar");
dojo.require("dijit.MenuBarItem");
dojo.require("dijit.PopupMenuBarItem");
dojo.require("dijit.form.Button");
dojo.require("dijit.form.TextBox");
dojo.require("dijit.form.SimpleTextarea");
dojo.require("dojo.dom");
dojo.require("dojo.json");
dojo.require("dojo.store.Memory");
dojo.require("dijit.tree.ObjectStoreModel");
dojo.require("dijit.Tree");
dojo.require("dojo.store.Observable");
var admin;
dojo.ready(function() {
admin = new HAFlow.Admin(new HAFlow.UI());
admin.init();
});
HAFlow.Admin = function(ui) {
this.basePath = dojo.byId("basePath").value;
this.ui = ui;
};
HAFlow.Admin.prototype.init = function() {
this.initUserInterface();
this.createAddModuleUserInterface();
this.loadModuleListData();
};
HAFlow.Admin.prototype.createAddModuleUserInterface = function() {
var text = "";
text += "<div>";
text += "<div>";
text += "<span>Name:</span>";
text += "<div id=\"add_module_name_container\"></div>";
text += "</div>";
text += "<div>";
text += "<div>Configurations: (displayName,key)</div>";
text += "<div id=\"add_module_configuration_container\"></div>";
text += "</div>";
text += "<div id=\"add_module_button\"></div>";
text += "</div>";
$("#" + this.addModuleContainerId).html(text);
var _currentInstance = this;
if (dijit.byId("add_module_name") != null) {
dijit.registry.remove("add_module_name");
}
var moduleNameTextBox = new dijit.form.TextBox({
id : "add_module_name",
});
moduleNameTextBox.placeAt(dojo.byId("add_module_name_container"));
moduleNameTextBox.startup();
if (dijit.byId("add_module_configuration") != null) {
dijit.registry.remove("add_module_configuration");
}
var textarea = new dijit.form.SimpleTextarea({
id : "add_module_configuration",
rows : 5,
cols : 50
});
textarea.placeAt(dojo.byId("add_module_configuration_container"));
textarea.startup();
var button = new dijit.form.Button({
label : "Add Module",
onClick : function() {
var module = {
name : $("#add_module_name").val(),
configurations : _currentInstance.extractConfiguration($(
"#add_module_configuration").val())
};
_currentInstance.addModule(_currentInstance, module);
}
});
button.placeAt(dojo.byId("add_module_button"));
button.startup();
};
HAFlow.Admin.prototype.extractConfiguration = function(source) {
var splited = source.split("\n");
var regex = (/\((.*),(.*)\)/);
var i;
var result = [];
for (i = 0; i < splited.length; i++) {
var r = regex.exec(splited[i]);
result.push({
id : HAFlow.generateUUID(),
displayName : r[1],
key : r[2]
});
}
return result;
};
HAFlow.Admin.prototype.loadModuleListData = function() {
var _currentInstance = this;
$.ajax({
url : _currentInstance.basePath + "module",
type : "GET",
dataType : "json",
success : function(data, status) {
_currentInstance.moduleList = data;
_currentInstance.generateModuleList(_currentInstance);
},
error : function(request, status, error) {
HAFlow.showDialog("Error",
"An error occurred while loading module list: " + error);
}
});
};
HAFlow.Admin.prototype.generateModuleList = function(instance) {
var text = "";
text += "<div>";
var i, j;
for (i = 0; i < this.moduleList.modules.length; i++) {
text += "<div>";
text += "<div><span>Id: " + this.moduleList.modules[i].id
+ "</span></div>";
text += "<div><span>Name: " + this.moduleList.modules[i].name
+ "</span></div>";
text += "<div>Configurations:</div>";
for (j = 0; j < this.moduleList.modules[i].configurations.length; j++) {
text += "<div>";
text += "<div>-->Id: "
+ this.moduleList.modules[i].configurations[j].id
+ "</div>";
text += "<div>-->Key: "
+ this.moduleList.modules[i].configurations[j].key
+ "</div>";
text += "<div>-->Display Name: "
+ this.moduleList.modules[i].configurations[j].displayName
+ "</div>";
text += "</div>";
text += "<p/>";
}
text += "<div id=\"remove_module_" + this.moduleList.modules[i].id
+ "\"></div>";
text += "<p/>";
text += "</div>";
}
text += "</div>";
$("#" + this.moduleListContainerId).html(text);
for (i = 0; i < this.moduleList.modules.length; i++) {
var _currentInstance = this;
var button = new dijit.form.Button({
label : "Remove Module",
moduleId : this.moduleList.modules[i].id,
onClick : function() {
var moduleId = $(this).attr("moduleId");
_currentInstance.removeModule(_currentInstance, moduleId);
}
});
button.placeAt(dojo.byId("remove_module_"
+ this.moduleList.modules[i].id));
button.startup();
}
};
HAFlow.Admin.prototype.removeModule = function(instance, moduleId) {
$.ajax({
url : instance.basePath + "module/" + moduleId,
type : "DELETE",
contentType : "application/json",
data : JSON.stringify({}),
dataType : "json",
success : function(data, status) {
if (data.success) {
HAFlow.showDialog("Remove Module", "Module removed.");
} else {
HAFlow.showDialog("Remove Module",
"An error occurred while removing module.");
}
instance.loadModuleListData();
},
error : function(request, status, error) {
HAFlow.showDialog("Error",
"An error occurred while removing module: " + error);
}
});
};
HAFlow.Admin.prototype.addModule = function(instance, module) {
$.ajax({
url : instance.basePath + "module",
type : "POST",
contentType : "application/json",
data : JSON.stringify(module),
dataType : "json",
success : function(data, status) {
if (data.success) {
HAFlow.showDialog("Add Module", "Module added.");
} else {
HAFlow.showDialog("Add Module",
"An error occurred while adding module.");
}
instance.loadModuleListData();
},
error : function(request, status, error) {
HAFlow.showDialog("Error",
"An error occurred while adding module: " + error);
}
});
};
HAFlow.Admin.prototype.initUserInterface = function() {
this.ui.init();
this.initUserInterfaceId();
this.initMainMenu();
this.initModuleListContainer();
this.initAddModuleContainer();
this.ui.refresh();
};
HAFlow.Admin.prototype.initUserInterfaceId = function() {
this.moduleListContainerId = "moduleListContainer";
this.addModuleContainerId = "addModuleContainer";
};
HAFlow.Admin.prototype.initMainMenu = function() {
this.menu = {};
this.initModuleMenu();
};
HAFlow.Admin.prototype.initModuleMenu = function() {
this.menu.moduleMenu = new dijit.Menu({
id : "moduleMenu"
});
this.menu.moduleMenu.newModuleMenuItem = new dijit.MenuItem({
id : "newModuleMenuItem",
label : "New Module",
});
this.menu.moduleMenu.modifyModuleMenuItem = new dijit.MenuItem({
id : "modifyModuleMenuItem",
label : "Modify Module"
});
this.menu.moduleMenu.removeModuleMenuItem = new dijit.MenuItem({
id : "removeModuleMenuItem",
label : "Remove Module"
});
this.menu.moduleMenu.addChild(this.menu.moduleMenu.newModuleMenuItem);
this.menu.moduleMenu.addChild(this.menu.moduleMenu.modifyModuleMenuItem);
this.menu.moduleMenu.addChild(this.menu.moduleMenu.removeModuleMenuItem);
this.menu.moduleMenu.startup();
this.ui.mainMenu.addChild(new dijit.PopupMenuBarItem({
id : "modulePopupMenuBarItem",
label : "Module",
popup : this.menu.moduleMenu
}));
};
HAFlow.Admin.prototype.initModuleListContainer = function() {
var moduleListContentPane = (new dijit.layout.ContentPane({
id : this.moduleListContainerId,
title : "Module List"
}));
this.ui.centerContainer.addChild(moduleListContentPane);
};
HAFlow.Admin.prototype.initAddModuleContainer = function() {
var addModuleContentPane = (new dijit.layout.ContentPane({
id : this.addModuleContainerId,
title : "Add Module"
}));
this.ui.centerContainer.addChild(addModuleContentPane);
};

View File

@ -1 +0,0 @@
@CHARSET "UTF-8";