001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.test.server.util;
006:
007: import org.apache.commons.io.FileUtils;
008:
009: import com.tc.process.HeartBeatService;
010: import com.tc.test.TestConfigObject;
011: import com.tc.test.server.appserver.AppServerInstallation;
012: import com.tc.test.server.appserver.AppServerFactory;
013: import com.tc.text.Banner;
014: import com.tc.util.PortChooser;
015: import com.tc.util.concurrent.ThreadUtil;
016: import com.tc.util.runtime.Os;
017:
018: import java.io.File;
019: import java.io.IOException;
020: import java.net.Socket;
021: import java.text.SimpleDateFormat;
022: import java.util.Date;
023:
024: public class AppServerUtil {
025:
026: private static final PortChooser pc = new PortChooser();
027: private static final TestConfigObject config = TestConfigObject
028: .getInstance();
029:
030: public static int getPort() throws Exception {
031: return pc.chooseRandomPort();
032: }
033:
034: public static void waitForPort(int port, long waitTime) {
035: final long timeout = System.currentTimeMillis() + waitTime;
036: while (System.currentTimeMillis() < timeout) {
037: Socket s = null;
038: try {
039: s = new Socket("127.0.0.1", port);
040: return;
041: } catch (IOException ioe) {
042: // try again
043: } finally {
044: if (s != null) {
045: try {
046: s.close();
047: } catch (IOException ioe) {
048: // ignore
049: }
050: }
051: }
052: ThreadUtil.reallySleep(1000);
053: }
054:
055: throw new RuntimeException("Port " + port
056: + " cannot be reached, timeout = " + waitTime);
057: }
058:
059: public static String getFullName(String serverName,
060: String majorVersion, String minorVersion) {
061: return serverName.toLowerCase() + "-"
062: + majorVersion.toLowerCase() + "."
063: + minorVersion.toLowerCase();
064: }
065:
066: public static boolean awaitShutdown(int timewait) {
067: long start = System.currentTimeMillis();
068: long timeout = timewait + start;
069: boolean foundAlive = false;
070: do {
071: ThreadUtil.reallySleep(5000);
072: foundAlive = HeartBeatService.anyAppServerAlive();
073: } while (foundAlive && System.currentTimeMillis() < timeout);
074:
075: return foundAlive;
076: }
077:
078: public static void shutdownAndArchive(File from, File to) {
079: shutdown();
080: archive(from, to);
081: }
082:
083: public static void forceShutdownAndArchive(File from, File to) {
084: System.out.println("Send kill signal to app servers...");
085: HeartBeatService.sendKillSignalToChildren();
086: archive(from, to);
087: }
088:
089: public static void shutdown() {
090: awaitShutdown(2 * 60 * 1000);
091: System.out.println("Send kill signal to app servers...");
092: HeartBeatService.sendKillSignalToChildren();
093: }
094:
095: public static File createSandbox(File tempDir) {
096: File sandbox = null;
097: if (Os.isWindows()) {
098: sandbox = new File(config.cacheDir(), "sandbox");
099: } else {
100: sandbox = new File(tempDir, "sandbox");
101: }
102:
103: try {
104: if (sandbox.exists()) {
105: if (sandbox.isDirectory()) {
106: FileUtils.cleanDirectory(sandbox);
107: } else {
108: throw new RuntimeException(sandbox
109: + " exists, but is not a directory");
110: }
111: }
112: } catch (IOException e) {
113: File prev = sandbox;
114: sandbox = new File(sandbox.getAbsolutePath()
115: + "-"
116: + new SimpleDateFormat("yyyyMMdd-HHmmss")
117: .format(new Date()));
118: Banner
119: .warnBanner("Caught IOException setting up workDir as "
120: + prev + ", using " + sandbox + " instead");
121: }
122:
123: if (!sandbox.exists() && !sandbox.mkdirs()) {
124: throw new RuntimeException("Failed to create sandbox: "
125: + sandbox);
126: }
127:
128: return sandbox;
129: }
130:
131: public static AppServerInstallation createAppServerInstallation(
132: AppServerFactory appServerFactory, File installDir,
133: File sandbox) throws Exception {
134: AppServerInstallation installation = null;
135: String appserverHome = config.appserverHome();
136: if (appserverHome != null && !appserverHome.trim().equals("")) {
137: installation = appServerFactory.createInstallation(
138: new File(appserverHome), sandbox);
139: } else {
140: throw new AssertionError(
141: "No appserver found! You must define: "
142: + TestConfigObject.APP_SERVER_HOME);
143: }
144: return installation;
145: }
146:
147: public static void archive(File from, File to) {
148: if (!from.equals(to)) {
149: System.out.println("Copying files from " + from + " to "
150: + to);
151: try {
152: com.tc.util.io.TCFileUtils.copyFile(from, to);
153: } catch (IOException ioe) {
154: Banner
155: .warnBanner("IOException caught while copying workingDir files");
156: ioe.printStackTrace();
157: }
158: System.out.println("Delete files in: " + from);
159: try {
160: FileUtils.forceDelete(from);
161: } catch (IOException e) {
162: e.printStackTrace();
163: }
164: }
165: }
166: }
|