From 38d50a40c0f29f921403f98bc287187866c9bf50 Mon Sep 17 00:00:00 2001 From: Zhen Tang Date: Mon, 3 Jun 2013 16:49:45 +0800 Subject: [PATCH] Modules can be discovered and imported automatically. Module administration tools removed. --- .../java/haflow/entity/Configuration.java | 58 ---- src/main/java/haflow/entity/Module.java | 20 +- .../haflow/entity/ModuleConfiguration.java | 23 ++ src/main/java/haflow/entity/Node.java | 13 +- src/main/java/haflow/module/ModuleLoader.java | 74 +++++ .../java/haflow/module/ModuleMetadata.java | 9 + .../java/haflow/module/annotation/Module.java | 14 + .../annotation/ModuleConfiguration.java | 14 + .../java/haflow/module/basic/EndModule.java | 20 ++ .../java/haflow/module/basic/StartModule.java | 20 ++ .../java/haflow/service/ModuleService.java | 132 --------- .../haflow/ui/controller/HomeController.java | 4 - .../ui/controller/ModuleController.java | 37 --- .../java/haflow/ui/helper/FlowHelper.java | 21 +- .../java/haflow/ui/helper/ModuleHelper.java | 113 +------ .../java/haflow/ui/model/AddModuleModel.java | 33 --- .../haflow/ui/model/AddModuleResultModel.java | 40 --- .../haflow/ui/model/ConfigurationModel.java | 12 - .../haflow/ui/model/ModifyModuleModel.java | 44 --- .../ui/model/ModifyModuleResultModel.java | 40 --- .../haflow/ui/model/RemoveModuleModel.java | 8 - .../ui/model/RemoveModuleResultModel.java | 40 --- src/main/java/haflow/utility/ClassHelper.java | 152 ++++++++++ src/main/resources/hibernate.cfg.xml | 2 - src/main/webapp/WEB-INF/views/admin.jsp | 39 --- src/main/webapp/script/haflow.admin.js | 278 ------------------ src/main/webapp/style/haflow.admin.css | 1 - 27 files changed, 347 insertions(+), 914 deletions(-) delete mode 100644 src/main/java/haflow/entity/Configuration.java create mode 100644 src/main/java/haflow/entity/ModuleConfiguration.java create mode 100644 src/main/java/haflow/module/ModuleLoader.java create mode 100644 src/main/java/haflow/module/ModuleMetadata.java create mode 100644 src/main/java/haflow/module/annotation/Module.java create mode 100644 src/main/java/haflow/module/annotation/ModuleConfiguration.java create mode 100644 src/main/java/haflow/module/basic/EndModule.java create mode 100644 src/main/java/haflow/module/basic/StartModule.java delete mode 100644 src/main/java/haflow/service/ModuleService.java delete mode 100644 src/main/java/haflow/ui/model/AddModuleModel.java delete mode 100644 src/main/java/haflow/ui/model/AddModuleResultModel.java delete mode 100644 src/main/java/haflow/ui/model/ModifyModuleModel.java delete mode 100644 src/main/java/haflow/ui/model/ModifyModuleResultModel.java delete mode 100644 src/main/java/haflow/ui/model/RemoveModuleModel.java delete mode 100644 src/main/java/haflow/ui/model/RemoveModuleResultModel.java create mode 100644 src/main/java/haflow/utility/ClassHelper.java delete mode 100644 src/main/webapp/WEB-INF/views/admin.jsp delete mode 100644 src/main/webapp/script/haflow.admin.js delete mode 100644 src/main/webapp/style/haflow.admin.css diff --git a/src/main/java/haflow/entity/Configuration.java b/src/main/java/haflow/entity/Configuration.java deleted file mode 100644 index c08c657..0000000 --- a/src/main/java/haflow/entity/Configuration.java +++ /dev/null @@ -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; - } - -} diff --git a/src/main/java/haflow/entity/Module.java b/src/main/java/haflow/entity/Module.java index f748aa9..fe178a9 100644 --- a/src/main/java/haflow/entity/Module.java +++ b/src/main/java/haflow/entity/Module.java @@ -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 configurations; + private Set 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 getConfigurations() { + public Set getConfigurations() { return configurations; } - public void setConfigurations(Set configurations) { + public void setConfigurations(Set configurations) { this.configurations = configurations; } diff --git a/src/main/java/haflow/entity/ModuleConfiguration.java b/src/main/java/haflow/entity/ModuleConfiguration.java new file mode 100644 index 0000000..aff60ec --- /dev/null +++ b/src/main/java/haflow/entity/ModuleConfiguration.java @@ -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; + } + +} diff --git a/src/main/java/haflow/entity/Node.java b/src/main/java/haflow/entity/Node.java index f159441..0f0a58c 100644 --- a/src/main/java/haflow/entity/Node.java +++ b/src/main/java/haflow/entity/Node.java @@ -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") diff --git a/src/main/java/haflow/module/ModuleLoader.java b/src/main/java/haflow/module/ModuleLoader.java new file mode 100644 index 0000000..38125bb --- /dev/null +++ b/src/main/java/haflow/module/ModuleLoader.java @@ -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 searchForModules() { + try { + Map modules = new HashMap(); + List 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()); + 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; + } + } +} diff --git a/src/main/java/haflow/module/ModuleMetadata.java b/src/main/java/haflow/module/ModuleMetadata.java new file mode 100644 index 0000000..d3b4696 --- /dev/null +++ b/src/main/java/haflow/module/ModuleMetadata.java @@ -0,0 +1,9 @@ +package haflow.module; + +import java.util.Map; + +import org.w3c.dom.Document; + +public interface ModuleMetadata { + public Document generate(Map configurations); +} diff --git a/src/main/java/haflow/module/annotation/Module.java b/src/main/java/haflow/module/annotation/Module.java new file mode 100644 index 0000000..96e13ab --- /dev/null +++ b/src/main/java/haflow/module/annotation/Module.java @@ -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(); +} diff --git a/src/main/java/haflow/module/annotation/ModuleConfiguration.java b/src/main/java/haflow/module/annotation/ModuleConfiguration.java new file mode 100644 index 0000000..eaa6afa --- /dev/null +++ b/src/main/java/haflow/module/annotation/ModuleConfiguration.java @@ -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(); +} diff --git a/src/main/java/haflow/module/basic/EndModule.java b/src/main/java/haflow/module/basic/EndModule.java new file mode 100644 index 0000000..ed45bdb --- /dev/null +++ b/src/main/java/haflow/module/basic/EndModule.java @@ -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 configurations) { + // TODO Auto-generated method stub + return null; + } + +} diff --git a/src/main/java/haflow/module/basic/StartModule.java b/src/main/java/haflow/module/basic/StartModule.java new file mode 100644 index 0000000..341a34e --- /dev/null +++ b/src/main/java/haflow/module/basic/StartModule.java @@ -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 configurations) { + // TODO Auto-generated method stub + return null; + } + +} diff --git a/src/main/java/haflow/service/ModuleService.java b/src/main/java/haflow/service/ModuleService.java deleted file mode 100644 index 214e51f..0000000 --- a/src/main/java/haflow/service/ModuleService.java +++ /dev/null @@ -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 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()); - 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 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 getModuleList() { - Session session = this.getSessionHelper().openSession(); - try { - Query query = session.createQuery("from Module module"); - List modules = (List) 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; - } - } -} diff --git a/src/main/java/haflow/ui/controller/HomeController.java b/src/main/java/haflow/ui/controller/HomeController.java index 697acf0..0fee8a2 100644 --- a/src/main/java/haflow/ui/controller/HomeController.java +++ b/src/main/java/haflow/ui/controller/HomeController.java @@ -11,8 +11,4 @@ public class HomeController { return new ModelAndView("main"); } - @RequestMapping("/admin") - public ModelAndView admin() { - return new ModelAndView("admin"); - } } diff --git a/src/main/java/haflow/ui/controller/ModuleController.java b/src/main/java/haflow/ui/controller/ModuleController.java index b40c9e8..e799b17 100644 --- a/src/main/java/haflow/ui/controller/ModuleController.java +++ b/src/main/java/haflow/ui/controller/ModuleController.java @@ -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); - } } diff --git a/src/main/java/haflow/ui/helper/FlowHelper.java b/src/main/java/haflow/ui/helper/FlowHelper.java index b2d253c..4fdc0f6 100644 --- a/src/main/java/haflow/ui/helper/FlowHelper.java +++ b/src/main/java/haflow/ui/helper/FlowHelper.java @@ -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(), diff --git a/src/main/java/haflow/ui/helper/ModuleHelper.java b/src/main/java/haflow/ui/helper/ModuleHelper.java index 9efa6d2..12e8be2 100644 --- a/src/main/java/haflow/ui/helper/ModuleHelper.java +++ b/src/main/java/haflow/ui/helper/ModuleHelper.java @@ -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 moduleList = this.getModuleService().getModuleList(); + Set moduleList = this.getModuleLoader().searchForModules() + .keySet(); ModuleListModel moduleListModel = new ModuleListModel(); moduleListModel.setModules(new ArrayList()); for (Module module : moduleList) { @@ -46,10 +38,9 @@ public class ModuleHelper { moduleBriefModel.setName(module.getName()); moduleBriefModel .setConfigurations(new HashSet()); - 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()); - 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 configurations = new HashSet(); - 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 newConfigurations = new HashSet(); - 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; - } } diff --git a/src/main/java/haflow/ui/model/AddModuleModel.java b/src/main/java/haflow/ui/model/AddModuleModel.java deleted file mode 100644 index f2c77e8..0000000 --- a/src/main/java/haflow/ui/model/AddModuleModel.java +++ /dev/null @@ -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 configurations; - - @XmlElement - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - @XmlElementWrapper(name = "configurations") - @XmlElement(name = "configuration") - public Set getConfigurations() { - return configurations; - } - - public void setConfigurations(Set configurations) { - this.configurations = configurations; - } - -} diff --git a/src/main/java/haflow/ui/model/AddModuleResultModel.java b/src/main/java/haflow/ui/model/AddModuleResultModel.java deleted file mode 100644 index b8219ab..0000000 --- a/src/main/java/haflow/ui/model/AddModuleResultModel.java +++ /dev/null @@ -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; - } -} diff --git a/src/main/java/haflow/ui/model/ConfigurationModel.java b/src/main/java/haflow/ui/model/ConfigurationModel.java index 6920a05..1052b73 100644 --- a/src/main/java/haflow/ui/model/ConfigurationModel.java +++ b/src/main/java/haflow/ui/model/ConfigurationModel.java @@ -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; diff --git a/src/main/java/haflow/ui/model/ModifyModuleModel.java b/src/main/java/haflow/ui/model/ModifyModuleModel.java deleted file mode 100644 index 1d5eedd..0000000 --- a/src/main/java/haflow/ui/model/ModifyModuleModel.java +++ /dev/null @@ -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 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 getNewConfigurations() { - return newConfigurations; - } - - public void setNewConfigurations(Set newConfigurations) { - this.newConfigurations = newConfigurations; - } - -} diff --git a/src/main/java/haflow/ui/model/ModifyModuleResultModel.java b/src/main/java/haflow/ui/model/ModifyModuleResultModel.java deleted file mode 100644 index d497a6a..0000000 --- a/src/main/java/haflow/ui/model/ModifyModuleResultModel.java +++ /dev/null @@ -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; - } -} diff --git a/src/main/java/haflow/ui/model/RemoveModuleModel.java b/src/main/java/haflow/ui/model/RemoveModuleModel.java deleted file mode 100644 index eca50f9..0000000 --- a/src/main/java/haflow/ui/model/RemoveModuleModel.java +++ /dev/null @@ -1,8 +0,0 @@ -package haflow.ui.model; - -import javax.xml.bind.annotation.XmlRootElement; - -@XmlRootElement(name = "removeModule") -public class RemoveModuleModel { - -} diff --git a/src/main/java/haflow/ui/model/RemoveModuleResultModel.java b/src/main/java/haflow/ui/model/RemoveModuleResultModel.java deleted file mode 100644 index 9237883..0000000 --- a/src/main/java/haflow/ui/model/RemoveModuleResultModel.java +++ /dev/null @@ -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; - } -} diff --git a/src/main/java/haflow/utility/ClassHelper.java b/src/main/java/haflow/utility/ClassHelper.java new file mode 100644 index 0000000..434398a --- /dev/null +++ b/src/main/java/haflow/utility/ClassHelper.java @@ -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 getClassNameByFile(String packageName, + String filePath, List className, + boolean searchInChildPackage) throws Exception { + List classNames = new ArrayList(); + 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 getClassNameByJar(String jarPath, + boolean searchInChildPackage) throws Exception { + List classNames = new ArrayList(); + 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 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 getClassNameByJars(URL[] jarFiles, String packagePath, + boolean searchInChildPackage) throws Exception { + List classNames = new ArrayList(); + 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 getClassNames(String packageName, + boolean searchInchildPackage) { + try { + List 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; + } + } + +} diff --git a/src/main/resources/hibernate.cfg.xml b/src/main/resources/hibernate.cfg.xml index 3bb3611..aa92dce 100644 --- a/src/main/resources/hibernate.cfg.xml +++ b/src/main/resources/hibernate.cfg.xml @@ -15,10 +15,8 @@ org.hibernate.dialect.MySQLDialect update - - diff --git a/src/main/webapp/WEB-INF/views/admin.jsp b/src/main/webapp/WEB-INF/views/admin.jsp deleted file mode 100644 index 9a29e49..0000000 --- a/src/main/webapp/WEB-INF/views/admin.jsp +++ /dev/null @@ -1,39 +0,0 @@ -<%@ page language="java" contentType="text/html; charset=utf-8" - pageEncoding="utf-8"%> - -<% - String path = request.getContextPath(); - String basePath = request.getScheme() + "://" - + request.getServerName() + ":" + request.getServerPort() - + path + "/"; -%> - - - -HA Flow Administration - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/main/webapp/script/haflow.admin.js b/src/main/webapp/script/haflow.admin.js deleted file mode 100644 index c0dd1fb..0000000 --- a/src/main/webapp/script/haflow.admin.js +++ /dev/null @@ -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 += "
"; - text += "
"; - text += "Name:"; - text += "
"; - text += "
"; - text += "
"; - text += "
Configurations: (displayName,key)
"; - text += "
"; - text += "
"; - text += "
"; - text += "
"; - $("#" + 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 += "
"; - var i, j; - for (i = 0; i < this.moduleList.modules.length; i++) { - text += "
"; - text += "
Id: " + this.moduleList.modules[i].id - + "
"; - text += "
Name: " + this.moduleList.modules[i].name - + "
"; - text += "
Configurations:
"; - for (j = 0; j < this.moduleList.modules[i].configurations.length; j++) { - text += "
"; - text += "
-->Id: " - + this.moduleList.modules[i].configurations[j].id - + "
"; - text += "
-->Key: " - + this.moduleList.modules[i].configurations[j].key - + "
"; - text += "
-->Display Name: " - + this.moduleList.modules[i].configurations[j].displayName - + "
"; - text += "
"; - text += "

"; - } - text += "

"; - text += "

"; - text += "

"; - } - text += "
"; - $("#" + 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); -}; diff --git a/src/main/webapp/style/haflow.admin.css b/src/main/webapp/style/haflow.admin.css deleted file mode 100644 index bf67b66..0000000 --- a/src/main/webapp/style/haflow.admin.css +++ /dev/null @@ -1 +0,0 @@ -@CHARSET "UTF-8"; \ No newline at end of file