KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > core > process > AbstractUniversalProcess


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;
32
33 public abstract class AbstractUniversalProcess implements UniversalProcess {
34
35   protected static final String JavaDoc LOCALHOST = getLocalHost();
36
37   public final static String JavaDoc DEFAULT_USERNAME = System.getProperty("user.name");
38   public final static String JavaDoc DEFAULT_HOSTNAME = LOCALHOST;
39
40   protected String JavaDoc hostname = DEFAULT_HOSTNAME;
41   protected String JavaDoc username;
42   
43   protected String JavaDoc[] environment;
44   
45   protected String JavaDoc command;
46   
47   protected boolean isStarted;
48   protected boolean isFinished;
49   
50   protected String JavaDoc certificateLocation;
51   protected String JavaDoc privateKeyLocation;
52   protected String JavaDoc securityFile;
53
54   
55   //
56
// -- CONSTRUCTORS -----------------------------------------------
57
//
58

59   protected AbstractUniversalProcess() {
60   }
61
62   
63   //
64
// -- PUBLIC METHODS -----------------------------------------------
65
//
66

67   //
68
// -- implements UniversalProcess -----------------------------------------------
69
//
70

71   public String JavaDoc getCommand() {
72     if (isStarted) {
73       // is started we cache the command
74
if (command == null) command = buildCommand();
75       return command;
76     } else {
77       return buildCommand();
78     }
79   }
80
81   public void setEnvironment(String JavaDoc[] environment) {
82     checkStarted();
83     this.environment = environment;
84   }
85   
86   public String JavaDoc[] getEnvironment() {
87     return environment;
88   }
89
90
91   public String JavaDoc getHostname() {
92     return hostname;
93   }
94   
95   public void setHostname(String JavaDoc hostname) {
96     checkStarted();
97     if (hostname == null) throw new NullPointerException JavaDoc();
98     this.hostname = hostname;
99   }
100
101   public String JavaDoc getUsername() {
102     return username;
103   }
104   
105   public void setUsername(String JavaDoc username) {
106     checkStarted();
107     this.username = username;
108   }
109
110
111   public void startProcess() throws java.io.IOException JavaDoc {
112     checkStarted();
113     isStarted = true;
114     if (logger.isDebugEnabled()) {
115     logger.debug(getCommand());
116     }
117     internalStartProcess(getCommand());
118   }
119
120
121   public void stopProcess() {
122     if (! isStarted) throw new IllegalStateException JavaDoc("Process not yet started");
123     if (isFinished) return;
124     internalStopProcess();
125   }
126   
127   public int waitFor() throws InterruptedException JavaDoc{
128     return internalWaitFor();
129   }
130
131
132   public boolean isStarted() {
133     return isStarted;
134   }
135
136   public boolean isFinished() {
137     return isFinished;
138   }
139
140   
141   public String JavaDoc toString() {
142     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
143     toString(sb);
144     return sb.toString();
145   }
146   
147   // SECURITY
148
public void setCertificateLocation(String JavaDoc file) {
149          certificateLocation = file;
150      }
151
152      public String JavaDoc getCertificateLocation() {
153          return certificateLocation;
154      }
155
156      public void setPrivateKeyLocation(String JavaDoc privatekey) {
157          privateKeyLocation = privatekey;
158      }
159
160      public String JavaDoc getPrivateKeyLocation() {
161          return privateKeyLocation;
162      }
163
164      public void setSecurityFile(String JavaDoc securityFile) {
165          this.securityFile = securityFile;
166      }
167
168      public String JavaDoc getSecurityFile() {
169          return securityFile;
170      }
171
172   
173   //
174
// -- PROTECTED METHODS -----------------------------------------------
175
//
176

177   protected void toString(StringBuffer JavaDoc sb) {
178     sb.append("Process ");
179     sb.append(this.getClass().getName());
180     sb.append(" hostname=");
181     sb.append(hostname);
182     sb.append(" username=");
183     sb.append(username);
184     sb.append(" isStarted=");
185     sb.append(isStarted);
186     sb.append(" isFinished=");
187     sb.append(isFinished);
188     sb.append("\n command=");
189     sb.append(buildCommand());
190     if (environment != null) {
191       sb.append("\n environment=");
192       for (int i=0; i<environment.length; i++) {
193         sb.append("\n variable[");
194         sb.append(i);
195         sb.append("]=");
196         sb.append(environment[i]);
197       }
198     }
199     sb.append("\n");
200   }
201
202   
203   protected void checkStarted() {
204     if (isStarted) throw new IllegalStateException JavaDoc("Process already started");
205   }
206
207
208   protected abstract String JavaDoc buildCommand();
209   
210
211   protected abstract void internalStartProcess(String JavaDoc commandToExecute) throws java.io.IOException JavaDoc;
212   
213   
214   protected abstract void internalStopProcess();
215   
216   protected abstract int internalWaitFor() throws InterruptedException JavaDoc;
217   
218
219   
220   // -- PRIVATE METHODS -----------------------------------------------
221
//
222

223   private static String JavaDoc getLocalHost() {
224     try {
225       return java.net.InetAddress.getLocalHost().getCanonicalHostName();
226     } catch (java.net.UnknownHostException JavaDoc e) {
227       return "localhost";
228     }
229   }
230   
231 }
232
Popular Tags