diff options
Diffstat (limited to 'src/main/java/com/c2kernel/process/Bootstrap.java')
| -rw-r--r-- | src/main/java/com/c2kernel/process/Bootstrap.java | 241 |
1 files changed, 144 insertions, 97 deletions
diff --git a/src/main/java/com/c2kernel/process/Bootstrap.java b/src/main/java/com/c2kernel/process/Bootstrap.java index 4f64adf..bcc5e68 100644 --- a/src/main/java/com/c2kernel/process/Bootstrap.java +++ b/src/main/java/com/c2kernel/process/Bootstrap.java @@ -1,14 +1,16 @@ package com.c2kernel.process;
import java.net.InetAddress;
-import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Set;
import java.util.StringTokenizer;
import org.custommonkey.xmlunit.Diff;
import org.custommonkey.xmlunit.XMLUnit;
import com.c2kernel.common.ObjectNotFoundException;
-import com.c2kernel.entity.TraceableEntity;
+import com.c2kernel.entity.proxy.AgentProxy;
import com.c2kernel.entity.proxy.ItemProxy;
import com.c2kernel.events.Event;
import com.c2kernel.events.History;
@@ -20,13 +22,15 @@ import com.c2kernel.lifecycle.instance.predefined.ServerPredefinedStepContainer; import com.c2kernel.lifecycle.instance.stateMachine.Transition;
import com.c2kernel.lookup.AgentPath;
import com.c2kernel.lookup.DomainPath;
-import com.c2kernel.lookup.EntityPath;
-import com.c2kernel.lookup.LDAPLookup;
+import com.c2kernel.lookup.ItemPath;
+import com.c2kernel.lookup.Lookup;
import com.c2kernel.lookup.Path;
import com.c2kernel.lookup.RolePath;
import com.c2kernel.persistency.ClusterStorage;
import com.c2kernel.persistency.outcome.Outcome;
import com.c2kernel.persistency.outcome.Viewpoint;
+import com.c2kernel.process.resource.DefaultResourceImportHandler;
+import com.c2kernel.process.resource.ResourceImportHandler;
import com.c2kernel.property.Property;
import com.c2kernel.property.PropertyArrayList;
import com.c2kernel.property.PropertyDescription;
@@ -43,6 +47,8 @@ import com.c2kernel.utils.Logger; public class Bootstrap
{
static DomainPath thisServerPath;
+ static HashMap<String, ResourceImportHandler> resHandlerCache = new HashMap<String, ResourceImportHandler>();
+ static HashMap<String, AgentProxy> systemAgents = new HashMap<String, AgentProxy>();
/**
* Run everything without timing-out the service wrapper
@@ -67,7 +73,7 @@ public class Bootstrap Logger.msg("Bootstrap.run() - Initialising Server Item Workflow");
initServerItemWf();
- // register modules
+ Gateway.getModuleManager().setUser(systemAgents.get("system"));
Gateway.getModuleManager().registerModules();
Logger.msg("Bootstrap.run() - Bootstrapping complete");
@@ -98,10 +104,8 @@ public class Bootstrap String itemType = thisItem.substring(0,delim);
String itemName = thisItem.substring(delim+1);
try {
- String data = Gateway.getResource().getTextResource(ns, "boot/"+thisItem+(itemType.equals("OD")?".xsd":".xml"));
- if (data == null)
- Logger.die("No data found for "+getDataType(itemType)+" "+itemName);
- verifyResource(ns, itemName, 0, itemType, data, reset);
+ String location = "boot/"+thisItem+(itemType.equals("OD")?".xsd":".xml");
+ verifyResource(ns, itemName, 0, itemType, location, 0, reset);
} catch (Exception e) {
Logger.error(e);
Logger.die("Error importing bootstrap items. Unsafe to continue.");
@@ -110,122 +114,164 @@ public class Bootstrap }
- public static void verifyResource(String ns, String itemName, Integer version, String itemType, String data, boolean reset) throws Exception {
+ public static DomainPath verifyResource(String ns, String itemName, Integer version, String itemType, String dataLocation, int layer, boolean reset) throws Exception {
if (version == null) version = 0;
- Logger.msg(1, "Bootstrap.verifyResource() - Verifying version "+version+" of "+getDataType(itemType)+" "+itemName);
+ ResourceImportHandler typeImpHandler = getHandler(itemType);
+ Logger.msg(1, "Bootstrap.verifyResource() - Verifying version "+version+" of "+typeImpHandler.getName()+" "+itemName);
- Enumeration<Path> en = Gateway.getLDAPLookup().search(getTypeRoot(itemType), itemName);
+ // Find or create Item for Resource
+ DomainPath modDomPath = typeImpHandler.getPath(itemName, ns);
ItemProxy thisProxy;
- Outcome newOutcome = new Outcome(0, data, getDataType(itemType), 0);
-
- if (!en.hasMoreElements()) {
- Logger.msg("Bootstrap.verifyResource() - "+getDataType(itemType)+" "+itemName+" not found. Creating new.");
- thisProxy = createResourceItem(itemType, itemName, ns);
+ Iterator<Path> en = Gateway.getLookup().search(typeImpHandler.getTypeRoot(), itemName);
+ if (!en.hasNext()) {
+ Logger.msg("Bootstrap.verifyResource() - "+typeImpHandler.getName()+" "+itemName+" not found. Creating new.");
+ thisProxy = createResourceItem(typeImpHandler, itemName, layer, ns);
}
else {
- DomainPath path = (DomainPath)en.nextElement();
- thisProxy = (ItemProxy)Gateway.getProxyManager().getProxy(path);
- try {
- Viewpoint currentData = (Viewpoint)thisProxy.getObject(ClusterStorage.VIEWPOINT+"/"+getDataType(itemType)+"/"+version);
+ DomainPath path = (DomainPath)en.next();
+ thisProxy = Gateway.getProxyManager().getProxy(path);
+
+ // Verify module property and location
+
+ String moduleName = (ns==null?"kernel":ns);
+ String itemModule;
+ try{
+ itemModule = thisProxy.getProperty("Module");
+ if (!itemModule.equals("") && !itemModule.equals("null") && !moduleName.equals(itemModule)) {
+ Logger.error("Bootstrap.verifyResource() - Module clash! Resource '"+itemName+"' included in module "+moduleName+" but is assigned to '"+itemModule+"'. Not overwriting.");
+ return path;
+ }
+ } catch (ObjectNotFoundException ex) {
+ itemModule = "";
+ }
+
+ if (!moduleName.equals(itemModule)) { // write module property
+ Gateway.getStorage().put(thisProxy.getSystemKey(), new Property("Module", moduleName, false), thisProxy);
+ }
+
+ // overwrite layer if different
+ int currentLayer = -1;
+ try {
+ String layerProp = thisProxy.getProperty("Layer");
+ currentLayer = Integer.parseInt(layerProp);
+ } catch (Exception e) { }
+ if (currentLayer != layer)
+ Gateway.getStorage().put(thisProxy.getSystemKey(), new Property("Layer", String.valueOf(layer), false), thisProxy);
+
+ if (!modDomPath.equals(path)) { // move item to module subtree
+ Logger.msg("Module item "+itemName+" found with path "+path.toString()+". Moving to "+modDomPath.toString());
+ modDomPath.setEntity(new ItemPath(thisProxy.getSystemKey()));
+ if (!modDomPath.exists())
+ Gateway.getLookup().add(modDomPath);
+ Gateway.getLookup().delete(path);
+ }
+ }
+
+ // Verify/Import Outcomes, creating events and views as necessary
+ Set<Outcome> impList = typeImpHandler.getResourceOutcomes(itemName, ns, dataLocation, version);
+ for (Outcome newOutcome : impList) {
+ try {
+ Viewpoint currentData = (Viewpoint)thisProxy.getObject(ClusterStorage.VIEWPOINT+"/"+newOutcome.getSchemaType()+"/"+version);
Outcome oldData = currentData.getOutcome();
XMLUnit.setIgnoreWhitespace(true);
XMLUnit.setIgnoreComments(true);
Diff xmlDiff = new Diff(newOutcome.getDOM(), oldData.getDOM());
if (xmlDiff.identical()) {
Logger.msg(5, "Bootstrap.verifyResource() - Data identical, no update required");
- return;
+ continue;
}
else {
Logger.msg("Difference found in "+itemName+": "+xmlDiff.toString());
if (!reset && !currentData.getEvent().getStepPath().equals("Bootstrap")) {
Logger.msg("Version "+version+" was not set by Bootstrap, and reset not requested. Not overwriting.");
- return;
+ continue;
}
}
} catch (ObjectNotFoundException ex) {
- Logger.error("Bootstrap.verifyResource() - Item "+itemName+" exists but version "+version+" not found! Attempting to insert new.");
+ Logger.msg("Bootstrap.verifyResource() - Item "+itemName+" exists but version "+version+" not found! Attempting to insert new.");
}
+
+ // data was missing or doesn't match
+ Logger.msg("Bootstrap.verifyResource() - Writing new "+newOutcome.getSchemaType()+" v"+version+" to "+typeImpHandler.getName()+" "+itemName);
+ History hist = new History(thisProxy.getSystemKey(), thisProxy);
+ Transition predefDone = new Transition(0, "Done", 0, 0);
+ Event newEvent = hist.addEvent("system", "Admin", "Bootstrap", "Bootstrap", "Bootstrap", newOutcome.getSchemaType(), 0, "PredefinedStep", 0, predefDone, String.valueOf(version));
+ newOutcome.setID(newEvent.getID());
+ Viewpoint newLastView = new Viewpoint(thisProxy.getSystemKey(), newOutcome.getSchemaType(), "last", 0, newEvent.getID());
+ Viewpoint newNumberView = new Viewpoint(thisProxy.getSystemKey(), newOutcome.getSchemaType(), String.valueOf(version), 0, newEvent.getID());
+ Gateway.getStorage().put(thisProxy.getSystemKey(), newOutcome, thisProxy);
+ Gateway.getStorage().put(thisProxy.getSystemKey(), newLastView, thisProxy);
+ Gateway.getStorage().put(thisProxy.getSystemKey(), newNumberView, thisProxy);
}
- // data was missing or doesn't match
- Logger.msg("Bootstrap.verifyResource() - Writing new version "+version+" to "+getDataType(itemType)+" "+itemName);
- History hist = new History(thisProxy.getSystemKey(), thisProxy);
- Transition predefDone = new Transition(0, "Done", 0, 0);
- Event newEvent = hist.addEvent("system", "Admin", "Bootstrap", "Bootstrap", "Bootstrap", getDataType(itemType), 0, "PredefinedStep", 0, predefDone, String.valueOf(version));
- newOutcome.setID(newEvent.getID());
- Viewpoint newLastView = new Viewpoint(thisProxy.getSystemKey(), getDataType(itemType), "last", 0, newEvent.getID());
- Viewpoint newZeroView = new Viewpoint(thisProxy.getSystemKey(), getDataType(itemType), String.valueOf(version), 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);
+ Gateway.getStorage().commit(thisProxy);
+ return modDomPath;
+ }
+
+ private static ResourceImportHandler getHandler(String resType) throws Exception {
+ if (resHandlerCache.containsKey(resType))
+ return resHandlerCache.get(resType);
+ ResourceImportHandler handler;
+ Object handlerProp = Gateway.getProperties().get("ResourceImportHandler."+resType);
+ if (handlerProp instanceof ResourceImportHandler)
+ handler = (ResourceImportHandler)handlerProp;
+ else if (handlerProp instanceof String) { //class name
+ try {
+ Class<?> handlerClass = Class.forName((String)handlerProp);
+ handler = (ResourceImportHandler) handlerClass.newInstance();
+ } catch (ClassNotFoundException e) {
+ throw new Exception("Handler class "+handlerProp+" for importing "+resType+" resources not found");
+ } catch (ClassCastException e) {
+ throw new Exception(handlerProp+" for importing "+resType+" was not a ResourceImportHandler");
+ }
+ }
+ else
+ handler = new DefaultResourceImportHandler(resType);
+
+ resHandlerCache.put(resType, handler);
+ return handler;
}
- /**
+ /**
* @param itemType
* @param itemName
+ * @param layer
* @param data
*/
- private static ItemProxy createResourceItem(String itemType, String itemName, String ns) throws Exception {
+ private static ItemProxy createResourceItem(ResourceImportHandler impHandler, String itemName, int layer, String ns) throws Exception {
// create props
- PropertyDescriptionList pdList = (PropertyDescriptionList)Gateway.getMarshaller().unmarshall(Gateway.getResource().getTextResource(null, "boot/property/"+itemType+"Prop.xml"));
+ PropertyDescriptionList pdList = impHandler.getPropDesc();
PropertyArrayList props = new PropertyArrayList();
for (int i = 0; i < pdList.list.size(); i++) {
PropertyDescription pd = pdList.list.get(i);
String propName = pd.getName();
- String propVal = propName.equals("Name")?itemName:pd.getDefaultValue();
+ String propVal;
+ if (propName.equals("Name"))
+ propVal = itemName;
+ else if (propName.equals("Layer"))
+ propVal = String.valueOf(layer);
+ else propVal = pd.getDefaultValue();
props.list.add(new Property(propName, propVal, pd.getIsMutable()));
}
CompositeActivity ca = new CompositeActivity();
- if (ns!=null && Gateway.getProperties().getBoolean("Module.debug", false)) {
- String wf;
- if (itemType.equals("CA")) wf = "ManageCompositeActDef";
- else if (itemType.equals("EA")) wf = "ManageElementaryActDef";
- else if (itemType.equals("OD")) wf = "ManageSchema";
- else if (itemType.equals("SC")) wf = "ManageScript";
- else throw new Exception("Unknown bootstrap item type: "+itemType);
- ca = (CompositeActivity) ((CompositeActivityDef)LocalObjectLoader.getActDef(wf, 0)).instantiate();
- }
-
- EntityPath entityPath = Gateway.getLDAPLookup().getNextKeyManager().generateNextEntityKey();
- TraceableEntity newItem = (TraceableEntity)Gateway.getCorbaServer().createEntity(entityPath);
- Gateway.getLDAPLookup().add(entityPath);
- newItem.initialise(
- 1,
- Gateway.getMarshaller().marshall(props),
- Gateway.getMarshaller().marshall(ca));
- 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);
- }
-
- public static DomainPath getTypeRoot(String type) throws Exception {
- if (type.equals("CA") || type.equals("EA"))
- return new DomainPath("/desc/ActivityDesc/");
- if (type.equals("SC"))
- return new DomainPath("/desc/Script/");
- if (type.equals("OD"))
- return new DomainPath("/desc/OutcomeDesc/");
- if (type.equals("SM"))
- return new DomainPath("/desc/StateMachine");
- throw new Exception("Unknown bootstrap item type: "+type);
- }
+ if (ns!=null && Gateway.getProperties().getBoolean("Module.debug", false))
+ try {
+ ca = (CompositeActivity) ((CompositeActivityDef)LocalObjectLoader.getActDef(impHandler.getWorkflowName(), 0)).instantiate();
+ } catch (ObjectNotFoundException ex) {
+ Logger.error("Module resource workflow "+impHandler.getWorkflowName()+" not found. Using empty.");
+ }
- private static String getDataType(String type) throws Exception {
- if (type.equals("CA"))
- return "CompositeActivityDef";
- if (type.equals("EA"))
- return "ElementaryActivityDef";
- if (type.equals("OD"))
- return "Schema";
- if (type.equals("SC"))
- return "Script";
- if (type.equals("SM"))
- return "StateMachine";
- throw new Exception("Unknown bootstrap item type: "+type);
+ ItemPath entityPath = Gateway.getNextKeyManager().generateNextEntityKey();
+ Gateway.getCorbaServer().createEntity(entityPath);
+ Gateway.getLookup().add(entityPath);
+ DomainPath newDomPath = impHandler.getPath(itemName, ns);
+ newDomPath.setEntity(entityPath);
+ Gateway.getLookup().add(newDomPath);
+ ItemProxy newItemProxy = Gateway.getProxyManager().getProxy(entityPath);
+ newItemProxy.initialise( 1, props, ca, null);
+ return newItemProxy;
}
/**************************************************************************
@@ -233,9 +279,9 @@ public class Bootstrap **************************************************************************/
private static void checkAgent(String name, String pass, String role, boolean joblist) throws Exception {
Logger.msg(1, "Bootstrap.checkAgent() - Checking for existence of '"+name+"' user.");
- LDAPLookup lookup = Gateway.getLDAPLookup();
+ Lookup lookup = Gateway.getLookup();
try {
- lookup.getRoleManager().getAgentPath(name);
+ systemAgents.put(name, Gateway.getProxyManager().getAgentProxy(lookup.getAgentPath(name)));
Logger.msg(3, "Bootstrap.checkAgent() - User '"+name+"' found.");
return;
} catch (ObjectNotFoundException ex) { }
@@ -243,23 +289,24 @@ public class Bootstrap RolePath rolePath;
try {
- rolePath = lookup.getRoleManager().getRolePath(role);
+ rolePath = lookup.getRolePath(role);
} catch (ObjectNotFoundException ex) {
- rolePath = lookup.getRoleManager().createRole(role, joblist);
+ rolePath = lookup.createRole(role, joblist);
}
try {
- EntityPath entityPath = lookup.getNextKeyManager().generateNextEntityKey();
+ ItemPath entityPath = Gateway.getNextKeyManager().generateNextEntityKey();
AgentPath agentPath = new AgentPath(entityPath.getSysKey(), name);
agentPath.setPassword(pass);
Gateway.getCorbaServer().createEntity(agentPath);
- Gateway.getLDAPLookup().add(agentPath);
+ Gateway.getLookup().add(agentPath);
// assign admin role
Logger.msg("Bootstrap.checkAgent() - Assigning role '"+role+"'");
rolePath.addAgent(agentPath);
Gateway.getStorage().put(agentPath.getSysKey(), new Property("Name", name, true), null);
Gateway.getStorage().put(agentPath.getSysKey(), new Property("Type", "Agent", false), null);
+ systemAgents.put(name, Gateway.getProxyManager().getAgentProxy(agentPath));
Logger.msg("Bootstrap.checkAgent() - Done");
} catch (Exception ex) {
Logger.error("Unable to create "+name+" user.");
@@ -286,16 +333,16 @@ public class Bootstrap public static void createServerItem() throws Exception {
String serverName = Gateway.getProperties().getProperty("ItemServer.name");
thisServerPath = new DomainPath("/servers/"+serverName);
- EntityPath serverEntity;
+ ItemPath serverEntity;
try {
serverEntity = thisServerPath.getEntity();
} catch (ObjectNotFoundException ex) {
Logger.msg("Creating server item "+thisServerPath);
- serverEntity = Gateway.getLDAPLookup().getNextKeyManager().generateNextEntityKey();
+ serverEntity = Gateway.getNextKeyManager().generateNextEntityKey();
Gateway.getCorbaServer().createEntity(serverEntity);
- Gateway.getLDAPLookup().add(serverEntity);
+ Gateway.getLookup().add(serverEntity);
thisServerPath.setEntity(serverEntity);
- Gateway.getLDAPLookup().add(thisServerPath);
+ Gateway.getLookup().add(thisServerPath);
}
Gateway.getStorage().put(serverEntity.getSysKey(), new Property("Name", serverName, false), null);
Gateway.getStorage().put(serverEntity.getSysKey(), new Property("Type", "Server", false), null);
@@ -315,7 +362,7 @@ public class Bootstrap PredefinedStepContainer predef = (PredefinedStepContainer)wf.search("workflow/predefined");
wf.getChildGraphModel().removeVertex(predef);
wf.addChild(new ServerPredefinedStepContainer(), predef.getCentrePoint());
- wf.initialise(thisServerPath.getSysKey(), Gateway.getLDAPLookup().getRoleManager().getAgentPath("system"));
+ wf.initialise(thisServerPath.getSysKey(), systemAgents.get("system").getPath());
Gateway.getStorage().put(thisServerPath.getSysKey(), wf, null);
}
}
|
