FileDocCategorySizeDatePackage
ModuleDeployer.javaAPI DocGlassfish v2 API29937Fri May 04 22:34:32 BST 2007com.sun.enterprise.deployment.backend

ModuleDeployer

public abstract class ModuleDeployer extends Deployer
ModuleDeployer is an abstract class with common methods required for deploying Web Modules and EJB Modules.
author
bnevins
version

Fields Summary
protected com.sun.enterprise.instance.BaseManager
modulesMgr
protected String
moduleName
protected File
moduleDir
protected File
stubsDir
protected File
jspDir
protected File
xmlDir
protected File
jwsDir
protected File
originalModuleDir
protected String
originalContextRoot
protected com.sun.enterprise.instance.ModuleEnvironment
moduleEnv
protected boolean
isMaybeCMPDropTables
private boolean
isReg
private boolean
shouldRollback
private boolean
moduleDirWasRenamed
private com.sun.enterprise.deployment.Application
moduleDD
private DeploymentStatus
J2EECPhaseStatus
private static com.sun.enterprise.util.i18n.StringManager
localStrings
Constructors Summary
protected ModuleDeployer(DeploymentRequest r)

		super(r);
	
Methods Summary
protected final voidbegin()

		super.begin();
		
		// get environment object refs
		
		InstanceEnvironment instanceEnv = getInstanceEnv();
		moduleEnv						= request.getModuleEnv();
		moduleName						= request.getName();
		
		// check them
		
		if(moduleEnv == null)
		{
			String msg = localStrings.getString("enterprise.deployment.backend.null_moduleenvironment");
			throw new IASDeploymentException(msg);
		}
		
		try
		{
			moduleEnv.verify();
			modulesMgr = createConfigManager(instanceEnv, moduleEnv);
                        isReg = DeploymentServiceUtils.isRegistered(moduleName, request.getType());

			setDeployCommand();
			
			if(request.isReDeploy())
			{
                            //  first let's try to get the application from the
                            // instance manager cache
                            // if it's not there, get it from the request which
                            // is set through deployment context cache
                            moduleDD = getManager().getRegisteredDescriptor(
                                moduleName);
                            if (moduleDD == null) { 
                                moduleDD = request.getDescriptor();
                            } 

                                originalModuleDir = new File(DeploymentServiceUtils.getLocation(moduleName, request.getType()));
				unregister();
                                removePolicy();
			}
		}
		catch(Exception e)
		{
                    if (e instanceof IASDeploymentException) {
                        throw (IASDeploymentException)e; 
                    } else {
			throw new IASDeploymentException(e);
                    }
		}
		
		shouldRollback = true;
		
		// for redeploy -- when we get to doRequestFinish() -- the module will NOT be registered
		// any longer.
	
private voidbeginFinish()

		setDirs();

                if (request.isDeploy()) {
                    // if the directories already exist on disk, wipe them out
                    // We do this because otherwise they'd never get this 
                    // Module deployed without wiping the dirs manually
                    liquidate();
                }

                // send PRE_DEPLOY event so the deployment event
                // listener can do the necessary work
		if(request.isReDeploy()) {
                    DeploymentEventInfo info = new DeploymentEventInfo(
                        moduleDir, stubsDir, moduleDD, getRequest());
                    DeploymentEvent ev = new DeploymentEvent(
                        DeploymentEventType.PRE_DEPLOY, info);
                    DeploymentEventManager.notifyDeploymentEvent(ev);

                    liquidate();
                }

	
public voidcleanup_internal()
Attempt to delete the deployed directory-tree.

This method call is guaranteed to never throw any kind of Exception

                try
                {
                        if(request.isUnDeploy())
                        {
                                if(isMaybeCMPDropTables)
                                        dropTables();

                                liquidate();
                        }

                        // nothing to do for Deploy
                }
                catch(Exception e)
                {
                        logger.warning("Exception caught and ignored in cleanup_internal()");
                }
    
protected abstract com.sun.enterprise.instance.BaseManagercreateConfigManager(com.sun.enterprise.instance.InstanceEnvironment ienv, com.sun.enterprise.instance.ModuleEnvironment menv)

protected abstract voiddeploy()

public voiddoRequest()

		doRequestPrepare();
		doRequestFinish();
	
public voiddoRequestFinish()

		try
		{
                        // retrieve the J2EECPhase deployment status
                        J2EECPhaseStatus = request.getCurrentDeploymentStatus();
			if(request.isDeploy() || request.isReDeploy())
			{
                                // create a DeploymentStatus for predeploy stage
                                // it is a substage of J2EECPhase status
                                DeploymentStatus preDeployStatus =
                                    new DeploymentStatus(J2EECPhaseStatus);
                                request.setCurrentDeploymentStatus(
                                    preDeployStatus);
				beginFinish();
                                preDeploy();

                                // create a DeploymentStatus for runEJBC stage
                                // it is a substage of J2EECPhase status
                                DeploymentStatus runEJBCStatus =
                                    new DeploymentStatus(J2EECPhaseStatus);
                                request.setCurrentDeploymentStatus(
                                    runEJBCStatus);
				deploy();
                                register();

                                // create a DeploymentStatus for postDeploy
                                // stage, it is a substage of J2EECPhase status
                                DeploymentStatus postDeployStatus =
                                    new DeploymentStatus(J2EECPhaseStatus);
                                request.setCurrentDeploymentStatus(
                                    postDeployStatus);
				postDeploy();

				generatePolicy();
                        } else if(request.isUnDeploy())
			{
				beginFinish();
				preundeploy();
				undeploy();
				removePolicy();
			}
			else
			{
				String msg = localStrings.getString(
				"enterprise.deployment.backend.unknown_deployment_command" );
				throw new IASDeploymentException( msg );
			}
		}
		catch(Exception e)
		{
			if(shouldRollback)
				rollback();
			
			String msg = localStrings.getString(
			"enterprise.deployment.backend.dorequest_exception");
                        if (e.getCause() != null )
                            msg+= e.getCause().toString();
			logger.log(Level.FINE, msg, e);
                        if (e instanceof IASDeploymentException) {
                            throw (IASDeploymentException) e;
                        } else {
                            throw new IASDeploymentException(msg, e);
                        }
		}
		finally
		{
			finish();
		}
	
public voiddoRequestPrepare()

		try
		{
			begin();
		}
		catch(Exception e)
		{
			if(shouldRollback)
				rollback();
			
			String msg = localStrings.getString(
			"enterprise.deployment.backend.dorequest_exception");
                        if (e.getCause() != null )
                            msg+= e.getCause().toString();

			logger.log(Level.WARNING, msg, e);
			throw new IASDeploymentException(msg, e);
		}
	
protected voiddropTables()
Call into CMP to drop tables. This is called just before files are deleted as part of the cleanup() call. We do it very late in the process since dropping tables can't be rolled-back. Thus all of the other steps that can have errors that require a rollback have already been done successfully - or we wouldn't be here. bnevins April 2003

This method call is guaranteed to never throw any kind of Exception

                assert isMaybeCMPDropTables; // programmer error if this is false!
                                                                                
                try
                {
                    DeploymentEventInfo info = new DeploymentEventInfo(moduleDir, stubsDir, moduleDD, request);
                                                                               
                    DeploymentEvent ev       = new DeploymentEvent(DeploymentEventType.PRE_UNDEPLOY, info);                                                 
                    DeploymentEventManager.notifyDeploymentEvent(ev);
                }
                catch(Throwable t)
                {
                        // yes we are swallowing all possible errors from outside this package!   
                        logger.log( Level.WARNING,

                        "enterprise.deployment_pre_undeploy_event_error", t);
                }
    
protected DeploymentEventInfogetEventInfo()
Create DeploymentEvent info instance.

return
DeploymentEventInfo

	
		return new DeploymentEventInfo(moduleDir, stubsDir,
			request.getDescriptor(), getRequest());
    
protected com.sun.enterprise.instance.BaseManagergetManager()

return
the BaseManager implementation for this deployer

		return modulesMgr;
	
protected java.util.ListgetModuleClasspath(com.sun.enterprise.deployment.archivist.Archivist archivist, com.sun.enterprise.deployment.deploy.shared.AbstractArchive archive)

return
the module classpath

	    try {
		String location = request.getDeployedDirectory().getAbsolutePath();
		return EJBClassPathUtils.getModuleClasspath(request.getName(), location, this.getManager());
	    } catch(Exception e) {
		throw new IASDeploymentException(e);
	    }
	
protected final java.io.FilegetModuleDir()

		return moduleDir;
	
protected voidhandlePostDeployEvent()
Called from postDeploy and postRedeploy to process extra events.

                DeploymentEventInfo info = getEventInfo();
                DeploymentEvent ev	 = new DeploymentEvent(
                DeploymentEventType.POST_DEPLOY, info);
                DeploymentEventManager.notifyDeploymentEvent(ev);

     
protected voidliquidate(boolean isRollback)

            if (request.isUnDeploy()) {                 
                if (! (DeploymentServiceUtils.isDirectoryDeployed(moduleName,                    request.getType()) || request.isReload()) ) {
                    cleanAndCheck(moduleDir);
                }
            } else {
                if (isArchive()) {
                    cleanAndCheck(moduleDir);
                }
            }
                                                                                            if (isRollback) {
                DeleteOrKeepFailedStubs(stubsDir);
            } else {
                cleanAndCheck(stubsDir);
            }
                                                                               
            cleanAndCheck(jspDir);
            cleanAndCheck(xmlDir);
            cleanAndCheck(jwsDir);

            liquidateTimeStamp = System.currentTimeMillis();
        
protected voidliquidate()

            liquidate(false);
        
protected com.sun.enterprise.deployment.ApplicationloadDescriptors()

return
a fully initialized and validated deployment descriptors for this deployment request.

            Application app = super.loadDescriptors();
            (new com.sun.enterprise.webservice.WsUtil()).genWSInfo(app, request);
            return app;
        
protected booleanneedsJSPs()

		// override this for any module that works with generated JSPs
		return false;
	
protected booleanneedsStubs()

		// override this for any module that needs stubs created
		return false;
	
protected voidpostDeploy()


                // handle undeleted files if there is any
                handleUndeletedFiles();

                // save the object type in optional attributes 
                Properties optionalAttributes = request.getOptionalAttributes();
                if (optionalAttributes == null) {
                    optionalAttributes = new Properties();
                }
                String resourceType = getResourceType(moduleDir);
                if(resourceType != null) {
                    optionalAttributes.setProperty(ServerTags.OBJECT_TYPE,
                        resourceType);
                }

                request.setExternallyManagedApp(
                    isExternallyManagedApp(moduleDir));
    
                handlePostDeployEvent();
          
protected abstract voidpreDeploy()

protected voidpreundeploy()

            try {
                // setup the cmp stuff for undeployment...
                // do this here before unregistering the descriptor
                if(getRequest().isMaybeCMPDropTables())
                {
                    isMaybeCMPDropTables = true;

                    // first let's try to get the application from the
                    // instance manager cache
                    // if it's not there, get it from the request which
                    // is set through deployment context cache
                    moduleDD = getManager().getRegisteredDescriptor(
                        moduleName);
                    if (moduleDD == null) { 
                        moduleDD = request.getDescriptor();
                    } 
                }
            } catch(Throwable t) {
                // yes we are swallowing all possible errors from outside this package!   
                logger.log( Level.WARNING,

                "enterprise.deployment_pre_undeploy_event_error", t);
            }
    
protected voidregister()

		modulesMgr.registerDescriptor(moduleName, request.getDescriptor());
	
public voidremovePolicy()

        //no op
    
private voidrollback()

            try {
                unregister();
                liquidate(true);
            } catch(Exception e) {
                // Can't do anything about it!!
                logger.log( Level.WARNING,
                    "enterprise.deployment_rollback_error", e);
            }
	
protected com.sun.enterprise.util.zip.ZipItem[]runEJBC()
runs the ejbc compiler for the deployable module

		ZipItem[] clientStubs = null;
		
		try
		{
			
			// ejbc timing info
			IASEJBCTimes timing	 = new IASEJBCTimes();
			
			EJBCompiler compiler = new EJBCompiler
			(
			moduleName,
			moduleDir,
			stubsDir,
			getManager(),
			request,
			timing
			);
			
			// runs ejbc
			clientStubs = compiler.compile();
			
			// add the ejbc timing info to deployment time
			addEJBCTime(timing);
			
		} catch (Exception e)
		{
			logger.log( Level.WARNING,
			"enterprise.deployment_ejbc_error", e );
			String msg = localStrings.getString(
			"enterprise.deployment.backend.ejbc_error" );
			throw new IASDeploymentException(msg, e);
		}
		
		// returns the client stubs or an empty array if no stubs
		return clientStubs;
	
protected voidrunVerifier()
Runs the verifier on this module if verification is ON in the deployment request.

throws
IASDeploymentException if an error found in this module after verification

        if (request.isVerifying()) {
            try {
                String archive = request.getDeployedDirectory().getCanonicalPath();
                File jspOutDir = (request.getPrecompileJSP())? jspDir:null;
                new AppVerifier().verify(request.getDescriptor(),
                        (new FileArchiveFactory()).openArchive(archive),
                        request.getCompleteClasspath(),
                        jspOutDir);
            } catch (Exception e) {
                String msg = localStrings.getString(
                        "enterprise.deployment.backend.verifier_error");
                throw new IASDeploymentException(msg);
            }
        }
    
private voidsetDeployCommand()

		if(request.isUnDeploy())
		{
			if(!isReg)
			{
				String msg = localStrings.getString(
				"enterprise.deployment.backend.undeploy_error_module_not_registered" );
				throw new IASDeploymentException( msg );
			}
		}
		
		else if(request.isDeploy())
		{
			if(isReg)
			{
				//WBN 2/27/02 - By definition -- a REDEPLOY is now a forced DEPLOY if
				// the Module already exists
				if(request.isForced())
				{
					request.setCommand(DeploymentCommand.REDEPLOY);
				}
				else
				{
					String msg = localStrings.getString(
					"enterprise.deployment.backend.deploy_error_module_exists" );
					throw new IASDeploymentException( msg );
				}
			}
			else
			{
				// isReg is false.  This means that it isn't registered as the current type
				// of module.  But we might be clashing with a registered module of a different flavor.
				// E.g. this may be an ejb deployment and there is a web module already deployed
				// with the same name.
				// this will throw an IASDeploymentException if it is registered to another type...
				checkRegisteredAnywhereElse(moduleName);
			}
		}
	
private voidsetDirs()

		assert modulesMgr != null;
		assert moduleName != null;
		assert moduleEnv != null;
		
		if(request.isDeploy())
			setDirsDeploy();
		else if(request.isReDeploy())
			setDirsReDeploy();
		else if(request.isUnDeploy())
			setDirsUnDeploy();
		else
		{
			String msg = localStrings.getString(
			"enterprise.deployment.backend.deployment_type_error" );
			throw new IASDeploymentException( msg );
		}
		
		request.setDeployedDirectory(moduleDir);
		request.setJSPDirectory(jspDir);
		request.setStubsDirectory(stubsDir);
		request.setGeneratedXMLDirectory(xmlDir);
	
private voidsetDirsDeploy()

		if(isReg)
		{
			String msg = localStrings.getString(
			"enterprise.deployment.backend.deploy_error_module_exists" );
			throw new IASDeploymentException( msg );
		}
                xmlDir = new File(moduleEnv.getModuleGeneratedXMLPath());
                jwsDir = new File(moduleEnv.getJavaWebStartPath());
		
		if(needsStubs())
		{
			stubsDir = new File(moduleEnv.getModuleStubPath());
		}
		else
			stubsDir = null;
		
		if(needsJSPs())
		{
			assert (modulesMgr instanceof WebModulesManager);
			jspDir = new File(moduleEnv.getModuleJSPPath());
		}
		if(isArchive())
		{
			File parent = new File(getInstanceEnv().getModuleRepositoryPath());
                        moduleDir = new File(parent, moduleName);
                        moduleDir.mkdirs();
		}
		else if(isDirectory())
		{
			FileSource fileSource = request.getFileSource();
			
			if(fileSource == null || !fileSource.exists())
			{
				String msg = localStrings.getString(
				"enterprise.deployment.backend.file_source_does_not_exist",
				fileSource );
				throw new IASDeploymentException( msg );
			}
			
			moduleDir = fileSource.getFile();
			
			if(!FileUtils.safeIsDirectory(moduleDir))
			{
				String msg = localStrings.getString(
				"enterprise.deployment.backend.deployment_directory_does_not_exist",
				moduleDir.getPath() );
				throw new IASDeploymentException( msg );
			}
		}
		else
		{
			String msg = localStrings.getString(
			"enterprise.deployment.backend.deployment_not_dir_or_archive" );
			throw new IASDeploymentException( msg );
		}
	
private voidsetDirsReDeploy()

		if(!isReg)
		{
			String msg = localStrings.getString(
			"enterprise.deployment.backend.redeploy_error_module_not_registered" );
			throw new IASDeploymentException( msg );
		}
		
                xmlDir = new File(modulesMgr.getGeneratedXMLLocation(moduleName));
                jwsDir = new File(moduleEnv.getJavaWebStartPath());

		// let's get the easy stuff out of the way first -- stubs & jsps
		stubsDir = null;
		jspDir   = null;
		
		if(needsStubs()) {
			stubsDir	= new File(modulesMgr.getStubLocation(moduleName));
                }
		if(needsJSPs())
		{
			assert (modulesMgr instanceof WebModulesManager);
			WebModulesManager mgr = (WebModulesManager)modulesMgr;
			jspDir = new File(mgr.getJSPLocation(moduleName));
		}
		
		if(isArchive())
		{
			// be sure we have the original deployed directory
			if(!FileUtils.safeIsDirectory(originalModuleDir))
			{
				String msg = localStrings.getString(
				"enterprise.deployment.backend.modulesmanager_error_getting_module_location",
				moduleName );
				throw new IASDeploymentException(msg);
			}
			
			moduleDir		= originalModuleDir;
		}
		else if(isDirectory())
		{
			FileSource fileSource = request.getFileSource();
			
			if(!fileSource.exists())
			{
				String msg = localStrings.getString(
				"enterprise.deployment.backend.file_source_does_not_exist",
				fileSource );
				throw new IASDeploymentException( msg );
			}
			
			assert fileSource.isDirectory();
			moduleDir = fileSource.getFile();
			
		}
		else
		{
			String msg = localStrings.getString(
			"enterprise.deployment.backend.redeployment_not_dir_or_archive" );
			throw new IASDeploymentException( msg );
		}
		
		moduleDir.mkdirs();
	
private voidsetDirsUnDeploy()

		// Use the already registered location
		try
		{
			if(!isReg)
			{
				String msg = localStrings.getString(
				"enterprise.deployment.backend.undeploy_error_module_not_registered" );
				throw new IASDeploymentException( msg );
			}
			
			moduleDir	= new File(DeploymentServiceUtils.getLocation(moduleName, request.getType()));
                        xmlDir = new File(modulesMgr.getGeneratedXMLLocation(moduleName));
                        jwsDir = new File(moduleEnv.getJavaWebStartPath());
			stubsDir = null;
			jspDir   = null;
			
			if(needsStubs())
				stubsDir	= new File(modulesMgr.getStubLocation(moduleName));
			if(needsJSPs())
			{
				assert (modulesMgr instanceof WebModulesManager);
				WebModulesManager mgr = (WebModulesManager)modulesMgr;
				jspDir = new File(mgr.getJSPLocation(moduleName));
			}
		}
		catch(Exception e)
		{
			String msg = localStrings.getString(
			"enterprise.deployment.backend.error_getting_module_directory",
			e );
			throw new IASDeploymentException( msg );
		}
	
protected voidsetShared(boolean what)

	/* WBN 510-02
	 * This feature wasn't implemented by core server.
	 * I'm commenting-out the call but leaving everything else
	 * in place -- since it will probably be added in a later
	 * version
	 */
		//modulesMgr.setShared(moduleName, what);
	
private voidundeploy()

		try
		{
			unregister();
		}
		catch(ConfigException e)
		{
			String msg = localStrings.getString(
			"enterprise.deployment.backend.config_exception_on_remove",
			moduleName, e );
			throw new IASDeploymentException( msg, e);
		}
	
protected voidunregister()

		modulesMgr.unregisterDescriptor(moduleName);