KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > corba > SharkCORBAServer


1 package org.enhydra.shark.corba;
2
3 // Server will use the naming service.
4
// The package containing special exceptions thrown by the name service.
5

6 import java.util.Properties JavaDoc;
7
8 import org.enhydra.shark.Shark;
9 import org.enhydra.shark.api.client.wfbase.BaseException;
10 import org.enhydra.shark.corba.WorkflowService.*;
11 import org.enhydra.shark.utilities.DeadlineChecker;
12 import org.enhydra.shark.utilities.LimitChecker;
13 import org.omg.CORBA.ORB JavaDoc;
14 import org.omg.CosNaming.*;
15 import org.omg.CosNaming.NamingContextPackage.*;
16 import org.omg.WfBase.NameValueInfo;
17 import org.omg.WorkflowModel.WfCreateProcessEventAudit;
18 import org.omg.WorkflowModel.WfDataEventAudit;
19 import org.omg.WorkflowModel.WfStateEventAudit;
20 import org.omg.WorkflowModel._WfCreateProcessEventAuditStub;
21 import org.omg.WorkflowModel._WfDataEventAuditStub;
22 import org.omg.WorkflowModel._WfStateEventAuditStub;
23
24 /**
25  * The server class for engine. It is registered in 'CORBA' name server.
26  * The client applications has to get this object from the name server.
27  *
28  * @author Sasa Bojanic
29  * @author Vladimir Puskas
30  * @author Rich Robinson
31  * @author Andy Zeneski (jaz@ofbiz.org)
32  */

33 public class SharkCORBAServer extends _SharkInterfaceImplBase {
34
35    private org.enhydra.shark.Shark shark;
36
37    private ORB JavaDoc orb;
38
39    private String JavaDoc engineName;
40
41    private String JavaDoc nsHost;
42
43    private String JavaDoc nsPort;
44
45    boolean trackObjects;
46
47    public SharkCORBAServer(String JavaDoc engineName,
48                            String JavaDoc nsHost,
49                            String JavaDoc nsPort,
50                            Shark shark) {
51       // all parameters are required and cannot be null
52
if (engineName == null) throw new IllegalArgumentException JavaDoc("Engine name cannot be null");
53       if (nsHost == null) throw new IllegalArgumentException JavaDoc("Name server host cannot be null");
54       if (nsPort == null) throw new IllegalArgumentException JavaDoc("Name server port cannot be null");
55       if (shark == null) throw new IllegalArgumentException JavaDoc("Shark instance cannot be null");
56
57       this.engineName = engineName;
58       this.nsHost = nsHost;
59       this.nsPort = nsPort;
60       this.shark = shark;
61       this.trackObjects = Boolean.valueOf(shark.getProperties()
62          .getProperty("CORBAServer.TrackAndDisconnect", "false")).booleanValue();
63       boolean ignoreProblematicRequester=new Boolean JavaDoc(shark.getProperties().getProperty("CORBAServer.ignoreProblematicRequester","true")).booleanValue();
64       WfLinkingRequesterForCORBA.setIgnoreProblematicRequesterProcess(ignoreProblematicRequester);
65
66       
67       // Register a shutdown hook that cleanly shuts down Shark
68
Runtime.getRuntime().addShutdownHook(new Thread JavaDoc() {
69          public void run() {
70             shutdownORB();
71             if (null != p) {
72                p.destroy();
73             }
74          }
75       });
76    }
77
78    public void startCORBAServer() throws BaseException {
79       // orb conf
80
String JavaDoc[] a1 = {
81                   "-ORBInitialHost", nsHost, "-ORBInitialPort", nsPort
82       };
83       String JavaDoc[] a2 = {
84                   "-ORBInitRef",
85                   "NameService=corbaloc::"
86                                                       + nsHost + ":" + nsPort
87                                                       + "/NameService"
88       };
89
90       // create and initialize the ORB
91
String JavaDoc vers = System.getProperty("java.version");
92       if (vers.compareTo("1.4") < 0) {
93          orb = ORB.init(a1, null);
94       } else {
95          orb = ORB.init(a2, null);
96       }
97       // get the root naming context
98
org.omg.CORBA.Object JavaDoc objRef = null;
99       try {
100          objRef = orb.resolve_initial_references("NameService");
101       } catch (Exception JavaDoc ex) {
102          // The following code is included to enable Shark Server to be
103
// started
104
// from Shark project script that starts all project parts at
105
// ones
106
for (int i = 0; i < 25; i++) {
107
108             try {
109                Thread.sleep(2500);
110                objRef = orb.resolve_initial_references("NameService");
111             } catch (Exception JavaDoc ex2) {}
112             if (objRef != null) {
113                break;
114             }
115          }
116       }
117
118       // check the root
119
if (objRef == null) { throw new BaseException("Unable to obtain initial reference from orb"); }
120
121       // get the naming context
122
NamingContext ncRef = NamingContextHelper.narrow(objRef);
123       if (ncRef == null) { throw new BaseException("Null NamingContext"); }
124
125       // bind the object reference in naming
126
NameComponent nc = new NameComponent(engineName, "");
127       NameComponent path[] = {
128          nc
129       };
130       try {
131          ncRef.rebind(path, this);
132       } catch (NotFound e) {
133          throw new BaseException(e);
134       } catch (CannotProceed e) {
135          throw new BaseException(e);
136       } catch (InvalidName e) {
137          throw new BaseException(e);
138       }
139
140       // start the orb
141
orb.run(); // WARNING this blocks; objects calling this method
142
// should be threaded
143

144    }
145
146    public ORB JavaDoc getBoundORB() {
147       return orb;
148    }
149
150    public void shutdownORB() {
151       try {
152          orb.disconnect(this);
153          orb.shutdown(false);
154       } catch (Exception JavaDoc e) {
155          // maybe throw some log4j in here
156
}
157    }
158
159    void shutdown() {
160       shutdownORB();
161       System.exit(0);
162    }
163
164    public AdminMisc getAdminMisc() {
165       return new AdminMiscCORBA(this, shark.getAdminInterface()
166          .getAdminMisc());
167    }
168
169    public ExecutionAdministration getExecutionAdministration() {
170       return new ExecutionAdminCORBA(this, shark.getAdminInterface()
171          .getExecutionAdministration());
172    }
173
174    public MappingAdministration getMappingAdministration() {
175       return new MappingAdminCORBA(this, shark.getAdminInterface()
176          .getParticipantMappingAdministration(), shark.getAdminInterface()
177          .getApplicationMappingAdministration());
178    }
179
180    public PackageAdministration getPackageAdministration() {
181       return new PackageAdminCORBA(this, shark.getAdminInterface()
182          .getPackageAdministration());
183    }
184
185    public UserGroupAdministration getUserGroupAdministration() {
186       return new UserGroupAdminCORBA(this, shark.getAdminInterface()
187          .getUserGroupAdministration());
188    }
189
190    public CacheAdministration getCacheAdministration() {
191       return new CacheAdminCORBA(this, shark.getAdminInterface()
192          .getCacheAdministration());
193    }
194
195    public DeadlineAdministration getDeadlineAdministration() {
196       return new DeadlineAdminCORBA(this, shark.getAdminInterface()
197          .getDeadlineAdministration());
198    }
199
200    public LimitAdministration getLimitAdministration() {
201       return new LimitAdminCORBA(this, shark.getAdminInterface()
202          .getLimitAdministration());
203    }
204
205    public RepositoryMgr getRepositoryManager() {
206       return new RepositoryManagerCORBA(this, shark.getRepositoryManager());
207    }
208
209    public SharkConnection getSharkConnection() {
210       return new SharkConnectionCORBA(this, shark.getSharkConnection());
211    }
212
213    public ExpressionBuilderManager getExpressionBuilderManager() {
214       return new ExpressionBuilderMgrCORBA(this, shark.getExpressionBuilderManager());
215    }
216
217    public NameValueInfo[] getProperties() {
218       return SharkCORBAUtilities.makeCORBANameValueInfoArray(shark.getProperties());
219    }
220
221    boolean validateUser(String JavaDoc username, String JavaDoc pwd) {
222       try {
223          return shark.validateUser(username, pwd);
224       } catch (Exception JavaDoc ex) {
225          return true;
226       }
227    }
228
229    private static Process JavaDoc p;
230
231    private static long nameserverRestart_ms = 0;
232
233    //private static Thread penelope;
234
// If exists, first argument is the path to Shark configuration file
235
public static void main(String JavaDoc args[]) {
236       try {
237          if (args != null && args.length > 0) {
238             Shark.configure(args[0]);
239          } else {
240             Shark.configure();
241          }
242
243          org.enhydra.shark.Shark shark = Shark.getInstance();
244
245          Properties JavaDoc properties = shark.getProperties();
246          String JavaDoc nameserverhost = properties.getProperty("nameserverhost",
247                                                         "localhost");
248          final String JavaDoc nameserverport = properties.getProperty("nameserverport",
249                                                               "10123");
250          String JavaDoc enginename = properties.getProperty("enginename", "Shark");
251          final String JavaDoc nameServerExecutable = properties.getProperty("nameserver.executable",
252                                                                     null);
253          String JavaDoc nameserverRestart = properties.getProperty("nameserver.restarting_period_minutes",
254                                                            "0");
255          
256          double nsrmin = 0;
257          try {
258             nsrmin = Double.parseDouble(nameserverRestart);
259             nameserverRestart_ms = (long) (nsrmin * 60 * 1000);
260          } catch (Exception JavaDoc ex) {}
261          // create default user
262
try {
263             org.enhydra.shark.api.client.wfservice.UserGroupAdministration uga = shark.getAdminInterface()
264                .getUserGroupAdministration();
265             String JavaDoc adminGroup = properties.getProperty("DEFAULT_ADMINISTRATOR_GROUP_NAME",
266                                                        "AdminGroup");
267             String JavaDoc adminGroupDescripton = properties.getProperty("DEFAULT_ADMINISTRATOR_GROUP_DESCRIPTION",
268                                                                  "Default Admin Group");
269             String JavaDoc adminUsername = properties.getProperty("DEFAULT_ADMINISTRATOR_USERNAME",
270                                                           "admin");
271             String JavaDoc adminPwd = properties.getProperty("DEFAULT_ADMINISTRATOR_PASSWORD",
272                                                      "enhydra");
273             String JavaDoc adminFirstName = properties.getProperty("DEFAULT_ADMINISTRATOR_FIRST_NAME",
274                                                            "Administrator");
275             String JavaDoc adminLastName = properties.getProperty("DEFAULT_ADMINISTRATOR_LAST_NAME",
276                                                           "Admin");
277             String JavaDoc adminEmail = properties.getProperty("DEFAULT_ADMINISTRATOR_EMAIL",
278                                                        "admin@together.at");
279             try {
280                if (!uga.doesGroupExist(adminGroup)) {
281                   uga.createGroup(adminGroup, adminGroupDescripton);
282                }
283             } catch (Exception JavaDoc ex) {}
284             if (!uga.doesUserExist(adminUsername)) {
285                uga.createUser(adminGroup,
286                               adminUsername,
287                               adminPwd,
288                               adminFirstName,
289                               adminLastName,
290                               adminEmail);
291             }
292          } catch (Exception JavaDoc ex) {}
293
294          // initialize deadline timer
295
String JavaDoc delay = null;
296          if (args != null && args.length > 1) {
297             delay = args[1];
298          } else {
299             delay = properties.getProperty("Deadlines.pollingTime", "300000");
300          }
301          if (properties.getProperty("Deadlines.SERVER_SIDE_CHECKING", "false")
302             .equals("true")) {
303             new DeadlineChecker(shark.getAdminInterface()
304                .getDeadlineAdministration(), Long.parseLong(delay));
305          }
306
307          // initialize limit timer
308
String JavaDoc ldelay = null;
309          String JavaDoc lacn = properties.getProperty("LimitAgentManagerClassName");
310          if (lacn != null) {
311             if (args != null && args.length > 2) {
312                ldelay = args[2];
313             } else {
314                ldelay = properties.getProperty("Limits.pollingTime", "60000");
315             }
316             if (properties.getProperty("Limits.SERVER_SIDE_CHECKING", "false")
317                .equals("true")) {
318                new LimitChecker(shark.getAdminInterface()
319                   .getLimitAdministration(), Long.parseLong(ldelay));
320             }
321          }
322
323          // create/start the server
324
final SharkCORBAServer ws = new SharkCORBAServer(enginename,
325                                                           nameserverhost,
326                                                           nameserverport,
327                                                           shark);
328          if (null != nameServerExecutable) {
329             if (nameserverRestart_ms > 0) {
330                System.out.println("Nameserver will be restarted every "
331                                   + nameserverRestart + " minutes!");
332             }
333
334             new Thread JavaDoc() {
335                public void run() {
336                   while (true) {
337                      try {
338                         p = Runtime.getRuntime().exec(nameServerExecutable
339                                                       + " -ORBInitialPort "
340                                                       + nameserverport);
341
342                         System.out.println("Nameserver is (re)started!");
343                         // (re)register with name server in a separate
344
// thread
345
new Thread JavaDoc() {
346                            public void run() {
347                               try {
348                                  ws.startCORBAServer();
349                               } catch (Exception JavaDoc ex) {
350                                  ex.printStackTrace(System.out);
351                                  throw new Error JavaDoc();
352
353                               }
354                            }
355                         }.start();
356                         if (nameserverRestart_ms > 0) {
357                            new Thread JavaDoc() {
358                               public void run() {
359                                  try {
360                                     Thread.sleep(nameserverRestart_ms);
361                                  } catch (Exception JavaDoc ex) {} finally {
362                                     p.destroy();
363                                  }
364                               }
365                            }.start();
366                         }
367
368                      } catch (Throwable JavaDoc t) {
369                         t.printStackTrace();
370                         System.out.println("Didn't start the nameserver");
371                         //throw new RootError("Didn't start the
372
// nameserver",t);
373
}
374
375                      while (true) {
376                         try {
377                            p.waitFor();
378                         } catch (InterruptedException JavaDoc e) {
379                            continue;
380                         }
381
382                         // Process is terminated
383
break;
384                      }
385                   }
386                }
387             }.start();
388          } else {
389             ws.startCORBAServer();
390          }
391
392       } catch (Throwable JavaDoc e) {
393          e.printStackTrace(System.out);
394          throw new Error JavaDoc();
395       }
396    }
397
398    public void doneWith(org.omg.CORBA.Object JavaDoc toDisconnect) {
399       if (toDisconnect instanceof Collective) {
400          ((Collective)toDisconnect).__disband(orb);
401       } else {
402          try {
403             if (toDisconnect instanceof WfCreateProcessEventAudit) {
404                WfLinkingRequesterForCORBA.emptyCollective(((WfCreateProcessEventAudit)toDisconnect).process_key(),getBoundORB());
405             }
406             if (toDisconnect instanceof WfDataEventAudit) {
407                WfLinkingRequesterForCORBA.emptyCollective(((WfDataEventAudit)toDisconnect).process_key(),getBoundORB());
408             }
409             if (toDisconnect instanceof WfStateEventAudit) {
410                WfLinkingRequesterForCORBA.emptyCollective(((WfStateEventAudit)toDisconnect).process_key(),getBoundORB());
411             }
412             orb.disconnect(toDisconnect);
413          } catch (Exception JavaDoc ex) {
414          }
415       }
416    }
417 }
418
419
Popular Tags