KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > core > process > rsh > maprsh > MapRshProcess


1 /*
2 * ################################################################
3 *
4 * ProActive: The Java(TM) library for Parallel, Distributed,
5 * Concurrent computing with Security and Mobility
6 *
7 * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8 * Contact: proactive-support@inria.fr
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24 *
25 * Initial developer(s): The ProActive Team
26 * http://www.inria.fr/oasis/ProActive/contacts.html
27 * Contributor(s):
28 *
29 * ################################################################
30 */

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 /**
40  * <p>
41  * The MapRshProcess class is able to start any class, of the ProActive library,
42  * using maprsh.
43  * </p><p>
44  * For instance:
45  * </p><pre>
46  * ..........
47  * JVMProcess process = new JVMProcessImpl(new StandardOutputMessageLogger());
48  * process.setParameters("///toto");
49  * MapRshProcess maprsh = new MapRshProcess(process);
50  * maprsh.setHostname("waha owenii");
51  * maprsh.startProcess();
52  * .....
53  * </pre>
54  * @author ProActive Team
55  * @version 1.0, 2002/09/20
56  * @since ProActive 0.9.4
57  */

58
59 public class MapRshProcess extends AbstractExternalProcessDecorator {
60     private static final String JavaDoc FILE_SEPARATOR = System.getProperty(
61             "file.separator");
62     private static final String JavaDoc 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 JavaDoc scriptLocation = DEFAULT_SCRIPT_LOCATION;
67     private String JavaDoc parallelize = null;
68
69     //private String logFile = System.getProperty("user.home")+System.getProperty("file.separator")+"oasisgridlog.txt";
70
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         /**
82          * Set the -n option with the given parameter for the maprsh command
83          * @param parallelize
84          */

85     public void setParallelization(String JavaDoc parallelize) {
86         this.parallelize = parallelize;
87     }
88
89         /**
90          * Returns the degree of parallelization of maprsh command (value of -n option)
91          * @return String
92          */

93     public String JavaDoc getParallelization() {
94         return this.parallelize;
95     }
96     
97
98     /**
99      * Sets the variable scriptLocation with the given location
100      * @param string
101      */

102     public void setScriptLocation(String JavaDoc scriptLocation) {
103         this.scriptLocation = scriptLocation;
104     }
105     
106
107         /**
108          * Returns the value of scriptLocation
109          * @return String
110          */

111     public String JavaDoc getScriptLocation() {
112         return scriptLocation;
113     }
114
115     //
116
// -- PROTECTED METHODS -----------------------------------------------
117
//
118
protected String JavaDoc internalBuildCommand() {
119         return buildMapRshCommand() + buildEnvironmentCommand();
120     }
121
122     protected String JavaDoc buildMapRshCommand() {
123         StringBuffer JavaDoc command = new StringBuffer JavaDoc();
124         try {
125             java.io.File JavaDoc script = new java.io.File JavaDoc(scriptLocation);
126             byte[] b = getBytesFromInputStream(new java.io.FileInputStream JavaDoc(
127                         script));
128             String JavaDoc scriptText = new String JavaDoc(b);
129             scriptText = removeJavaCommand(scriptText);
130             //System.out.println(scriptText);
131
scriptText = appendJavaCommand(scriptText);
132             if (logger.isDebugEnabled()) {
133                 logger.debug(scriptText);
134             }
135             b = scriptText.getBytes();
136             // script.delete();
137
java.io.OutputStream JavaDoc out = new java.io.BufferedOutputStream JavaDoc(new java.io.FileOutputStream JavaDoc(
138                         script));
139             out.write(b, 0, b.length);
140             out.flush();
141             out.close();
142         } catch (Exception JavaDoc 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     /**
158      * Method appendJavaCommand.
159      * @param scriptText
160      * @return String
161      */

162     private String JavaDoc appendJavaCommand(String JavaDoc scriptText) {
163         StringBuffer JavaDoc newScriptText = new StringBuffer JavaDoc(scriptText.length());
164         String JavaDoc targetCommand = targetProcess.getCommand();
165
166         newScriptText.append(scriptText);
167         newScriptText.append("\ntime " + targetCommand + " ) & \n");
168         return newScriptText.toString();
169     }
170
171
172     /**
173      * Method removeJavaCommand.
174      * @param scriptText
175      * @return String
176      */

177     private String JavaDoc removeJavaCommand(String JavaDoc scriptText) {
178         int marker = scriptText.lastIndexOf("}");
179         String JavaDoc newScriptText = scriptText.substring(0, marker + 1);
180
181         //System.out.println(newScriptText);
182
return newScriptText;
183     }
184     
185
186     public static void main(String JavaDoc[] args) {
187         try {
188             JVMProcess process = new JVMProcessImpl(new StandardOutputMessageLogger());
189             process.setParameters("///toto");
190
191             //ExternalProcess process = new SimpleExternalProcess("ls -la");
192
MapRshProcess maprsh = new MapRshProcess(process);
193             maprsh.setHostname("waha owenii");
194             maprsh.startProcess();
195         } catch (Exception JavaDoc e) {
196             e.printStackTrace();
197         }
198     }
199     
200
201     /**
202     * Returns an array of bytes containing the bytecodes for
203     * the class represented by the InputStream
204     * @param in the inputstream of the class file
205     * @return the bytecodes for the class
206     * @exception java.io.IOException if the class cannot be read
207     */

208     private static byte[] getBytesFromInputStream(java.io.InputStream JavaDoc in)
209         throws java.io.IOException JavaDoc {
210         java.io.DataInputStream JavaDoc din = new java.io.DataInputStream JavaDoc(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