From 684e01bb190c7d0b95347b732aeb3cdeda4740d7 Mon Sep 17 00:00:00 2001 From: abranson Date: Tue, 18 Oct 2011 17:00:33 +0200 Subject: Module support --- source/com/c2kernel/process/AbstractMain.java | 44 ---- source/com/c2kernel/process/Bootstrap.java | 156 ++++-------- source/com/c2kernel/process/Gateway.java | 77 +++--- source/com/c2kernel/process/Module.java | 303 +++++++++++++++++++++++ source/com/c2kernel/process/ModuleManager.java | 81 ++++++ source/com/c2kernel/process/StandardServer.java | 5 +- source/com/c2kernel/process/UserCodeProcess.java | 6 +- 7 files changed, 478 insertions(+), 194 deletions(-) create mode 100644 source/com/c2kernel/process/Module.java create mode 100644 source/com/c2kernel/process/ModuleManager.java (limited to 'source/com/c2kernel/process') diff --git a/source/com/c2kernel/process/AbstractMain.java b/source/com/c2kernel/process/AbstractMain.java index 9282d45..7401d1b 100644 --- a/source/com/c2kernel/process/AbstractMain.java +++ b/source/com/c2kernel/process/AbstractMain.java @@ -16,7 +16,6 @@ import java.io.PrintStream; import com.c2kernel.utils.FileStringUtility; import com.c2kernel.utils.Logger; -import com.c2kernel.utils.Resource; /************************************************************************** * @@ -106,20 +105,6 @@ abstract public class AbstractMain } logStream = new PrintStream(new FileOutputStream(args[++i], true)); } - else if(args[i].equals("-resURL")) - { - String resString = args[++i]; - System.out.println("AbstractMain::readC2KArgs() - Resource location:" + - resString); - Resource.setKernelBaseURL(resString); - } - else if(args[i].equals("-domResURL")) - { - String resString = args[++i]; - System.out.println("AbstractMain::readC2KArgs() - Domain resource location:" + - resString); - Resource.setDomainBaseURL(resString); - } i++; } @@ -173,33 +158,4 @@ abstract public class AbstractMain return c2kProps; } - /************************************************************************** - * Required for most of the client & server application - * - * reads arguments - * initialises C2K properties - * initialises ORB - * initialises VirtualTreeManager - * initialises EventValueFactory + ProxyEventValueFactory - * initialises - **************************************************************************/ - static protected void standardSetUp(String[] args) - throws Exception - { - //Reads the C2Kernel arguments, and inits the Gateway with them - Gateway.init(readC2KArgs(args)); - - } - - - /************************************************************************** - * - **************************************************************************/ - static protected void standardTearDown() - throws Exception - { - Gateway.close(); - Logger.msg(5, "AbstractMain::standardTearDown() - DONE."); - } - } diff --git a/source/com/c2kernel/process/Bootstrap.java b/source/com/c2kernel/process/Bootstrap.java index 5011b7f..5b83d89 100644 --- a/source/com/c2kernel/process/Bootstrap.java +++ b/source/com/c2kernel/process/Bootstrap.java @@ -2,7 +2,6 @@ package com.c2kernel.process; import java.net.InetAddress; import java.util.Enumeration; -import java.util.HashMap; import java.util.StringTokenizer; import com.c2kernel.common.ObjectNotFoundException; @@ -15,7 +14,6 @@ import com.c2kernel.lifecycle.instance.CompositeActivity; import com.c2kernel.lifecycle.instance.Workflow; import com.c2kernel.lifecycle.instance.predefined.PredefinedStepContainer; import com.c2kernel.lifecycle.instance.predefined.ServerPredefinedStepContainer; -import com.c2kernel.lifecycle.instance.predefined.entitycreation.NewItem; import com.c2kernel.lifecycle.instance.stateMachine.States; import com.c2kernel.lifecycle.instance.stateMachine.Transitions; import com.c2kernel.lookup.AgentPath; @@ -44,7 +42,6 @@ import com.c2kernel.utils.Resource; public class Bootstrap { - static HashMap bootstrapFactoryItems = new HashMap(); static DomainPath thisServerPath; /** @@ -68,8 +65,6 @@ public class Bootstrap // verify the server item's wf initServerItemWf(); - // create the factories etc. - createBootstrapFactoryItems(); Logger.msg("Bootstrap.run() - Bootstrapping complete"); } catch (Exception e) { Logger.error(e); @@ -86,45 +81,11 @@ public class Bootstrap String bootItems; Logger.msg(1, "Verifying kernel boot items"); bootItems = FileStringUtility.url2String(Resource.getKernelResourceURL("boot/allbootitems.txt")); - verifyBootDataItems(bootItems, true); - if (Resource.getDomainBaseURL() == null) - Logger.msg(1, "No Domain base URL. Skipping domain boot check."); - else { - try { - Logger.msg(1, "Verifying domain boot items"); - bootItems = FileStringUtility.url2String(Resource.getDomainResourceURL("boot/allbootitems.txt")); - verifyBootDataItems(bootItems, false); - } catch (Exception ex) { - Logger.error(ex); - Logger.msg("No domain boot items found."); - return; - } - Logger.msg(1, "Verifying domain agents"); - - try { - bootItems = FileStringUtility.url2String(Resource.getDomainResourceURL("boot/domainagents.txt")); - verifyDomainAgents(bootItems); - } catch (Exception ex) { - Logger.error(ex); - Logger.msg("No domain agents found."); - return; - } - } - + verifyBootDataItems(bootItems, null); Logger.msg(1, "Boot data items complete"); - - } - - private static void verifyDomainAgents(String agentFile) throws Exception { - StringTokenizer str = new StringTokenizer(agentFile, "\n\r"); - while (str.hasMoreTokens()) { - String thisItem = str.nextToken(); - StringTokenizer agent = new StringTokenizer(thisItem, ","); - checkAgent(agent.nextToken(), agent.nextToken(), agent.nextToken(), agent.nextToken().equalsIgnoreCase("true")); - } } - private static void verifyBootDataItems(String bootList, boolean isKernel) { + private static void verifyBootDataItems(String bootList, String ns) { StringTokenizer str = new StringTokenizer(bootList, "\n\r"); while (str.hasMoreTokens()) { String thisItem = str.nextToken(); @@ -132,63 +93,62 @@ public class Bootstrap String itemType = thisItem.substring(0,delim); String itemName = thisItem.substring(delim+1); try { - String data = Resource.getTextResource("boot/"+thisItem+".xml"); + String data = Resource.getTextResource(ns, "boot/"+thisItem+(itemType.equals("OD")?".xsd":".xml")); if (data == null) Logger.die("No data found for "+getDataType(itemType)+" "+itemName); - if (itemType.equals("factory")) { - bootstrapFactoryItems.put(itemName, data); - continue; - } - - Logger.msg(1, "Bootstrap.verifyBootItems() - Verifying data of "+getDataType(itemType)+" "+itemName); - Enumeration en = Gateway.getLDAPLookup().search(getTypeRoot(itemType), itemName); - ItemProxy thisProxy; - - if (!en.hasMoreElements()) { - Logger.msg("Bootstrap.verifyBootItems() - "+getDataType(itemType)+" "+itemName+" not found. Creating new."); - thisProxy = createBootstrapDataItem(itemType, itemName, isKernel); - } - else { - DomainPath path = (DomainPath)en.nextElement(); - thisProxy = (ItemProxy)Gateway.getProxyManager().getProxy(path); - try { - Viewpoint currentData = (Viewpoint)thisProxy.getObject(ClusterStorage.VIEWPOINT+"/"+getDataType(itemType)+"/last"); - String oldData = currentData.getOutcome().getData(); - if (data.equals(oldData)) { - Logger.msg(5, "Bootstrap.verifyBootItems() - Data identical, no update required"); - - continue; - } - } catch (ObjectNotFoundException ex) { - Logger.error("Bootstrap.verifyBootItems() - Item exists but no data found! Attempting to insert new."); - } - } - // data was missing or doesn't match - Logger.msg("Bootstrap.verifyBootItems() - Writing new data to "+getDataType(itemType)+" "+itemName); - History hist = (History)thisProxy.getObject(ClusterStorage.HISTORY); - Event newEvent = hist.addEvent("system", "Admin", Transitions.DONE, "Import", "Import", "Import", States.FINISHED); - Outcome newOutcome = new Outcome(newEvent.getID(), data, getDataType(itemType), 0); - Viewpoint newLastView = new Viewpoint(thisProxy.getSystemKey(), getDataType(itemType), "last", 0, newEvent.getID()); - Viewpoint newZeroView = new Viewpoint(thisProxy.getSystemKey(), getDataType(itemType), "0", 0, newEvent.getID()); - Gateway.getStorage().put(thisProxy.getSystemKey(), newOutcome, thisProxy); - Gateway.getStorage().put(thisProxy.getSystemKey(), newLastView, thisProxy); - Gateway.getStorage().put(thisProxy.getSystemKey(), newZeroView, thisProxy); - Gateway.getStorage().commit(thisProxy); + verifyResource(ns, itemName, itemType, data); } catch (Exception e) { Logger.error(e); Logger.die("Error importing bootstrap items. Unsafe to continue."); } } } + + + public static void verifyResource(String ns, String itemName, String itemType, String data) throws Exception { + Logger.msg(1, "Bootstrap.verifyResource() - Verifying data of "+getDataType(itemType)+" "+itemName); + Enumeration en = Gateway.getLDAPLookup().search(getTypeRoot(itemType), itemName); + ItemProxy thisProxy; + + if (!en.hasMoreElements()) { + Logger.msg("Bootstrap.verifyResource() - "+getDataType(itemType)+" "+itemName+" not found. Creating new."); + thisProxy = createResourceItem(itemType, itemName, ns); + } + else { + DomainPath path = (DomainPath)en.nextElement(); + thisProxy = (ItemProxy)Gateway.getProxyManager().getProxy(path); + try { + Viewpoint currentData = (Viewpoint)thisProxy.getObject(ClusterStorage.VIEWPOINT+"/"+getDataType(itemType)+"/last"); + String oldData = currentData.getOutcome().getData(); + if (data.equals(oldData)) { + Logger.msg(5, "Bootstrap.verifyResource() - Data identical, no update required"); + return; + } + } catch (ObjectNotFoundException ex) { + Logger.error("Bootstrap.verifyResource() - Item exists but no data found! Attempting to insert new."); + } + } + // data was missing or doesn't match + Logger.msg("Bootstrap.verifyResource() - Writing new data to "+getDataType(itemType)+" "+itemName); + History hist = (History)thisProxy.getObject(ClusterStorage.HISTORY); + Event newEvent = hist.addEvent("system", "Admin", Transitions.DONE, "Import", "Import", "Import", States.FINISHED); + Outcome newOutcome = new Outcome(newEvent.getID(), data, getDataType(itemType), 0); + Viewpoint newLastView = new Viewpoint(thisProxy.getSystemKey(), getDataType(itemType), "last", 0, newEvent.getID()); + Viewpoint newZeroView = new Viewpoint(thisProxy.getSystemKey(), getDataType(itemType), "0", 0, newEvent.getID()); + Gateway.getStorage().put(thisProxy.getSystemKey(), newOutcome, thisProxy); + Gateway.getStorage().put(thisProxy.getSystemKey(), newLastView, thisProxy); + Gateway.getStorage().put(thisProxy.getSystemKey(), newZeroView, thisProxy); + Gateway.getStorage().commit(thisProxy); + } /** * @param itemType * @param itemName * @param data */ - private static ItemProxy createBootstrapDataItem(String itemType, String itemName, boolean isKernel) throws Exception { + private static ItemProxy createResourceItem(String itemType, String itemName, String ns) throws Exception { // create props - PropertyDescriptionList pdList = (PropertyDescriptionList)CastorXMLUtility.unmarshall(Resource.getTextResource("boot/property/"+itemType+"Prop.xml")); + PropertyDescriptionList pdList = (PropertyDescriptionList)CastorXMLUtility.unmarshall(Resource.getTextResource(null, "boot/property/"+itemType+"Prop.xml")); PropertyArrayList props = new PropertyArrayList(); for (int i = 0; i < pdList.list.size(); i++) { PropertyDescription pd = pdList.list.get(i); @@ -204,7 +164,7 @@ public class Bootstrap 1, CastorXMLUtility.marshall(props), CastorXMLUtility.marshall(new CompositeActivity())); - DomainPath newDomPath = new DomainPath(getTypeRoot(itemType).toString()+"/system/"+(isKernel?"kernel":"domain")+"/"+itemName); + DomainPath newDomPath = new DomainPath(getTypeRoot(itemType).toString()+"/system/"+(ns==null?"kernel":ns)+"/"+itemName); newDomPath.setEntity(entityPath); Gateway.getLDAPLookup().add(newDomPath); return (ItemProxy)Gateway.getProxyManager().getProxy(entityPath); @@ -325,32 +285,4 @@ public class Bootstrap Gateway.getStorage().put(thisServerPath.getSysKey(), wf, null); // add this proxy server in case it was just registered, or the port has changed } - - public static void createBootstrapFactoryItems() throws Exception { - Logger.msg(1, "Verifying local object factories"); - for (String itemName : bootstrapFactoryItems.keySet()) { - String itemXML = bootstrapFactoryItems.get(itemName); - Logger.msg(2, "Verifying "+itemName); - NewItem item = (NewItem)CastorXMLUtility.unmarshall(itemXML); - DomainPath factPath = new DomainPath(new DomainPath(item.initialPath), item.name); - if (factPath.exists()) continue; - ItemProxy serverEntity = (ItemProxy)Gateway.getProxyManager().getProxy(thisServerPath); - serverEntity.requestAction( - Gateway.getLDAPLookup().getRoleManager().getAgentPath("system").getSysKey(), - "workflow/predefined/CreateNewItem", - Transitions.DONE, - itemXML); - Logger.msg("Bootstrap.createBootstrapFactoryItems() - Created factory item: "+itemName); - ItemProxy factProxy = (ItemProxy)Gateway.getProxyManager().getProxy(factPath); - String propDesc = Resource.getTextResource("boot/property/"+itemName+"Prop.xml"); - History hist = (History)factProxy.getObject(ClusterStorage.HISTORY); - Event newEvent = hist.addEvent("system", "Admin", Transitions.DONE, "Import", "Import", "Import", States.FINISHED); - Outcome newOutcome = new Outcome(newEvent.getID(), propDesc, "PropertyDescription", 0); - Viewpoint newLastView = new Viewpoint(factProxy.getSystemKey(), "PropertyDescription", "last", 0, newEvent.getID()); - Gateway.getStorage().put(factProxy.getSystemKey(), newOutcome, factProxy); - Gateway.getStorage().put(factProxy.getSystemKey(), newLastView, factProxy); - Gateway.getStorage().commit(factProxy); - } - } - } diff --git a/source/com/c2kernel/process/Gateway.java b/source/com/c2kernel/process/Gateway.java index 3a03088..f399351 100644 --- a/source/com/c2kernel/process/Gateway.java +++ b/source/com/c2kernel/process/Gateway.java @@ -5,8 +5,8 @@ package com.c2kernel.process; * @author $Author: abranson $ */ +import java.io.IOException; import java.net.MalformedURLException; -import java.net.URL; import java.util.Enumeration; import java.util.Properties; @@ -51,6 +51,7 @@ import com.c2kernel.utils.server.SimpleTCPIPServer; public class Gateway { static private Properties mC2KProps; + static private ModuleManager mModules; static private org.omg.CORBA.ORB mORB; static private LDAPLookup mLDAPLookup; static private TransactionManager mStorage; @@ -59,6 +60,7 @@ public class Gateway static private SimpleTCPIPServer mHTTPServer; + private Gateway() { } /** @@ -69,18 +71,21 @@ public class Gateway * If null, the java system properties are used * @throws InvalidDataException - invalid properties caused a failure in initialisation */ - static public void init(Properties props) throws InvalidDataException { + static public void init(Properties props, boolean isServer) throws InvalidDataException { + // if supplied props are null, use system props if (props == null) props = System.getProperties(); - // set resource URLs from config - String resURL = props.getProperty("KernelResourceURL"); - if (resURL != null && resURL.length()>0) - Resource.setKernelBaseURL(resURL); - - resURL = props.getProperty("DomainResourceURL"); - if (resURL != null && resURL.length()>0) - Resource.setDomainBaseURL(resURL); + // report version info + Logger.msg("Kernel version: "+Resource.getKernelVersion()); + + // init module manager + try { + mModules = new ModuleManager(ClassLoader.getSystemResources("module.xml"), isServer); + } catch (IOException e) { + Logger.error(e); + throw new InvalidDataException("Could not load module definitions. Classpath problem", ""); + } // Start with default props from kernel jar try { @@ -88,6 +93,13 @@ public class Gateway } catch (MalformedURLException ex) { Logger.die("Default properties not found. Probable cause is missing resources"); } + + // merge in module props + Properties moduleProperties = mModules.getAllModuleProperties(); + for (Enumeration e = moduleProperties.propertyNames(); e.hasMoreElements();) { + String propName = (String)e.nextElement(); + mC2KProps.put(propName, moduleProperties.get(propName)); + } // Overwrite with supplied props for (Enumeration e = props.propertyNames(); e.hasMoreElements();) { @@ -98,15 +110,9 @@ public class Gateway // dump properties dumpC2KProps(7); - // report version info - Logger.msg("Domain version: "+Resource.getDomainVersion()); - Logger.msg("Kernel version: "+Resource.getKernelVersion()); - - // load kernel and domain mapfiles + // load kernel mapfiles try { CastorXMLUtility.loadMapsFrom(Resource.getKernelResourceURL("mapFiles/")); - if (Resource.getDomainBaseURL()!=null) - CastorXMLUtility.loadMapsFrom(Resource.getDomainResourceURL("mapFiles/")); } catch (MalformedURLException e1) { throw new InvalidDataException("Invalid Resource Location", ""); } @@ -117,12 +123,9 @@ public class Gateway Language.isTranlated=true; Language.mTableOfTranslation = FileStringUtility.loadLanguageFile(languageFile); } - - try { - Resource.setImportURL(new URL("file:"+getProperty("Import.dir"))); - } catch (MalformedURLException e) { - Logger.error("Import directory not set. "+getProperty("Import.dir")); - } + + // run module startup scripts + mModules.runScripts("startup"); } /** @@ -162,25 +165,32 @@ public class Gateway // start checking bootstrap items Bootstrap.run(); + // register modules + mModules.registerModules(); + } catch (Exception ex) { Logger.error(ex); Logger.die("Exception starting server components. Shutting down."); } // start the http server - try { - int httpPort = Integer.parseInt(Gateway.getProperty("ItemServer.HTTP.port")); - Logger.msg(2, "Starting HTTP Server on port "+httpPort); - mHTTPServer = new SimpleTCPIPServer(httpPort, ItemHTTPBridge.class, 5); - mHTTPServer.startListening(); - } catch (NumberFormatException ex) { - Logger.msg(3, "Invalid or no HTTP port defined. HTTP server not available."); - } +// try { +// int httpPort = Integer.parseInt(Gateway.getProperty("ItemServer.HTTP.port")); +// Logger.msg(2, "Starting HTTP Server on port "+httpPort); +// mHTTPServer = new SimpleTCPIPServer(httpPort, ItemHTTPBridge.class, 5); +// mHTTPServer.startListening(); +// } catch (NumberFormatException ex) { +// Logger.msg(3, "Invalid or no HTTP port defined. HTTP server not available."); +// } System.out.println("Server '"+Gateway.getCentreId()+"' initialised."); } - /** + public static ModuleManager getModuleManager() { + return mModules; + } + + /** * Connects to the LDAP server in an administrative context - using the admin username and * password given in the LDAP.user and LDAP.password props of the kernel properties. * @@ -327,6 +337,9 @@ public class Gateway */ public static void close() { + // run shutdown module scripts + mModules.runScripts("shutdown"); + // shut down servers if running if (mCorbaServer != null) mCorbaServer.close(); diff --git a/source/com/c2kernel/process/Module.java b/source/com/c2kernel/process/Module.java new file mode 100644 index 0000000..e32c72e --- /dev/null +++ b/source/com/c2kernel/process/Module.java @@ -0,0 +1,303 @@ +package com.c2kernel.process; + +import java.io.StringReader; +import java.util.ArrayList; +import java.util.HashMap; +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.ObjectNotFoundException; +import com.c2kernel.entity.proxy.ItemProxy; +import com.c2kernel.events.Event; +import com.c2kernel.events.History; +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.Property; +import com.c2kernel.lifecycle.instance.stateMachine.States; +import com.c2kernel.lifecycle.instance.stateMachine.Transitions; +import com.c2kernel.lookup.DomainPath; +import com.c2kernel.persistency.ClusterStorage; +import com.c2kernel.persistency.outcome.Outcome; +import com.c2kernel.persistency.outcome.Viewpoint; +import com.c2kernel.scripting.ErrorInfo; +import com.c2kernel.scripting.Script; +import com.c2kernel.scripting.ScriptingEngineException; +import com.c2kernel.utils.CastorXMLUtility; +import com.c2kernel.utils.Logger; +import com.c2kernel.utils.Resource; + +public class Module { + + private final String ns, name, desc, version; + private String resURL; + private final ArrayList dependency = new ArrayList(); + private final Properties clientProps = new Properties(); + private final Properties serverProps = new Properties(); + private final HashMap clientScripts = new HashMap(); + private final HashMap serverScripts = new HashMap(); + private final ArrayList imports = new ArrayList(); + private static DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + private final DocumentBuilder parser; + + static { + dbf.setValidating(false); + dbf.setNamespaceAware(false); + } + + public Module(String moduleXML) throws Exception { + parser = dbf.newDocumentBuilder(); + Document moduleDOM = parser.parse(new InputSource(new StringReader(moduleXML))); + + Element root = (Element)moduleDOM.getElementsByTagName("CristalModule").item(0); + + // Get module metadata + ns = root.getAttribute("ns"); + name = root.getAttribute("name"); + Element info = (Element)moduleDOM.getElementsByTagName("Info").item(0); + desc = ((Text)info.getElementsByTagName("Description").item(0).getFirstChild()).getData(); + version = ((Text)info.getElementsByTagName("Version").item(0).getFirstChild()).getData(); + NodeList nl = info.getElementsByTagName("Dependency"); + for (int i=0; i0) { + resURL = ((Text)nl.item(0).getFirstChild()).getData(); + Resource.addModuleBaseURL(ns, resURL); + } + + // Get config properties + nl = root.getElementsByTagName("Config"); + for (int i=0; i0) { + Element impElem = (Element)nl.item(0); + nl = impElem.getChildNodes(); + for (int i=0; i scripts = isServer?serverScripts:clientScripts; + Script thisScript = scripts.get(event); + if (thisScript == null) return null; + try { + Object result = thisScript.execute(); + if (result instanceof ErrorInfo) + return (ErrorInfo)result; + else + return new ErrorInfo(result.toString()); + } catch (ScriptingEngineException ex) { + Logger.error(ex); + return new ErrorInfo("Error running "+event+" script in module "+ns); + } + } + + public void importAll(ItemProxy serverEntity) { + for (ModuleImport thisImp : imports) { + if (thisImp instanceof ModuleResource) { + ModuleResource thisRes = (ModuleResource)thisImp; + try { + Bootstrap.verifyResource(ns, thisRes.importName, thisRes.resourceType, Resource.getTextResource(ns, thisRes.resourceLocation)); + } catch (Exception ex) { + Logger.error(ex); + } + } + else if (thisImp instanceof ModuleItem) { + ModuleItem thisItem = (ModuleItem)thisImp; + try { + NewItem item = new NewItem(thisItem.importName, "/desc/"+ns, thisItem.workflow); + item.propertyList = thisItem.props; + DomainPath itemPath = new DomainPath(new DomainPath(item.initialPath), item.name); + if (itemPath.exists()) continue; + serverEntity.requestAction( + Gateway.getLDAPLookup().getRoleManager().getAgentPath("system").getSysKey(), + "workflow/predefined/CreateNewItem", + Transitions.DONE, + CastorXMLUtility.marshall(item)); + Logger.msg("Module.importAll() - Created item: "+thisItem.importName); + ItemProxy newProxy = (ItemProxy)Gateway.getProxyManager().getProxy(itemPath); + History hist = (History)newProxy.getObject(ClusterStorage.HISTORY); + for (String thisView : thisItem.outcomes.keySet()) { + String[] info = thisView.split(":"); + int version = Integer.parseInt(info[1]); + String data = Resource.getTextResource(ns, thisItem.outcomes.get(thisView)); + Event newEvent = hist.addEvent("system", "Admin", Transitions.DONE, "Import", "Import", "Import", States.FINISHED); + Outcome newOutcome = new Outcome(newEvent.getID(), data, info[0], version); + Viewpoint newLastView = new Viewpoint(newProxy.getSystemKey(), info[0], info[2], version, newEvent.getID()); + Gateway.getStorage().put(newProxy.getSystemKey(), newOutcome, newProxy); + Gateway.getStorage().put(newProxy.getSystemKey(), newLastView, newProxy); + } + for (Dependency thisDep : thisItem.deps) { + Gateway.getStorage().put(newProxy.getSystemKey(), thisDep.create(), newProxy); + } + Gateway.getStorage().commit(newProxy); + } catch (Exception ex) { + Logger.error("Error importing item "+thisItem.importName+" from module "+name); + Logger.error(ex); + } + } + else if (thisImp instanceof ModuleAgent) { + ModuleAgent thisAgent = (ModuleAgent)thisImp; + try { + Gateway.getLDAPLookup().getRoleManager().getAgentPath(thisAgent.importName); + Logger.msg(3, "Module.importAll() - User '"+thisAgent.importName+"' found."); + return; + } catch (ObjectNotFoundException ex) { } + Logger.msg("Module.importAll() - User '"+thisAgent.importName+"' not found. Creating."); + + NewAgent agent = new NewAgent(thisAgent.importName, thisAgent.password); + agent.roles = thisAgent.roles; + try { + serverEntity.requestAction( + Gateway.getLDAPLookup().getRoleManager().getAgentPath("system").getSysKey(), + "workflow/predefined/CreateNewAgent", + Transitions.DONE, + CastorXMLUtility.marshall(agent)); + } catch (Exception ex) { + Logger.error("Error importing agent "+thisAgent.importName+" from module "+name); + Logger.error(ex); + } + } + } + } + + public Properties getClientProperties() { + return clientProps; + } + + public Properties getServerProperties() { + return serverProps; + } + + public String getNs() { + return ns; + } + public String getName() { + return name; + } + public String getDesc() { + return desc; + } + public String getVersion() { + return version; + } + public String getResURL() { + return resURL; + } + public ArrayList getDependencies() { + return dependency; + } + public boolean hasDependency(String dep) { + return dependency.contains(dep); + } + + public abstract class ModuleImport { + String importName; + } + + public class ModuleResource extends ModuleImport { + String resourceType; + String resourceLocation; + } + + public class ModuleItem extends ModuleImport { + ArrayList props = new ArrayList(); + HashMap outcomes = new HashMap(); + ArrayList deps = new ArrayList(); + String workflow; + } + + public class ModuleAgent extends ModuleImport { + String password; + ArrayList roles = new ArrayList(); + } +} \ No newline at end of file diff --git a/source/com/c2kernel/process/ModuleManager.java b/source/com/c2kernel/process/ModuleManager.java new file mode 100644 index 0000000..e067a88 --- /dev/null +++ b/source/com/c2kernel/process/ModuleManager.java @@ -0,0 +1,81 @@ +package com.c2kernel.process; + +import java.net.URL; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.Properties; + +import com.c2kernel.common.ObjectNotFoundException; +import com.c2kernel.entity.proxy.ItemProxy; +import com.c2kernel.lookup.DomainPath; +import com.c2kernel.utils.FileStringUtility; +import com.c2kernel.utils.Logger; + +public class ModuleManager { + ArrayList modules = new ArrayList(); + Properties props = new Properties(); + boolean isServer; + + public ModuleManager(Enumeration moduleEnum, boolean isServer) { + this.isServer = isServer; + ArrayList loadedModules = new ArrayList(); + while(moduleEnum.hasMoreElements()) { + URL newModuleURL = moduleEnum.nextElement(); + try { + Module newModule = new Module(FileStringUtility.url2String(newModuleURL)); + modules.add(newModule); + loadedModules.add(newModule.getName()); + Properties modProp = isServer?newModule.getServerProperties():newModule.getClientProperties(); + for (Enumeration e = modProp.propertyNames(); e.hasMoreElements();) { + String propName = (String)e.nextElement(); + props.put(propName, modProp.get(propName)); + } + } catch (Exception e) { + Logger.error("Could not load module description from "+newModuleURL); + Logger.error(e); + } + } + + Logger.debug(5, "Checking dependencies"); + boolean depFailed = false; + for (Module thisMod : modules) { + ArrayList deps = thisMod.getDependencies(); + for (String dep : deps) { + if (!loadedModules.contains(dep)) { + Logger.error("UNMET MODULE DEPENDENCY: "+thisMod.getName()+" requires "+dep); + depFailed = true; + } + } + } + if (depFailed) Logger.die("Unmet module dependencies. Cannot continue"); + } + + 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) { + thisMod.runScript(event, isServer); + } + } + + public void registerModules() throws ObjectNotFoundException { + ItemProxy serverEntity = (ItemProxy)Gateway.getProxyManager().getProxy(new DomainPath("/servers/"+Gateway.getProperty("ItemServer.name"))); + Logger.debug(3, "Registering modules"); + for (Module thisMod : modules) { + Logger.debug(4, "Registering module "+thisMod.getName()); + thisMod.importAll(serverEntity); + } + } +} diff --git a/source/com/c2kernel/process/StandardServer.java b/source/com/c2kernel/process/StandardServer.java index 72b761e..6804da5 100644 --- a/source/com/c2kernel/process/StandardServer.java +++ b/source/com/c2kernel/process/StandardServer.java @@ -43,7 +43,7 @@ public class StandardServer extends AbstractMain implements WrapperListener throws Exception { // read args and init Gateway - standardSetUp(args); + Gateway.init(readC2KArgs(args), true); // connect to LDAP as root Gateway.connect(); @@ -112,8 +112,7 @@ public class StandardServer extends AbstractMain implements WrapperListener public int stop(int arg0) { try { - // close gateway - standardTearDown(); + Gateway.close(); } catch( Exception ex ) { diff --git a/source/com/c2kernel/process/UserCodeProcess.java b/source/com/c2kernel/process/UserCodeProcess.java index 9c2deab..7779802 100644 --- a/source/com/c2kernel/process/UserCodeProcess.java +++ b/source/com/c2kernel/process/UserCodeProcess.java @@ -125,7 +125,7 @@ public class UserCodeProcess extends StandardClient implements EntityProxyObserv // shut down try { - standardTearDown(); + Gateway.close(); } catch( Exception ex ) { @@ -196,7 +196,7 @@ public class UserCodeProcess extends StandardClient implements EntityProxyObserv try { - standardSetUp(args); + Gateway.init(readC2KArgs(args), false); UserCodeProcess proc = getInstance(); new Thread(proc).start(); Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { @@ -212,7 +212,7 @@ public class UserCodeProcess extends StandardClient implements EntityProxyObserv try { - standardTearDown(); + Gateway.close(); } catch(Exception ex1) { -- cgit v1.2.3