1 23 package com.sun.enterprise.tools.deployment.main; 24 25 import java.rmi.RemoteException ; 26 import java.io.*; 27 import java.util.Hashtable ; 28 import java.util.Vector ; 29 import java.util.Enumeration ; 30 import java.util.Properties ; 31 import java.util.Set ; 32 import java.net.*; 33 34 import org.omg.CORBA.ORB ; 35 import javax.rmi.CORBA.Tie ; 36 import javax.rmi.PortableRemoteObject ; 37 import javax.naming.InitialContext ; 38 import javax.naming.Context ; 39 40 import com.sun.enterprise.util.NotificationListener; 41 import com.sun.enterprise.util.NotificationEvent; 42 import com.sun.enterprise.util.ORBManager; 43 import com.sun.ejb.sqlgen.DBInfo; 44 45 import com.sun.enterprise.tools.deployment.backend.JarInstaller; 46 import com.sun.enterprise.tools.deployment.backend.DeploymentSession; 47 import com.sun.enterprise.tools.deployment.backend.DeploymentSessionImpl; 48 import com.sun.enterprise.util.LocalStringManagerImpl; 49 import com.sun.enterprise.deployment.Application; 50 import com.sun.enterprise.deployment.Descriptor; 51 import com.sun.enterprise.resource.ConnectorInfo; 52 import com.sun.enterprise.resource.PoolingException; 53 54 56 60 61 public class ServerManager 62 { 63 private static final String OMG_ORB_INIT_PORT_PROPERTY = 64 "org.omg.CORBA.ORBInitialPort"; private static final String OMG_ORB_INIT_HOST_PROPERTY = 66 "org.omg.CORBA.ORBInitialHost"; private static final String DEFAULT_ORB_INIT_HOST = "localhost"; private static final String DEFAULT_ORB_INIT_PORT = "1050"; 70 73 74 private static LocalStringManagerImpl localStrings = 75 new LocalStringManagerImpl(ServerManager.class); 76 77 80 81 82 public static String NOTIFICATION_TYPE = "ServerManager"; 84 85 public static String SERVER_LISTENER_ADDED = "serverListenerAdded"; public static String SERVER_LISTENER_REMOVED = "serverListenerRemoved"; 88 89 public static String SERVER_ADDED = "addServer"; public static String SERVER_REMOVED = "removeServer"; public static String SERVER_SELECTED = "setCurrentServer"; 93 94 public static String APP_DEPLOYED = "deployedApplication"; public static String APP_UNDEPLOYED = "undeployedApplication"; public static String SA_DEPLOYED = "deployedApplication"; public static String SA_UNDEPLOYED = "undeployedStandAlone"; 99 101 102 private static String LOCAL_SERVER = "local"; 105 public static String SERVER_PROPERTY = "name"; 107 public static String LOCAL_HOST = "localhost"; private static String SERVERS_FILENAME = "servers"; 110 private File preferencesDirectory; 111 private Vector listeners = new Vector (); 112 private Hashtable serverNameToListenerMap = new Hashtable (); 113 private String currentServer; 114 private Context initialContext; 115 116 119 public ServerManager(File preferencesDirectory) 120 { 121 this.preferencesDirectory = preferencesDirectory; 122 } 123 124 127 128 145 146 public void undeployApplication(String applicationName, String serverName) 147 throws ServerException 148 { 149 JarInstaller installer = this.getJarInstaller(serverName); 150 try { 151 installer.undeployApplication(applicationName); 152 this.changed(APP_UNDEPLOYED, applicationName); 153 } catch (Throwable t) { 154 throw new ServerException(localStrings.getLocalString( 155 "enterprise.tools.deployment.main.erroruninstallingapplicationfromserver", 156 "Error uninstalling {0} from {1}", 157 new Object [] {applicationName, serverName})); 158 } 159 160 } 161 162 public void undeployApplication(Object appList[], String serverName) 163 throws ServerException 164 { 165 for (int i = 0; i < appList.length; i++) { 166 if (appList[i] instanceof String ) { 167 this.undeployApplication((String )appList[i], serverName); 168 } else { 169 } 171 } 172 } 173 174 175 public void deployedApplication(Application app) 176 { 177 this.changed(APP_DEPLOYED, app.getName()); 178 } 179 180 183 184 public void undeployConnector(String rarName, String serverName) 185 throws ServerException 186 { 187 JarInstaller installer = this.getJarInstaller(serverName); 188 try { 189 installer.undeployConnector(rarName); 190 this.changed(SA_UNDEPLOYED, rarName); 191 } catch (Throwable t) { 192 throw new ServerException(localStrings.getLocalString( 193 "enterprise.tools.deployment.main.erroruninstallingapplicationfromserver", 194 "Error uninstalling {0} from {1}", 195 new Object [] { rarName, serverName })); 196 } 197 } 198 199 public void undeployConnector(Object rarList[], String serverName) 200 throws ServerException 201 { 202 for (int i = 0; i < rarList.length; i++) { 203 if (rarList[i] instanceof String ) { 204 this.undeployConnector((String )rarList[i], serverName); 205 } else { 206 } 208 } 209 } 210 211 212 public void deployedStandAlone(Descriptor desc) 213 { 214 this.changed(SA_DEPLOYED, desc.getName()); 215 } 216 217 220 221 222 public void addServer(String serverName) 223 throws ServerException 224 { 225 JarInstaller jarInstaller = this.getJarInstaller(serverName); 226 if (serverNameToListenerMap.containsKey(serverName)) { 227 this.setCurrentServer(serverName); 229 this.changed(SERVER_SELECTED, serverName); 230 } else { 231 ServerListener serverListener = null; 232 try { 233 serverListener = this.createServerListener(serverName); 234 jarInstaller.addRemoteNotificationListener(serverListener); 235 } catch (Exception e) { 236 System.out.println(localStrings.getLocalString( 237 "enterprise.tools.deployment.main.errorgettingserverlistener", 238 "Error getting server listener")); 239 } 240 serverNameToListenerMap.put(serverName, serverListener); 241 this.setCurrentServer(serverName); 242 this.changed(SERVER_ADDED, serverName); 243 } 244 } 245 246 247 public void removeServer(String hostName) 248 { 249 if (serverNameToListenerMap.containsKey(hostName)) { 250 ServerListener serverListener = 251 (ServerListener)serverNameToListenerMap.get(hostName); 252 try { 253 serverNameToListenerMap.remove(hostName); 254 if ((this.getCurrentServer() != null) && 255 this.getCurrentServer().equals(hostName)) { 256 this.currentServer = null; 257 } 258 this.changed(SERVER_REMOVED, hostName); 259 JarInstaller jarInstaller = this.getJarInstaller(hostName); 260 jarInstaller.removeRemoteNotificationListener(serverListener); 261 } catch (Exception e) { 262 } 265 } 266 } 267 268 269 public String getCurrentServer() 270 { 271 if ((this.currentServer != null) && 272 serverNameToListenerMap.containsKey(this.currentServer)) { 273 return currentServer; 274 } 275 return null; 276 } 277 278 279 public void setCurrentServer(String serverName) 280 { 281 this.currentServer = serverName; 282 String notificationString = ""; if (serverName != null) { 284 notificationString = serverName; 285 } 286 this.changed(SERVER_SELECTED, notificationString); 287 } 288 289 290 291 public Vector getServerNames() 292 { 293 Vector v = new Vector (); 294 for (Enumeration e = this.serverNameToListenerMap.keys(); 295 e.hasMoreElements();) { 296 v.addElement(e.nextElement()); 297 } 298 return v; 299 } 300 301 304 305 306 public void addNotificationListener(NotificationListener nl) 307 { 308 listeners.addElement(nl); 309 this.changed(SERVER_LISTENER_ADDED, ""); } 311 312 313 public void removeNotificationListener(NotificationListener nl) 314 { 315 this.listeners.removeElement(nl); 316 this.changed(SERVER_LISTENER_REMOVED, ""); } 318 319 320 protected void changed() 321 { 322 Vector listenersClone = null; 323 synchronized (listeners) { 324 listenersClone = (Vector )listeners.clone(); 325 } 326 for (Enumeration e = listenersClone.elements(); e.hasMoreElements();) { 327 NotificationListener nl = (NotificationListener) e.nextElement(); 328 nl.notification(new NotificationEvent(this, NOTIFICATION_TYPE)); 329 } 330 } 331 332 protected void changed(String type, String name) 333 { 334 Vector listenersClone = null; 335 synchronized (listeners) { 336 listenersClone = (Vector )listeners.clone(); 337 } 338 NotificationEvent event = new NotificationEvent(this, type, 339 SERVER_PROPERTY, name); 340 for (Enumeration e = listenersClone.elements(); e.hasMoreElements();) { 341 NotificationListener nl = (NotificationListener) e.nextElement(); 342 nl.notification(event); 343 } 344 345 } 346 347 350 351 355 public DeploymentSession createDeploymentSession(String serverName) 356 throws Exception 357 { 358 try { 359 DeploymentSession ds = new DeploymentSessionImpl(); 360 PortableRemoteObject.exportObject(ds); 361 Tie servantsTie = javax.rmi.CORBA.Util.getTie(ds); 362 servantsTie.orb(ORBManager.getORB()); 363 return ds; 364 } catch (Throwable t) { 365 throw new ServerException(localStrings.getLocalString( 366 "enterprise.tools.deployment.main.couldnotgetorbforserver", 367 "Couldn't get orb for ({0}) {1}", 368 new Object [] {"createDeploymentSession",serverName})); } 370 } 371 372 private ServerListener createServerListener(String serverName) 373 throws Exception 374 { 375 try { 376 ServerListener listener = new ServerListener(this); 377 PortableRemoteObject.exportObject(listener); 378 Tie servantsTie = javax.rmi.CORBA.Util.getTie(listener); 379 servantsTie.orb(ORBManager.getORB()); 380 return listener; 381 } catch (Throwable t) { 382 throw new ServerException(localStrings.getLocalString( 383 "enterprise.tools.deployment.main.couldnotgetorbforserver", 384 "Couldn't get orb for ({0}) {1}", 385 new Object [] {"createCallBack", serverName})); } 387 388 389 } 390 391 public boolean isInstalled(String applicationName, String serverName) 392 throws ServerException 393 { 394 JarInstaller installer = this.getJarInstaller(serverName); 395 if (installer != null) { 396 try { 397 Vector applicationNames = installer.getApplicationNames(); 398 for (int i = 0; i < applicationNames.size(); i++) { 399 if (applicationName.equals(applicationNames.elementAt(i))) { 400 return true; 401 } 402 } 403 } catch (Throwable t) { 404 throw new ServerException(localStrings.getLocalString( 405 "enterprise.tools.deployment.main.couldnotapplicationlistfromserver", 406 "Couldn't get application list from {0}", 407 new Object [] {serverName})); 408 } 409 } 410 return false; 411 } 412 413 416 417 public Vector getApplicationNamesForServer(String serverName) 418 throws ServerException 419 { 420 Vector v = null; 421 if (serverName == null) { 422 return v; 423 } 424 JarInstaller installer = this.getJarInstaller(serverName); 425 if (installer != null) { 426 try { 427 v = installer.getApplicationNames(); 428 } catch (RemoteException re) { 429 throw new ServerException(localStrings.getLocalString( 430 "enterprise.tools.deployment.main.errorgettingappnamefromserverwithreason", 431 "Error obtaining application names from {0} \n reason {1}", 432 new Object [] {serverName,re.getMessage()})); 433 } 434 } 435 return v; 436 } 437 438 public Vector getApplicationNames() 439 throws ServerException 440 { 441 return this.getApplicationNamesForServer(getCurrentServer()); 442 } 443 444 447 448 public Vector getConnectorNamesForServer(String serverName) 449 throws ServerException 450 { 451 Vector v = null; 452 if (serverName != null) { 453 JarInstaller installer = this.getJarInstaller(serverName); 454 if (installer != null) { 455 try { 456 v = new Vector (); 457 ConnectorInfo ci = installer.listConnectors(); 458 for (int i = 0; i < ci.connectors.length; i++) { 459 v.add(ci.connectors[i].toString()); 460 } 461 } catch (Exception re) { 462 throw new ServerException( 463 localStrings.getLocalString( 464 "enterprise.tools.deployment.main.errorgettingappnamefromserverwithreason", 465 "Error obtaining application names from {0} \n reason {1}", 466 new Object [] { serverName, re.toString() })); 467 } 468 } 469 } 470 return v; 471 } 472 473 public Vector getConnectorNames() 474 throws ServerException 475 { 476 return this.getConnectorNamesForServer(getCurrentServer()); 477 } 478 479 482 483 public Vector getConnectionFactoriesForServer(String serverName) 484 throws ServerException 485 { 486 Vector v = null; 487 if (serverName != null) { 488 JarInstaller installer = this.getJarInstaller(serverName); 489 if (installer != null) { 490 try { 491 v = new Vector (); 492 ConnectorInfo ci = installer.listConnectors(); 493 for (int i = 0; i < ci.connectionFactories.length; i++) { 494 v.add(ci.connectionFactories[i].toString()); 495 } 496 } catch (Exception re) { 497 throw new ServerException(localStrings.getLocalString( 498 "enterprise.tools.deployment.main.errorgettingappnamefromserverwithreason", 499 "Error obtaining application names from {0} \n reason {1}", 500 new Object [] { serverName, re.toString() })); 501 } 502 } 503 } 504 return v; 505 } 506 507 public Vector getConnectionFactories() 508 throws ServerException 509 { 510 return this.getConnectionFactoriesForServer(getCurrentServer()); 511 } 512 513 520 public Set getConnectionFactoryPropertyTemplate(String appName, 521 String connectorName) 522 throws RemoteException , PoolingException, ServerException { 523 524 String serverName = getCurrentServer(); 525 JarInstaller installer = this.getJarInstaller(serverName); 526 return installer.getConnectionFactoryPropertyTemplate 527 (appName, connectorName); 528 } 529 530 531 532 public void addConnectionFactory(String appName, 533 String connectorName, 534 String jndiName, 535 String xaRecoveryUser, 536 String xaRecoveryPassword, 537 Properties props) 538 throws RemoteException , PoolingException, ServerException { 539 540 String serverName = getCurrentServer(); 541 JarInstaller installer = this.getJarInstaller(serverName); 542 installer.addConnectionFactory(appName, connectorName, 543 jndiName, xaRecoveryUser, 544 xaRecoveryPassword, props); 545 } 546 547 public void removeConnectionFactory(String jndiName) 548 throws RemoteException , PoolingException, ServerException { 549 550 String serverName = getCurrentServer(); 551 JarInstaller installer = this.getJarInstaller(serverName); 552 installer.removeConnectionFactory(jndiName); 553 } 554 555 public Set listConnectorResources() 556 throws RemoteException , ServerException { 557 String serverName = getCurrentServer(); 558 JarInstaller installer = this.getJarInstaller(serverName); 559 Set res = installer.listConnectorResources(); 560 return res; 561 } 562 563 567 public JarInstaller getServerForName(String serverName) 568 throws ServerException 569 { 570 return this.getJarInstaller(serverName); 571 } 572 573 private JarInstaller getJarInstaller(String serverName) 574 throws ServerException 575 { 576 try { 577 if ( serverName.equalsIgnoreCase("local") ) serverName = "localhost"; 580 String initialPort = System.getProperty(OMG_ORB_INIT_PORT_PROPERTY); 581 if (initialPort == null) 582 initialPort = String.valueOf(ORBManager.getORBInitialPort()); 583 584 String corbaName = "corbaname:iiop:" + serverName + ":" + initialPort + "#" + JarInstaller.JNDI_NAME; 587 597 598 throw new ServerException(""); 600 } catch (Throwable t) { 601 String msg = localStrings.getLocalString( 602 "enterprise.tools.deployment.main.couldnotconnecttoserver", 603 "Couldn''t connect to {0}", 604 new Object [] { serverName }); 605 throw new ServerException(msg); 608 } 609 } 610 611 private Context getIC() 612 { 613 if ( initialContext == null ) { 614 Hashtable env = new Hashtable (); 615 env.put("java.naming.corba.orb", ORBManager.getORB()); try { 617 initialContext = new InitialContext (env); 618 } catch ( Exception ex ) { 619 ex.printStackTrace(); 620 } 621 } 622 return initialContext; 623 } 624 625 626 public DBInfo getDBInfo(String serverName) 627 throws ServerException 628 { 629 try { 630 if ( serverName.equalsIgnoreCase("local") ) serverName = "localhost"; 633 String initialPort = System.getProperty(OMG_ORB_INIT_PORT_PROPERTY); 634 if (initialPort == null) 635 initialPort = String.valueOf(ORBManager.getORBInitialPort()); 636 637 String corbaName = "corbaname:iiop:" + serverName + ":" + initialPort + "#" + DBInfo.JNDI_NAME; 640 Object objref = getIC().lookup(corbaName); 641 Object o = PortableRemoteObject.narrow(objref, DBInfo.class); 642 DBInfo info = (DBInfo) o; 643 return info; 644 } catch (Throwable t) { 645 throw new ServerException(localStrings.getLocalString( 646 "enterprise.tools.deployment.main.couldnotgetdbinfofromserver", 647 "Could not get db info from the J2EE server {0}", 648 new Object [] {serverName})); 649 } 650 651 } 652 653 654 public Hashtable restoreFromUserHome() 655 throws IOException 656 { 657 Hashtable badServerNamesToExceptions = new Hashtable (); 658 File serversFile = new File(preferencesDirectory, SERVERS_FILENAME); 659 if (serversFile.exists()) { 660 FileInputStream fis = new FileInputStream(serversFile); 661 Properties servers = new Properties (); 662 servers.load(fis); 663 fis.close(); 664 for (Enumeration e = servers.propertyNames(); 665 e.hasMoreElements();) { 666 String serverName = (String ) e.nextElement(); 667 try { 668 this.addServer(serverName); 669 } catch (Throwable ex) { 670 badServerNamesToExceptions.put(badServerNamesToExceptions, 671 ex); 672 } 673 } 674 } 675 return badServerNamesToExceptions; 676 } 677 678 679 public void saveToUserHome() 680 throws IOException 681 { 682 File serversFile = new File(preferencesDirectory, SERVERS_FILENAME); 683 FileOutputStream fos = new FileOutputStream(serversFile); 684 Properties serversP = new Properties (); 685 for (Enumeration e = this.getServerNames().elements(); 686 e.hasMoreElements();) { 687 String nextServer = (String ) e.nextElement(); 688 serversP.put(nextServer, nextServer); 689 } 690 serversP.store(fos, "J2EE Servers"); if( fos != null ) { 692 fos.close(); 693 } 694 } 695 696 697 private String printList() 698 { 699 String s = "Server Manager "; for (Enumeration e = this.getServerNames().elements(); 701 e.hasMoreElements();) { 702 s = s + "\n\t" + e.nextElement(); } 704 return s; 705 } 706 707 708 public String toString() 709 { 710 return "ServerManager"; } 712 713 } 714 715 | Popular Tags |