FileDocCategorySizeDatePackage
DeployTool.javaAPI DocGlassfish v2 API17051Fri May 04 22:35:00 BST 2007None

DeployTool

public class DeployTool extends Object
The master object of the J2EE deployment tool.
author
Danny Coward

Fields Summary
private ApplicationManager
applicationManager
private StandAloneManager
standaloneManager
private ServerManager
serverManager
private com.sun.enterprise.tools.packager.ComponentPackager
componentPackager
private File
workingDirectory
private File
toolHomeDirectory
private static com.sun.enterprise.util.LocalStringManagerImpl
localStrings
public static final String
HOME_DIR
private static DeployTool
deployToolInstance
public static final String
UI_STARTUP_CLASS
protected UIFactory
uiFactory
private String
userdir
Constructors Summary
public DeployTool(boolean hasUI, String dir)
Construct a new deploytool indicating whether this tool has a UI or not.

                       
    
    //add an method for specifying userdir.  IASRI 4691307 
        
        userdir = dir;
        _deploytool(hasUI);
    
public DeployTool(boolean hasUI)

        _deploytool(hasUI);
    
Methods Summary
private void_deploytool(boolean hasUI)

        
	deployToolInstance = this;

        //If user specify userdir, will set that to UIConfig so that getConfigDirectory() will return that.
        // IASRI 4691307
        /* cannot do this because server.xml excludes ui directory. Will cause problem to build.
        if (userdir != null){
                UIConfig.setUserDir(userdir);
        }
         **/
	this.uiFactory = createUIFactory(hasUI);
	
        if (this.uiFactory != null){
	    /* ui */
            // IASRI 4691307
            // If the server build.xml include the ui directory, we won't need to test userdir here.
            // just call uiFactory.getConfigDirectory();
            File cfgDir = null;
            if (userdir == null){
                cfgDir = this.uiFactory.getConfigDirectory();
            }else{
                cfgDir = new File(userdir);
                cfgDir.mkdirs();
            }
	    File tmpDir = this.uiFactory.getTempDirectory();
            System.out.println("user directory = " + cfgDir);    // IASRI 4691307 info user. // NOI18N
            System.out.println("temp directory = " + tmpDir);    // IASRI 4691307 info user. // NOI18N
	    this.applicationManager = new ApplicationManager(cfgDir, tmpDir);
	    this.standaloneManager  = new StandAloneManager(cfgDir, tmpDir);
	    this.serverManager      = new ServerManager(cfgDir);
	    this.uiFactory.startUI();

	} else {

	    /* non-ui */
	    File toolDir = this.getToolHomeDirectory();
	    File tempDir = this.getWorkingDirectory();
            System.out.println("user directory = " + toolDir);   // IASRI 4691307 info user. // NOI18N
            System.out.println("temp directory = " + tempDir);   // IASRI 4691307 info user. // NOI18N
	    this.applicationManager = new ApplicationManager(toolDir, tempDir);
	    this.standaloneManager  = new StandAloneManager(toolDir, tempDir);
	    this.serverManager      = new ServerManager(toolDir);

	}

    
protected static UIFactorycreateUIFactory(boolean hasUI)


	if (hasUI) {

 	    try {

	    	/* locate the class (make sure it's a UIFactory) */
	    	Class uiFactoryClass = Class.forName(UI_STARTUP_CLASS);
	    	if (!UIFactory.class.isAssignableFrom(uiFactoryClass)) {
	    	    throw new ClassCastException("Class does not implement UIFactory"); // NOI18N
	    	}

	    	/* instantiate/initialize ui */
  	    	return (UIFactory)uiFactoryClass.newInstance();

	    } catch (Throwable t) {

	    	System.out.println(localStrings.getLocalString(
		    "enterprise.tools.deployment.main.error_creating_ui",
		    "Unable to create UI: " + t));

	    }

	}

	return null;

    
public voiddeploy(Application application, java.lang.String serverName, com.sun.enterprise.tools.deployment.backend.DeploymentSession deploymentSession, java.io.File clientCodeFile)

        deploy(application.getName(), application.getApplicationArchivist().getApplicationFile(), serverName, deploymentSession, clientCodeFile);   
    
public voiddeploy(java.lang.String applicationName, java.io.File appArchiveFile, java.lang.String serverName, com.sun.enterprise.tools.deployment.backend.DeploymentSession deploymentSession, java.io.File clientCodeFile)

            
	System.out.println(localStrings.getLocalString(
		"enterprise.tools.deployment.main.deployapplicationfileonserversaveasclientjar",
		"Deploy the application in {0} on the server {1} saving the client jar as {2}", 
		new Object[] { appArchiveFile, serverName, clientCodeFile }));
	Object[] msg = { applicationName, serverName };
	// byte[] clientCode = null;
	String clientCode = null;
	Log.print(this,localStrings.getLocalString(
		"enterprise.tools.deployment.main.deploytool.deploy_command",
		"Deploy {0} on {1}", msg));
	JarInstaller backend = this.getServerManager().getServerForName(serverName);
	DeploymentSession deploymentSessionToUse = null;
	if (deploymentSession == null) {
	    deploymentSessionToUse = this.getServerManager().createDeploymentSession(serverName);
	} else {
	    deploymentSessionToUse = deploymentSession;
	}
	
	FileInputStream fis = new FileInputStream(appArchiveFile);
	DataInputStream dis = new DataInputStream(fis);
	byte[] jarData = new byte[(int) appArchiveFile.length()];
	dis.readFully(jarData);
	dis.close();
	fis.close();
	clientCode = backend.deployApplication(jarData, applicationName, deploymentSessionToUse);
	Log.print(this, localStrings.getLocalString(
						    "enterprise.tools.deployment.main.clientcodeat",
						    "client code at {0}", new Object[] {clientCode}));
	if (clientCode != null && clientCodeFile != null) {
	    writeClientJarToFile(clientCode, clientCodeFile);
	    deploymentSessionToUse.notification(new NotificationEvent(this, DeploymentSession.CLIENT_CODE_RETURNED, this));
	    deploymentSessionToUse.setStatusMessage(localStrings.getLocalString(
										"enterprise.tools.deployment.main.clientcodefordeployedapplicationsavedtofile",
										"Client code for the deployed application {0} saved to {1}", new Object[] {applicationName, clientCodeFile}));
	}
    
public ApplicationdeployStandaloneModule(java.io.File moduleFile)
Return a generated Application object given a module file


        // derive an app name
        File tmpDir = FileUtil.getTempDirectory();
        String appName = "app." + moduleFile.getName().replace('.", '_"); // NOI18N
	File appFile = new File(tmpDir, appName + ".ear"); // NOI18N
        int i = 0;
        while (appFile.exists()) {
            i++;
	    appFile = new File(tmpDir, appName + "_" + i + ".ear"); // NOI18N
	}
        // update appName based on appFile
        if (i > 0) {
            appName = appName + "_" + i; // NOI18N
        }
        appFile.deleteOnExit();
        Application app = new Application(appName, appFile);
        if (EjbBundleArchivist.isEjbBundle(moduleFile)) {
            app.addEjbJarFile(moduleFile);
        } else if (WebBundleArchivist.isWebBundle(moduleFile)) {
            app.addWebJarFile(moduleFile);
        } else if (ApplicationClientArchivist.isApplicationClientJar(moduleFile)) {
            app.addAppClientJarFile(moduleFile);
        } else {
            throw new IllegalArgumentException("Unsupported module type: " + // NOI18N
                                               moduleFile);
        }
        return app;
    
public voiddoGenerateSQL(java.lang.String applicationFilename, java.lang.String serverName, boolean overWrite)
Open the given application from the supplied filename and update any of the CMP entity beans therein with default generated SQL statements for the persistent methods from the datdbases running on the given server. Use overwrite to desructively overwrite existing SQL statements.

	DBInfo dbInfo = this.getServerManager().getDBInfo(serverName);
	Application application = ApplicationArchivist.openAT(                  //bug# 4774785; 4691307
						new File(applicationFilename));

	Iterator itr = application.getEjbBundleDescriptors().iterator(); 
	while ( itr.hasNext() ) {
	    EjbBundleDescriptor ebd = (EjbBundleDescriptor)itr.next();

            SQLGenerator.generateSQL(ebd, ebd.getCMPResourceReference(), 
                                     overWrite, dbInfo);
	}

	application.getApplicationArchivist().save(application.getApplicationArchivist().getApplicationFile(), true);
	System.out.println(localStrings.getLocalString(
						       "enterprise.tools.deployment.main.donegeneratingSQL",
						       "Done generating SQL"));
    
public ApplicationManagergetApplicationManager()
Gets the object responsible for managing applications.

	return applicationManager;
    
public com.sun.enterprise.tools.packager.ComponentPackagergetComponentPackager()

	if (this.componentPackager == null) {
	    this.componentPackager = new ComponentPackager();
	}
	return this.componentPackager;
    
public static com.sun.enterprise.tools.deployment.main.DeployToolgetDeployToolInstance()

	// The created DeployTool instance is needed by the deploytool ui.
	// This method allows the ui to obtain the instance.
	return deployToolInstance;
    
public ServerManagergetServerManager()
Gets the object responsible for managing servers.

	return serverManager;
    
public StandAloneManagergetStandAloneManager()
Gets the object responsible for managing stand-alone objects.

	return standaloneManager;
    
public java.io.FilegetToolHomeDirectory()
Gets the user home directory.

        
	
        
        // IASRI 4691307  for supporting -userdir option
        //test to see if userdir was set, if so use that instead of home directory.
        
        //String home = System.getProperty("user.home");
	//if (home == null) {
	    //home = "";
	//}
        //return new File(home, HOME_DIR);
        
        File homedir;
        if (userdir != null){
            homedir =  new File(userdir);
        }else{
            String home = System.getProperty("user.home");
            if (home == null) {
                home = ""; // NOI18N
            }
            homedir= new File(home, HOME_DIR);
        }
        homedir.mkdirs();
        return homedir;
	// end of IASRI 4691307
    
public java.io.FilegetWorkingDirectory()
Return the working directory of the tool.

	// this give /var/tmp ! which does not seem to be writable
	//String temp = System.getProperty("java.io.tmpdir");
	//if (temp == null) {
	//    temp = "/tmp";
	//}
	//return new File(temp, "jpedeploytool");

	String home = System.getProperty("user.home");
	if (home == null) {
	    home = ""; // NOI18N
	}
	File tmp = new File(home, "tmp"); // NOI18N
	tmp.mkdirs();
	return tmp;
    
public static voidmain(java.lang.String[] args)

        try {
            DeployTool tool = new DeployTool(false);
            Application app = tool.deployStandaloneModule
                (new File("/home/tcng/Test/ejb.jar")); // NOI18N
            System.err.println(app.toString());
            app = tool.deployStandaloneModule
                (new File("/home/tcng/Test/app.jar")); // NOI18N
            System.err.println(app.toString());
            app = tool.deployStandaloneModule
                (new File("/home/tcng/Test/web.war")); // NOI18N
            System.err.println(app.toString());
            
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    
private voidsaveAsBytes(byte[] data, java.io.File file)

	if (data == null) {
	    throw new IOException(localStrings.getLocalString(
							      "enterprise.tools.deployment.main.nulldataforclientcodefile",
							      "null data for client code file"));
	}
	FileOutputStream fileStream = new FileOutputStream(file);
	fileStream.write(data, 0, data.length);
	fileStream.close();
    
public voidsetRuntimeDeploymentInfo(Application application, java.io.File runtimeDeploymentInfo)
Parse the given runtime information file and update the given application with its data.

	//ApplicationArchivist archivist = new ApplicationArchivist();
	FileInputStream fis = new FileInputStream(runtimeDeploymentInfo);
	RuntimeDescriptorNode node = (RuntimeDescriptorNode) RuntimeDescriptorNode.readRuntimeDescriptorNodes(fis).elementAt(0);
	node.updateRuntimeInformation(application);
	this.getApplicationManager().saveApplication(application);
	
	Object[] msg = {application.getName()};
	Log.print(this, localStrings.getLocalString( "enterprise.tools.deployment.main.deploytool.setruntime_command", "Done setting runtime deployment information on {0} to: {1}", msg));
    
public java.lang.StringtoString()
Formatted version of me as a String.

	return "Deploy Tool"; // NOI18N
    
private voidwriteClientJarToFile(java.lang.String clientCode, java.io.File clientCodeFile)

	URL u = new URL(clientCode);
	HttpURLConnection http = (HttpURLConnection) u.openConnection();
	int code = http.getResponseCode();
	if(code != 200) {
	    System.out.println(localStrings.getLocalString(
							   "enterprise.tools.deployment.main.cannotdownloadURL",
							   "Cannot download URL {0}", new Object[] {clientCode}));
	    System.out.println(localStrings.getLocalString(
							   "enterprise.tools.deployment.main.status",
							   "Status: {0}", new Object[] {new Integer(code)}));
	    throw new IOException(localStrings.getLocalString(
							      "enterprise.tools.deployment.main.cannotdownloadURL",
							      "Cannot download URL {0}", new Object[] {clientCode}));
	}
	BufferedInputStream is = new BufferedInputStream(http.getInputStream());
	FileOutputStream fos = new FileOutputStream(clientCodeFile);
	int len = 0;
	int contentLength = http.getContentLength();
	// System.out.println("CONTENT LENGTH:" + contentLength);
	byte[] buf = new byte[contentLength+1];
	while((len = is.read(buf)) != -1)
	    fos.write(buf, 0, len);