001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.config.schema.dynamic;
005:
006: import com.tc.logging.TCLogger;
007: import com.tc.logging.TCLogging;
008:
009: import java.io.File;
010: import java.io.IOException;
011: import java.net.InetAddress;
012: import java.net.UnknownHostException;
013: import java.text.SimpleDateFormat;
014: import java.util.Date;
015:
016: /**
017: * Substitutes parameters into strings — for example, '%h' becomes the host name, and so on.
018: */
019: public class ParameterSubstituter {
020:
021: private static final TCLogger logger = TCLogging
022: .getLogger(ParameterSubstituter.class);
023:
024: public static String substitute(String source) {
025: if (source == null)
026: return null;
027:
028: StringBuffer out = new StringBuffer();
029: char[] sourceChars = source.toCharArray();
030:
031: for (int i = 0; i < sourceChars.length; ++i) {
032: if (sourceChars[i] == '%') {
033: char nextChar = sourceChars[++i];
034: String value = "" + nextChar;
035:
036: switch (nextChar) {
037: case 'd':
038: value = getUniqueTempDirectory();
039: break;
040:
041: case 'D':
042: value = getDatestamp();
043: break;
044:
045: case 'h':
046: value = getHostname();
047: break;
048:
049: case 'i':
050: value = getIpAddress();
051: break;
052:
053: case 'H':
054: value = System.getProperty("user.home");
055: break;
056:
057: case 'n':
058: value = System.getProperty("user.name");
059: break;
060:
061: case 'o':
062: value = System.getProperty("os.name");
063: break;
064:
065: case 'a':
066: value = System.getProperty("os.arch");
067: break;
068:
069: case 'v':
070: value = System.getProperty("os.version");
071: break;
072:
073: case 't':
074: value = System.getProperty("java.io.tmpdir");
075: break;
076:
077: case '(':
078: StringBuffer propertyName = new StringBuffer();
079: boolean foundEnd = false;
080:
081: while (++i < sourceChars.length) {
082: if (sourceChars[i] == ')') {
083: foundEnd = true;
084: break;
085: }
086: propertyName.append(sourceChars[i]);
087: }
088:
089: if (foundEnd) {
090: String prop = propertyName.toString();
091: String defaultValue = "";
092: int index = prop.lastIndexOf(":");
093:
094: if (index > 0) {
095: prop = prop.substring(0, index);
096: defaultValue = prop.substring(index + 1);
097: }
098:
099: value = System.getProperty(prop);
100: if (value == null)
101: value = defaultValue;
102: } else {
103: value = "%(" + propertyName.toString();
104: }
105: break;
106:
107: default:
108: // nothing here; don't do any substitution
109: break;
110: }
111:
112: out.append(value);
113: } else {
114: out.append(sourceChars[i]);
115: }
116: }
117:
118: return out.toString();
119: }
120:
121: private static String uniqueTempDirectory = null;
122:
123: private static synchronized String getUniqueTempDirectory() {
124: if (uniqueTempDirectory == null) {
125: try {
126: File theFile = File
127: .createTempFile("terracotta", "data");
128: theFile.delete();
129: if (!theFile.mkdir()) {
130: logger
131: .warn("We were unable to create the directory '"
132: + theFile.getAbsolutePath()
133: + "' as a temporary directory "
134: + "for Terracotta data; we will use the raw temporary directory, '"
135: + System
136: .getProperty("java.io.tmpdir")
137: + "', instead.");
138: uniqueTempDirectory = System
139: .getProperty("java.io.tmpdir");
140: } else {
141: logger
142: .info("Using directory '"
143: + theFile.getAbsolutePath()
144: + "' for data from this Terracotta process.");
145: uniqueTempDirectory = theFile.getAbsolutePath();
146: }
147: } catch (IOException ioe) {
148: logger
149: .warn("We were unable to create a new, empty temporary directory for Terracotta data; we will use the "
150: + "raw temporary directory, '"
151: + System.getProperty("java.io.tmpdir")
152: + "', instead.");
153: uniqueTempDirectory = System
154: .getProperty("java.io.tmpdir");
155: }
156: }
157:
158: return uniqueTempDirectory;
159: }
160:
161: private static synchronized String getDatestamp() {
162: SimpleDateFormat format = new SimpleDateFormat(
163: "yyyyMMddHHmmssSSS");
164: return format.format(new Date(System.currentTimeMillis()));
165: }
166:
167: private static String getHostname() {
168: try {
169: return InetAddress.getLocalHost().getCanonicalHostName();
170: } catch (UnknownHostException uhe) {
171: return "unknown-host";
172: }
173: }
174:
175: private static String getIpAddress() {
176: try {
177: return InetAddress.getLocalHost().getHostAddress();
178: } catch (UnknownHostException uhe) {
179: return "unknown-ip-address";
180: }
181: }
182:
183: }
|