KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > config > schema > dynamic > ParameterSubstituter


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.config.schema.dynamic;
5
6 import com.tc.logging.TCLogger;
7 import com.tc.logging.TCLogging;
8
9 import java.io.File JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.net.InetAddress JavaDoc;
12 import java.net.UnknownHostException JavaDoc;
13 import java.text.SimpleDateFormat JavaDoc;
14 import java.util.Date JavaDoc;
15
16 /**
17  * Substitutes parameters into strings — for example, '%h' becomes the host name, and so on.
18  */

19 public class ParameterSubstituter {
20
21   private static final TCLogger logger = TCLogging.getLogger(ParameterSubstituter.class);
22
23   public static String JavaDoc substitute(String JavaDoc source) {
24     if (source == null) return null;
25
26     StringBuffer JavaDoc out = new StringBuffer JavaDoc();
27     char[] sourceChars = source.toCharArray();
28
29     for (int i = 0; i < sourceChars.length; ++i) {
30       if (sourceChars[i] == '%') {
31         char nextChar = sourceChars[++i];
32         String JavaDoc value = "" + nextChar;
33
34         switch (nextChar) {
35           case 'd':
36             value = getUniqueTempDirectory();
37             break;
38
39           case 'D':
40             value = getDatestamp();
41             break;
42             
43           case 'h':
44             value = getHostname();
45             break;
46
47           case 'i':
48             value = getIpAddress();
49             break;
50
51           case 'H':
52             value = System.getProperty("user.home");
53             break;
54
55           case 'n':
56             value = System.getProperty("user.name");
57             break;
58
59           case 'o':
60             value = System.getProperty("os.name");
61             break;
62
63           case 'a':
64             value = System.getProperty("os.arch");
65             break;
66
67           case 'v':
68             value = System.getProperty("os.version");
69             break;
70
71           case 't':
72             value = System.getProperty("java.io.tmpdir");
73             break;
74
75           case '(':
76             StringBuffer JavaDoc propertyName = new StringBuffer JavaDoc();
77             boolean foundEnd = false;
78
79             while (++i < sourceChars.length) {
80               if (sourceChars[i] == ')') {
81                 foundEnd = true;
82                 break;
83               }
84               propertyName.append(sourceChars[i]);
85             }
86
87             if (foundEnd) {
88               String JavaDoc prop = propertyName.toString();
89               String JavaDoc defaultValue = "";
90               int index = prop.lastIndexOf(":");
91
92               if (index > 0) {
93                 prop = prop.substring(0, index);
94                 defaultValue = prop.substring(index + 1);
95               }
96
97               value = System.getProperty(prop);
98               if (value == null) value = defaultValue;
99             } else {
100               value = "%(" + propertyName.toString();
101             }
102             break;
103
104           default:
105             // nothing here; don't do any substitution
106
break;
107         }
108
109         out.append(value);
110       } else {
111         out.append(sourceChars[i]);
112       }
113     }
114
115     return out.toString();
116   }
117
118   private static String JavaDoc uniqueTempDirectory = null;
119
120   private static synchronized String JavaDoc getUniqueTempDirectory() {
121     if (uniqueTempDirectory == null) {
122       try {
123         File JavaDoc theFile = File.createTempFile("terracotta", "data");
124         theFile.delete();
125         if (!theFile.mkdir()) {
126           logger.warn("We were unable to create the directory '" + theFile.getAbsolutePath()
127                       + "' as a temporary directory "
128                       + "for Terracotta data; we will use the raw temporary directory, '"
129                       + System.getProperty("java.io.tmpdir") + "', instead.");
130           uniqueTempDirectory = System.getProperty("java.io.tmpdir");
131         } else {
132           logger.info("Using directory '" + theFile.getAbsolutePath() + "' for data from this Terracotta process.");
133           uniqueTempDirectory = theFile.getAbsolutePath();
134         }
135       } catch (IOException JavaDoc ioe) {
136         logger.warn("We were unable to create a new, empty temporary directory for Terracotta data; we will use the "
137                     + "raw temporary directory, '" + System.getProperty("java.io.tmpdir") + "', instead.");
138         uniqueTempDirectory = System.getProperty("java.io.tmpdir");
139       }
140     }
141
142     return uniqueTempDirectory;
143   }
144
145   private static synchronized String JavaDoc getDatestamp() {
146     SimpleDateFormat JavaDoc format = new SimpleDateFormat JavaDoc("yyyyMMddHHmmssSSS");
147     return format.format(new Date JavaDoc(System.currentTimeMillis()));
148   }
149   
150   private static String JavaDoc getHostname() {
151     try {
152       return InetAddress.getLocalHost().getCanonicalHostName();
153     } catch (UnknownHostException JavaDoc uhe) {
154       return "unknown-host";
155     }
156   }
157
158   private static String JavaDoc getIpAddress() {
159     try {
160       return InetAddress.getLocalHost().getHostAddress();
161     } catch (UnknownHostException JavaDoc uhe) {
162       return "unknown-ip-address";
163     }
164   }
165
166 }
167
Popular Tags