org/objectweb/proactive/core/process/rsh/maprsh/MapRshProcess.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
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     //private String logFile = System.getProperty("user.home")+System.getProperty("file.separator")+"oasisgridlog.txt";
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     // -- PROTECTED METHODS -----------------------------------------------
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             //System.out.println(scriptText);
00150             scriptText = appendJavaCommand(scriptText);
00151             if (logger.isDebugEnabled()) {
00152                 logger.debug(scriptText);
00153             }
00154             b = scriptText.getBytes();
00155             // script.delete();
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         //System.out.println(newScriptText);
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             //ExternalProcess process = new SimpleExternalProcess("ls -la");
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 }

Generated on Mon Jan 22 15:16:09 2007 for ProActive by  doxygen 1.5.1