diff options
| author | Andrew Branson <andrew.branson@cern.ch> | 2012-07-06 15:50:45 +0200 |
|---|---|---|
| committer | Andrew Branson <andrew.branson@cern.ch> | 2012-07-06 15:50:45 +0200 |
| commit | b53164978a9a264fbe26679c07e32731a4d495f9 (patch) | |
| tree | b0f0335625c4ee11012d51df3da5daae270bdd2f /src/main/java/com/c2kernel/process | |
| parent | 24314dc1699c7e73048fa24e33729f1aa1ec0e86 (diff) | |
Remove XML parsing from module processing, use Castor unmarshalling
instead.
Create module item with collection of imported Items and module XML
registered as an outcome.
Diffstat (limited to 'src/main/java/com/c2kernel/process')
4 files changed, 51 insertions, 129 deletions
diff --git a/src/main/java/com/c2kernel/process/module/Module.java b/src/main/java/com/c2kernel/process/module/Module.java index 22f4543..4a0987a 100644 --- a/src/main/java/com/c2kernel/process/module/Module.java +++ b/src/main/java/com/c2kernel/process/module/Module.java @@ -1,26 +1,19 @@ package com.c2kernel.process.module;
-import java.io.StringReader;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Properties;
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
-import org.w3c.dom.NodeList;
-import org.w3c.dom.Text;
-import org.xml.sax.InputSource;
-
import com.c2kernel.common.CannotManageException;
import com.c2kernel.common.ObjectAlreadyExistsException;
import com.c2kernel.common.ObjectCannotBeUpdated;
import com.c2kernel.common.ObjectNotFoundException;
import com.c2kernel.entity.proxy.ItemProxy;
+import com.c2kernel.lifecycle.instance.predefined.entitycreation.Dependency;
+import com.c2kernel.lifecycle.instance.predefined.entitycreation.DependencyMember;
import com.c2kernel.lifecycle.instance.predefined.entitycreation.NewAgent;
import com.c2kernel.lifecycle.instance.predefined.entitycreation.NewItem;
+import com.c2kernel.lifecycle.instance.predefined.entitycreation.Outcome;
import com.c2kernel.lookup.DomainPath;
import com.c2kernel.process.Bootstrap;
import com.c2kernel.process.Gateway;
@@ -37,109 +30,12 @@ public class Module { public ModuleImports imports = new ModuleImports();
public ArrayList<ModuleConfig> config = new ArrayList<ModuleConfig>();
public ArrayList<ModuleScript> scripts = new ArrayList<ModuleScript>();
- private static DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
- private DocumentBuilder parser;
- private Document moduleDOM;
+ public NewItem moduleItem;
- static {
- dbf.setValidating(false);
- dbf.setNamespaceAware(false);
- }
-
public Module() {
super();
}
- public Module(String moduleXML) throws ModuleException {
- try {
- parser = dbf.newDocumentBuilder();
- moduleDOM = parser.parse(new InputSource(new StringReader(moduleXML)));
- } catch (Exception e) {
- Logger.error(e);
- throw new ModuleException("Could not process modules. XML Parser exception");
- }
-
- Element root = (Element)moduleDOM.getElementsByTagName("CristalModule").item(0);
-
- // Get module metadata
- ns = root.getAttribute("ns");
- name = root.getAttribute("name");
-
- Element infoElem = (Element)moduleDOM.getElementsByTagName("Info").item(0);
- info = new ModuleInfo();
- info.desc = ((Text)infoElem.getElementsByTagName("Description").item(0).getFirstChild()).getData();
- info.version = ((Text)infoElem.getElementsByTagName("Version").item(0).getFirstChild()).getData();
- NodeList nl = infoElem.getElementsByTagName("Dependency");
- for (int i=0; i<nl.getLength();i++)
- info.dependency.add(((Text)nl.item(i).getFirstChild()).getData());
-
- // register resource URL
- nl = root.getElementsByTagName("ResourceURL");
- if (nl.getLength()>0) {
- resURL = ((Text)nl.item(0).getFirstChild()).getData();
- Resource.addModuleBaseURL(ns, resURL);
- }
-
- // Get config properties
- nl = root.getElementsByTagName("Config");
- for (int i=0; i<nl.getLength();i++) {
- Element confElement = (Element)nl.item(i);
- String target = confElement.getAttribute("target");
- String name = confElement.getAttribute("name");
- String value = ((Text)confElement.getFirstChild()).getData();
- config.add(new ModuleConfig(name, value, target));
- }
-
- // find scripts
- nl = root.getElementsByTagName("Script");
- for (int i=0; i<nl.getLength();i++) {
- Element confElement = (Element)nl.item(i);
- String target = confElement.getAttribute("target");
- String event = confElement.getAttribute("event");
- String lang = confElement.getAttribute("lang");
- scripts.add(new ModuleScript(target, event, lang, ((Text)confElement.getFirstChild()).getData()));
- }
-
- // Get imports
- nl = moduleDOM.getElementsByTagName("Imports");
- if (nl.getLength()>0) {
- Element impElem = (Element)nl.item(0);
- nl = impElem.getChildNodes();
- for (int i=0; i<nl.getLength();i++) {
- if (!(nl.item(i) instanceof Element)) continue;
- Element imp = (Element)nl.item(i);
- ModuleImport newImp;
- String type = imp.getTagName();
- if (type.equals("Resource")) {
- ModuleResource newRes = new ModuleResource(imp);
- newImp = newRes;
- }
- else if (type.equals("Item")) {
- NewItem newItem = new NewItem(ns, imp);
- newImp = newItem;
- }
- else if (type.equals("Agent")) {
- NewAgent newAgent = new NewAgent(imp);
- newImp = newAgent;
- }
- else {
- Logger.warning("Unknown import type "+type);
- continue;
- }
-
- newImp.name = imp.getAttribute("name");
- imports.list.add(newImp);
- Logger.msg(8, "Found import "+imports.list.size()+"- "+newImp.name+": "+newImp.getClass().getSimpleName());
-
- }
- }
-
- }
-
- public static com.c2kernel.property.Property newProperty(Element p) {
- return new com.c2kernel.property.Property(p.getAttribute("name"), ((Text)p.getFirstChild()).getData());
- }
-
public void runScript(String event, boolean isServer) throws ScriptingEngineException {
for (ModuleScript script : scripts) {
if (script.shouldRun(event, isServer)) {
@@ -156,7 +52,31 @@ public class Module { }
}
- public void importAll(ItemProxy serverEntity) throws ObjectCannotBeUpdated, ObjectNotFoundException, CannotManageException, ObjectAlreadyExistsException, NoSuchAlgorithmException {
+ public void addModuleItem(String moduleXML) {
+ NewItem moduleItem = new NewItem(name, "/desc/modules/", "ModuleWorkflow");
+ // Module properties
+ moduleItem.properties.add(new com.c2kernel.property.Property("Namespace", ns));
+ moduleItem.properties.add(new com.c2kernel.property.Property("Name", name));
+ moduleItem.properties.add(new com.c2kernel.property.Property("Type", "Module"));
+ moduleItem.properties.add(new com.c2kernel.property.Property("Version", info.version));
+ // Add dependency for all children
+ Dependency children = new Dependency("Contents");
+ for (ModuleImport thisImport : imports.list) {
+ String path = thisImport.getPath(ns);
+ if (path != null)
+ children.dependencyMemberList.add(
+ new DependencyMember(path+"/"+thisImport.name));
+ }
+ moduleItem.dependencyList.add(children);
+ // Add moduleXML
+ Outcome moduleOutcome = new Outcome("Module", "0", "last", null);
+ moduleOutcome.data = moduleXML;
+ moduleItem.outcomes.add(moduleOutcome);
+ imports.list.add(moduleItem);
+ }
+
+ public void importAll(ItemProxy serverEntity, String moduleXML) throws ObjectCannotBeUpdated, ObjectNotFoundException, CannotManageException, ObjectAlreadyExistsException, NoSuchAlgorithmException {
+ addModuleItem(moduleXML);
for (ModuleImport thisImp : imports.list) {
Logger.msg(5, "Importing "+thisImp.name+" "+thisImp.getClass().getSimpleName());
if (thisImp instanceof ModuleResource) {
@@ -169,7 +89,7 @@ public class Module { }
else if (thisImp instanceof NewItem) {
NewItem thisItem = (NewItem)thisImp;
- thisItem.ns=ns;
+ thisItem.setNamespace(ns);
try {
new DomainPath(new DomainPath(thisItem.initialPath), thisItem.name).getEntity();
Logger.msg(3, "Module.importAll() - Item '"+thisItem.name+"' found.");
diff --git a/src/main/java/com/c2kernel/process/module/ModuleImport.java b/src/main/java/com/c2kernel/process/module/ModuleImport.java index 60193c7..422b3d2 100644 --- a/src/main/java/com/c2kernel/process/module/ModuleImport.java +++ b/src/main/java/com/c2kernel/process/module/ModuleImport.java @@ -1,13 +1,9 @@ package com.c2kernel.process.module;
-import org.w3c.dom.Element;
-
public abstract class ModuleImport {
- Element elem;
public String name;
- public ModuleImport(Element elem) {
- this.elem = elem;
- }
+ public abstract String getPath(String ns);
+
}
\ No newline at end of file diff --git a/src/main/java/com/c2kernel/process/module/ModuleManager.java b/src/main/java/com/c2kernel/process/module/ModuleManager.java index ca47ec9..0870c6f 100644 --- a/src/main/java/com/c2kernel/process/module/ModuleManager.java +++ b/src/main/java/com/c2kernel/process/module/ModuleManager.java @@ -4,6 +4,7 @@ import java.net.URL; import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Enumeration;
+import java.util.HashMap;
import java.util.Properties;
import com.c2kernel.common.CannotManageException;
@@ -16,9 +17,11 @@ import com.c2kernel.process.Gateway; import com.c2kernel.scripting.ScriptingEngineException;
import com.c2kernel.utils.FileStringUtility;
import com.c2kernel.utils.Logger;
+import com.c2kernel.utils.Resource;
public class ModuleManager {
ArrayList<Module> modules = new ArrayList<Module>();
+ HashMap<String, String> modulesXML = new HashMap<String, String>();
Properties props = new Properties();
boolean isServer;
@@ -29,10 +32,11 @@ public class ModuleManager { while(moduleEnum.hasMoreElements()) {
URL newModuleURL = moduleEnum.nextElement();
try {
- //Module newModule = (Module)Gateway.getMarshaller().unmarshall(FileStringUtility.url2String(newModuleURL));
- //Resource.addModuleBaseURL(newModule.ns, newModule.resURL);
- Module newModule = new Module(FileStringUtility.url2String(newModuleURL));
+ String moduleXML = FileStringUtility.url2String(newModuleURL);
+ Module newModule = (Module)Gateway.getMarshaller().unmarshall(moduleXML);
+ Resource.addModuleBaseURL(newModule.ns, newModule.resURL);
modules.add(newModule);
+ modulesXML.put(newModule.ns, moduleXML);
if (loadedModules.contains(newModule.getName())) throw new ModuleException("Module name clash: "+newModule.getName());
if (moduleNs.contains(newModule.getNs())) throw new ModuleException("Module namespace clash: "+newModule.getNs());
Logger.debug(4, "Module found: "+newModule.getNs()+" - "+newModule.getName());
@@ -96,7 +100,7 @@ public class ModuleManager { Logger.debug(3, "Registering modules");
for (Module thisMod : modules) {
Logger.msg("Registering module "+thisMod.getName());
- thisMod.importAll(serverEntity);
+ thisMod.importAll(serverEntity, modulesXML.get(thisMod.ns));
Logger.msg("Module "+thisMod.getName()+" registered");
}
}
diff --git a/src/main/java/com/c2kernel/process/module/ModuleResource.java b/src/main/java/com/c2kernel/process/module/ModuleResource.java index f355f6f..274e522 100644 --- a/src/main/java/com/c2kernel/process/module/ModuleResource.java +++ b/src/main/java/com/c2kernel/process/module/ModuleResource.java @@ -1,21 +1,23 @@ package com.c2kernel.process.module;
-import org.w3c.dom.Element;
-import org.w3c.dom.Text;
-
public class ModuleResource extends ModuleImport {
public String resourceType;
public String resourceLocation;
public ModuleResource() {
- super(null);
}
- public ModuleResource(Element elem) {
- super(elem);
- resourceType = elem.getAttribute("type");
- resourceLocation = ((Text)elem.getFirstChild()).getData();
+ @Override
+ public String getPath(String ns) {
+ StringBuffer path = new StringBuffer();
+ if (resourceType.equals("CA") || resourceType.equals("EA"))
+ path.append("/desc/ActivityDesc/");
+ if (resourceType.equals("SC"))
+ path.append("/desc/Script/");
+ if (resourceType.equals("OD"))
+ path.append("/desc/OutcomeDesc/");
+ path.append("system/").append(ns==null?"kernel":ns);
+ return path.toString();
}
-
}
\ No newline at end of file |
