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.appserver;
006:
007: import org.apache.commons.io.IOUtils;
008:
009: import com.tc.util.runtime.Os;
010:
011: import java.io.File;
012: import java.io.FileOutputStream;
013: import java.io.IOException;
014: import java.util.HashMap;
015: import java.util.Map;
016: import java.util.Properties;
017:
018: /**
019: * This concrete implementation allows it's creator to set values while the appserver itself interacts with the
020: * immutable {@link AppServerParameters} interface.
021: */
022: public class StandardAppServerParameters implements AppServerParameters {
023:
024: private final Map wars = new HashMap();
025: private final String instanceName;
026: private final Properties props;
027: private String jvmArgs = "";
028: private String classpath = "";
029:
030: public StandardAppServerParameters(String instanceName,
031: Properties props) {
032: this .instanceName = instanceName;
033: this .props = props;
034: }
035:
036: public void addWar(String context, File file) {
037: wars.put(context, file);
038: }
039:
040: public final void addWar(File war) {
041: String name = war.getName();
042: addWar(name.substring(0, name.length() - 4), war);
043: }
044:
045: public final String jvmArgs() {
046: return jvmArgs;
047: }
048:
049: public final void appendJvmArgs(String jvmArgsVar) {
050: this .jvmArgs += jvmArgsVar + " ";
051: }
052:
053: public final String classpath() {
054: return classpath;
055: }
056:
057: protected final void appendClasspath(String classpathVar) {
058: this .classpath += classpathVar + " ";
059: }
060:
061: public final Map wars() {
062: return wars;
063: }
064:
065: public final String instanceName() {
066: return instanceName;
067: }
068:
069: public final Properties properties() {
070: return props;
071: }
072:
073: public String writeTerracottaClassPathFile() {
074: FileOutputStream fos = null;
075:
076: try {
077: File tempFile = File.createTempFile("tc-classpath",
078: instanceName);
079: tempFile.deleteOnExit();
080: fos = new FileOutputStream(tempFile);
081: fos.write(System.getProperty("java.class.path").getBytes());
082:
083: String rv = tempFile.getAbsolutePath();
084: if (Os.isWindows()) {
085: rv = "/" + rv;
086: }
087:
088: return rv;
089: } catch (IOException ioe) {
090: throw new AssertionError(ioe);
091: } finally {
092: IOUtils.closeQuietly(fos);
093: }
094:
095: }
096:
097: public void appendSysProp(String name, int value) {
098: appendSysProp(name, Integer.toString(value));
099: }
100:
101: public void appendSysProp(String name, String value) {
102: if (!name.startsWith("-")) {
103: name = "-D" + name;
104: }
105: if (value == null)
106: appendJvmArgs(name);
107: else
108: appendJvmArgs(name + "=" + value);
109: }
110:
111: public void appendSysProp(String name) {
112: appendSysProp(name, null);
113: }
114:
115: public void appendSysProp(String name, boolean b) {
116: appendSysProp(name, Boolean.toString(b));
117: }
118:
119: }
|