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.rsh.maprsh;
00032
00033 import org.objectweb.proactive.core.process.AbstractExternalProcessDecorator;
00034 import org.objectweb.proactive.core.process.ExternalProcess;
00035 import org.objectweb.proactive.core.process.JVMProcess;
00036 import org.objectweb.proactive.core.process.JVMProcessImpl;
00037 import org.objectweb.proactive.core.process.UniversalProcess;
00038
00039
00059 public class MapRshProcess extends AbstractExternalProcessDecorator {
00060 private static final String FILE_SEPARATOR = System.getProperty(
00061 "file.separator");
00062 private static final String DEFAULT_SCRIPT_LOCATION = System.getProperty(
00063 "user.home") + FILE_SEPARATOR + "ProActive" + FILE_SEPARATOR +
00064 "scripts" + FILE_SEPARATOR + "unix" + FILE_SEPARATOR +
00065 "gridexperiment" + FILE_SEPARATOR + "oasis-exp";
00066 protected String scriptLocation = DEFAULT_SCRIPT_LOCATION;
00067 private String parallelize = null;
00068
00069
00070 public MapRshProcess() {
00071 super();
00072 setCompositionType(GIVE_COMMAND_AS_PARAMETER);
00073 }
00074
00075 public MapRshProcess(ExternalProcess targetProcess) {
00076 super(targetProcess);
00077 setCompositionType(GIVE_COMMAND_AS_PARAMETER);
00078 }
00079
00084 public void setParallelization(String parallelize) {
00085 this.parallelize = parallelize;
00086 }
00087
00092 public String getParallelization() {
00093 return this.parallelize;
00094 }
00095
00100 public void setScriptLocation(String scriptLocation) {
00101 this.scriptLocation = scriptLocation;
00102 }
00103
00108 public String getScriptLocation() {
00109 return scriptLocation;
00110 }
00111
00115 public String getProcessId() {
00116 return "maprsh_" + targetProcess.getProcessId();
00117 }
00118
00122 public int getNodeNumber() {
00123 return targetProcess.getNodeNumber();
00124 }
00125
00129 public UniversalProcess getFinalProcess() {
00130 checkStarted();
00131 return targetProcess.getFinalProcess();
00132 }
00133
00134
00135
00136
00137 protected String internalBuildCommand() {
00138 return buildMapRshCommand() + buildEnvironmentCommand();
00139 }
00140
00141 protected String buildMapRshCommand() {
00142 StringBuilder command = new StringBuilder();
00143 try {
00144 java.io.File script = new java.io.File(scriptLocation);
00145 byte[] b = getBytesFromInputStream(new java.io.FileInputStream(
00146 script));
00147 String scriptText = new String(b);
00148 scriptText = removeJavaCommand(scriptText);
00149
00150 scriptText = appendJavaCommand(scriptText);
00151 if (logger.isDebugEnabled()) {
00152 logger.debug(scriptText);
00153 }
00154 b = scriptText.getBytes();
00155
00156 java.io.OutputStream out = new java.io.BufferedOutputStream(new java.io.FileOutputStream(
00157 script));
00158 out.write(b, 0, b.length);
00159 out.flush();
00160 out.close();
00161 } catch (Exception e) {
00162 e.printStackTrace();
00163 }
00164 command.append("/usr/local/bin/maprsh ");
00165 if (parallelize != null) {
00166 command.append(parallelize + " ");
00167 }
00168 command.append(scriptLocation + " " + hostname);
00169 if (logger.isDebugEnabled()) {
00170 logger.debug(command.toString());
00171 }
00172 return command.toString();
00173 }
00174
00180 private String appendJavaCommand(String scriptText) {
00181 StringBuilder newScriptText = new StringBuilder(scriptText.length());
00182 String targetCommand = targetProcess.getCommand();
00183
00184 newScriptText.append(scriptText);
00185 newScriptText.append("\ntime " + targetCommand + " ) & \n");
00186 return newScriptText.toString();
00187 }
00188
00194 private String removeJavaCommand(String scriptText) {
00195 int marker = scriptText.lastIndexOf("}");
00196 String newScriptText = scriptText.substring(0, marker + 1);
00197
00198
00199 return newScriptText;
00200 }
00201
00202 public static void main(String[] args) {
00203 try {
00204 JVMProcess process = new JVMProcessImpl(new StandardOutputMessageLogger());
00205 process.setParameters("///toto");
00206
00207
00208 MapRshProcess maprsh = new MapRshProcess(process);
00209 maprsh.setHostname("waha owenii");
00210 maprsh.startProcess();
00211 } catch (Exception e) {
00212 e.printStackTrace();
00213 }
00214 }
00215
00223 private static byte[] getBytesFromInputStream(java.io.InputStream in)
00224 throws java.io.IOException {
00225 java.io.DataInputStream din = new java.io.DataInputStream(in);
00226 byte[] bytecodes = new byte[in.available()];
00227 try {
00228 din.readFully(bytecodes);
00229 } finally {
00230 if (din != null) {
00231 din.close();
00232 }
00233 }
00234 return bytecodes;
00235 }
00236 }