KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > corbaclient > SharkClient


1 package org.enhydra.shark.corbaclient;
2
3 import java.awt.BorderLayout JavaDoc;
4 import java.awt.Color JavaDoc;
5 import java.awt.GraphicsEnvironment JavaDoc;
6 import java.awt.Rectangle JavaDoc;
7 import java.awt.event.WindowAdapter JavaDoc;
8 import java.awt.event.WindowEvent JavaDoc;
9 import java.net.URL JavaDoc;
10 import java.lang.reflect.Method JavaDoc;
11 import javax.swing.Action JavaDoc;
12 import javax.swing.ImageIcon JavaDoc;
13 import javax.swing.JFrame JavaDoc;
14 import javax.swing.JMenuBar JavaDoc;
15 import javax.swing.UIManager JavaDoc;
16 import javax.swing.WindowConstants JavaDoc;
17
18 import org.enhydra.shark.corba.WorkflowService.AdminMisc;
19 import org.enhydra.shark.corba.WorkflowService.CacheAdministration;
20 import org.enhydra.shark.corba.WorkflowService.DeadlineAdministration;
21 import org.enhydra.shark.corba.WorkflowService.ExecutionAdministration;
22 import org.enhydra.shark.corba.WorkflowService.LimitAdministration;
23 import org.enhydra.shark.corba.WorkflowService.MappingAdministration;
24 import org.enhydra.shark.corba.WorkflowService.PackageAdministration;
25 import org.enhydra.shark.corba.WorkflowService.RepositoryMgr;
26 import org.enhydra.shark.corba.WorkflowService.SharkConnection;
27 import org.enhydra.shark.corba.WorkflowService.SharkInterface;
28 import org.enhydra.shark.corba.WorkflowService.SharkInterfaceHelper;
29 import org.enhydra.shark.corba.WorkflowService.UserGroupAdministration;
30 import org.enhydra.shark.corbaclient.actions.Connect;
31 import org.enhydra.shark.corbaclient.actions.Disconnect;
32 import org.enhydra.shark.corbaclient.actions.Exit;
33 import org.enhydra.shark.corbaclient.actions.Refresh;
34 import org.enhydra.shark.corbaclient.actions.SetRefreshingRate;
35 import org.enhydra.shark.corbaclient.actions.ShutdownEngine;
36 import org.omg.CORBA.ORB JavaDoc;
37 import org.omg.CosNaming.NameComponent JavaDoc;
38 import org.omg.CosNaming.NamingContext JavaDoc;
39 import org.omg.CosNaming.NamingContextHelper JavaDoc;
40
41 /**
42  * The base abstract class for creating workflow clients (administration,
43  * monitoring, worklist handling). Implements four esential actions
44  * used by every client: Connect, Disconnect, SetRefreshingRate and
45  * Exit. It also defines the visual framework for clients.
46  * <p> It creates and starts the Thread that is used for refreshing
47  * the engine data (it calls the refresh method of this class).
48  * <p> The implementation classes has to implement some abstract
49  * methods, such as refresh, createCenterComponent, etc.
50  *
51  * @author Sasa Bojanic
52  * @version 1.0
53  */

54 public abstract class SharkClient extends ActionPanel {
55
56     // Application Icon. From resource file.
57
protected static ImageIcon JavaDoc appIcon;
58     // Application Icon. From resource file.
59
protected static ImageIcon JavaDoc logoIcon;
60     // Application Title and other stuff. From resource file.
61
protected static String JavaDoc appTitle;
62
63     // The frame of application
64
protected JFrame JavaDoc myFrame;
65
66     protected JMenuBar JavaDoc menubar;
67
68     protected SplashScreen splashScreen;
69     // periodical refreshment
70
protected RefreshingRate refreshingRateInSeconds;
71
72     protected boolean isRefresherActive = false;
73     protected boolean isRefresherEnabled = true;
74
75     protected Thread JavaDoc refresher = new Thread JavaDoc() {
76         public void run() {
77             while (true) {
78                 try {
79                     if (isRefresherEnabled && username != null) {
80                         isRefresherActive = true;
81                         refresh(true);
82                     }
83                 } catch (Exception JavaDoc ex) {
84                 }
85                 isRefresherActive = false;
86                 try {
87                     Thread.sleep(refreshingRateInSeconds.getRefreshingRate() * 1000);
88                 } catch (Exception JavaDoc ex) {
89                     isRefresherActive = false;
90                 }
91             }
92         }
93     };
94
95     protected static SharkInterface workflowServer;
96     protected static SharkConnection workflowService;
97     protected static AdminMisc adminMiscUtilities;
98     protected static CacheAdministration cacheAdmin;
99     protected static DeadlineAdministration deadlineAdmin;
100     protected static ExecutionAdministration execAdmin;
101     protected static LimitAdministration limitAdmin;
102     protected static MappingAdministration mappingAdmin;
103     protected static PackageAdministration pkgAdmin;
104     protected static RepositoryMgr repositoryMgr;
105     protected static UserGroupAdministration userGroupAdmin;
106     protected static CommonExpressionBuilder commonExpressionBuilder;
107
108     protected static String JavaDoc username = null;
109     protected static String JavaDoc pwd = null;
110
111     private static String JavaDoc host;
112     private static String JavaDoc port;
113     private static String JavaDoc workflowServerName;
114
115     protected static ORB JavaDoc orb;
116     public static boolean POA = false;
117
118     public static ORB JavaDoc getORB() {
119         return orb;
120     }
121
122     public static SharkInterface findWorkflowServer(String JavaDoc host, String JavaDoc port,
123                                                     String JavaDoc workflowServerName) {
124         if (workflowServer == null || !host.equals(SharkClient.host) ||
125                 !port.equals(SharkClient.port) || !workflowServerName.equals(workflowServerName)) {
126             try {
127                 SharkClient.host = host.trim();
128                 SharkClient.port = port.trim();
129                 SharkClient.workflowServerName = workflowServerName.trim();
130                 String JavaDoc vers = System.getProperty("java.version");
131                 String JavaDoc[] args = new String JavaDoc[0];
132                 if (vers.compareTo("1.4") < 0) {
133                     args = new String JavaDoc[]{
134                                 "-ORBInitialHost", SharkClient.host, "-ORBInitialPort", SharkClient.port
135                         };
136                 } else
137                     args = new String JavaDoc[]{
138                                 "-ORBInitRef", "NameService=corbaloc::" + SharkClient.host + ":" + SharkClient.port + "/NameService"};
139
140                 // Create and initialize the ORB
141
orb = ORB.init(args, null);
142                 System.out.println("Initializing ORB");
143
144                 try {
145
146                     Class JavaDoc poaHelper = ClassLoader.getSystemClassLoader().loadClass(("org.omg.PortableServer.POAHelper"));
147                     Method JavaDoc m = poaHelper.getDeclaredMethod("narrow", new Class JavaDoc[]{org.omg.CORBA.Object JavaDoc.class});
148                     Object JavaDoc poa = m.invoke(null, new Object JavaDoc[]{orb.resolve_initial_references("RootPOA")});
149                     ((org.omg.PortableServer.POA JavaDoc) poa).the_POAManager().activate();
150                     POA = true;
151                 } catch (Exception JavaDoc e) {
152                     // No POA implementation
153
POA = false;
154                     System.out.println("Using BOA implementation " + e);
155                 }
156
157                 // Get the root naming context
158
org.omg.CORBA.Object JavaDoc objRef = orb.resolve_initial_references("NameService");
159                 NamingContext JavaDoc ncRef = NamingContextHelper.narrow(objRef);
160                 System.out.println("Shark Interface connected to NameService with " + args[1]);
161                 // Resolve the object reference in naming
162
NameComponent JavaDoc nc = new NameComponent JavaDoc(SharkClient.workflowServerName, "");
163                 NameComponent JavaDoc path[] = {nc};
164                 workflowServer = SharkInterfaceHelper.narrow(ncRef.resolve(path));
165                 if (workflowServer != null) System.out.println("connected to " + workflowServerName);
166             } catch (Exception JavaDoc e) {
167                 System.err.println("findWorkflowServer: " + e);
168                 //e.printStackTrace();
169
return null;
170
171             }
172         }
173         return workflowServer;
174     }
175
176     public static AdminMisc getAdminMiscUtilities() {
177         if (adminMiscUtilities == null) {
178             adminMiscUtilities = findWorkflowServer(SharkClient.host, SharkClient.port, SharkClient.workflowServerName).getAdminMisc();
179             try {
180                 adminMiscUtilities.connect(username, pwd, "", "");
181             } catch (Exception JavaDoc ex) {
182             }
183         }
184         return adminMiscUtilities;
185     }
186
187     public static CacheAdministration getCacheAdmin() {
188         if (cacheAdmin == null) {
189             cacheAdmin = findWorkflowServer(SharkClient.host, SharkClient.port, SharkClient.workflowServerName).getCacheAdministration();
190             try {
191                 cacheAdmin.connect(username, pwd, "", "");
192             } catch (Exception JavaDoc ex) {
193             }
194         }
195         return cacheAdmin;
196     }
197
198     public static DeadlineAdministration getDeadlineAdmin() {
199         if (deadlineAdmin == null) {
200             deadlineAdmin = findWorkflowServer(SharkClient.host, SharkClient.port, SharkClient.workflowServerName).getDeadlineAdministration();
201             try {
202                 deadlineAdmin.connect(username, pwd, "", "");
203             } catch (Exception JavaDoc ex) {
204             }
205         }
206         return deadlineAdmin;
207     }
208
209     public static ExecutionAdministration getExecAmin() {
210         if (execAdmin == null) {
211             execAdmin = findWorkflowServer(SharkClient.host, SharkClient.port, SharkClient.workflowServerName).getExecutionAdministration();
212             try {
213                 execAdmin.connect(username, pwd, "", "");
214             } catch (Exception JavaDoc ex) {
215             }
216         }
217         return execAdmin;
218     }
219
220     public static LimitAdministration getLimitAdmin() {
221         if (limitAdmin == null) {
222             limitAdmin = findWorkflowServer(SharkClient.host, SharkClient.port, SharkClient.workflowServerName).getLimitAdministration();
223             try {
224                 limitAdmin.connect(username, pwd, "", "");
225             } catch (Exception JavaDoc ex) {
226             }
227         }
228         return limitAdmin;
229     }
230
231     public static MappingAdministration getMappingAdmin() {
232         if (mappingAdmin == null) {
233             mappingAdmin = findWorkflowServer(SharkClient.host, SharkClient.port, SharkClient.workflowServerName).getMappingAdministration();
234             try {
235                 mappingAdmin.connect(username, pwd, "", "");
236             } catch (Exception JavaDoc ex) {
237             }
238         }
239         return mappingAdmin;
240     }
241
242     public static PackageAdministration getPackageAmin() {
243         if (pkgAdmin == null) {
244             pkgAdmin = findWorkflowServer(SharkClient.host, SharkClient.port, SharkClient.workflowServerName).getPackageAdministration();
245             try {
246                 pkgAdmin.connect(username, pwd, "", "");
247             } catch (Exception JavaDoc ex) {
248             }
249         }
250         return pkgAdmin;
251     }
252
253     public static RepositoryMgr getRepositoryManager() {
254         if (repositoryMgr == null) {
255             repositoryMgr = findWorkflowServer(SharkClient.host, SharkClient.port, SharkClient.workflowServerName).getRepositoryManager();
256             try {
257                 repositoryMgr.connect(username, pwd, "", "");
258             } catch (Exception JavaDoc ex) {
259             }
260         }
261         return repositoryMgr;
262     }
263
264     public static UserGroupAdministration getUserGroupAmin() {
265         if (userGroupAdmin == null) {
266             userGroupAdmin = findWorkflowServer(SharkClient.host, SharkClient.port, SharkClient.workflowServerName).getUserGroupAdministration();
267             try {
268                 userGroupAdmin.connect(username, pwd, "", "");
269             } catch (Exception JavaDoc ex) {
270             }
271         }
272         return userGroupAdmin;
273     }
274
275     public static CommonExpressionBuilder getCommonExpressionBuilder() {
276         if (commonExpressionBuilder == null) {
277             ExecutionAdministration ea = getExecAmin();
278             commonExpressionBuilder = new CommonExpressionBuilder(ea, findWorkflowServer(SharkClient.host, SharkClient.port, SharkClient.workflowServerName).getExpressionBuilderManager());
279         }
280         return commonExpressionBuilder;
281     }
282
283     public boolean isRefresherActive() {
284         return isRefresherActive;
285     }
286
287     public void startRefresher() {
288         isRefresherEnabled = true;
289     }
290
291     public void stopRefresher() {
292         isRefresherEnabled = false;
293     }
294
295     //****************************** DEFAULT INITIALIZATION ***********************
296
private void initResources() {
297         try {
298             String JavaDoc vers = System.getProperty("java.version");
299             if (vers.compareTo("1.4") < 0) {
300                 System.out.println("!!!WARNING: application must be run with a " +
301                                 "1.4 or higher version VM!!!");
302             }
303
304             // application title
305
appTitle = ResourceManager.getLanguageDependentString(getAppTitleResourceString());
306
307             // Logo
308
URL JavaDoc url = ResourceManager.getResource(getLogoResourceString());
309             if (url != null) logoIcon = new ImageIcon JavaDoc(url);
310
311             // Icon
312
url = ResourceManager.getResource(getIconResourceString());
313             if (url != null) appIcon = new ImageIcon JavaDoc(url);
314
315             try {
316                 //UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
317
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
318             } catch (Exception JavaDoc exc) {
319                 System.err.println("Error loading L&F: " + exc);
320             }
321         } catch (Throwable JavaDoc t) {
322             System.err.println("uncaught exception: " + t);
323             t.printStackTrace();
324         }
325     }
326     //*********************** END OF DEFAULT INITIALIZATION ***********************
327

328     //************************** CONSTRUCTOR ***********************
329
/**
330      * Creates instance of main application class.
331      */

332     public SharkClient(JFrame JavaDoc frame) {
333         super();
334         myFrame = frame;
335         splashScreen = new SplashScreen(frame);
336
337         super.init();
338         initResources();
339         int rris;
340         try {
341             rris = Integer.parseInt(ResourceManager.getLanguageDependentString(getRefreshingRateString()));
342         } catch (Exception JavaDoc ex) {
343             rris = 50;
344         }
345         refreshingRateInSeconds = new RefreshingRate(frame, rris);
346
347
348         menubar = BarFactory.createMenubar(getMenubarResourceString(), commands);
349         // adding menubar to the main panel
350
add(menubar, BorderLayout.NORTH);
351         // adding center component
352
add(createCenterComponent(), BorderLayout.CENTER);
353
354         myFrame.setBackground(Color.lightGray);
355         myFrame.getContentPane().setLayout(new BorderLayout JavaDoc());
356         myFrame.getContentPane().add(this, BorderLayout.CENTER);
357         myFrame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
358         myFrame.addWindowListener(new AppCloser());
359         myFrame.pack();
360         // set default location to be centered and size to be almost maximized
361
//Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
362
Rectangle JavaDoc screenSize = GraphicsEnvironment
363         .getLocalGraphicsEnvironment()
364                         .getDefaultScreenDevice()
365                         .getDefaultConfiguration()
366                         .getBounds();
367
368         int xMinus = 50, yMinus = 100;
369         myFrame.setBounds(xMinus / 2, yMinus / 2, screenSize.width - xMinus,
370                 screenSize.height - yMinus);
371
372         if (appIcon != null) myFrame.setIconImage(appIcon.getImage());
373         myFrame.setTitle(appTitle + " - " + ResourceManager.getLanguageDependentString("NoneKey"));
374
375         myFrame.setVisible(true);
376
377         refresher.start();
378         getAction(Utils.getUnqualifiedClassName(Connect.class)).actionPerformed(null);
379         if (username == null) {
380             System.exit(0);
381         }
382         requestFocus();
383     }
384     //************************* END OF WORKLISTHANDLER CONSTRUCTOR *****************
385

386     //************** APPCLOSER CLASS FOR CLOSING APPLICATION WINDOW ***************
387
/**
388      * To shutdown when run as an application.
389      */

390     protected final class AppCloser extends WindowAdapter JavaDoc {
391         public void windowClosing(WindowEvent JavaDoc e) {
392             getAction(Utils.getUnqualifiedClassName(Exit.class)).actionPerformed(null);
393         }
394     }
395     //**** END OF CREATING APPLICATION CLOSER COMPONENT FOR APPLICATION WINDOW ****
396

397     protected void createActions() {
398         defaultActions = new Action JavaDoc[]{
399                     new Connect(this),
400                     new Disconnect(this),
401                     new ShutdownEngine(this),
402                     new Refresh(this),
403                     new SetRefreshingRate(this),
404                     new Exit(this)
405             };
406     }
407
408     public void setTitle() {
409         myFrame.setTitle(appTitle + " - " + getFullUserName());
410     }
411
412     public String JavaDoc getFullUserName() {
413         String JavaDoc name = ResourceManager.getLanguageDependentString("NoneKey");
414         if (username != null) {
415             try {
416                 name = getUserGroupAmin().getUserRealName(username);
417                 if (name == null || name.trim().length() == 0) {
418                     name = username;
419                 }
420             } catch (Exception JavaDoc ex) {
421             }
422         }
423         return name;
424     }
425
426     public JFrame JavaDoc getFrame() {
427         return myFrame;
428     }
429
430     public SplashScreen getSplashScreen() {
431         return splashScreen;
432     }
433
434     public static String JavaDoc getAppTitle() {
435         return appTitle;
436     }
437
438     public static SharkConnection getService() {
439         return workflowService;
440     }
441
442     public static SharkInterface getServer() {
443         return workflowServer;
444     }
445
446     public static void setService(SharkConnection wcs) {
447         workflowService = wcs;
448         if (wcs == null) {
449             adminMiscUtilities = null;
450             cacheAdmin = null;
451             execAdmin = null;
452             mappingAdmin = null;
453             pkgAdmin = null;
454             repositoryMgr = null;
455             userGroupAdmin = null;
456             workflowServer = null;
457             commonExpressionBuilder = null;
458         }
459     }
460
461     public static String JavaDoc getUsername() {
462         return username;
463     }
464
465     public static void setUsername(String JavaDoc username) {
466         SharkClient.username = username;
467     }
468
469     public static void setPassword(String JavaDoc pwd) {
470         SharkClient.pwd = pwd;
471     }
472
473     public void setORB(ORB JavaDoc orb) {
474         this.orb = orb;
475     }
476
477     public void enableControls(boolean enable) {
478         // enable/disable menubar
479
for (int i = 0; i < menubar.getComponentCount(); i++) {
480             menubar.getComponent(i).setEnabled(enable);
481         }
482     }
483
484     public RefreshingRate getRefreshingRate() {
485         return refreshingRateInSeconds;
486     }
487
488     public void onDisconnectOrShutdown() {
489     }
490
491     protected abstract String JavaDoc getAppTitleResourceString();
492
493     protected abstract String JavaDoc getLogoResourceString();
494
495     protected abstract String JavaDoc getIconResourceString();
496
497     protected abstract String JavaDoc getMenubarResourceString();
498
499     protected abstract String JavaDoc getRefreshingRateString();
500
501     public abstract void clearComponents();
502
503     public abstract void refresh(boolean mandatoryRefreshing);
504
505 }
506
507
Popular Tags