1 31 package org.objectweb.proactive.core.process; 32 33 public abstract class AbstractUniversalProcess implements UniversalProcess { 34 35 protected static final String LOCALHOST = getLocalHost(); 36 37 public final static String DEFAULT_USERNAME = System.getProperty("user.name"); 38 public final static String DEFAULT_HOSTNAME = LOCALHOST; 39 40 protected String hostname = DEFAULT_HOSTNAME; 41 protected String username; 42 43 protected String [] environment; 44 45 protected String command; 46 47 protected boolean isStarted; 48 protected boolean isFinished; 49 50 protected String certificateLocation; 51 protected String privateKeyLocation; 52 protected String securityFile; 53 54 55 59 protected AbstractUniversalProcess() { 60 } 61 62 63 67 71 public String getCommand() { 72 if (isStarted) { 73 if (command == null) command = buildCommand(); 75 return command; 76 } else { 77 return buildCommand(); 78 } 79 } 80 81 public void setEnvironment(String [] environment) { 82 checkStarted(); 83 this.environment = environment; 84 } 85 86 public String [] getEnvironment() { 87 return environment; 88 } 89 90 91 public String getHostname() { 92 return hostname; 93 } 94 95 public void setHostname(String hostname) { 96 checkStarted(); 97 if (hostname == null) throw new NullPointerException (); 98 this.hostname = hostname; 99 } 100 101 public String getUsername() { 102 return username; 103 } 104 105 public void setUsername(String username) { 106 checkStarted(); 107 this.username = username; 108 } 109 110 111 public void startProcess() throws java.io.IOException { 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 ("Process not yet started"); 123 if (isFinished) return; 124 internalStopProcess(); 125 } 126 127 public int waitFor() throws InterruptedException { 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 toString() { 142 StringBuffer sb = new StringBuffer (); 143 toString(sb); 144 return sb.toString(); 145 } 146 147 public void setCertificateLocation(String file) { 149 certificateLocation = file; 150 } 151 152 public String getCertificateLocation() { 153 return certificateLocation; 154 } 155 156 public void setPrivateKeyLocation(String privatekey) { 157 privateKeyLocation = privatekey; 158 } 159 160 public String getPrivateKeyLocation() { 161 return privateKeyLocation; 162 } 163 164 public void setSecurityFile(String securityFile) { 165 this.securityFile = securityFile; 166 } 167 168 public String getSecurityFile() { 169 return securityFile; 170 } 171 172 173 177 protected void toString(StringBuffer 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 ("Process already started"); 205 } 206 207 208 protected abstract String buildCommand(); 209 210 211 protected abstract void internalStartProcess(String commandToExecute) throws java.io.IOException ; 212 213 214 protected abstract void internalStopProcess(); 215 216 protected abstract int internalWaitFor() throws InterruptedException ; 217 218 219 220 223 private static String getLocalHost() { 224 try { 225 return java.net.InetAddress.getLocalHost().getCanonicalHostName(); 226 } catch (java.net.UnknownHostException e) { 227 return "localhost"; 228 } 229 } 230 231 } 232 | Popular Tags |