package gri.gridp;
import gri.gridp.modules.managers.ModuleManager;
import gri.gridp.modules.managers.SimpleModuleManager;
import gri.gridp.tasks.RunScriptTask;
import gri.gridp.tasks.ssh.SshScriptTask;
import gri.gridp.tasks.ssh.SessionFactory;
import gri.gridp.tasks.managers.GridpTaskManager;
import gri.tasks.managers.TaskManager;
import java.io.File;
/**
* Utility class which allows for easy construction of a ModuleManager
* and TaskManager.
*
* @author rogersda
*/
public class GridpFactory {
public static final int WINDOWS = 1;
public static final int UNIX = 2;
// ----------------------------------------------------------- Properties
File moduleFolder;
String scriptFile;
String execCmd;
// --------------------------------------------------------- Constructors
public GridpFactory(File moduleFolder, String scriptFile, String execCmd) {
this.moduleFolder = moduleFolder;
this.scriptFile = scriptFile;
this.execCmd = execCmd;
}
public GridpFactory(File moduleFolder, int mode) {
this(moduleFolder, null, null);
setDefaults(mode);
}
public GridpFactory(File moduleFolder) {
this(moduleFolder, WINDOWS);
}
public GridpFactory(String moduleFolder) {
this(new File(moduleFolder));
}
// ------------------------------------------------------------ Accessors
/**
* Sets the folder containing the modules we will load
*/
public void setModuleFolder(File folder) {
this.moduleFolder = folder;
}
public File getModuleFolder() {return moduleFolder;}
/**
* Sets the name of the script file that will be created to
* run jobs. e.g. "job.bash" or "job.bat"
*/
public void setScriptFile(String file) {
this.scriptFile = file;
}
public String getScriptFile() {return scriptFile;}
/**
* Sets the command that will be invoked to run the script file.
* "bash job.bash" or "cmd.exe /C job.bat"
*/
public void setExecutionCommand(String execCmd) {
this.execCmd = execCmd;
}
public String getExecutionCommand() {return execCmd;}
public void setWindowsDefaults() {
this.scriptFile = "job.bat";
this.execCmd = "cmd.exe /C job.bat";
}
public void setUnixDefaults() {
this.scriptFile = "job.bash";
this.execCmd = "bash job.bash";
}
public void setDefaults(int mode) {
if (mode == WINDOWS)
setWindowsDefaults();
else if (mode == UNIX)
setUnixDefaults();
else
throw new IllegalArgumentException("Unknown mode: " + mode);
}
// ------------------------------------------------------- Implementation
/**
* Creates a local TaskManager for GRIDP tasks
*/
public TaskManager getTaskManager() {
ModuleManager moduleManager = getModuleManager();
RunScriptTask runScript = new RunScriptTask(scriptFile, execCmd);
return new GridpTaskManager(moduleManager, runScript);
}
/**
* Creates an SSH TaskManager to run GRIDP tasks on remote computers
* over SSH. The remote computer and login information are provided
* by the SessionFactory.
*/
public TaskManager getSshTaskManager(SessionFactory sessionFactory) {
ModuleManager moduleManager = getModuleManager();
SshScriptTask runScript = new SshScriptTask(sessionFactory, scriptFile, execCmd);
return new GridpTaskManager(moduleManager, runScript);
}
public ModuleManager getModuleManager() {
return new SimpleModuleManager(moduleFolder);
}
}