Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2011 Michael Ruflin, Andr´┐Ż Locher, Claudia von Bastian. This file is part of Tatool. Tatool is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Tatool is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with Tatool. If not, see <http://www.gnu.org/licenses/>. /
 
 package ch.tatool.app.service.exec.impl;
 
 
Executor implementation

Author(s):
Michael Ruflin
 
 public class ExecutorImpl implements Executor {
 
     private static Logger logger = LoggerFactory.getLogger(ExecutorImpl.class);
     
     // Injected properties
     
    
DataService.
 
     private DataService dataService;
    
    
Service that created this object.
 
     
     // internal
     
    
ExecutionDisplayProvider.
 
     private ExecutionDisplayProvider executionDisplayProvider;
    
    
ExecutionContext object.
 
     private ExecutionContextImpl executionContext = null;
    
    
Variable used to flag whether execution should be continued.
 
     private volatile boolean continueModule = true
    
    
variable used to indicate that the current element should be finished asap.
 
     private volatile boolean finishCurrentCycle = false;

    
Thread executing the module.
 
     private Thread executionThread;
    
    
This object holds the module, session and trial information for the current execution as well as other execution relevant data
 
     private ExecutionDataImpl executionData;
    
    
Manages the phase executables.
 
     private PhaseRunnableManagerImpl phaseRunnableManager;
    
    
Next element selector strategy.
 
     private ExecutionStrategy selectorStrategy;
    
    
holds the execution tree.
 
     private ElementTreeImpl elementTree;
    
    
Currently executed element
 
     private volatile Executable currentExecutable;
     private volatile Element currentElement;
    
    
Currently executed phase executable
 
     private volatile PhaseRunnable currentPhaseExecutable;
    
    
    
Constructor.
 
    public ExecutorImpl() {
    }
    
    
Sets the ExecutionServiceImpl this executor works for.
    void setExecutionServiceImpl(ExecutionServiceImpl executionService) {
    	this. = executionService;
    }
    
    
Sets the data service object.
    void setDataService(DataService dataService) {
    	this. = dataService;
    }
    
    void setExecutionDisplayProvider(ExecutionDisplayProvider executionDisplayProvider) {
    	this. = executionDisplayProvider;
    }
    
    public void setup(Module module) {
    	 = new ElementTreeImpl();
    	 = new ExecutionDataImpl();
    	.setModule(module);
    }
    
    
Starts a module. Merely creates the ExecutionContext and the module execution thread. See runModule for the actual module execution code
    public void execute(boolean blockCallerThread) {        
        // start the execution in a new thread
         = new Thread(
            new Runnable() {
                public void run() {
                	runModule();
                }
            }
        );
        .setName("Module execution thread");
        .setDaemon(false);
        .start();
        
        // wait until that thread finished if blockCallerThread is true
        // this is used for testing, as element tests finish execution after the
        // initial element test thread returns
        if (blockCallerThread) {
        	while (.isAlive()) {
        		try {
        			.join();
        		} catch (InterruptedException ie) {
        			.error("Interrupted while waiting for execution thread to finish"ie);
        		}
        	}
        }
    }
    
    
Runs a module session. This method is executed from a separate thread.
    private void runModule() {
    	// initialize the execution context
    	
        // run until we get signaled to not run any more tasks
         = true
        
    	// open the view
        if ( != null) {
        	.open();
        }
        
        // Inform external listeners about execution start
    	
    	// execute elements as long as the module should continue
        while () {
        	runElement();
        }
        
        // make sure the session is closed at the end if not canceled
        .markSessionEnd();
        
        checkCloseSession();
        
        // inform listeners about execution end
        
    	// close the view
        if ( != null) {
             = null;
        }
        
        // make sure a new thread can be created
        .clear();
         = null;
    }

    
Executes a full element execution cycle consisting of phase executables, and a single executable execution.
    private void runElement() {
    	// reset the finish current element execution flag
    	 = true;
   
        // find the next element to execute or exit if none could be found
    	
    	if (! ) {
    		return;
    	}
    	
    	// fetch the new execution element
        Element newElement = .getTop();
        Executable newExecutable = null;
        if (newElement == null) {
        	 = false;
        	return;
        }
        
        // fetch the executable executable
        newExecutable = newElement.getExecutable();
        if (newExecutable == null) {
        	 = false;
        	return;
        }
        
        // make sure we have an open session
        if () {
        	checkOpenSession();
        }
        
        if () {
        }
        
        // run the executable
        if () {
        	runExecutable(newElementnewExecutable);
        }
        
        if () {
        }
        
        // check whether the session should be closed
        if () {
        	checkCloseSession();
        }
    }
    
    
Executes an ExecutableElement
    private void runExecutable(Element elementExecutable executable) {
    	 = element;
    	 = executable;
    	
        // first inform the task execution listeners
        // execute the executable
        if () {
	        executable.setExecutionContext();
        	executable.execute();
        	executable.setExecutionContext(null);
        }
        // give listeners a chance to change trial data or do other work
        if () {
        }
        
        // store current execution data
    	// do some cleanup
 
        // clear what we just executed
         = null;
         = null;
    }
        
    // Session management
    
    
Checks whether a session is open, creates a new session if not.
    private void checkOpenSession() {
    	if (.openSession()) {
        }
    }
    
    
Checks whether the session closure is requested. Closes the session in that case.
    private void checkCloseSession() {
    		// execute pre phase elements, inform listeners and execute post phase elements
            
            // close session
            .closeSession();
    	}
    }
    
    // Selector strategy
    
Set the selector strategy.
	public void setExecutionStrategy(ExecutionStrategy selectorStrategy) {
		this. = selectorStrategy;
	}

Get the selector strategy.
	}
    
    // Phase Executables
    
    
Phase executable element support.
    private void executePhaseExecutables(ExecutionPhase phase) {
    	.setPhase(phase);
    	for (PhaseRunnableData phaseRunnableData : .getPhaseExecutablesData(phase)) {
    		if (!  || ! ) {
    			return;
    		}
    		
    		// execute the executable
    		 = phaseRunnableData.getPhaseExecutable();
    		 = null;
    		if (! phaseRunnableData.recurring) {
    		}
    	}
    }

    
Stops the module.
    private void stopModuleImpl() {
    	 = false;
         = false;
        stopCurrentCycle();
    }
    
    private void stopCurrentCycle() {
        // stop a currently running task
        Executable executable = ;
        if (executable != null) {
            .info("cancelTask: Cancelling executable " + executable.getId());
            executable.cancel();
        }
        
        // stop currently running phase executable
        PhaseRunnable phaseRunnable = ;
        if (phaseRunnable != null) {
        	.info("stopping current phase executable " + phaseRunnable.toString());
        	phaseRunnable.stop();
        }
    }
    
    // Helper methods
    
    
Sets a given phase and informs all listeners.
    private void setPhaseAndInform(ExecutionPhase phase) {
    	.setPhase(phase);
    }    
    
    // Executor interface
    
    
Stops the current element execution. This will stop the execution of the current element and issue the next element to be scheduled using the outcome provided
    private void stopCurrentElementExecutionImpl() {
    	 = false;
    }
    
    
Finishes the execution of the current executable
	public void stopCurrentElementExecution() {
	}

Stops the current execution when called.
	public void stopExecution() {
	}

Get the manager that holds the PhaseExecutables. Contrary to ExecutableElements, a PhaseRunnable is run by the executor directly without firing any events before or after execution. It can be used to pause the executor or do other quick work in between elements.
	}

Get the execution tree this executor works on.
		return ;
	}
	}
	}
	}
	}
New to GrepCode? Check out our FAQ X