1 package org.enhydra.shark.corbaclient; 2 3 import java.awt.BorderLayout ; 4 import java.awt.Color ; 5 import java.awt.GraphicsEnvironment ; 6 import java.awt.Rectangle ; 7 import java.awt.event.WindowAdapter ; 8 import java.awt.event.WindowEvent ; 9 import java.net.URL ; 10 import java.lang.reflect.Method ; 11 import javax.swing.Action ; 12 import javax.swing.ImageIcon ; 13 import javax.swing.JFrame ; 14 import javax.swing.JMenuBar ; 15 import javax.swing.UIManager ; 16 import javax.swing.WindowConstants ; 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 ; 37 import org.omg.CosNaming.NameComponent ; 38 import org.omg.CosNaming.NamingContext ; 39 import org.omg.CosNaming.NamingContextHelper ; 40 41 54 public abstract class SharkClient extends ActionPanel { 55 56 protected static ImageIcon appIcon; 58 protected static ImageIcon logoIcon; 60 protected static String appTitle; 62 63 protected JFrame myFrame; 65 66 protected JMenuBar menubar; 67 68 protected SplashScreen splashScreen; 69 protected RefreshingRate refreshingRateInSeconds; 71 72 protected boolean isRefresherActive = false; 73 protected boolean isRefresherEnabled = true; 74 75 protected Thread refresher = new Thread () { 76 public void run() { 77 while (true) { 78 try { 79 if (isRefresherEnabled && username != null) { 80 isRefresherActive = true; 81 refresh(true); 82 } 83 } catch (Exception ex) { 84 } 85 isRefresherActive = false; 86 try { 87 Thread.sleep(refreshingRateInSeconds.getRefreshingRate() * 1000); 88 } catch (Exception 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 username = null; 109 protected static String pwd = null; 110 111 private static String host; 112 private static String port; 113 private static String workflowServerName; 114 115 protected static ORB orb; 116 public static boolean POA = false; 117 118 public static ORB getORB() { 119 return orb; 120 } 121 122 public static SharkInterface findWorkflowServer(String host, String port, 123 String 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 vers = System.getProperty("java.version"); 131 String [] args = new String [0]; 132 if (vers.compareTo("1.4") < 0) { 133 args = new String []{ 134 "-ORBInitialHost", SharkClient.host, "-ORBInitialPort", SharkClient.port 135 }; 136 } else 137 args = new String []{ 138 "-ORBInitRef", "NameService=corbaloc::" + SharkClient.host + ":" + SharkClient.port + "/NameService"}; 139 140 orb = ORB.init(args, null); 142 System.out.println("Initializing ORB"); 143 144 try { 145 146 Class poaHelper = ClassLoader.getSystemClassLoader().loadClass(("org.omg.PortableServer.POAHelper")); 147 Method m = poaHelper.getDeclaredMethod("narrow", new Class []{org.omg.CORBA.Object .class}); 148 Object poa = m.invoke(null, new Object []{orb.resolve_initial_references("RootPOA")}); 149 ((org.omg.PortableServer.POA ) poa).the_POAManager().activate(); 150 POA = true; 151 } catch (Exception e) { 152 POA = false; 154 System.out.println("Using BOA implementation " + e); 155 } 156 157 org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService"); 159 NamingContext ncRef = NamingContextHelper.narrow(objRef); 160 System.out.println("Shark Interface connected to NameService with " + args[1]); 161 NameComponent nc = new NameComponent (SharkClient.workflowServerName, ""); 163 NameComponent path[] = {nc}; 164 workflowServer = SharkInterfaceHelper.narrow(ncRef.resolve(path)); 165 if (workflowServer != null) System.out.println("connected to " + workflowServerName); 166 } catch (Exception e) { 167 System.err.println("findWorkflowServer: " + e); 168 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 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 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 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 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 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 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 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 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 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 private void initResources() { 297 try { 298 String 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 appTitle = ResourceManager.getLanguageDependentString(getAppTitleResourceString()); 306 307 URL url = ResourceManager.getResource(getLogoResourceString()); 309 if (url != null) logoIcon = new ImageIcon (url); 310 311 url = ResourceManager.getResource(getIconResourceString()); 313 if (url != null) appIcon = new ImageIcon (url); 314 315 try { 316 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 318 } catch (Exception exc) { 319 System.err.println("Error loading L&F: " + exc); 320 } 321 } catch (Throwable t) { 322 System.err.println("uncaught exception: " + t); 323 t.printStackTrace(); 324 } 325 } 326 328 332 public SharkClient(JFrame 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 ex) { 343 rris = 50; 344 } 345 refreshingRateInSeconds = new RefreshingRate(frame, rris); 346 347 348 menubar = BarFactory.createMenubar(getMenubarResourceString(), commands); 349 add(menubar, BorderLayout.NORTH); 351 add(createCenterComponent(), BorderLayout.CENTER); 353 354 myFrame.setBackground(Color.lightGray); 355 myFrame.getContentPane().setLayout(new BorderLayout ()); 356 myFrame.getContentPane().add(this, BorderLayout.CENTER); 357 myFrame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); 358 myFrame.addWindowListener(new AppCloser()); 359 myFrame.pack(); 360 Rectangle 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 386 390 protected final class AppCloser extends WindowAdapter { 391 public void windowClosing(WindowEvent e) { 392 getAction(Utils.getUnqualifiedClassName(Exit.class)).actionPerformed(null); 393 } 394 } 395 397 protected void createActions() { 398 defaultActions = new Action []{ 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 getFullUserName() { 413 String 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 ex) { 421 } 422 } 423 return name; 424 } 425 426 public JFrame getFrame() { 427 return myFrame; 428 } 429 430 public SplashScreen getSplashScreen() { 431 return splashScreen; 432 } 433 434 public static String 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 getUsername() { 462 return username; 463 } 464 465 public static void setUsername(String username) { 466 SharkClient.username = username; 467 } 468 469 public static void setPassword(String pwd) { 470 SharkClient.pwd = pwd; 471 } 472 473 public void setORB(ORB orb) { 474 this.orb = orb; 475 } 476 477 public void enableControls(boolean enable) { 478 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 getAppTitleResourceString(); 492 493 protected abstract String getLogoResourceString(); 494 495 protected abstract String getIconResourceString(); 496 497 protected abstract String getMenubarResourceString(); 498 499 protected abstract String getRefreshingRateString(); 500 501 public abstract void clearComponents(); 502 503 public abstract void refresh(boolean mandatoryRefreshing); 504 505 } 506 507 | Popular Tags |