00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 package org.objectweb.proactive.core.process.nordugrid;
00032
00033 import java.io.BufferedReader;
00034 import java.io.BufferedWriter;
00035 import java.io.File;
00036 import java.io.FileReader;
00037 import java.io.FileWriter;
00038 import java.util.ArrayList;
00039 import java.util.Random;
00040
00041 import org.apache.log4j.Logger;
00042 import org.objectweb.proactive.ProActive;
00043 import org.objectweb.proactive.core.ProActiveException;
00044 import org.objectweb.proactive.core.descriptor.data.ProActiveDescriptor;
00045 import org.objectweb.proactive.core.process.AbstractExternalProcessDecorator;
00046 import org.objectweb.proactive.core.process.UniversalProcess;
00047 import org.objectweb.proactive.core.process.filetransfer.FileTransferDefinition;
00048 import org.objectweb.proactive.core.process.filetransfer.FileTransferDefinition.FileDescription;
00049 import org.objectweb.proactive.core.process.filetransfer.FileTransferWorkShop;
00050 import org.objectweb.proactive.core.util.log.Loggers;
00051 import org.objectweb.proactive.core.util.log.ProActiveLogger;
00052
00053
00061 public class NGProcess extends AbstractExternalProcessDecorator {
00062 private static final String FILE_SEPARATOR = System.getProperty(
00063 "file.separator");
00064 private static final String DEFAULT_SCRIPT_LOCATION = System.getProperty(
00065 "user.home") + FILE_SEPARATOR + "ProActive" + FILE_SEPARATOR +
00066 "scripts" + FILE_SEPARATOR + "unix" + FILE_SEPARATOR + "cluster" +
00067 FILE_SEPARATOR + "ngStartRuntime.sh ";
00068 public final static String DEFAULT_NGPATH = "ngsub";
00069 protected String count = "1";
00070 protected String stderr = null;
00071 protected String stdout = null;
00072 protected String queue = null;
00073 protected String jobname = null;
00074 protected String executable_path = DEFAULT_SCRIPT_LOCATION;
00075 protected String tmp_executable;
00076 protected String DEFAULT_INPUT_FILE = "(inputfiles = ";
00077 protected String inputFiles;
00078 protected ArrayList<String> command_buffer;
00079
00080
00081
00082
00083
00087 public NGProcess() {
00088 super();
00089 setCompositionType(GIVE_COMMAND_AS_PARAMETER);
00090 this.command_path = DEFAULT_NGPATH;
00091 this.hostname = null;
00092 FILE_TRANSFER_DEFAULT_PROTOCOL = "nordugrid";
00093 }
00094
00095 public void setCount(String count) {
00096 this.count = count;
00097 }
00098
00099 public void setQueue(String queue) {
00100 this.queue = queue;
00101 }
00102
00103 public void setStderr(String stderr) {
00104 this.stderr = stderr;
00105 }
00106
00107 public void setStdout(String stdout) {
00108 this.stdout = stdout;
00109 }
00110
00111 public void setJobname(String jobname) {
00112 this.jobname = jobname;
00113 }
00114
00115 protected String internalBuildCommand() {
00116 buildExecutable();
00117 command_buffer = new ArrayList<String>();
00118 return buildNGSUBCommand() + " " + buildXRSLCommand();
00119 }
00120
00121 protected void internalStartProcess(String xRslCommand)
00122 throws java.io.IOException {
00123 String[] ng_command = command_buffer.toArray(new String[] { });
00124 int j = new Integer(count).intValue();
00125
00126 for (int i = 0; i < j; i++) {
00127
00128
00129
00130 try {
00131 externalProcess = Runtime.getRuntime().exec(ng_command);
00132 java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(
00133 externalProcess.getInputStream()));
00134 java.io.BufferedReader err = new java.io.BufferedReader(new java.io.InputStreamReader(
00135 externalProcess.getErrorStream()));
00136 java.io.BufferedWriter out = new java.io.BufferedWriter(new java.io.OutputStreamWriter(
00137 externalProcess.getOutputStream()));
00138 handleProcess(in, out, err);
00139 } catch (java.io.IOException e) {
00140 isFinished = true;
00141
00142 e.printStackTrace();
00143 }
00144 }
00145 }
00146
00147 protected boolean internalFileTransferDefaultProtocol() {
00148 FileTransferWorkShop fts = getFileTransferWorkShopDeploy();
00149 FileTransferDefinition[] ftDefinitions = fts.getAllFileTransferDefinitions();
00150 Logger fileTransferLogger = ProActiveLogger.getLogger(Loggers.DEPLOYMENT_FILETRANSFER);
00151 StringBuilder sb = new StringBuilder();
00152 for (int i = 0; i < ftDefinitions.length; i++) {
00153
00154 FileDescription[] files = ftDefinitions[i].getAll();
00155 for (int j = 0; j < files.length; j++) {
00156 String fullfilename = fts.getAbsoluteSrcPath(files[j]);
00157
00158
00159 if (!FileTransferWorkShop.isLocalReadable(fullfilename) &&
00160 !FileTransferWorkShop.isRemote(fullfilename)) {
00161 logger.info(fullfilename);
00162 if (fileTransferLogger.isDebugEnabled()) {
00163 fileTransferLogger.debug(
00164 "Skiping. Unreadable for FileTransfer:" +
00165 fullfilename);
00166 }
00167 continue;
00168 }
00169
00170 sb.append("(\"" + files[j].getDestName() + "\" \"" +
00171 fullfilename + "\")");
00172 }
00173 }
00174 if (fileTransferLogger.isDebugEnabled()) {
00175 fileTransferLogger.debug("NorduGrid FileTransfer:" + sb.toString());
00176 }
00177 if (sb.length() > 0) {
00178 inputFiles = sb.toString();
00179 }
00180
00181
00182
00183 return true;
00184 }
00185
00186 public String getProcessId() {
00187 return "nordugrid_" + targetProcess.getProcessId();
00188 }
00189
00190 public int getNodeNumber() {
00191 return (new Integer(count).intValue());
00192 }
00193
00194 public UniversalProcess getFinalProcess() {
00195 checkStarted();
00196 return targetProcess.getFinalProcess();
00197 }
00198
00202 public static void main(String[] args) {
00203 try {
00204 ProActiveDescriptor pad = ProActive.getProactiveDescriptor(
00205 "/user/rquilici/home/ProActive/descriptors/nqueen.xml");
00206 pad.activateMappings();
00207 } catch (ProActiveException e) {
00208
00209 e.printStackTrace();
00210 }
00211 }
00212
00213 public void setExecutable(String exec) {
00214 this.executable_path = exec;
00215 }
00216
00217 private String buildNGSUBCommand() {
00218 command_buffer.add(command_path);
00219 command_buffer.add("-c");
00220 command_buffer.add(hostname);
00221 return command_path + " -c " + hostname;
00222 }
00223
00224 private String buildXRSLCommand() {
00225
00226
00227 String xRSL_command = "&(executable=" + tmp_executable + ")";
00228 if (jobname != null) {
00229 xRSL_command = xRSL_command + "(jobname=" + jobname + ")";
00230 }
00231 if (stdout != null) {
00232 xRSL_command = xRSL_command + "(stdout=" + stdout + ")";
00233 }
00234
00235 if (stderr != null) {
00236 xRSL_command = xRSL_command + "(stderr=" + stderr + ")";
00237 }
00238
00239 if (queue != null) {
00240 xRSL_command = xRSL_command + "(queue=" + queue + ")";
00241 }
00242 if (inputFiles != null) {
00243 xRSL_command = xRSL_command + DEFAULT_INPUT_FILE + inputFiles +
00244 ")";
00245 }
00246
00247
00248
00249
00250
00251 if (logger.isDebugEnabled()) {
00252 logger.debug(xRSL_command);
00253 }
00254 command_buffer.add(xRSL_command);
00255 return xRSL_command;
00256 }
00257
00258 private void buildExecutable() {
00259 File tmp_executableFile;
00260 Random random = new Random();
00261 int index = executable_path.lastIndexOf("/");
00262 String executable = executable_path.substring(index + 1);
00263 this.tmp_executable = "tmp_" + executable + random.nextInt();
00264 String tmp_executable_path = executable_path.replace(executable,
00265 tmp_executable);
00266
00267
00268 try {
00269 tmp_executableFile = new File(tmp_executable_path);
00270 if (tmp_executableFile.exists()) {
00271 tmp_executableFile.delete();
00272 }
00273 tmp_executableFile.deleteOnExit();
00274 BufferedReader reader = new BufferedReader(new FileReader(
00275 executable_path));
00276
00277 BufferedWriter writer = new BufferedWriter(new FileWriter(
00278 tmp_executableFile));
00279 while (true) {
00280 String line = reader.readLine();
00281 if (line == null) {
00282 break;
00283 }
00284 writer.write(line);
00285 writer.newLine();
00286 }
00287 reader.close();
00288
00289 writer.write(targetProcess.getCommand());
00290 writer.close();
00291 } catch (Exception e) {
00292 e.printStackTrace();
00293 }
00294 if (!tmp_executable_path.startsWith("file://")) {
00295
00296
00297
00298 tmp_executable_path = "file://" + tmp_executable_path;
00299 }
00300
00301
00302 if (inputFiles != null) {
00303 inputFiles = inputFiles + "(\"" + tmp_executable + "\" \"" +
00304 tmp_executable_path + "\")";
00305 } else {
00306 inputFiles = "(\"" + tmp_executable + "\" \"" +
00307 tmp_executable_path + "\")";
00308 }
00309 }
00310 }