summaryrefslogtreecommitdiff
path: root/src/main/java/com/c2kernel/process/module/Module.java
diff options
context:
space:
mode:
authorAndrew Branson <andrew.branson@cern.ch>2012-07-06 15:50:45 +0200
committerAndrew Branson <andrew.branson@cern.ch>2012-07-06 15:50:45 +0200
commitb53164978a9a264fbe26679c07e32731a4d495f9 (patch)
treeb0f0335625c4ee11012d51df3da5daae270bdd2f /src/main/java/com/c2kernel/process/module/Module.java
parent24314dc1699c7e73048fa24e33729f1aa1ec0e86 (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/module/Module.java')
-rw-r--r--src/main/java/com/c2kernel/process/module/Module.java140
1 files changed, 30 insertions, 110 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.");