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; 00032 00033 import org.objectweb.proactive.core.process.filetransfer.FileTransferWorkShop; 00034 import org.objectweb.proactive.core.util.HostsInfos; 00035 import org.objectweb.proactive.core.util.UrlBuilder; 00036 import org.objectweb.proactive.core.util.log.Loggers; 00037 import org.objectweb.proactive.core.util.log.ProActiveLogger; 00038 00039 00040 public abstract class AbstractUniversalProcess implements UniversalProcess { 00041 protected static final String LOCALHOST = getLocalHost(); 00042 public final static String DEFAULT_USERNAME = System.getProperty( 00043 "user.name"); 00044 public final static String DEFAULT_HOSTNAME = LOCALHOST; 00045 protected String hostname = DEFAULT_HOSTNAME; 00046 protected String username; 00047 protected String[] environment; 00048 protected String command; 00049 protected String command_path; 00050 protected boolean isStarted; 00051 protected boolean isFinished; 00052 protected String certificateLocation; 00053 protected String privateKeyLocation; 00054 protected String securityFile; 00055 00056 // 00057 // -- CONSTRUCTORS ----------------------------------------------- 00058 // 00059 protected AbstractUniversalProcess() { 00060 } 00061 00062 // 00063 // -- PUBLIC METHODS ----------------------------------------------- 00064 // 00065 // 00066 // -- implements UniversalProcess ----------------------------------------------- 00067 // 00068 public String getCommand() { 00069 if (isStarted) { 00070 // is started we cache the command 00071 if (command == null) { 00072 command = buildCommand(); 00073 } 00074 return command; 00075 } else { 00076 return buildCommand(); 00077 } 00078 } 00079 00080 public void startFileTransfer() { 00081 if (ProActiveLogger.getLogger(Loggers.DEPLOYMENT_FILETRANSFER) 00082 .isDebugEnabled()) { 00083 ProActiveLogger.getLogger(Loggers.DEPLOYMENT_FILETRANSFER).debug("FileTransfer initializations "); 00084 } 00085 00086 FileTransferWorkShop ftwDeploy = getFileTransferWorkShopDeploy(); 00087 FileTransferWorkShop ftwRetrieve = getFileTransferWorkShopRetrieve(); 00088 00089 //Set process envirorment information into the FileTransferWorkshop 00090 pushProcessAttributes(ftwDeploy.dstInfoParams); 00091 pushProcessAttributes(ftwRetrieve.srcInfoParams); 00092 00093 internalStartFileTransfer(ftwDeploy); 00094 } 00095 00099 abstract FileTransferWorkShop getFileTransferWorkShopDeploy(); 00100 00104 abstract FileTransferWorkShop getFileTransferWorkShopRetrieve(); 00105 00106 public void setEnvironment(String[] environment) { 00107 checkStarted(); 00108 this.environment = environment; 00109 } 00110 00111 public String[] getEnvironment() { 00112 return environment; 00113 } 00114 00115 public String getHostname() { 00116 return hostname; 00117 } 00118 00119 public void setHostname(String hostname) { 00120 checkStarted(); 00121 if (hostname == null) { 00122 throw new NullPointerException(); 00123 } 00124 this.hostname = hostname; 00125 } 00126 00127 public String getUsername() { 00128 return username; 00129 } 00130 00131 public void setUsername(String username) { 00132 checkStarted(); 00133 this.username = username; 00134 } 00135 00136 public void startProcess() throws java.io.IOException { 00137 checkStarted(); 00138 isStarted = true; 00139 if (username != null) { 00140 HostsInfos.setUserName(hostname, username); 00141 } 00142 00143 //before starting the process we execute the filetransfer 00144 startFileTransfer(); 00145 00146 if (logger.isDebugEnabled()) { 00147 logger.debug(getCommand()); 00148 } 00149 internalStartProcess(getCommand()); 00150 } 00151 00152 public void stopProcess() { 00153 if (!isStarted) { 00154 throw new IllegalStateException("Process not yet started"); 00155 } 00156 if (isFinished) { 00157 return; 00158 } 00159 internalStopProcess(); 00160 } 00161 00162 public int waitFor() throws InterruptedException { 00163 return internalWaitFor(); 00164 } 00165 00166 public int exitValue() throws IllegalThreadStateException { 00167 return internalExitValue(); 00168 } 00169 00170 public boolean isStarted() { 00171 return isStarted; 00172 } 00173 00174 public boolean isFinished() { 00175 return isFinished; 00176 } 00177 00178 public boolean isHierarchical() { 00179 return false; 00180 } 00181 00182 public boolean isSequential() { 00183 return false; 00184 } 00185 00186 public boolean isDependent() { 00187 return false; 00188 } 00189 00190 public void setCommandPath(String path) { 00191 this.command_path = path; 00192 } 00193 00194 public String getCommandPath() { 00195 return command_path; 00196 } 00197 00198 public String toString() { 00199 StringBuilder sb = new StringBuilder(); 00200 toString(sb); 00201 return sb.toString(); 00202 } 00203 00204 public void setStarted(boolean isStarted) { 00205 this.isStarted = isStarted; 00206 } 00207 00208 public void setFinished(boolean isFinished) { 00209 this.isFinished = isFinished; 00210 } 00211 00212 // 00213 // -- PROTECTED METHODS ----------------------------------------------- 00214 // 00215 protected void toString(StringBuilder sb) { 00216 sb.append("Process "); 00217 sb.append(this.getClass().getName()); 00218 sb.append(" hostname="); 00219 sb.append(hostname); 00220 sb.append(" username="); 00221 sb.append(username); 00222 sb.append(" isStarted="); 00223 sb.append(isStarted); 00224 sb.append(" isFinished="); 00225 sb.append(isFinished); 00226 sb.append("\n command="); 00227 sb.append(buildCommand()); 00228 if (environment != null) { 00229 sb.append("\n environment="); 00230 for (int i = 0; i < environment.length; i++) { 00231 sb.append("\n variable["); 00232 sb.append(i); 00233 sb.append("]="); 00234 sb.append(environment[i]); 00235 } 00236 } 00237 sb.append("\n"); 00238 } 00239 00240 protected void checkStarted() { 00241 if (isStarted) { 00242 throw new IllegalStateException("Process already started"); 00243 } 00244 } 00245 00246 protected abstract String buildCommand(); 00247 00248 protected abstract void internalStartProcess(String commandToExecute) 00249 throws java.io.IOException; 00250 00254 protected abstract void internalStartFileTransfer(FileTransferWorkShop fts); 00255 00256 protected abstract void internalStopProcess(); 00257 00258 protected abstract int internalWaitFor() throws InterruptedException; 00259 00260 protected abstract int internalExitValue() 00261 throws IllegalThreadStateException; 00262 00274 protected void pushProcessAttributes( 00275 FileTransferWorkShop.StructureInformation infoParams) { 00276 if ((infoParams.getUsername().length() <= 0) && (username != null) && 00277 (username.length() > 0)) { 00278 infoParams.setUsername(username); 00279 } 00280 00281 if ((infoParams.getHostname().length() <= 0) && (hostname != null) && 00282 (hostname.length() > 0) && 00283 !hostname.equalsIgnoreCase(DEFAULT_HOSTNAME)) { 00284 infoParams.setHostname(hostname); 00285 } 00286 } 00287 00288 // -- PRIVATE METHODS ----------------------------------------------- 00289 // 00290 private static String getLocalHost() { 00291 try { 00292 return UrlBuilder.getHostNameorIP(java.net.InetAddress.getLocalHost()); 00293 } catch (java.net.UnknownHostException e) { 00294 return "localhost"; 00295 } 00296 } 00297 }