From 254ee6f47eebfc00462c10756a92066e82cc1a96 Mon Sep 17 00:00:00 2001 From: Andrew Branson Date: Tue, 21 Jun 2011 15:46:02 +0200 Subject: Initial commit --- source/com/c2kernel/process/Bootstrap.java | 356 +++++++++++++++++++++++++++++ 1 file changed, 356 insertions(+) create mode 100755 source/com/c2kernel/process/Bootstrap.java (limited to 'source/com/c2kernel/process/Bootstrap.java') diff --git a/source/com/c2kernel/process/Bootstrap.java b/source/com/c2kernel/process/Bootstrap.java new file mode 100755 index 0000000..61ce46f --- /dev/null +++ b/source/com/c2kernel/process/Bootstrap.java @@ -0,0 +1,356 @@ +package com.c2kernel.process; + +import java.net.InetAddress; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Iterator; +import java.util.StringTokenizer; + +import com.c2kernel.common.ObjectNotFoundException; +import com.c2kernel.entity.TraceableEntity; +import com.c2kernel.entity.agent.ActiveEntity; +import com.c2kernel.entity.proxy.ItemProxy; +import com.c2kernel.events.Event; +import com.c2kernel.events.History; +import com.c2kernel.lifecycle.CompositeActivityDef; +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; +import com.c2kernel.lookup.DomainPath; +import com.c2kernel.lookup.EntityPath; +import com.c2kernel.lookup.LDAPLookup; +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.property.Property; +import com.c2kernel.property.PropertyArrayList; +import com.c2kernel.property.PropertyDescription; +import com.c2kernel.property.PropertyDescriptionList; +import com.c2kernel.utils.CastorXMLUtility; +import com.c2kernel.utils.FileStringUtility; +import com.c2kernel.utils.LocalObjectLoader; +import com.c2kernel.utils.Logger; +import com.c2kernel.utils.Resource; + +/** + * @version $Revision: 1.25 $ $Date: 2006/01/10 09:48:32 $ + * @author $Author: abranson $ + */ + +public class Bootstrap +{ + static HashMap bootstrapFactoryItems = new HashMap(); + static DomainPath thisServerPath; + + /** + * Run everything without timing-out the service wrapper + */ + public static void run() throws Exception { + // check for system agents + checkAdminAgents(); + + // create the server's mother item + createServerItem(); + new Thread(new Runnable() { + public void run() { + try { + Thread.currentThread().setName("Bootstrapper"); + + // make sure all of the boot items are up-to-date + verifyBootDataItems(); + + // verify the server item's wf + initServerItemWf(); + + // create the factories etc. + createBootstrapFactoryItems(); + Logger.msg("Bootstrap.run() - Bootstrapping complete"); + } catch (Exception e) { + Logger.error(e); + Logger.die("Exception performing bootstrap. Check that everything is OK."); + } + } + }).start(); + } + + /************************************************************************** + * Checks all kernel descriptions, stored in resources + **************************************************************************/ + public static void verifyBootDataItems() throws Exception { + 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; + } + } + + 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) { + StringTokenizer str = new StringTokenizer(bootList, "\n\r"); + while (str.hasMoreTokens()) { + String thisItem = str.nextToken(); + int delim = thisItem.indexOf('/'); + String itemType = thisItem.substring(0,delim); + String itemName = thisItem.substring(delim+1); + try { + String data = Resource.getTextResource("boot/"+thisItem+".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); + } catch (Exception e) { + Logger.error(e); + Logger.die("Error importing bootstrap items. Unsafe to continue."); + } + } + } + + /** + * @param itemType + * @param itemName + * @param data + */ + private static ItemProxy createBootstrapDataItem(String itemType, String itemName, boolean isKernel) throws Exception { + // create props + PropertyDescriptionList pdList = (PropertyDescriptionList)CastorXMLUtility.unmarshall(Resource.getTextResource("boot/property/"+itemType+"Prop.xml")); + PropertyArrayList props = new PropertyArrayList(); + for (int i = 0; i < pdList.list.size(); i++) { + PropertyDescription pd = (PropertyDescription) pdList.list.get(i); + String propName = pd.getName(); + String propVal = propName.equals("Name")?itemName:pd.getDefaultValue(); + props.list.add(new Property(propName, propVal)); + } + + EntityPath entityPath = Gateway.getLDAPLookup().getNextKeyManager().generateNextEntityKey(); + TraceableEntity newItem = (TraceableEntity)Gateway.getCorbaServer().createEntity(entityPath); + Gateway.getLDAPLookup().add(entityPath); + newItem.initialise( + 1, + CastorXMLUtility.marshall(props), + CastorXMLUtility.marshall(new CompositeActivity())); + DomainPath newDomPath = new DomainPath(getTypeRoot(itemType).toString()+"/system/"+(isKernel?"kernel":"domain")+"/"+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/"); + throw new Exception("Unknown bootstrap item type: "+type); + } + + 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"; + throw new Exception("Unknown bootstrap item type: "+type); + + } + + /************************************************************************** + * Checks for the existence of the admin users so you can use Cristal + **************************************************************************/ + 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(); + try { + AgentPath agentPath = lookup.getRoleManager().getAgentPath(name); + Logger.msg(3, "Bootstrap.checkAgent() - User '"+name+"' found."); + return; + } catch (ObjectNotFoundException ex) { } + Logger.msg("Bootstrap.checkAgent() - User '"+name+"' not found. Creating."); + + RolePath rolePath; + try { + rolePath = lookup.getRoleManager().getRolePath(role); + } catch (ObjectNotFoundException ex) { + rolePath = lookup.getRoleManager().createRole(role, joblist); + } + + try { + EntityPath entityPath = lookup.getNextKeyManager().generateNextEntityKey(); + AgentPath agentPath = new AgentPath(entityPath.getSysKey(), name); + agentPath.setPassword(pass); + ActiveEntity adminAgent = (ActiveEntity)Gateway.getCorbaServer().createEntity(agentPath); + Gateway.getLDAPLookup().add(agentPath); + + // assign admin role + Logger.msg("Bootstrap.checkAgent() - Assigning role '"+role+"'"); + rolePath.addAgent(agentPath); + Gateway.getStorage().put(agentPath.getSysKey(), new Property("Name", name), null); + Gateway.getStorage().put(agentPath.getSysKey(), new Property("Type", "Agent"), null); + Logger.msg("Bootstrap.checkAgent() - Done"); + } catch (Exception ex) { + Logger.error("Unable to create "+name+" user."); + throw ex; + } + } + + /** + * + */ + public static void checkAdminAgents() throws Exception { + // check for administrative user + String adminPassword = Gateway.getProperty("AdminPassword", "admin12345"); + + checkAgent("admin", adminPassword, "Admin", false); + + // check for import user + checkAgent("system", adminPassword, "Admin", false); + + // check for local usercode user + checkAgent(InetAddress.getLocalHost().getHostName(), "uc", "UserCode", true); + } + + public static void createServerItem() throws Exception { + String serverName = Gateway.getProperty("ItemServer.name"); + thisServerPath = new DomainPath("/servers/"+serverName); + EntityPath serverEntity; + try { + serverEntity = thisServerPath.getEntity(); + } catch (ObjectNotFoundException ex) { + Logger.msg("Creating server item "+thisServerPath); + serverEntity = Gateway.getLDAPLookup().getNextKeyManager().generateNextEntityKey(); + TraceableEntity newItem = (TraceableEntity)Gateway.getCorbaServer().createEntity(serverEntity); + Gateway.getLDAPLookup().add(serverEntity); + thisServerPath.setEntity(serverEntity); + Gateway.getLDAPLookup().add(thisServerPath); + } + Gateway.getStorage().put(serverEntity.getSysKey(), new Property("Name", serverName), null); + Gateway.getStorage().put(serverEntity.getSysKey(), new Property("Type", "Server"), null); + if (Gateway.getProperty("ItemServer.Proxy.port") != null) + Gateway.getStorage().put(serverEntity.getSysKey(), + new Property("ProxyPort", Gateway.getProperty("ItemServer.Proxy.port")), null); + if (Gateway.getProperty("ItemServer.Console.port") != null) + Gateway.getStorage().put(serverEntity.getSysKey(), + new Property("ConsolePort", Gateway.getProperty("ItemServer.Console.port")), null); + Gateway.getProxyManager().connectToProxyServer(Gateway.getProperty("ItemServer.name"), Integer.parseInt(Gateway.getProperty("ItemServer.Proxy.port"))); + + } + + public static void initServerItemWf() throws Exception { + CompositeActivityDef serverWfCa = (CompositeActivityDef)LocalObjectLoader.getActDef("ServerItemWorkflow", "last"); + Workflow wf = new Workflow((CompositeActivity)serverWfCa.instantiate()); + 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")); + 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 (Iterator iter = bootstrapFactoryItems.keySet().iterator(); iter.hasNext();) { + String itemName = (String) iter.next(); + String itemXML = (String)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); + } + } + +} -- cgit v1.2.3