1 31 package org.objectweb.proactive.core.process.rsh.maprsh; 32 33 import org.objectweb.proactive.core.process.AbstractExternalProcessDecorator; 34 import org.objectweb.proactive.core.process.ExternalProcess; 35 import org.objectweb.proactive.core.process.JVMProcess; 36 import org.objectweb.proactive.core.process.JVMProcessImpl; 37 38 39 58 59 public class MapRshProcess extends AbstractExternalProcessDecorator { 60 private static final String FILE_SEPARATOR = System.getProperty( 61 "file.separator"); 62 private static final String DEFAULT_SCRIPT_LOCATION = System.getProperty( 63 "user.home") + FILE_SEPARATOR + "ProActive" + FILE_SEPARATOR + 64 "scripts" + FILE_SEPARATOR + "unix" + FILE_SEPARATOR + 65 "gridexperiment" + FILE_SEPARATOR + "oasis-exp"; 66 protected String scriptLocation = DEFAULT_SCRIPT_LOCATION; 67 private String parallelize = null; 68 69 public MapRshProcess() { 71 super(); 72 setCompositionType(GIVE_COMMAND_AS_PARAMETER); 73 } 74 75 public MapRshProcess(ExternalProcess targetProcess) { 76 super(targetProcess); 77 setCompositionType(GIVE_COMMAND_AS_PARAMETER); 78 } 79 80 81 85 public void setParallelization(String parallelize) { 86 this.parallelize = parallelize; 87 } 88 89 93 public String getParallelization() { 94 return this.parallelize; 95 } 96 97 98 102 public void setScriptLocation(String scriptLocation) { 103 this.scriptLocation = scriptLocation; 104 } 105 106 107 111 public String getScriptLocation() { 112 return scriptLocation; 113 } 114 115 protected String internalBuildCommand() { 119 return buildMapRshCommand() + buildEnvironmentCommand(); 120 } 121 122 protected String buildMapRshCommand() { 123 StringBuffer command = new StringBuffer (); 124 try { 125 java.io.File script = new java.io.File (scriptLocation); 126 byte[] b = getBytesFromInputStream(new java.io.FileInputStream ( 127 script)); 128 String scriptText = new String (b); 129 scriptText = removeJavaCommand(scriptText); 130 scriptText = appendJavaCommand(scriptText); 132 if (logger.isDebugEnabled()) { 133 logger.debug(scriptText); 134 } 135 b = scriptText.getBytes(); 136 java.io.OutputStream out = new java.io.BufferedOutputStream (new java.io.FileOutputStream ( 138 script)); 139 out.write(b, 0, b.length); 140 out.flush(); 141 out.close(); 142 } catch (Exception e) { 143 e.printStackTrace(); 144 } 145 command.append("/usr/local/bin/maprsh "); 146 if (parallelize != null) { 147 command.append(parallelize + " "); 148 } 149 command.append(scriptLocation + " " + hostname); 150 if (logger.isDebugEnabled()) { 151 logger.debug(command.toString()); 152 } 153 return command.toString(); 154 } 155 156 157 162 private String appendJavaCommand(String scriptText) { 163 StringBuffer newScriptText = new StringBuffer (scriptText.length()); 164 String targetCommand = targetProcess.getCommand(); 165 166 newScriptText.append(scriptText); 167 newScriptText.append("\ntime " + targetCommand + " ) & \n"); 168 return newScriptText.toString(); 169 } 170 171 172 177 private String removeJavaCommand(String scriptText) { 178 int marker = scriptText.lastIndexOf("}"); 179 String newScriptText = scriptText.substring(0, marker + 1); 180 181 return newScriptText; 183 } 184 185 186 public static void main(String [] args) { 187 try { 188 JVMProcess process = new JVMProcessImpl(new StandardOutputMessageLogger()); 189 process.setParameters("///toto"); 190 191 MapRshProcess maprsh = new MapRshProcess(process); 193 maprsh.setHostname("waha owenii"); 194 maprsh.startProcess(); 195 } catch (Exception e) { 196 e.printStackTrace(); 197 } 198 } 199 200 201 208 private static byte[] getBytesFromInputStream(java.io.InputStream in) 209 throws java.io.IOException { 210 java.io.DataInputStream din = new java.io.DataInputStream (in); 211 byte[] bytecodes = new byte[in.available()]; 212 try { 213 din.readFully(bytecodes); 214 } finally { 215 if (din != null) { 216 din.close(); 217 } 218 } 219 return bytecodes; 220 } 221 } 222 | Popular Tags |