001: package org.enhydra.shark.corba;
002:
003: // Server will use the naming service.
004: // The package containing special exceptions thrown by the name service.
005:
006: import java.util.Properties;
007:
008: import org.enhydra.shark.api.admin.UserGroupManagerAdmin;
009: import org.enhydra.shark.api.client.wfservice.NameValue;
010: import org.enhydra.shark.api.client.wfservice.SharkInterface;
011: import org.enhydra.shark.api.common.SharkConstants;
012: import org.enhydra.shark.client.utilities.DeadlineChecker;
013: import org.enhydra.shark.client.utilities.SharkInterfaceWrapper;
014: import org.enhydra.shark.corba.WorkflowService.SharkConnection;
015: import org.enhydra.shark.corba.WorkflowService._SharkInterfaceImplBase;
016: import org.enhydra.shark.utilities.MiscUtilities;
017: import org.omg.CORBA.ORB;
018: import org.omg.CosNaming.NameComponent;
019: import org.omg.CosNaming.NamingContext;
020: import org.omg.CosNaming.NamingContextHelper;
021: import org.omg.CosNaming.NamingContextPackage.CannotProceed;
022: import org.omg.CosNaming.NamingContextPackage.InvalidName;
023: import org.omg.CosNaming.NamingContextPackage.NotFound;
024: import org.omg.WfBase.BaseException;
025: import org.omg.WfBase.NameValueInfo;
026: import org.omg.WorkflowModel.WfCreateProcessEventAudit;
027: import org.omg.WorkflowModel.WfDataEventAudit;
028: import org.omg.WorkflowModel.WfStateEventAudit;
029:
030: /**
031: * The server class for engine. It is registered in 'CORBA' name server. The client
032: * applications has to get this object from the name server.
033: *
034: * @author Sasa Bojanic
035: * @author Vladimir Puskas
036: * @author Rich Robinson
037: * @author Andy Zeneski (jaz@ofbiz.org)
038: */
039: public class SharkCORBAServer extends _SharkInterfaceImplBase {
040:
041: private SharkInterface shark;
042:
043: private ORB orb;
044:
045: private String engineName;
046:
047: private String nsHost;
048:
049: private String nsPort;
050:
051: boolean trackObjects;
052:
053: public SharkCORBAServer(String engineName, String nsHost,
054: String nsPort, SharkInterface shark) {
055: // all parameters are required and cannot be null
056: if (engineName == null)
057: throw new IllegalArgumentException(
058: "Engine name cannot be null");
059: if (nsHost == null)
060: throw new IllegalArgumentException(
061: "Name server host cannot be null");
062: if (nsPort == null)
063: throw new IllegalArgumentException(
064: "Name server port cannot be null");
065: if (shark == null)
066: throw new IllegalArgumentException(
067: "Shark instance cannot be null");
068:
069: this .engineName = engineName;
070: this .nsHost = nsHost;
071: this .nsPort = nsPort;
072: this .shark = shark;
073: Properties shkProps = new Properties();
074: try {
075: shkProps = MiscUtilities
076: .convertNameValueArrayToProperties(shark
077: .getProperties());
078: } catch (Exception ex) {
079: }
080:
081: this .trackObjects = Boolean.valueOf(
082: shkProps.getProperty("CORBAServer.TrackAndDisconnect",
083: "false")).booleanValue();
084: boolean ignoreProblematicRequester = new Boolean(shkProps
085: .getProperty("CORBAServer.ignoreProblematicRequester",
086: "true")).booleanValue();
087: WfLinkingRequesterForCORBA
088: .setIgnoreProblematicRequesterProcess(ignoreProblematicRequester);
089:
090: // Register a shutdown hook that cleanly shuts down Shark
091: Runtime.getRuntime().addShutdownHook(new Thread() {
092: public void run() {
093: shutdownORB();
094: if (null != p) {
095: p.destroy();
096: }
097: }
098: });
099: }
100:
101: public void startCORBAServer() throws Exception {
102: // orb conf
103: String[] a1 = { "-ORBInitialHost", nsHost, "-ORBInitialPort",
104: nsPort };
105: String[] a2 = {
106: "-ORBInitRef",
107: "NameService=corbaloc::" + nsHost + ":" + nsPort
108: + "/NameService" };
109:
110: // create and initialize the ORB
111: String vers = System.getProperty("java.version");
112: if (vers.compareTo("1.4") < 0) {
113: orb = ORB.init(a1, null);
114: } else {
115: orb = ORB.init(a2, null);
116: }
117: // get the root naming context
118: org.omg.CORBA.Object objRef = null;
119: try {
120: objRef = orb.resolve_initial_references("NameService");
121: } catch (Exception ex) {
122: // The following code is included to enable Shark Server to be
123: // started
124: // from Shark project script that starts all project parts at
125: // ones
126: for (int i = 0; i < 25; i++) {
127:
128: try {
129: Thread.sleep(2500);
130: objRef = orb
131: .resolve_initial_references("NameService");
132: } catch (Exception ex2) {
133: }
134: if (objRef != null) {
135: break;
136: }
137: }
138: }
139:
140: // check the root
141: if (objRef == null) {
142: throw new Exception(
143: "Unable to obtain initial reference from orb");
144: }
145:
146: // get the naming context
147: NamingContext ncRef = NamingContextHelper.narrow(objRef);
148: if (ncRef == null) {
149: throw new Exception("Null NamingContext");
150: }
151:
152: // bind the object reference in naming
153: NameComponent nc = new NameComponent(engineName, "");
154: NameComponent path[] = { nc };
155: try {
156: ncRef.rebind(path, this );
157: } catch (NotFound e) {
158: throw new Exception(e);
159: } catch (CannotProceed e) {
160: throw new Exception(e);
161: } catch (InvalidName e) {
162: throw new Exception(e);
163: }
164:
165: // start the orb
166: orb.run(); // WARNING this blocks; objects calling this method
167: // should be threaded
168:
169: }
170:
171: public ORB getBoundORB() {
172: return orb;
173: }
174:
175: public void shutdownORB() {
176: try {
177: orb.disconnect(this );
178: orb.shutdown(false);
179: } catch (Exception e) {
180: // maybe throw some log4j in here
181: }
182: }
183:
184: void shutdown() {
185: shutdownORB();
186: System.exit(0);
187: }
188:
189: public SharkInterface getShark() {
190: return shark;
191: }
192:
193: public SharkConnection getSharkConnection() throws BaseException {
194: try {
195: return new SharkConnectionCORBA(this , shark
196: .getSharkConnection());
197: } catch (Exception ex) {
198: throw new BaseException();
199: }
200: }
201:
202: public NameValueInfo[] getProperties() {
203: NameValue[] props = null;
204: try {
205: props = shark.getProperties();
206: } catch (Exception ex) {
207: }
208: return SharkCORBAUtilities.makeCORBANameValueInfoArray(props);
209: }
210:
211: boolean validateUser(String username, String pwd) {
212: try {
213: Object plugin = shark
214: .getPlugIn(SharkConstants.PLUGIN_USER_GROUP);
215: if (plugin instanceof UserGroupManagerAdmin) {
216: UserGroupManagerAdmin uga = (UserGroupManagerAdmin) plugin;
217: return uga.validateUser(username, pwd);
218: }
219: } catch (Exception ex) {
220: }
221: return true;
222: }
223:
224: private static Process p;
225:
226: private static long nameserverRestart_ms = 0;
227:
228: // private static Thread penelope;
229: // If exists, first argument is the path to Shark configuration file
230: public static void main(String args[]) {
231: try {
232: SharkInterfaceWrapper.setProperties(args[0], true);
233: org.enhydra.shark.api.client.wfservice.SharkInterface shark = SharkInterfaceWrapper
234: .getShark();
235:
236: Properties properties = MiscUtilities
237: .convertNameValueArrayToProperties(shark
238: .getProperties());
239: String nameserverhost = properties.getProperty(
240: "nameserverhost", "localhost");
241: final String nameserverport = properties.getProperty(
242: "nameserverport", "10123");
243: String enginename = properties.getProperty("enginename",
244: "Shark");
245: final String nameServerExecutable = properties.getProperty(
246: "nameserver.executable", null);
247: String nameserverRestart = properties.getProperty(
248: "nameserver.restarting_period_minutes", "0");
249:
250: double nsrmin = 0;
251: try {
252: nsrmin = Double.parseDouble(nameserverRestart);
253: nameserverRestart_ms = (long) (nsrmin * 60 * 1000);
254: } catch (Exception ex) {
255: }
256: // initialize deadline timer
257: String delay = null;
258: if (args.length > 1) {
259: delay = args[1];
260: } else {
261: delay = properties.getProperty("Deadlines.pollingTime",
262: "300000");
263: }
264: if (properties.getProperty(
265: "Deadlines.SERVER_SIDE_CHECKING", "false").equals(
266: "true")) {
267: DeadlineChecker dc = new DeadlineChecker(
268: SharkInterfaceWrapper.getDefaultUser(), null,
269: Long.parseLong(delay), 100, 10, true);
270: }
271:
272: // create/start the server
273: final SharkCORBAServer ws = new SharkCORBAServer(
274: enginename, nameserverhost, nameserverport, shark);
275: if (null != nameServerExecutable) {
276: if (nameserverRestart_ms > 0) {
277: System.out
278: .println("Nameserver will be restarted every "
279: + nameserverRestart + " minutes!");
280: }
281:
282: new Thread() {
283: public void run() {
284: while (true) {
285: try {
286: p = Runtime.getRuntime().exec(
287: nameServerExecutable
288: + " -ORBInitialPort "
289: + nameserverport);
290:
291: System.out
292: .println("Nameserver is (re)started!");
293: // (re)register with name server in a separate
294: // thread
295: new Thread() {
296: public void run() {
297: try {
298: ws.startCORBAServer();
299: } catch (Exception ex) {
300: ex
301: .printStackTrace(System.out);
302: throw new Error();
303:
304: }
305: }
306: }.start();
307: if (nameserverRestart_ms > 0) {
308: new Thread() {
309: public void run() {
310: try {
311: Thread
312: .sleep(nameserverRestart_ms);
313: } catch (Exception ex) {
314: } finally {
315: p.destroy();
316: }
317: }
318: }.start();
319: }
320:
321: } catch (Throwable t) {
322: t.printStackTrace();
323: System.out
324: .println("Didn't start the nameserver");
325: // throw new RootError("Didn't start the
326: // nameserver",t);
327: }
328:
329: while (true) {
330: try {
331: p.waitFor();
332: } catch (InterruptedException e) {
333: continue;
334: }
335:
336: // Process is terminated
337: break;
338: }
339: }
340: }
341: }.start();
342: } else {
343: ws.startCORBAServer();
344: }
345:
346: } catch (Throwable e) {
347: e.printStackTrace(System.out);
348: throw new Error();
349: }
350: }
351:
352: public void doneWith(org.omg.CORBA.Object toDisconnect) {
353: if (toDisconnect instanceof Collective) {
354: ((Collective) toDisconnect).__disband(orb);
355: } else {
356: try {
357: if (toDisconnect instanceof WfCreateProcessEventAudit) {
358: WfLinkingRequesterForCORBA.emptyCollective(
359: ((WfCreateProcessEventAudit) toDisconnect)
360: .process_key(), getBoundORB());
361: }
362: if (toDisconnect instanceof WfDataEventAudit) {
363: WfLinkingRequesterForCORBA.emptyCollective(
364: ((WfDataEventAudit) toDisconnect)
365: .process_key(), getBoundORB());
366: }
367: if (toDisconnect instanceof WfStateEventAudit) {
368: WfLinkingRequesterForCORBA.emptyCollective(
369: ((WfStateEventAudit) toDisconnect)
370: .process_key(), getBoundORB());
371: }
372: orb.disconnect(toDisconnect);
373: } catch (Exception ex) {
374: }
375: }
376: }
377:
378: public static boolean doesTransactionExist() {
379: return false;
380: }
381:
382: }
|