KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > core > process > ssh > SSHProcess


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.ssh;
32
33 import org.objectweb.proactive.core.process.AbstractExternalProcessDecorator;
34 import org.objectweb.proactive.core.process.SimpleExternalProcess;
35 import org.objectweb.proactive.core.process.ExternalProcess;
36
37 /**
38  * <p>
39  * The SSHProcess class is able to start any class, of the ProActive library,
40  * using ssh protocol.
41  * </p><p>
42  * For instance:
43  * </p><pre>
44  * .......
45  * SSHProcess ssh = new SSHProcess(new SimpleExternalProcess("ls -lsa"));
46  * ssh.setHostname("hostname.domain.fr");
47  * ssh.startProcess();
48  * .....
49  * </pre>
50  * @author ProActive Team
51  * @version 1.0, 2002/09/20
52  * @since ProActive 0.9.4
53  */

54
55 public class SSHProcess extends AbstractExternalProcessDecorator {
56   
57   //
58
// -- CONSTRUCTORS -----------------------------------------------
59
//
60

61   /**
62    * Creates a new SSHProcess
63    * Used with XML Descriptor
64    */

65   public SSHProcess() {
66     super();
67   
68   }
69   
70   /**
71    * Creates a new SSHProcess
72    * @param targetProcess The target process associated to this process. The target process
73    * represents the process that will be launched after logging remote host with ssh protocol
74    */

75   public SSHProcess(ExternalProcess targetProcess) {
76     super(targetProcess);
77   }
78     
79
80   //
81
// -- PUBLIC METHODS -----------------------------------------------
82
//
83

84   public static void main(String JavaDoc[] args) {
85     try {
86
87       SSHProcess ssh = new SSHProcess(new SimpleExternalProcess("ls -lsa"));
88       ssh.setHostname("galere1.inria.fr");
89       ssh.startProcess();
90
91     } catch (Exception JavaDoc e) {
92       e.printStackTrace();
93     }
94   }
95
96
97   //
98
// -- PROTECTED METHODS -----------------------------------------------
99
//
100

101   protected String JavaDoc internalBuildCommand() {
102     return buildSSHCommand()+buildEnvironmentCommand();
103   }
104   
105   
106   protected String JavaDoc buildSSHCommand() {
107     if (IS_WINDOWS_SYSTEM) {
108       return buildWindowsSSHCommand();
109     } else {
110       return buildUnixSSHCommand();
111     }
112   }
113   
114   
115   protected String JavaDoc buildUnixSSHCommand() {
116     StringBuffer JavaDoc command = new StringBuffer JavaDoc();
117     command.append("ssh");
118     // append username
119
if (username != null) {
120       command.append(" -l ");
121       command.append(username);
122     }
123     // append host
124
command.append(" ");
125     command.append(hostname);
126     command.append(" ");
127     if(logger.isDebugEnabled()){
128     logger.debug(command.toString());
129     }
130     return command.toString();
131   }
132   
133   
134   protected String JavaDoc buildWindowsSSHCommand() {
135     StringBuffer JavaDoc command = new StringBuffer JavaDoc();
136     command.append("ssh");
137     command.append(" ");
138     command.append(hostname);
139     // append username
140
if (username != null) {
141       command.append(" -l ");
142       command.append(username);
143     }
144     // append host
145
command.append(" ");
146     return command.toString();
147   }
148   
149   
150   //
151
// -- PRIVATE METHODS -----------------------------------------------
152
//
153

154 }
155
Popular Tags