1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
|
package com.c2kernel.process;
import java.net.InetAddress;
import java.util.Enumeration;
import java.util.HashMap;
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.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.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.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;
import com.c2kernel.property.PropertyDescriptionList;
import com.c2kernel.utils.FileStringUtility;
import com.c2kernel.utils.LocalObjectLoader;
import com.c2kernel.utils.Logger;
/**
* @version $Revision: 1.25 $ $Date: 2006/01/10 09:48:32 $
* @author $Author: abranson $
*/
public class Bootstrap
{
static DomainPath thisServerPath;
static HashMap<String, ResourceImportHandler> resHandlerCache = new HashMap<String, ResourceImportHandler>();
/**
* 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() {
@Override
public void run() {
try {
Thread.currentThread().setName("Bootstrapper");
// make sure all of the boot items are up-to-date
Logger.msg("Bootstrap.run() - Verifying kernel boot data items");
verifyBootDataItems();
// verify the server item's wf
Logger.msg("Bootstrap.run() - Initialising Server Item Workflow");
initServerItemWf();
// register modules
Gateway.getModuleManager().registerModules();
Logger.msg("Bootstrap.run() - Bootstrapping complete");
} catch (Throwable 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(Gateway.getResource().getKernelResourceURL("boot/allbootitems.txt"));
verifyBootDataItems(bootItems, null, true);
Logger.msg(1, "Boot data items complete");
}
private static void verifyBootDataItems(String bootList, String ns, boolean reset) {
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 location = "boot/"+thisItem+(itemType.equals("OD")?".xsd":".xml");
verifyResource(ns, itemName, 0, itemType, location, reset);
} catch (Exception e) {
Logger.error(e);
Logger.die("Error importing bootstrap items. Unsafe to continue.");
}
}
}
public static DomainPath verifyResource(String ns, String itemName, Integer version, String itemType, String dataLocation, boolean reset) throws Exception {
if (version == null) version = 0;
ResourceImportHandler typeImpHandler = getHandler(itemType);
Logger.msg(1, "Bootstrap.verifyResource() - Verifying version "+version+" of "+typeImpHandler.getName()+" "+itemName);
// Find or create Item for Resource
DomainPath modDomPath = typeImpHandler.getPath(itemName, ns);
ItemProxy thisProxy;
Enumeration<Path> en = Gateway.getLDAPLookup().search(typeImpHandler.getTypeRoot(), itemName);
if (!en.hasMoreElements()) {
Logger.msg("Bootstrap.verifyResource() - "+typeImpHandler.getName()+" "+itemName+" not found. Creating new.");
thisProxy = createResourceItem(typeImpHandler, itemName, ns);
}
else {
DomainPath path = (DomainPath)en.nextElement();
thisProxy = (ItemProxy)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);
}
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 EntityPath(thisProxy.getSystemKey()));
if (!modDomPath.exists())
Gateway.getLDAPLookup().add(modDomPath);
Gateway.getLDAPLookup().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");
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.");
continue;
}
}
} catch (ObjectNotFoundException ex) {
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);
}
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 data
*/
private static ItemProxy createResourceItem(ResourceImportHandler impHandler, String itemName, String ns) throws Exception {
// create props
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();
props.list.add(new Property(propName, propVal, pd.getIsMutable()));
}
CompositeActivity ca = new CompositeActivity();
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.");
}
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 = impHandler.getPath(itemName, ns);
newDomPath.setEntity(entityPath);
Gateway.getLDAPLookup().add(newDomPath);
return (ItemProxy)Gateway.getProxyManager().getProxy(entityPath);
}
/**************************************************************************
* 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 {
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);
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, true), null);
Gateway.getStorage().put(agentPath.getSysKey(), new Property("Type", "Agent", false), 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.getProperties().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.getProperties().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();
Gateway.getCorbaServer().createEntity(serverEntity);
Gateway.getLDAPLookup().add(serverEntity);
thisServerPath.setEntity(serverEntity);
Gateway.getLDAPLookup().add(thisServerPath);
}
Gateway.getStorage().put(serverEntity.getSysKey(), new Property("Name", serverName, false), null);
Gateway.getStorage().put(serverEntity.getSysKey(), new Property("Type", "Server", false), null);
Gateway.getStorage().put(serverEntity.getSysKey(), new Property("KernelVersion", Gateway.getKernelVersion(), true), null);
if (Gateway.getProperties().getProperty("ItemServer.Proxy.port") != null)
Gateway.getStorage().put(serverEntity.getSysKey(),
new Property("ProxyPort", Gateway.getProperties().getProperty("ItemServer.Proxy.port"), false), null);
Gateway.getStorage().put(serverEntity.getSysKey(),
new Property("ConsolePort", String.valueOf(Logger.getConsolePort()), true), null);
Gateway.getProxyManager().connectToProxyServer(Gateway.getProperties().getProperty("ItemServer.name"), Gateway.getProperties().getInt("ItemServer.Proxy.port"));
}
public static void initServerItemWf() throws Exception {
CompositeActivityDef serverWfCa = (CompositeActivityDef)LocalObjectLoader.getActDef("ServerItemWorkflow", 0);
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);
}
}
|