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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
|
package com.c2kernel.process;
/**
* @version $Revision: 1.17 $ $Date: 2005/10/12 12:51:54 $
* @author $Author: abranson $
*/
import java.net.MalformedURLException;
import java.util.Enumeration;
import java.util.Properties;
import com.c2kernel.common.InvalidDataException;
import com.c2kernel.common.ObjectNotFoundException;
import com.c2kernel.entity.CorbaServer;
import com.c2kernel.entity.proxy.AgentProxy;
import com.c2kernel.entity.proxy.ProxyManager;
import com.c2kernel.entity.proxy.ProxyServer;
import com.c2kernel.lookup.AgentPath;
import com.c2kernel.lookup.Lookup;
import com.c2kernel.persistency.ClusterStorageException;
import com.c2kernel.persistency.NextKeyManager;
import com.c2kernel.persistency.TransactionManager;
import com.c2kernel.process.auth.Authenticator;
import com.c2kernel.process.module.ModuleManager;
import com.c2kernel.process.resource.Resource;
import com.c2kernel.process.resource.ResourceLoader;
import com.c2kernel.utils.CastorXMLUtility;
import com.c2kernel.utils.FileStringUtility;
import com.c2kernel.utils.Language;
import com.c2kernel.utils.Logger;
import com.c2kernel.utils.ObjectProperties;
/**************************************************************************
* The Gateway is the central object of a CRISTAL process. It initializes,
* maintains and shuts down every other subsystem in both the client and the
* server.
*
* Child objects:
* <ul>
* <li>Lookup - Provides access to the CRISTAL directory. Find or
* search for Items or Agents.
* <li>EntityProxyManager - Gives a local proxy object for Entities found
* in LDAP. Execute activities in Items, query or subscribe to Entity data.
* <li>TransactionManager - Access to the configured CRISTAL databases
* <li>CorbaServer - Manages the memory pool of active Entities
* <li>mORB - the Orbacus CORBA ORB
* </ul>
*
* @author $Author: abranson $ $Date: 2005/10/12 12:51:54 $
* @version $Revision: 1.17 $
**************************************************************************/
public class Gateway
{
static private ObjectProperties mC2KProps;
static private ModuleManager mModules;
static private org.omg.CORBA.ORB mORB;
static private boolean orbDestroyed = false;
static private Lookup mLookup;
static private NextKeyManager mNextKeyManager;
static private TransactionManager mStorage;
static private ProxyManager mProxyManager;
static private ProxyServer mProxyServer;
static private CorbaServer mCorbaServer;
static private CastorXMLUtility mMarshaller;
static private ResourceLoader mResource;
private Gateway() { }
/**
* Initialises the Gateway and all of the client objects it holds, with
* the exception of the Lookup, which is initialised during connect()
*
* @param props - java.util.Properties containing all application properties.
* 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 {
init(props, null);
}
/**
* Initialises the Gateway and all of the client objects it holds, with
* the exception of the Lookup, which is initialised during connect()
*
* @param props - java.util.Properties containing all application properties.
* If null, the java system properties are used
* @param res - ResourceLoader for the kernel to use to resolve all class resource requests
* such as for bootstrap descriptions and version information
* @throws InvalidDataException - invalid properties caused a failure in initialisation
*/
static public void init(Properties props, ResourceLoader res) throws InvalidDataException {
// Init properties & resources
mC2KProps = new ObjectProperties();
mResource = res;
if (mResource == null) mResource = new Resource();
// report version info
Logger.msg("Kernel version: "+getKernelVersion());
// load kernel mapfiles
try {
mMarshaller = new CastorXMLUtility(mResource.getKernelResourceURL("mapFiles/"));
} catch (MalformedURLException e1) {
throw new InvalidDataException("Invalid Resource Location", "");
}
// init module manager
try {
mModules = new ModuleManager(mResource.getModuleDefURLs(), AbstractMain.runningAsWrapper);
} catch (Exception e) {
Logger.error(e);
throw new InvalidDataException("Could not load module definitions.", "");
}
// 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 argument props
if (props != null) mC2KProps.putAll(props);
// dump properties
dumpC2KProps(7);
//Initialise language file
String languageFile = getProperty("language.file");
if (languageFile != null && languageFile.length() > 0) {
Language.isTranlated=true;
Language.mTableOfTranslation = FileStringUtility.loadLanguageFile(languageFile);
}
}
/**
* Makes this process capable of creating and managing server entities. Runs the
* bootstrap to create the root LDAP contexts, initialises the CORBA server and
* time-out manager.
*
* @throws InvalidDataException - error initialising
*/
static public void startServer() throws InvalidDataException {
try {
// check top level directory contexts
mLookup.initializeDirectory();
// init next key manager
mNextKeyManager = (NextKeyManager)mC2KProps.getInstance("NextKeyManager");
// start entity proxy server
mProxyServer = new ProxyServer(mC2KProps.getProperty("ItemServer.name"));
// Init ORB - set various config
String serverName = mC2KProps.getProperty("ItemServer.name");
if (serverName != null)
mC2KProps.put("com.sun.CORBA.ORBServerHost", serverName);
String serverPort = mC2KProps.getProperty("ItemServer.iiop", "1500");
mC2KProps.put("com.sun.CORBA.ORBServerPort", serverPort);
//TODO: externalize this (or replace corba completely)
mC2KProps.put("com.sun.CORBA.POA.ORBServerId", "1");
mC2KProps.put("com.sun.CORBA.POA.ORBPersistentServerPort", serverPort);
//Standard initialisation of the ORB
mORB = org.omg.CORBA.ORB.init(new String[0], mC2KProps);
Logger.msg("Gateway.init() - ORB initialised. ORB is " + mORB.getClass().getName() );
// start corba server components
mCorbaServer = new CorbaServer();
// start checking bootstrap & module items
Bootstrap.run();
} catch (Exception ex) {
Logger.error(ex);
Logger.die("Exception starting server components. Shutting down.");
}
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.
*
* @throws InvalidDataException - bad params
* @throws ClusterStorageException - error starting storages
*/
static public void connect()
throws InvalidDataException,
ClusterStorageException
{
try {
Authenticator auth = (Authenticator)mC2KProps.getInstance("Authenticator");
auth.authenticate("System");
mLookup = (Lookup)mC2KProps.getInstance("Lookup");
mLookup.open(auth);
mStorage = new TransactionManager();
mProxyManager = new ProxyManager();
} catch (Exception ex) {
Logger.error(ex);
throw new InvalidDataException("Cannot connect server process. Please check config.", "");
}
}
/**
* Logs in with the given username and password, and initialises the lookup, storage and proxy manager.
*
* @param agentName - username
* @param agentPassword - password
* @return an AgentProxy on the requested user
* @throws InvalidDataException
* @throws ClusterStorageException
* @throws ClassNotFoundException
* @throws IllegalAccessException
* @throws InstantiationException
*/
static public AgentProxy connect(String agentName, String agentPassword, String resource)
throws InvalidDataException, ObjectNotFoundException, ClusterStorageException, InstantiationException, IllegalAccessException, ClassNotFoundException
{
Authenticator auth = (Authenticator)mC2KProps.getInstance("Authenticator");
if (!auth.authenticate(resource, agentName, agentPassword))
throw new InvalidDataException("Login failed", "");
mLookup = (Lookup)mC2KProps.getInstance("Lookup");
mLookup.open(auth);
mStorage = new TransactionManager();
mProxyManager = new ProxyManager();
// find agent proxy
AgentPath agentPath = mLookup.getAgentPath(agentName);
AgentProxy userProxy = (AgentProxy) mProxyManager.getProxy(agentPath);
userProxy.setAuthObj(auth);
// Run module startup scripts. Server does this during bootstrap
mModules.setUser(userProxy);
mModules.runScripts("startup");
return userProxy;
}
/**
* Shuts down all kernel api objects
*/
public static void close()
{
// run shutdown module scripts
mModules.runScripts("shutdown");
// shut down servers if running
if (mCorbaServer != null)
mCorbaServer.close();
mCorbaServer = null;
// disconnect from storages
if (mStorage != null)
mStorage.close();
mStorage = null;
// disconnect from ldap
if (mLookup != null)
mLookup.close();
mLookup = null;
// shut down proxy manager & server
if (mProxyServer != null)
mProxyServer.shutdownServer();
if (mProxyManager != null)
mProxyManager.shutdown();
mProxyManager = null;
// close log consoles
Logger.closeConsole();
// finally, destroy the ORB
if (!orbDestroyed) {
getORB().destroy();
orbDestroyed = true;
}
}
static public org.omg.CORBA.ORB getORB()
{
if (orbDestroyed) throw new RuntimeException("Gateway has been closed. ORB is destroyed.");
if (mORB == null)
mORB = org.omg.CORBA.ORB.init(new String[0], mC2KProps);
return mORB;
}
static public Lookup getLookup()
{
return mLookup;
}
static public CorbaServer getCorbaServer()
{
return mCorbaServer;
}
static public TransactionManager getStorage()
{
return mStorage;
}
static public CastorXMLUtility getMarshaller()
{
return mMarshaller;
}
static public ResourceLoader getResource()
{
return mResource;
}
static public ProxyManager getProxyManager()
{
return mProxyManager;
}
public static ProxyServer getProxyServer() {
return mProxyServer;
}
static public String getCentreId() {
return getProperty("LocalCentre");
}
@Deprecated
static public String getProperty(String propName) {
return getProperty(propName, null);
}
@Deprecated
static public String getProperty(String propName, String defaultValue) {
if (mC2KProps == null) return defaultValue;
return mC2KProps.getProperty(propName, defaultValue);
}
@Deprecated
static public void setProperty(String propName, String propValue) {
if (mC2KProps == null) return;
mC2KProps.put(propName, propValue);
}
static public Enumeration<?> propertyNames() {
return mC2KProps.propertyNames();
}
static public void dumpC2KProps(int logLevel) {
if (!Logger.doLog(logLevel)) return;
mC2KProps.dumpProps(logLevel);
}
static public ObjectProperties getProperties() {
return mC2KProps;
}
static public String getKernelVersion() {
try {
return mResource.getTextResource(null, "textFiles/version.txt");
} catch (Exception ex) {
return "No version info found";
}
}
public static NextKeyManager getNextKeyManager() {
return mNextKeyManager;
}
}
|