1 4 package com.tc.objectserver.control; 5 6 import org.apache.commons.lang.ArrayUtils; 7 8 import com.tc.admin.TCStop; 9 import com.tc.config.Directories; 10 import com.tc.config.schema.setup.StandardTVSConfigurationSetupManagerFactory; 11 import com.tc.process.LinkedJavaProcess; 12 import com.tc.process.StreamCopier; 13 import com.tc.server.TCServerMain; 14 15 import java.io.File ; 16 import java.io.FileNotFoundException ; 17 import java.io.FileOutputStream ; 18 import java.util.ArrayList ; 19 import java.util.Collection ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 23 public class ExtraProcessServerControl extends ServerControlBase { 24 private final String name; 25 private final boolean mergeOutput; 26 27 protected LinkedJavaProcess process; 28 protected final String configFileLoc; 29 protected final List jvmArgs; 30 private final File runningDirectory; 31 private File out; 32 private FileOutputStream fileOut; 33 private StreamCopier outCopier; 34 private StreamCopier errCopier; 35 36 public ExtraProcessServerControl(String host, int dsoPort, int adminPort, String configFileLoc, boolean mergeOutput) 37 throws FileNotFoundException { 38 this(new DebugParams(), host, dsoPort, adminPort, configFileLoc, mergeOutput); 39 } 40 41 public ExtraProcessServerControl(DebugParams debugParams, String host, int dsoPort, int adminPort, 42 String configFileLoc, boolean mergeOutput) throws FileNotFoundException { 43 this(debugParams, host, dsoPort, adminPort, configFileLoc, null, Directories.getInstallationRoot(), mergeOutput); 47 } 48 49 public ExtraProcessServerControl(DebugParams debugParams, String host, int dsoPort, int adminPort, 50 String configFileLoc, File runningDirectory, File installationRoot, 51 boolean mergeOutput) { 52 super(host, dsoPort, adminPort); 53 this.jvmArgs = new ArrayList (); 54 this.configFileLoc = configFileLoc; 55 this.mergeOutput = mergeOutput; 56 this.name = "DSO process @ " + getHost() + ":" + getDsoPort(); 57 this.runningDirectory = runningDirectory; 58 jvmArgs.add("-D" + Directories.TC_INSTALL_ROOT_PROPERTY_NAME + "=" + installationRoot); 59 jvmArgs.add("-D" + Directories.TC_INSTALL_ROOT_IGNORE_CHECKS_PROPERTY_NAME + "=true"); 60 debugParams.addDebugParamsTo(jvmArgs); 61 } 62 63 public ExtraProcessServerControl(DebugParams debugParams, String host, int dsoPort, int adminPort, 64 String configFileLoc, File runningDirectory, File installationRoot, 65 boolean mergeOutput, List jvmArgs, String undefString) { 66 super(host, dsoPort, adminPort); 67 68 this.jvmArgs = new ArrayList (); 69 for (Iterator i = jvmArgs.iterator(); i.hasNext();) { 70 String next = (String ) i.next(); 71 if (!next.equals(undefString)) { 72 this.jvmArgs.add(next); 73 } 74 } 75 76 this.configFileLoc = configFileLoc; 77 this.mergeOutput = mergeOutput; 78 this.name = "DSO process @ " + getHost() + ":" + getDsoPort(); 79 this.runningDirectory = runningDirectory; 80 this.jvmArgs.add("-D" + Directories.TC_INSTALL_ROOT_PROPERTY_NAME + "=" + installationRoot); 81 this.jvmArgs.add("-D" + Directories.TC_INSTALL_ROOT_IGNORE_CHECKS_PROPERTY_NAME + "=true"); 82 debugParams.addDebugParamsTo(jvmArgs); 83 } 84 85 public void mergeSTDOUT() { 86 this.process.mergeSTDOUT(); 87 } 88 89 public void mergeSTDERR() { 90 this.process.mergeSTDERR(); 91 } 92 93 protected String getMainClassName() { 94 return TCServerMain.class.getName(); 95 } 96 97 protected String [] getMainClassArguments() { 98 return new String [] { StandardTVSConfigurationSetupManagerFactory.CONFIG_SPEC_ARGUMENT_WORD, this.configFileLoc }; 99 } 100 101 public void writeOutputTo(File outputFile) { 102 if (mergeOutput) { throw new IllegalStateException (); } 103 this.out = outputFile; 104 } 105 106 public void start(long timeout) throws Exception { 107 System.err.println("Starting " + this.name + ": jvmArgs=" + jvmArgs + ", main=" + getMainClassName() 108 + ", main args=" + ArrayUtils.toString(getMainClassArguments())); 109 process = createLinkedJavaProcess(); 110 process.setJavaArguments((String []) jvmArgs.toArray(new String [jvmArgs.size()])); 111 process.start(); 112 if (mergeOutput) { 113 mergeSTDOUT(); 114 mergeSTDERR(); 115 } else if (out != null) { 116 fileOut = new FileOutputStream (out); 117 outCopier = new StreamCopier(process.STDOUT(), fileOut); 118 errCopier = new StreamCopier(process.STDERR(), fileOut); 119 outCopier.start(); 120 errCopier.start(); 121 } 122 waitUntilStarted(timeout); 123 System.err.println(this.name + " started."); 124 } 125 126 protected LinkedJavaProcess createLinkedJavaProcess() { 127 LinkedJavaProcess rv = new LinkedJavaProcess(getMainClassName(), getMainClassArguments()); 128 rv.setDirectory(this.runningDirectory); 129 return rv; 130 } 131 132 public void crash() throws Exception { 133 System.out.println("Crashing server " + this.name + "..."); 134 if (process != null) { 135 process.destroy(); 136 waitUntilShutdown(); 137 } 138 System.out.println(this.name + " crashed."); 139 } 140 141 public void attemptShutdown() throws Exception { 142 System.out.println("Shutting down server " + this.name + "..."); 143 TCStop stopper = new TCStop(getHost(), getAdminPort()); 144 stopper.stop(); 145 } 146 147 public void shutdown() throws Exception { 148 attemptShutdown(); 149 waitUntilShutdown(); 150 System.out.println(this.name + " stopped."); 151 } 152 153 private void waitUntilStarted(long timeout) throws Exception { 154 long timeoutTime = System.currentTimeMillis() + timeout; 155 while (true) { 156 if (isRunning()) return; 157 if (System.currentTimeMillis() > timeoutTime) { 158 throw new RuntimeException ("Timeout occurred waiting for server to start: " + timeout + " ms."); 160 } 161 Thread.sleep(1000); 162 } 163 } 164 165 public void waitUntilShutdown() throws Exception { 166 while (isRunning()) { 167 Thread.sleep(1000); 168 } 169 } 170 171 public int waitFor() throws Exception { 172 int rv = process.waitFor(); 173 174 if (outCopier != null) { 175 try { 176 outCopier.join(); 177 } catch (Exception e) { 178 e.printStackTrace(); 179 } 180 } 181 182 if (errCopier != null) { 183 try { 184 errCopier.join(); 185 } catch (Exception e) { 186 e.printStackTrace(); 187 } 188 } 189 190 if (fileOut != null) { 191 try { 192 fileOut.close(); 193 } catch (Exception e) { 194 e.printStackTrace(); 195 } 196 } 197 198 return rv; 199 } 200 201 public static final class DebugParams { 202 private final boolean debug; 203 private final int debugPort; 204 205 public DebugParams() { 206 this(false, 0); 207 } 208 209 public DebugParams(int debugPort) { 210 this(true, debugPort); 211 } 212 213 private DebugParams(boolean debug, int debugPort) { 214 if (debugPort < 0) throw new AssertionError ("Debug port must be >= 0: " + debugPort); 215 this.debugPort = debugPort; 216 this.debug = debug; 217 } 218 219 private void addDebugParamsTo(Collection jvmArgs) { 220 if (debug) { 221 jvmArgs.add("-Xdebug"); 222 String address = debugPort > 0 ? "address=" + debugPort + "," : ""; 223 jvmArgs.add("-Xrunjdwp:transport=dt_socket," + address + "server=y,suspend=n"); 224 } 225 } 226 } 227 228 } 229 | Popular Tags |