FileDocCategorySizeDatePackage
ProgressObjectImpl.javaAPI DocGlassfish v2 API25260Fri May 04 22:34:28 BST 2007com.sun.enterprise.deployapi

ProgressObjectImpl

public abstract class ProgressObjectImpl extends Object implements Runnable, com.sun.enterprise.deployment.client.JESProgressObject
Implementation of the Progress Object
author
dochez

Fields Summary
protected javax.enterprise.deploy.shared.CommandType
commandType
protected Object[]
args
private Vector
listeners
protected com.sun.enterprise.deployapi.SunTarget
target
protected com.sun.enterprise.deployapi.SunTarget[]
targetsList
protected String
moduleID
protected javax.enterprise.deploy.shared.ModuleType
moduleType
protected DeploymentStatusImpl
deploymentStatus
protected javax.enterprise.deploy.spi.TargetModuleID[]
targetModuleIDs
protected Vector
deliveredEvents
protected com.sun.enterprise.deployment.backend.DeploymentStatus
finalDeploymentStatus
protected boolean
deployActionCompleted
protected String
warningMessages
private static com.sun.enterprise.util.i18n.StringManager
localStrings
private static final String
MODULE_ID
private static final String
MODULE_TYPE
private static final String
KEY_SEPARATOR
private static final String
SUBMODULE_COUNT
private static final String
CONTEXT_ROOT
private static final String
WARNING_PREFIX
protected static final String
APPS_CONFIGMBEAN_OBJNAME
Constructors Summary
public ProgressObjectImpl(com.sun.enterprise.deployapi.SunTarget target)
Creates a new instance of ProgressObjectImpl


           
       
        this.target = target;
        deploymentStatus = new DeploymentStatusImpl(this);
        deploymentStatus.setState(StateType.RELEASED);
        finalDeploymentStatus = new com.sun.enterprise.deployment.backend.DeploymentStatus();
        deployActionCompleted = false;
    
public ProgressObjectImpl(com.sun.enterprise.deployapi.SunTarget[] targets)

        this.targetsList = targets;
        deploymentStatus = new DeploymentStatusImpl(this);
        deploymentStatus.setState(StateType.RELEASED);
        finalDeploymentStatus = new com.sun.enterprise.deployment.backend.DeploymentStatus();
        deployActionCompleted = false;
    
Methods Summary
public voidaddProgressListener(javax.enterprise.deploy.spi.status.ProgressListener pol)
Add a listener to receive Progress events on deployment actions.

param
The listener to receive events
see
ProgressEvent

	synchronized (listeners) {
            listeners.add(pol);
	    if (deliveredEvents.size() > 0) {
		Print.dprintln("Delivering undelivered messages...");
	        for (Iterator i = deliveredEvents.iterator(); i.hasNext();) {
		    pol.handleProgressEvent((ProgressEvent)i.next());
	        }
	    }
	}
    
public voidcancel()
(optional) A cancel request on an in-process operation stops all further processing of the operation and returns the environment to it original state before the operation was executed. An operation that has run to completion cannot be cancelled.

throws
OperationUnsupportedException this optional command is not supported by this implementation.

        throw new OperationUnsupportedException("cancel not supported");
    
protected booleancheckStatusAndAddStage(com.sun.enterprise.deployapi.SunTarget aTarget, com.sun.enterprise.deployment.client.RollBackAction rollback, java.lang.String action, com.sun.appserv.management.client.ConnectionSource dasConnection, com.sun.enterprise.deployment.backend.DeploymentStatus currentStatus)

        return checkStatusAndAddStage(aTarget, rollback, action, dasConnection, 
            currentStatus, false);
    
protected booleancheckStatusAndAddStage(com.sun.enterprise.deployapi.SunTarget aTarget, com.sun.enterprise.deployment.client.RollBackAction rollback, java.lang.String action, com.sun.appserv.management.client.ConnectionSource dasConnection, com.sun.enterprise.deployment.backend.DeploymentStatus currentStatus, boolean isStartPhase)
Given a Deployment status, this checks if the status is success If the status is failed, it tries roll back operations (if rollback is specified) and then sets up for an abnormal exit

        String statusMsg = getDeploymentStatusMessage(currentStatus, 
            isStartPhase);
        finalDeploymentStatus.addSubStage(currentStatus);
        if(statusMsg == null) {
            fireProgressEvent(StateType.RUNNING,
                                localStrings.getString("enterprise.deployment.client.action_completed", action),
                                aTarget);
            return true;
        }
        if(rollback != null) {
            com.sun.enterprise.deployment.backend.DeploymentStatus tmp = new
                        com.sun.enterprise.deployment.backend.DeploymentStatus();
            if(!rollback.rollback(dasConnection, tmp)) {
                fireProgressEvent(StateType.RUNNING, 
                                localStrings.getString("enterprise.deployment.client.action_failed", "Rollback failed"),
                                        aTarget);
                tmp.setStageStatus(com.sun.enterprise.deployment.backend.DeploymentStatus.FAILURE);
                tmp.setStageStatusMessage(localStrings.getString("enterprise.deployment.client.action_failed", "Rollback failed"));
            } else {
                fireProgressEvent(StateType.RUNNING, 
                                localStrings.getString("enterprise.deployment.client.action_completed", "Rollback"),
                                        aTarget);
                tmp.setStageStatus(com.sun.enterprise.deployment.backend.DeploymentStatus.SUCCESS);
                tmp.setStageStatusMessage(localStrings.getString("enterprise.deployment.client.action_completed", "Rollback"));
            }
            finalDeploymentStatus.addSubStage(tmp);
        }
        setupForAbnormalExit(localStrings.getString("enterprise.deployment.client.action_failed_with_message", action, statusMsg),  aTarget);
        return false;
    
protected voidfireProgressEvent(javax.enterprise.deploy.spi.status.ProgressEvent progressEvent)
Notifies all listeners that have registered interest for ProgressEvent notification.

        /*
         *Bug 4977764
         *Iteration failed due to concurrent modification of the vector.  Even though the add, remove, and fire 
         *methods synchronize on the listeners vector, a listener could conceivably invoke add or remove 
         *recursively, thereby triggering the concurrent modification exception.
         *
         *Fix: clone the listeners vector and iterate through the clone.  
         */
	Vector currentListeners = null;
        synchronized (listeners) {
            currentListeners = (Vector) listeners.clone();
            /*
             *The following add must remain inside the synchronized block.  Otherwise, there will be a small window
             *in which a new listener's registration could interleave with fireProgressEvent, registering itself 
             *after the listeners vector had been cloned (thus excluding the new listener from the iteration a
             *few lines below) but before the list of previously-delivered events had been updated.  
             *This would cause the new listener to miss the event that was firing.  
             *Keeping the following add inside the synchronized block ensures that updates to the listeners 
             *vector by addProgressListener and to deliveredEvents by fireProgressEvent do not interleave and therefore
             *all listeners will receive all events.
             */
            
            deliveredEvents.add(progressEvent);
        }

        for (Iterator listenersItr = currentListeners.iterator(); listenersItr.hasNext();) {
            ((ProgressListener)listenersItr.next()).handleProgressEvent(progressEvent);
        }
        currentListeners = null;
    
protected voidfireProgressEvent(javax.enterprise.deploy.shared.StateType state, java.lang.String message)
Notifies all listeners that have registered interest for ProgressEvent notification.

        fireProgressEvent(state, message, target);
    
protected voidfireProgressEvent(javax.enterprise.deploy.shared.StateType state, java.lang.String message, com.sun.enterprise.deployapi.SunTarget aTarget)
Notifies all listeners that have registered interest for ProgressEvent notification.

        
        StateType stateToBroadcast = (state != null) ? state : deploymentStatus.getState();

        /* new copy of DeploymentStatus */
	DeploymentStatusImpl depStatus = new DeploymentStatusImpl(this);
	depStatus.setState(stateToBroadcast);
        depStatus.setMessage(message);

        /*
         *Update this progress object's status before notifying listeners.
         */
        if (state != null) {
            deploymentStatus.setMessage(message);
            deploymentStatus.setState(state); // retain current state
	}
        
        /* send notification */
	SunTargetModuleID tmi = new SunTargetModuleID(moduleID, aTarget);
        tmi.setModuleType(getModuleType());
	fireProgressEvent(new ProgressEvent(this, tmi, depStatus));
    
public javax.enterprise.deploy.spi.status.ClientConfigurationgetClientConfiguration(javax.enterprise.deploy.spi.TargetModuleID id)
Return the ClientConfiguration object associated with the TargetModuleID.

return
ClientConfiguration for a given TargetModuleID or null if none exists.

        return null;
    
javax.enterprise.deploy.shared.CommandTypegetCommandType()

        return commandType;
    
public com.sun.enterprise.deployment.backend.DeploymentStatusgetCompletedStatus()
Retrieve the final deployment status which has complete details for each stage

        if(deployActionCompleted) {
            return finalDeploymentStatus;
        }
        return null;
    
public javax.enterprise.deploy.spi.status.DeploymentStatusgetDeploymentStatus()
Retrieve the status of this activity.

return
An object containing the status information.

        DeploymentStatusImpl result = new DeploymentStatusImpl(this);
        result.setState(deploymentStatus.getState());
        result.setMessage(deploymentStatus.getMessage());
        
        return result;
    
protected java.lang.StringgetDeploymentStatusMessage(com.sun.enterprise.deployment.backend.DeploymentStatus status)

    

        
        return getDeploymentStatusMessage(status, false);
    
protected java.lang.StringgetDeploymentStatusMessage(com.sun.enterprise.deployment.backend.DeploymentStatus status, boolean isStartPhase)
Parse the DeploymentStatus to get the status message within

        if(status == null) {
            return null;
        }
        // if stage status is success, return as it is
        if (status!=null && status.getStatus() >= com.sun.enterprise.deployment.backend.DeploymentStatus.SUCCESS) {
            return null;
        }
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        PrintWriter pw = new PrintWriter(bos);
        com.sun.enterprise.deployment.backend.DeploymentStatus.parseDeploymentStatus(status, pw);
        byte[] statusBytes = bos.toByteArray();
        String statusString = new String(statusBytes);
        // if stage status is WARNING, collect the warning messages
        if(status.getStatus() == com.sun.enterprise.deployment.backend.DeploymentStatus.WARNING) {
            if(warningMessages==null) {
                warningMessages = WARNING_PREFIX + statusString;
            } else {
                warningMessages += statusString;
            }

            // add additional messages if it failed at loading.
            if (isStartPhase) { 
                warningMessages = localStrings.getString("enterprise.deployment.client.start_failed_msg") + warningMessages;
            }

            return null;
        }
        // Failed stage; return the failure message
        return statusString;
    
public javax.enterprise.deploy.shared.ModuleTypegetModuleType()

return
the module type of this deployed module

        return moduleType;
    
public javax.enterprise.deploy.spi.TargetModuleID[]getResultTargetModuleIDs()
Retrieve the list of TargetModuleIDs successfully processed or created by the associated DeploymentManager operation.

return
a list of TargetModuleIDs.


        /**
         * this should go once CTS has fixed their bugs...
         */
        if (targetModuleIDs==null) {
            if(target != null) {
                initializeTargetModuleIDs(moduleID);
            } else if(targetsList != null) {
                initializeTargetModuleIDForAllServers(null, null);
            }
        }
        // will return null until the operation is completed
        return targetModuleIDs;
    
protected java.lang.StringgetThrowableString(java.lang.Throwable t)
Since DeploymentStatus only provides the ability to pass a String to the ProgressListener, the following is a convenience method for allowing the stack-trace from a Throwable to be converted to a String to send to the ProgressListeners.

	ByteArrayOutputStream bos = new ByteArrayOutputStream();
	PrintStream ps = new PrintStream(bos);
	t.printStackTrace(ps);
	ps.close(); // may not be necessary
	return bos.toString();
    
private voidinitTargetModuleIDWebURL(com.sun.enterprise.deployapi.SunTargetModuleID tm, com.sun.enterprise.admin.util.HostAndPort webHost, java.lang.String contextRoot)
private method to initialize the web url for the associated deployed web module

        
        if (webHost==null)
            return;
        
        try {
            // Patchup code for fixing netbeans issue 6221411; Need to find a 
            // good solution for this and WSDL publishing
            String host;
            SunDeploymentManager sdm = new SunDeploymentManager(tm.getConnectionInfo());
            if(sdm.isPE()) {
                host = tm.getConnectionInfo().getHostName();
            } else {
                host = webHost.getHost();
            }
            
            URL webURL = new URL("http", host, webHost.getPort(), contextRoot);
            tm.setWebURL(webURL.toExternalForm());
        } catch(Exception e) {
            Print.dprintStackTrace(e.getLocalizedMessage(),e);
        }
    
protected voidinitializeTargetModuleIDForAllServers(com.sun.enterprise.deployment.backend.DeploymentStatus status, javax.management.MBeanServerConnection mbsc)
Initialize the target module IDs with the application information stored in the DeploymentStatus for all the server in the target list.


        if(targetsList == null) {
            return;
        }

        targetModuleIDs = new SunTargetModuleID[targetsList.length];
        String moduleID = status == null 
                        ? this.moduleID : status.getProperty(MODULE_ID);
        String key = moduleID + KEY_SEPARATOR + MODULE_TYPE;
        ModuleType type = status == null
                        ? getModuleType()
                        : ModuleType.getModuleType((new Integer(status.getProperty(key))).intValue());

        for(int i=0; i<targetsList.length; i++) {
            SunTargetModuleID parentTargetModuleID = new SunTargetModuleID(moduleID, targetsList[i]);
            parentTargetModuleID.setModuleType(type);
            targetModuleIDs[i] = parentTargetModuleID;

            if (status != null) {
                // let's get the host name and port where the application was deployed 
                HostAndPort webHost=null;
                try {
                    Object[] params = new Object[]{ moduleID, Boolean.FALSE };
                    String[] signature = new String[]{ "java.lang.String", "boolean"};
                    ObjectName applicationsMBean = new ObjectName(APPS_CONFIGMBEAN_OBJNAME);                
                    webHost = (HostAndPort) mbsc.invoke(applicationsMBean, "getHostAndPort", params, signature);                        
                } catch(Exception e) {
                    Print.dprintStackTrace(e.getLocalizedMessage(), e);
                }

                key = moduleID + KEY_SEPARATOR + SUBMODULE_COUNT;
                if (status.getProperty(key) == null) { //standalone module
                    if (ModuleType.WAR.equals(type)) {
                        key = moduleID + KEY_SEPARATOR + CONTEXT_ROOT;
                        String contextRoot = status.getProperty(key);
                        initTargetModuleIDWebURL(parentTargetModuleID, webHost, contextRoot);
                     }
                } else {
                    int counter = (Integer.valueOf(status.getProperty(key))).intValue();
                    // now for each sub module            
                    for (int j = 0; j < counter; j++) {
                        //subModuleID
                        key = moduleID + KEY_SEPARATOR + MODULE_ID + KEY_SEPARATOR + String.valueOf(j);
                        String subModuleID = status.getProperty(key);
                        SunTargetModuleID subModule = new SunTargetModuleID(subModuleID, targetsList[i]);

                        //subModuleType 
                        key = subModuleID + KEY_SEPARATOR + MODULE_TYPE;
                        type = ModuleType.getModuleType((new Integer(status.getProperty(key))).intValue());
                        subModule.setModuleType(type);
                        if (ModuleType.WAR.equals(type) && webHost!=null) {
                            key = subModuleID + KEY_SEPARATOR + CONTEXT_ROOT;
                            String contextRoot = status.getProperty(key);
                            initTargetModuleIDWebURL(subModule, webHost, contextRoot);
                        }
                        parentTargetModuleID.addChildTargetModuleID(subModule);
                    }
                } 
            }
        }
    
protected voidinitializeTargetModuleIDs(java.lang.String moduleID)
initialize the target module IDs with the passed application moduleID and the descriptors

        SunTargetModuleID parentTargetModuleID = new SunTargetModuleID(moduleID, target);        
        parentTargetModuleID.setModuleType(getModuleType());
        
        targetModuleIDs = new SunTargetModuleID[1];        
        targetModuleIDs[0] = parentTargetModuleID;
    
public booleanisCancelSupported()
Tests whether the vendor supports a cancel opertation for deployment activities.

return
true if canceling an activity is supported by this platform.

        return false;
    
public booleanisStopSupported()
Tests whether the vendor supports a stop opertation for deployment activities.

return
true if canceling an activity is supported by this platform.

        return false;
    
public voidremoveProgressListener(javax.enterprise.deploy.spi.status.ProgressListener pol)
Remove a ProgressObject listener.

param
The listener being removed
see
ProgressEvent

	synchronized (listeners) {
            listeners.remove(pol);
	}
    
public abstract voidrun()
implement this method to do the actual processing

public voidsetCommand(javax.enterprise.deploy.shared.CommandType commandType, java.lang.Object[] args)

        this.commandType = commandType;
        this.args = args;
    
public voidsetModuleType(javax.enterprise.deploy.shared.ModuleType moduleType)
Sets the module type for this deployed module

param
the module type

        this.moduleType = moduleType;
    
protected voidsetupForAbnormalExit(java.lang.String errorMsg, com.sun.enterprise.deployapi.SunTarget aTarget)

        String i18nmsg = localStrings.getString("enterprise.deployment.client.action_failed", errorMsg);
        finalDeploymentStatus.setStageStatus(com.sun.enterprise.deployment.backend.DeploymentStatus.FAILURE);
        finalDeploymentStatus.setStageStatusMessage(i18nmsg);
        deployActionCompleted = true;
        fireProgressEvent(StateType.FAILED, i18nmsg, aTarget);
        return;
    
protected voidsetupForNormalExit(java.lang.String message, com.sun.enterprise.deployapi.SunTarget aTarget)

        String i18nmsg;
        // If we ever got some warning during any of the stages, the the final status is warning; else status=success
        if(warningMessages == null) {
            i18nmsg = localStrings.getString("enterprise.deployment.client.action_completed", message);
            finalDeploymentStatus.setStageStatus(com.sun.enterprise.deployment.backend.DeploymentStatus.SUCCESS);
        } else {
            i18nmsg = localStrings.getString("enterprise.deployment.client.action_completed_with_warning", warningMessages);
            finalDeploymentStatus.setStageStatus(com.sun.enterprise.deployment.backend.DeploymentStatus.WARNING);            
        }
        finalDeploymentStatus.setStageStatusMessage(i18nmsg);
        deployActionCompleted = true;
        fireProgressEvent(StateType.COMPLETED, i18nmsg, aTarget);
        return;
    
public voidstop()
(optional) A stop request on an in-process operation allows the operation on the current TargetModuleID to run to completion but does not process any of the remaining unprocessed TargetModuleID objects. The processed TargetModuleIDs must be returned by the method getResultTargetModuleIDs.

throws
OperationUnsupportedException this optional command is not supported by this implementation.

        throw new OperationUnsupportedException("stop not supported");