summaryrefslogtreecommitdiff
path: root/src/main/java/org/cristalise/kernel/process/module
diff options
context:
space:
mode:
authorAndrew Branson <andrew.branson@cern.ch>2014-10-07 09:18:11 +0200
committerAndrew Branson <andrew.branson@cern.ch>2014-10-07 09:18:11 +0200
commit0ed2c1124cf1b9e49a2ec1fa0126a8df09f9e758 (patch)
treee3a56cee83865f8c703deb790c15d3e79e871a82 /src/main/java/org/cristalise/kernel/process/module
parent50aa8aaab42fa62267aa1ae6a6070013096f5082 (diff)
Repackage to org.cristalise
Diffstat (limited to 'src/main/java/org/cristalise/kernel/process/module')
-rw-r--r--src/main/java/org/cristalise/kernel/process/module/Module.java224
-rw-r--r--src/main/java/org/cristalise/kernel/process/module/ModuleConfig.java42
-rw-r--r--src/main/java/org/cristalise/kernel/process/module/ModuleException.java29
-rw-r--r--src/main/java/org/cristalise/kernel/process/module/ModuleImport.java87
-rw-r--r--src/main/java/org/cristalise/kernel/process/module/ModuleImports.java78
-rw-r--r--src/main/java/org/cristalise/kernel/process/module/ModuleInfo.java34
-rw-r--r--src/main/java/org/cristalise/kernel/process/module/ModuleManager.java211
-rw-r--r--src/main/java/org/cristalise/kernel/process/module/ModuleResource.java80
-rw-r--r--src/main/java/org/cristalise/kernel/process/module/ModuleScript.java53
9 files changed, 838 insertions, 0 deletions
diff --git a/src/main/java/org/cristalise/kernel/process/module/Module.java b/src/main/java/org/cristalise/kernel/process/module/Module.java
new file mode 100644
index 0000000..840614b
--- /dev/null
+++ b/src/main/java/org/cristalise/kernel/process/module/Module.java
@@ -0,0 +1,224 @@
+/**
+ * This file is part of the CRISTAL-iSE kernel.
+ * Copyright (c) 2001-2014 The CRISTAL Consortium. All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 3 of the License, or (at
+ * your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ *
+ * http://www.fsf.org/licensing/licenses/lgpl.html
+ */
+package org.cristalise.kernel.process.module;
+
+import java.util.ArrayList;
+import java.util.Properties;
+
+import org.cristalise.kernel.common.ObjectNotFoundException;
+import org.cristalise.kernel.entity.imports.ImportAgent;
+import org.cristalise.kernel.entity.imports.ImportDependency;
+import org.cristalise.kernel.entity.imports.ImportDependencyMember;
+import org.cristalise.kernel.entity.imports.ImportItem;
+import org.cristalise.kernel.entity.imports.ImportOutcome;
+import org.cristalise.kernel.entity.imports.ImportRole;
+import org.cristalise.kernel.entity.proxy.AgentProxy;
+import org.cristalise.kernel.entity.proxy.ItemProxy;
+import org.cristalise.kernel.lookup.DomainPath;
+import org.cristalise.kernel.lookup.RolePath;
+import org.cristalise.kernel.process.Gateway;
+import org.cristalise.kernel.property.Property;
+import org.cristalise.kernel.scripting.ErrorInfo;
+import org.cristalise.kernel.scripting.ScriptingEngineException;
+import org.cristalise.kernel.utils.Logger;
+
+
+public class Module extends ImportItem {
+
+ private ModuleInfo info;
+ private String resURL;
+ private ModuleImports imports = new ModuleImports();
+ private ArrayList<ModuleConfig> config = new ArrayList<ModuleConfig>();
+ private ArrayList<ModuleScript> scripts = new ArrayList<ModuleScript>();
+
+ public Module() {
+ super();
+ // Module properties
+ properties.add(new Property("Type", "Module", false));
+ setInitialPath("/desc/modules/");
+ setWorkflow("NoWorkflow");
+ setWorkflowVer(0);
+ imports.list.add(this);
+ }
+
+ public void runScript(String event, AgentProxy user, boolean isServer) throws ScriptingEngineException {
+ for (ModuleScript script : scripts) {
+ if (script.shouldRun(event, isServer)) {
+ Logger.msg("Running "+script.event+" "+script.target+" script from "+name);
+ Object result = script.getScript(ns, user).execute();
+ if (result instanceof ErrorInfo) {
+ ErrorInfo error = (ErrorInfo) result;
+ Logger.error(error.toString());
+ if (error.getFatal())
+ throw new ScriptingEngineException("Fatal Script Error");
+ }
+ else if (result != null)
+ Logger.msg(result.toString());
+ }
+ }
+ }
+
+ public void setModuleXML(String moduleXML) {
+ ImportOutcome moduleOutcome = new ImportOutcome("Module", 0, "last", null);
+ moduleOutcome.data = moduleXML;
+ outcomes.add(moduleOutcome);
+ }
+
+ @Override
+ public void setNamespace(String ns) {
+ super.setNamespace(ns);
+ replaceProp(new Property("Namespace", ns, false));
+ }
+
+ @Override
+ public void setName(String name) {
+ super.setName(name);
+ replaceProp(new Property("Name", name, false));
+ }
+
+ private void replaceProp(Property newProp) {
+ for (Property prop : properties) {
+ if (prop.getName().equals("Namespace")) {
+ prop.setMutable(newProp.isMutable());
+ prop.setValue(newProp.getValue());
+ return;
+ }
+ }
+ properties.add(newProp);
+ }
+ public void importAll(ItemProxy serverEntity, AgentProxy systemAgent, String moduleXML, boolean reset) throws Exception {
+ setModuleXML(moduleXML);
+
+ for (ModuleResource thisRes : imports.getResources()) {
+ try {
+ thisRes.setNamespace(ns);
+ thisRes.create(systemAgent.getPath(), reset);
+ } catch (Exception ex) {
+ Logger.error(ex);
+ Logger.die("Error importing module resources. Unsafe to continue.");
+ }
+ }
+
+ for (ImportRole thisRole : imports.getRoles()) {
+ RolePath rolePath;
+ try {
+ String roleName = thisRole.name;
+ if (roleName.indexOf('/') > -1) roleName = roleName.substring(roleName.indexOf('/')+1);
+ rolePath = Gateway.getLookup().getRolePath(roleName);
+ if (rolePath.hasJobList() != thisRole.hasJobList()) {
+ Logger.msg("Module.importAll() - Role '"+thisRole.name+"' has incorrect joblist settings. Correcting.");
+ rolePath.setHasJobList(thisRole.hasJobList());
+ Gateway.getLookupManager().createRole(rolePath);
+ }
+ } catch (ObjectNotFoundException ex) {
+ Logger.msg("Module.importAll() - Role '"+thisRole.name+"' not found. Creating.");
+ thisRole.create(systemAgent.getPath(), reset);
+ }
+ }
+
+ for (ImportAgent thisAgent : imports.getAgents()) {
+ try {
+ Gateway.getLookup().getAgentPath(thisAgent.name);
+ Logger.msg(3, "Module.importAll() - User '"+thisAgent.name+"' found.");
+ continue;
+ } catch (ObjectNotFoundException ex) { }
+ Logger.msg("Module.importAll() - User '"+thisAgent.name+"' not found. Creating.");
+ thisAgent.create(systemAgent.getPath(), reset);
+ }
+
+ for (ImportItem thisItem : imports.getItems()) {
+ thisItem.setNamespace(ns);
+ thisItem.create(systemAgent.getPath(), reset);
+ }
+
+ }
+
+ public Properties getProperties(boolean isServer) {
+ Properties props = new Properties();
+ for (ModuleConfig thisProp : config) {
+ if (thisProp.include(isServer))
+ props.put(thisProp.name, thisProp.value);
+ }
+ return props;
+ }
+
+ public ArrayList<ModuleScript> getScripts() {
+ return scripts;
+ }
+
+ public void setResURL(String resURL) {
+ this.resURL = resURL;
+ }
+ public String getDesc() {
+ return info.desc;
+ }
+ public String getVersion() {
+ return info.version;
+ }
+ public String getResURL() {
+ return resURL;
+ }
+ public ArrayList<String> getDependencies() {
+ return info.dependency;
+ }
+ public boolean hasDependency(String dep) {
+ return info.dependency.contains(dep);
+ }
+
+ public ModuleInfo getInfo() {
+ return info;
+ }
+
+ public void setInfo(ModuleInfo info) {
+ this.info = info;
+ replaceProp(new Property("Version", info.version, true));
+ }
+
+ public ModuleImports getImports() {
+ return imports;
+ }
+
+ public void setImports(ModuleImports imp) {
+ // Add dependency for all children
+ imports = imp;
+ ImportDependency children = new ImportDependency("Contents");
+ for (ModuleImport thisImport : imports.list) {
+ DomainPath path = thisImport.domainPath;
+ if (path != null)
+ children.dependencyMemberList.add(new ImportDependencyMember(path.toString()));
+ }
+ dependencyList.add(children);
+ }
+
+ public void setConfig(ArrayList<ModuleConfig> config) {
+ this.config = config;
+ }
+
+ public void setScripts(ArrayList<ModuleScript> scripts) {
+ this.scripts = scripts;
+ }
+
+ public ArrayList<ModuleConfig> getConfig() {
+ return config;
+ }
+
+
+}
diff --git a/src/main/java/org/cristalise/kernel/process/module/ModuleConfig.java b/src/main/java/org/cristalise/kernel/process/module/ModuleConfig.java
new file mode 100644
index 0000000..1165e31
--- /dev/null
+++ b/src/main/java/org/cristalise/kernel/process/module/ModuleConfig.java
@@ -0,0 +1,42 @@
+/**
+ * This file is part of the CRISTAL-iSE kernel.
+ * Copyright (c) 2001-2014 The CRISTAL Consortium. All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 3 of the License, or (at
+ * your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ *
+ * http://www.fsf.org/licensing/licenses/lgpl.html
+ */
+package org.cristalise.kernel.process.module;
+
+public class ModuleConfig {
+
+ public String name;
+ public String value;
+ public String target;
+
+ public ModuleConfig() {
+ }
+
+ public ModuleConfig(String name, String value, String target) {
+ super();
+ this.name = name;
+ this.value = value;
+ this.target = target;
+ }
+
+ public boolean include(boolean isServer) {
+ return (target == null || target.length() == 0 || isServer == target.equals("server"));
+ }
+}
diff --git a/src/main/java/org/cristalise/kernel/process/module/ModuleException.java b/src/main/java/org/cristalise/kernel/process/module/ModuleException.java
new file mode 100644
index 0000000..e09107e
--- /dev/null
+++ b/src/main/java/org/cristalise/kernel/process/module/ModuleException.java
@@ -0,0 +1,29 @@
+/**
+ * This file is part of the CRISTAL-iSE kernel.
+ * Copyright (c) 2001-2014 The CRISTAL Consortium. All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 3 of the License, or (at
+ * your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ *
+ * http://www.fsf.org/licensing/licenses/lgpl.html
+ */
+package org.cristalise.kernel.process.module;
+
+public class ModuleException extends Exception {
+
+ public ModuleException(String msg) {
+ super(msg);
+ }
+
+}
diff --git a/src/main/java/org/cristalise/kernel/process/module/ModuleImport.java b/src/main/java/org/cristalise/kernel/process/module/ModuleImport.java
new file mode 100644
index 0000000..22faa5a
--- /dev/null
+++ b/src/main/java/org/cristalise/kernel/process/module/ModuleImport.java
@@ -0,0 +1,87 @@
+/**
+ * This file is part of the CRISTAL-iSE kernel.
+ * Copyright (c) 2001-2014 The CRISTAL Consortium. All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 3 of the License, or (at
+ * your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ *
+ * http://www.fsf.org/licensing/licenses/lgpl.html
+ */
+package org.cristalise.kernel.process.module;
+
+import org.cristalise.kernel.common.CannotManageException;
+import org.cristalise.kernel.common.InvalidCollectionModification;
+import org.cristalise.kernel.common.ObjectAlreadyExistsException;
+import org.cristalise.kernel.common.ObjectCannotBeUpdated;
+import org.cristalise.kernel.common.ObjectNotFoundException;
+import org.cristalise.kernel.lookup.AgentPath;
+import org.cristalise.kernel.lookup.DomainPath;
+import org.cristalise.kernel.lookup.InvalidItemPathException;
+import org.cristalise.kernel.lookup.ItemPath;
+
+
+public abstract class ModuleImport {
+
+ protected String ns;
+ protected String name;
+ protected DomainPath domainPath;
+ protected ItemPath itemPath;
+
+ public ModuleImport() {
+ }
+
+ public abstract void create(AgentPath agentPath, boolean reset) throws ObjectNotFoundException,
+ ObjectCannotBeUpdated, CannotManageException, ObjectAlreadyExistsException, InvalidCollectionModification;
+
+ public void setID( String uuid ) throws InvalidItemPathException
+ {
+ if (uuid != null && uuid.length() > 0) itemPath = new ItemPath(uuid);
+ }
+
+ public String getID() {
+ return itemPath==null?null:itemPath.getUUID().toString();
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public void setNamespace(String ns) {
+ this.ns = ns;
+ }
+
+ public String getNamespace() {
+ return ns;
+ }
+
+ public DomainPath getDomainPath() {
+ return domainPath;
+ }
+
+ public void setDomainPath(DomainPath domainPath) {
+ this.domainPath = domainPath;
+ }
+
+ public ItemPath getItemPath() {
+ return itemPath;
+ }
+
+ public void setItemPath(ItemPath itemPath) {
+ this.itemPath = itemPath;
+ }
+} \ No newline at end of file
diff --git a/src/main/java/org/cristalise/kernel/process/module/ModuleImports.java b/src/main/java/org/cristalise/kernel/process/module/ModuleImports.java
new file mode 100644
index 0000000..53c7e9d
--- /dev/null
+++ b/src/main/java/org/cristalise/kernel/process/module/ModuleImports.java
@@ -0,0 +1,78 @@
+/**
+ * This file is part of the CRISTAL-iSE kernel.
+ * Copyright (c) 2001-2014 The CRISTAL Consortium. All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 3 of the License, or (at
+ * your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ *
+ * http://www.fsf.org/licensing/licenses/lgpl.html
+ */
+package org.cristalise.kernel.process.module;
+
+import java.util.ArrayList;
+
+import org.cristalise.kernel.entity.imports.ImportAgent;
+import org.cristalise.kernel.entity.imports.ImportItem;
+import org.cristalise.kernel.entity.imports.ImportRole;
+import org.cristalise.kernel.utils.CastorArrayList;
+
+
+public class ModuleImports extends CastorArrayList<ModuleImport> {
+
+ public ModuleImports()
+ {
+ super();
+ }
+
+ public ModuleImports(ArrayList<ModuleImport> aList)
+ {
+ super(aList);
+ }
+
+ public ArrayList<ModuleResource> getResources() {
+ ArrayList<ModuleResource> subset = new ArrayList<ModuleResource>();
+ for (ModuleImport imp : list) {
+ if (imp instanceof ModuleResource)
+ subset.add((ModuleResource)imp);
+ }
+ return subset;
+ }
+
+ public ArrayList<ImportItem> getItems() {
+ ArrayList<ImportItem> subset = new ArrayList<ImportItem>();
+ for (ModuleImport imp : list) {
+ if (imp instanceof ImportItem)
+ subset.add((ImportItem)imp);
+ }
+ return subset;
+ }
+
+ public ArrayList<ImportAgent> getAgents() {
+ ArrayList<ImportAgent> subset = new ArrayList<ImportAgent>();
+ for (ModuleImport imp : list) {
+ if (imp instanceof ImportAgent)
+ subset.add((ImportAgent)imp);
+ }
+ return subset;
+ }
+
+ public ArrayList<ImportRole> getRoles() {
+ ArrayList<ImportRole> subset = new ArrayList<ImportRole>();
+ for (ModuleImport imp : list) {
+ if (imp instanceof ImportRole)
+ subset.add((ImportRole)imp);
+ }
+ return subset;
+ }
+}
diff --git a/src/main/java/org/cristalise/kernel/process/module/ModuleInfo.java b/src/main/java/org/cristalise/kernel/process/module/ModuleInfo.java
new file mode 100644
index 0000000..8f482fc
--- /dev/null
+++ b/src/main/java/org/cristalise/kernel/process/module/ModuleInfo.java
@@ -0,0 +1,34 @@
+/**
+ * This file is part of the CRISTAL-iSE kernel.
+ * Copyright (c) 2001-2014 The CRISTAL Consortium. All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 3 of the License, or (at
+ * your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ *
+ * http://www.fsf.org/licensing/licenses/lgpl.html
+ */
+package org.cristalise.kernel.process.module;
+
+import java.util.ArrayList;
+
+public class ModuleInfo {
+
+ public String desc;
+ public String version;
+ public ArrayList<String> dependency = new ArrayList<String>();
+
+ public ModuleInfo() {
+ }
+
+}
diff --git a/src/main/java/org/cristalise/kernel/process/module/ModuleManager.java b/src/main/java/org/cristalise/kernel/process/module/ModuleManager.java
new file mode 100644
index 0000000..d95abe6
--- /dev/null
+++ b/src/main/java/org/cristalise/kernel/process/module/ModuleManager.java
@@ -0,0 +1,211 @@
+/**
+ * This file is part of the CRISTAL-iSE kernel.
+ * Copyright (c) 2001-2014 The CRISTAL Consortium. All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 3 of the License, or (at
+ * your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ *
+ * http://www.fsf.org/licensing/licenses/lgpl.html
+ */
+package org.cristalise.kernel.process.module;
+
+import java.io.IOException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Properties;
+
+import org.cristalise.kernel.common.InvalidDataException;
+import org.cristalise.kernel.common.ObjectNotFoundException;
+import org.cristalise.kernel.entity.proxy.AgentProxy;
+import org.cristalise.kernel.entity.proxy.ItemProxy;
+import org.cristalise.kernel.lookup.DomainPath;
+import org.cristalise.kernel.persistency.outcome.OutcomeValidator;
+import org.cristalise.kernel.persistency.outcome.Schema;
+import org.cristalise.kernel.process.Gateway;
+import org.cristalise.kernel.scripting.ScriptingEngineException;
+import org.cristalise.kernel.utils.FileStringUtility;
+import org.cristalise.kernel.utils.Logger;
+
+
+public class ModuleManager {
+ ArrayList<Module> modules = new ArrayList<Module>();
+ HashMap<String, String> modulesXML = new HashMap<String, String>();
+ Properties props = new Properties();
+ AgentProxy user;
+ boolean isServer;
+ OutcomeValidator moduleValidator;
+
+ public ModuleManager(Enumeration<URL> moduleEnum, boolean isServer) throws ModuleException {
+ this.isServer = isServer;
+ try {
+ Schema moduleSchema = new Schema("Module", 0,
+ FileStringUtility.url2String(Gateway.getResource().getKernelResourceURL("boot/OD/Module.xsd")));
+ moduleValidator = new OutcomeValidator(moduleSchema);
+ } catch (InvalidDataException ex) {
+ Logger.error(ex);
+ throw new ModuleException("Module Schema is not valid");
+ } catch (IOException ex) {
+ throw new ModuleException("Could not load Module Schema from kernel resources");
+ }
+ ArrayList<String> loadedModules = new ArrayList<String>();
+ ArrayList<String> moduleNs = new ArrayList<String>();
+ while(moduleEnum.hasMoreElements()) {
+ URL newModuleURL = moduleEnum.nextElement();
+ try {
+ String moduleXML = FileStringUtility.url2String(newModuleURL);
+ String errors = moduleValidator.validate(moduleXML);
+ if (errors.length() > 0)
+ throw new ModuleException("Module XML found at "+newModuleURL+" was not valid: "+errors);
+ Module newModule = (Module)Gateway.getMarshaller().unmarshall(moduleXML);
+ if (newModule.getResURL() != null && newModule.getResURL().length()>0) Gateway.getResource().addModuleBaseURL(newModule.getNamespace(), newModule.getResURL());
+ modules.add(newModule);
+ modulesXML.put(newModule.getNamespace(), moduleXML);
+ if (loadedModules.contains(newModule.getName())) throw new ModuleException("Module name clash: "+newModule.getName());
+ if (moduleNs.contains(newModule.getNamespace())) throw new ModuleException("Module namespace clash: "+newModule.getNamespace());
+ Logger.debug(4, "Module found: "+newModule.getNamespace()+" - "+newModule.getName());
+ loadedModules.add(newModule.getName()); moduleNs.add(newModule.getNamespace());
+ } catch (ModuleException e) {
+ Logger.error("Could not load module description from "+newModuleURL);
+ throw e;
+ } catch (Exception e) {
+ Logger.error(e);
+ throw new ModuleException("Could not load module.xml from "+newModuleURL);
+ }
+ }
+
+ Logger.debug(5, "Checking dependencies");
+ boolean allDepsPresent = true;
+
+ ArrayList<String> prevModules = new ArrayList<String>();
+ for (int i=0; i<modules.size();i++) {
+ boolean depClean = false;
+ int skipped = 0;
+ Module thisMod = modules.get(i);
+ Logger.msg(5, "Checking dependencies of module "+thisMod.getName());
+ while (!depClean) {
+ ArrayList<String> deps = thisMod.getDependencies();
+ depClean = true;
+ for (String dep : deps) {
+ Logger.msg(6, thisMod.getName()+" depends on "+dep);
+ if (!loadedModules.contains(dep)) {
+ Logger.error("UNMET MODULE DEPENDENCY: "+thisMod.getName()+" requires "+dep);
+ allDepsPresent = false;
+ }
+ else if (!prevModules.contains(dep)) {
+ Logger.msg(1, "ModuleManager: Shuffling "+thisMod.getName()+" to the end to fulfil dependency on "+dep);
+ modules.remove(i);
+ modules.add(thisMod);
+ thisMod = modules.get(i);
+ skipped++;
+ depClean = false;
+ break;
+ }
+ }
+ if (skipped > modules.size()-i) {
+ StringBuffer badMod = new StringBuffer();
+ for (Module mod : modules.subList(i, modules.size())) {
+ badMod.append(mod.getName()).append(" ");
+ }
+ Logger.die("Circular module dependencies involving: "+badMod);
+ }
+ }
+ // Current module is 'next', this is the correct order to load the properties
+ Properties modProp = thisMod.getProperties(isServer);
+ for (Enumeration<?> e = modProp.propertyNames(); e.hasMoreElements();) {
+ String propName = (String)e.nextElement();
+ props.put(propName, modProp.get(propName));
+ }
+ prevModules.add(thisMod.getName());
+ }
+ if (!allDepsPresent) Logger.die("Unmet module dependencies. Cannot continue");
+ }
+
+ public void setUser(AgentProxy user) {
+ this.user = user;
+ }
+
+ public String getModuleVersions() {
+ StringBuffer ver = new StringBuffer();
+ for (Module thisMod : modules) {
+ if (ver.length()>0) ver.append("; ");
+ ver.append(thisMod.getName()+" ("+thisMod.getVersion()+")");
+ }
+ return ver.toString();
+ }
+
+
+ public Properties getAllModuleProperties() {
+ return props;
+ }
+
+ public void runScripts(String event) {
+ for (Module thisMod : modules) {
+ try {
+ thisMod.runScript(event, user, isServer);
+ } catch (ScriptingEngineException e) {
+ Logger.error(e);
+ Logger.die(e.getMessage());
+ }
+ }
+ }
+
+ public void registerModules() throws ModuleException {
+ ItemProxy serverEntity;
+ try {
+ serverEntity = Gateway.getProxyManager().getProxy(new DomainPath("/servers/"+Gateway.getProperties().getString("ItemServer.name")));
+ } catch (ObjectNotFoundException e) {
+ throw new ModuleException("Cannot find local server name.");
+ }
+ Logger.debug(3, "Registering modules");
+
+ boolean reset = Gateway.getProperties().getBoolean("Module.reset", false);
+
+ for (Module thisMod : modules) {
+
+ Logger.msg("Registering module "+thisMod.getName());
+ try {
+ String thisResetKey = "Module."+thisMod.getNamespace()+".reset";
+ boolean thisReset = reset;
+ if (Gateway.getProperties().containsKey(thisResetKey))
+ thisReset = Gateway.getProperties().getBoolean(thisResetKey);
+ thisMod.importAll(serverEntity, user, modulesXML.get(thisMod.getNamespace()), thisReset);
+ } catch (Exception e) {
+ Logger.error(e);
+ throw new ModuleException("Error importing items for module "+thisMod.getName());
+ }
+ Logger.msg("Module "+thisMod.getName()+" registered");
+
+ try {
+ thisMod.runScript("startup", user, true);
+ } catch (ScriptingEngineException e) {
+ Logger.error(e);
+ throw new ModuleException("Error in startup script for module "+thisMod.getName());
+ }
+
+ }
+ }
+
+ public void dumpModules() {
+ for (Module thisMod : modules) {
+ try {
+ FileStringUtility.string2File(thisMod.getName()+".xml", Gateway.getMarshaller().marshall(thisMod));
+ } catch (Exception e) {
+ Logger.error(e);
+ }
+ }
+
+ }
+}
diff --git a/src/main/java/org/cristalise/kernel/process/module/ModuleResource.java b/src/main/java/org/cristalise/kernel/process/module/ModuleResource.java
new file mode 100644
index 0000000..74b5673
--- /dev/null
+++ b/src/main/java/org/cristalise/kernel/process/module/ModuleResource.java
@@ -0,0 +1,80 @@
+/**
+ * This file is part of the CRISTAL-iSE kernel.
+ * Copyright (c) 2001-2014 The CRISTAL Consortium. All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 3 of the License, or (at
+ * your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ *
+ * http://www.fsf.org/licensing/licenses/lgpl.html
+ */
+package org.cristalise.kernel.process.module;
+
+import org.cristalise.kernel.common.CannotManageException;
+import org.cristalise.kernel.common.ObjectAlreadyExistsException;
+import org.cristalise.kernel.common.ObjectCannotBeUpdated;
+import org.cristalise.kernel.common.ObjectNotFoundException;
+import org.cristalise.kernel.lookup.AgentPath;
+import org.cristalise.kernel.process.Bootstrap;
+import org.cristalise.kernel.utils.Logger;
+
+
+public class ModuleResource extends ModuleImport {
+
+ public int version;
+ public String resourceType;
+ public String resourceLocation;
+
+ public ModuleResource() {
+ // if not given, version defaults to 0
+ version = 0;
+ }
+
+ @Override
+ public void create(AgentPath agentPath, boolean reset)
+ throws ObjectNotFoundException, ObjectCannotBeUpdated,
+ CannotManageException, ObjectAlreadyExistsException {
+ try {
+ domainPath = Bootstrap.verifyResource(ns, name, version, resourceType, itemPath, resourceLocation, reset);
+ } catch (Exception e) {
+ Logger.error(e);
+ throw new CannotManageException("Exception verifying module resource "+ns+"/"+name);
+ }
+ }
+
+ public int getVersion() {
+ return version;
+ }
+
+ public void setVersion(int version) {
+ this.version = version;
+ }
+
+ public String getResourceType() {
+ return resourceType;
+ }
+
+ public void setResourceType(String resourceType) {
+ this.resourceType = resourceType;
+ }
+
+ public String getResourceLocation() {
+ return resourceLocation;
+ }
+
+ public void setResourceLocation(String resourceLocation) {
+ this.resourceLocation = resourceLocation;
+ }
+
+
+} \ No newline at end of file
diff --git a/src/main/java/org/cristalise/kernel/process/module/ModuleScript.java b/src/main/java/org/cristalise/kernel/process/module/ModuleScript.java
new file mode 100644
index 0000000..4e81864
--- /dev/null
+++ b/src/main/java/org/cristalise/kernel/process/module/ModuleScript.java
@@ -0,0 +1,53 @@
+/**
+ * This file is part of the CRISTAL-iSE kernel.
+ * Copyright (c) 2001-2014 The CRISTAL Consortium. All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 3 of the License, or (at
+ * your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ *
+ * http://www.fsf.org/licensing/licenses/lgpl.html
+ */
+package org.cristalise.kernel.process.module;
+
+import org.cristalise.kernel.entity.proxy.AgentProxy;
+import org.cristalise.kernel.scripting.Script;
+import org.cristalise.kernel.scripting.ScriptingEngineException;
+
+public class ModuleScript {
+
+
+ public String target;
+ public String event;
+ public String lang;
+ public String script;
+ public ModuleScript() {
+ }
+
+ public ModuleScript(String target, String event, String lang, String script) {
+ super();
+ this.target = target;
+ this.event = event;
+ this.lang = lang;
+ this.script = script;
+ }
+
+ public Script getScript(String ns, AgentProxy user) throws ScriptingEngineException {
+ return new Script(lang, ns+" "+target+" "+event, script, user);
+ }
+
+ public boolean shouldRun(String event, boolean isServer) {
+ return ((this.target == null || this.target.length() == 0 || isServer == target.equals("server")) &&
+ (this.event == null || this.event.length() == 0 || event.equals(this.event)));
+ }
+}