FileDocCategorySizeDatePackage
ServerManager.javaAPI DocGlassfish v2 API24716Fri May 04 22:35:02 BST 2007com.sun.enterprise.tools.deployment.main

ServerManager

public class ServerManager extends Object
This is the class that handles connections to the J2EE server from client tools.
author
Danny Coward

Fields Summary
private static final String
OMG_ORB_INIT_PORT_PROPERTY
private static final String
OMG_ORB_INIT_HOST_PROPERTY
private static final String
DEFAULT_ORB_INIT_HOST
private static final String
DEFAULT_ORB_INIT_PORT
private static com.sun.enterprise.util.LocalStringManagerImpl
localStrings
public static String
NOTIFICATION_TYPE
public static String
SERVER_LISTENER_ADDED
server listener added/removed
public static String
SERVER_LISTENER_REMOVED
public static String
SERVER_ADDED
server added/removed/selected
public static String
SERVER_REMOVED
public static String
SERVER_SELECTED
public static String
APP_DEPLOYED
application delpoyed/undeployed
public static String
APP_UNDEPLOYED
public static String
SA_DEPLOYED
public static String
SA_UNDEPLOYED
private static String
LOCAL_SERVER
public static String
SERVER_PROPERTY
"localhost";
public static String
LOCAL_HOST
private static String
SERVERS_FILENAME
private File
preferencesDirectory
private Vector
listeners
private Hashtable
serverNameToListenerMap
private String
currentServer
private Context
initialContext
Constructors Summary
public ServerManager(File preferencesDirectory)
Construct a new server manager which restores and saves its state to the given directory.

    
                       
      
    
	this.preferencesDirectory = preferencesDirectory;
    
Methods Summary
public voidaddConnectionFactory(java.lang.String appName, java.lang.String connectorName, java.lang.String jndiName, java.lang.String xaRecoveryUser, java.lang.String xaRecoveryPassword, java.util.Properties props)


        String serverName = getCurrentServer();
        JarInstaller installer = this.getJarInstaller(serverName);
        installer.addConnectionFactory(appName, connectorName,
                                       jndiName, xaRecoveryUser,
                                       xaRecoveryPassword, props);
    
public voidaddNotificationListener(com.sun.enterprise.util.NotificationListener nl)
add a notificationlistsner for server changes.

	listeners.addElement(nl);
	this.changed(SERVER_LISTENER_ADDED, ""); // NOI18N
    
public voidaddServer(java.lang.String serverName)
Connect to a new server by hostnamne

	JarInstaller jarInstaller = this.getJarInstaller(serverName);
	if (serverNameToListenerMap.containsKey(serverName)) {
	    // already present
	    this.setCurrentServer(serverName);
	    this.changed(SERVER_SELECTED, serverName);
	} else {
	    ServerListener serverListener = null;
	    try {
		serverListener = this.createServerListener(serverName);
		jarInstaller.addRemoteNotificationListener(serverListener);
	    } catch (Exception e) {
		System.out.println(localStrings.getLocalString(
                  "enterprise.tools.deployment.main.errorgettingserverlistener",
		  "Error getting server listener"));
	    }
	    serverNameToListenerMap.put(serverName, serverListener);
	    this.setCurrentServer(serverName);
	    this.changed(SERVER_ADDED, serverName);
	}
    
protected voidchanged()
Force an update of listeners.

	Vector listenersClone = null;
	synchronized (listeners) {
	    listenersClone = (Vector)listeners.clone();
	}
	for (Enumeration e = listenersClone.elements(); e.hasMoreElements();) {
	    NotificationListener nl = (NotificationListener) e.nextElement();
	    nl.notification(new NotificationEvent(this, NOTIFICATION_TYPE));
	}
    
protected voidchanged(java.lang.String type, java.lang.String name)

	Vector listenersClone = null;
	synchronized (listeners) {
	    listenersClone = (Vector)listeners.clone();
	}
	NotificationEvent event = new NotificationEvent(this, type, 
            SERVER_PROPERTY, name);
	for (Enumeration e = listenersClone.elements(); e.hasMoreElements();) {
	    NotificationListener nl = (NotificationListener) e.nextElement();
	    nl.notification(event);
	}
	
    
public com.sun.enterprise.tools.deployment.backend.DeploymentSessioncreateDeploymentSession(java.lang.String serverName)
Creates a Session object for listening to and managing deployment progress reports.

	try {
	    DeploymentSession ds = new DeploymentSessionImpl();
	    PortableRemoteObject.exportObject(ds);
	    Tie servantsTie = javax.rmi.CORBA.Util.getTie(ds);
	    servantsTie.orb(ORBManager.getORB());
	    return ds;
	} catch (Throwable t) {
	    throw new ServerException(localStrings.getLocalString(
		"enterprise.tools.deployment.main.couldnotgetorbforserver",
		"Couldn't get orb for ({0}) {1}", 
                new Object[] {"createDeploymentSession",serverName})); // NOI18N
	}
    
private ServerListenercreateServerListener(java.lang.String serverName)

	try {
	    ServerListener listener = new ServerListener(this);
	    PortableRemoteObject.exportObject(listener);
	    Tie servantsTie = javax.rmi.CORBA.Util.getTie(listener);
	    servantsTie.orb(ORBManager.getORB());
	    return listener;
	} catch (Throwable t) {
	    throw new ServerException(localStrings.getLocalString(
		"enterprise.tools.deployment.main.couldnotgetorbforserver",
		"Couldn't get orb for ({0}) {1}", 
		new Object[] {"createCallBack", serverName})); // NOI18N
	}
    
    
    
public voiddeployedApplication(com.sun.enterprise.deployment.Application app)

	this.changed(APP_DEPLOYED, app.getName());
    
public voiddeployedStandAlone(com.sun.enterprise.deployment.Descriptor desc)

	this.changed(SA_DEPLOYED, desc.getName());
    
public java.util.VectorgetApplicationNames()

        return this.getApplicationNamesForServer(getCurrentServer());
     
public java.util.VectorgetApplicationNamesForServer(java.lang.String serverName)
Return a vector of application names.

	Vector v = null;
	if (serverName == null) {
	    return v;
	}
	JarInstaller installer = this.getJarInstaller(serverName);
	if (installer != null) {
	    try {
		v = installer.getApplicationNames();
	    } catch (RemoteException re) {
		throw new ServerException(localStrings.getLocalString(   
     "enterprise.tools.deployment.main.errorgettingappnamefromserverwithreason",
		    "Error obtaining application names from {0} \n reason {1}", 
                    new Object[] {serverName,re.getMessage()}));
	    }
	}
	return v;
     
public java.util.VectorgetConnectionFactories()

        return this.getConnectionFactoriesForServer(getCurrentServer());
    
public java.util.VectorgetConnectionFactoriesForServer(java.lang.String serverName)
Return a vector of connection-factory names.

	Vector v = null;
	if (serverName != null) {
	    JarInstaller installer = this.getJarInstaller(serverName);
	    if (installer != null) {
	        try {
		    v = new Vector();
	    	    ConnectorInfo ci = installer.listConnectors();
	    	    for (int i = 0; i < ci.connectionFactories.length; i++) {
		    	v.add(ci.connectionFactories[i].toString());
                    }
	    	} catch (Exception re) {
		    throw new ServerException(localStrings.getLocalString(   
     "enterprise.tools.deployment.main.errorgettingappnamefromserverwithreason",
		    "Error obtaining application names from {0} \n reason {1}", 
                    new Object[] { serverName, re.toString() }));
	    	}
	    }
	}
	return v;
    
public java.util.SetgetConnectionFactoryPropertyTemplate(java.lang.String appName, java.lang.String connectorName)
Return a set of EnvironmentProperties that represents the configuration properties of a connection factory

param
AppName Name of application (can be null if the adapter is deployed standalone)
param
connectorName Name of resource adapter


        String serverName = getCurrentServer();
        JarInstaller installer = this.getJarInstaller(serverName);
        return installer.getConnectionFactoryPropertyTemplate
            (appName, connectorName);
    
public java.util.VectorgetConnectorNames()

        return this.getConnectorNamesForServer(getCurrentServer());
    
public java.util.VectorgetConnectorNamesForServer(java.lang.String serverName)
Return a vector of connector names.

	Vector v = null;
	if (serverName != null) {
	    JarInstaller installer = this.getJarInstaller(serverName);
	    if (installer != null) {
	        try {
		    v = new Vector();
	    	    ConnectorInfo ci = installer.listConnectors();
	    	    for (int i = 0; i < ci.connectors.length; i++) {
		    	v.add(ci.connectors[i].toString());
                    }
	    	} catch (Exception re) {
		    throw new ServerException(
			localStrings.getLocalString(   
     "enterprise.tools.deployment.main.errorgettingappnamefromserverwithreason",
		    "Error obtaining application names from {0} \n reason {1}", 
                    new Object[] { serverName, re.toString() }));
	    	}
	    }
	}
	return v;
    
public java.lang.StringgetCurrentServer()
Return the name of the current server in this manager.

	if ((this.currentServer != null) && 
            serverNameToListenerMap.containsKey(this.currentServer)) {
	    return currentServer;
	}
	return null;
    
public com.sun.ejb.sqlgen.DBInfogetDBInfo(java.lang.String serverName)
Return information about the database under the given server.

	try {
	    if ( serverName.equalsIgnoreCase("local") ) // NOI18N
		serverName = "localhost"; // NOI18N

            String initialPort = System.getProperty(OMG_ORB_INIT_PORT_PROPERTY);
            if (initialPort == null)
                initialPort = String.valueOf(ORBManager.getORBInitialPort());
            
	    String corbaName = "corbaname:iiop:" + serverName + ":" + // NOI18N
                initialPort + "#" + DBInfo.JNDI_NAME; // NOI18N

            Object objref  = getIC().lookup(corbaName);
            Object o = PortableRemoteObject.narrow(objref, DBInfo.class);
	    DBInfo info = (DBInfo) o;
	    return info;
	} catch (Throwable t) {
	    throw new ServerException(localStrings.getLocalString(
		"enterprise.tools.deployment.main.couldnotgetdbinfofromserver",
		"Could not get db info from the J2EE server {0}", 
		new Object[] {serverName}));  
	}
    
    
private javax.naming.ContextgetIC()

	if ( initialContext == null ) {
	    Hashtable env = new Hashtable();
	    env.put("java.naming.corba.orb", ORBManager.getORB()); // NOI18N
	    try {
		initialContext = new InitialContext(env);
	    } catch ( Exception ex ) {
		ex.printStackTrace();
	    }
	}
	return initialContext;
    
private com.sun.enterprise.tools.deployment.backend.JarInstallergetJarInstaller(java.lang.String serverName)

	try {
	    if ( serverName.equalsIgnoreCase("local") ) // NOI18N
		serverName = "localhost"; // NOI18N

            String initialPort = System.getProperty(OMG_ORB_INIT_PORT_PROPERTY);
            if (initialPort == null)
                initialPort = String.valueOf(ORBManager.getORBInitialPort());

            String corbaName = "corbaname:iiop:" + serverName + ":" + // NOI18N
                initialPort + "#" + JarInstaller.JNDI_NAME; // NOI18N

            /* IASRI 4691307 commented out by Anissa.
             * The following code throws exception from the ORBManager and since we don't need any server
             * connection for AT, we will not perform this step.
             *
            Object objref  = getIC().lookup(corbaName);
            Object o = PortableRemoteObject.narrow(objref, JarInstaller.class);
	    JarInstaller installer = (JarInstaller) o;
	    return installer;
            * end of IASRI 4691307
             */
            
	    throw new ServerException(""); //NOI18N 
            
	} catch (Throwable t) {
	    String msg = localStrings.getLocalString(
		"enterprise.tools.deployment.main.couldnotconnecttoserver",
                "Couldn''t connect to {0}", 
		new Object[] { serverName });
	    //System.err.println(msg);  // IASRI 4691307
	    //UIUtils.printException(msg, t);
	    throw new ServerException(msg);  
	}
    
public com.sun.enterprise.tools.deployment.backend.JarInstallergetServerForName(java.lang.String serverName)
Return a server by name. return null if there is no server of that name.

	return this.getJarInstaller(serverName);
    
public java.util.VectorgetServerNames()
Returns a list of server names.

	Vector v = new Vector();
	for (Enumeration e = this.serverNameToListenerMap.keys(); 
            e.hasMoreElements();) {
	    v.addElement(e.nextElement());
	}
	return v;
    
public booleanisInstalled(java.lang.String applicationName, java.lang.String serverName)

	JarInstaller installer = this.getJarInstaller(serverName);
	if (installer != null) {
	    try {
		Vector applicationNames = installer.getApplicationNames();
		for (int i = 0; i < applicationNames.size(); i++) {
		    if (applicationName.equals(applicationNames.elementAt(i))) {
			return true;
		    }
		}
	    } catch (Throwable t) {
		throw new ServerException(localStrings.getLocalString(
	   "enterprise.tools.deployment.main.couldnotapplicationlistfromserver",
		    "Couldn't get application list from {0}", 
		    new Object[] {serverName}));
	    }
	}
	return false;
    
public java.util.SetlistConnectorResources()

        String serverName = getCurrentServer();
        JarInstaller installer = this.getJarInstaller(serverName);
        Set res = installer.listConnectorResources();
        return res;
    
private java.lang.StringprintList()
My pretty format.

	String s = "Server Manager "; // NOI18N
	for (Enumeration e = this.getServerNames().elements(); 
	    e.hasMoreElements();) {
	    s = s + "\n\t" + e.nextElement(); // NOI18N
	}
	return s;
    
public voidremoveConnectionFactory(java.lang.String jndiName)


        String serverName = getCurrentServer();
        JarInstaller installer = this.getJarInstaller(serverName);
        installer.removeConnectionFactory(jndiName);
    
public voidremoveNotificationListener(com.sun.enterprise.util.NotificationListener nl)
removes a notificationlistsner for server changes.

	this.listeners.removeElement(nl);
	this.changed(SERVER_LISTENER_REMOVED, ""); // NOI18N
    
public voidremoveServer(java.lang.String hostName)
Disconnect from new server by hostname

	if (serverNameToListenerMap.containsKey(hostName)) {
	    ServerListener serverListener = 
                (ServerListener)serverNameToListenerMap.get(hostName);
	    try {
		serverNameToListenerMap.remove(hostName);
		if ((this.getCurrentServer() != null) && 
                    this.getCurrentServer().equals(hostName)) {
		    this.currentServer = null;
		}
		this.changed(SERVER_REMOVED, hostName);
		JarInstaller jarInstaller = this.getJarInstaller(hostName);
		jarInstaller.removeRemoteNotificationListener(serverListener);
	    } catch (Exception e) {
		// leave for debug
		//System.out.println("Error removing notification listener from server");
	    } 
	}
    
public java.util.HashtablerestoreFromUserHome()
Reconnect to all the servers I knew about in the last session.

	Hashtable badServerNamesToExceptions = new Hashtable();
	File serversFile = new File(preferencesDirectory, SERVERS_FILENAME);
	if (serversFile.exists()) {
	    FileInputStream fis = new FileInputStream(serversFile);
	    Properties servers = new Properties();
	    servers.load(fis);  
	    fis.close();
	    for (Enumeration e = servers.propertyNames(); 
		e.hasMoreElements();) {
		String serverName = (String) e.nextElement();
		try {
		    this.addServer(serverName);  
		} catch (Throwable ex) {
		    badServerNamesToExceptions.put(badServerNamesToExceptions, 
			ex);
		}
	    }
	}
	return badServerNamesToExceptions;
    
public voidsaveToUserHome()
Save my current state to my directory.

	File serversFile = new File(preferencesDirectory, SERVERS_FILENAME);	
	FileOutputStream fos = new FileOutputStream(serversFile);
	Properties serversP = new Properties();
	for (Enumeration e = this.getServerNames().elements(); 
	    e.hasMoreElements();) {
	    String nextServer = (String) e.nextElement();
	    serversP.put(nextServer, nextServer);
	}
	serversP.store(fos, "J2EE Servers"); // NOI18N
        if( fos != null ) {
            fos.close();
        }
    
public voidsetCurrentServer(java.lang.String serverName)
Set the name of the current server in this manager.

	this.currentServer = serverName;
	String notificationString = ""; // NOI18N
	if (serverName != null) {
	    notificationString = serverName;
	}
	this.changed(SERVER_SELECTED, notificationString);
    
public java.lang.StringtoString()
My pretty format as a STring.

	return "ServerManager"; // NOI18N
    
public voidundeployApplication(java.lang.String applicationName, java.lang.String serverName)
The undeploy method.

	JarInstaller installer = this.getJarInstaller(serverName);
	try {
	    installer.undeployApplication(applicationName);
	    this.changed(APP_UNDEPLOYED, applicationName);
	} catch (Throwable t) { 
	    throw new ServerException(localStrings.getLocalString(
      "enterprise.tools.deployment.main.erroruninstallingapplicationfromserver",
		"Error uninstalling {0} from {1}", 
                new Object[] {applicationName, serverName}));
	}
	
    
public voidundeployApplication(java.lang.Object[] appList, java.lang.String serverName)

	for (int i = 0; i < appList.length; i++) {
	    if (appList[i] instanceof String) {
	        this.undeployApplication((String)appList[i], serverName);
	    } else {
		// XXX this should never occur
	    }
	}
    
public voidundeployConnector(java.lang.String rarName, java.lang.String serverName)

	JarInstaller installer = this.getJarInstaller(serverName);
	try {
	    installer.undeployConnector(rarName);
	    this.changed(SA_UNDEPLOYED, rarName);
	} catch (Throwable t) { 
	    throw new ServerException(localStrings.getLocalString(
      "enterprise.tools.deployment.main.erroruninstallingapplicationfromserver",
		"Error uninstalling {0} from {1}", 
                new Object[] { rarName, serverName }));
	}
    
public voidundeployConnector(java.lang.Object[] rarList, java.lang.String serverName)

	for (int i = 0; i < rarList.length; i++) {
	    if (rarList[i] instanceof String) {
	        this.undeployConnector((String)rarList[i], serverName);
	    } else {
		// XXX this should never occur
	    }
	}