1 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 ; 10 import java.io.IOException ; 11 import java.net.InetAddress ; 12 import java.net.UnknownHostException ; 13 import java.text.SimpleDateFormat ; 14 import java.util.Date ; 15 16 19 public class ParameterSubstituter { 20 21 private static final TCLogger logger = TCLogging.getLogger(ParameterSubstituter.class); 22 23 public static String substitute(String source) { 24 if (source == null) return null; 25 26 StringBuffer out = new StringBuffer (); 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 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 propertyName = new StringBuffer (); 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 prop = propertyName.toString(); 89 String 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 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 uniqueTempDirectory = null; 119 120 private static synchronized String getUniqueTempDirectory() { 121 if (uniqueTempDirectory == null) { 122 try { 123 File 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 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 getDatestamp() { 146 SimpleDateFormat format = new SimpleDateFormat ("yyyyMMddHHmmssSSS"); 147 return format.format(new Date (System.currentTimeMillis())); 148 } 149 150 private static String getHostname() { 151 try { 152 return InetAddress.getLocalHost().getCanonicalHostName(); 153 } catch (UnknownHostException uhe) { 154 return "unknown-host"; 155 } 156 } 157 158 private static String getIpAddress() { 159 try { 160 return InetAddress.getLocalHost().getHostAddress(); 161 } catch (UnknownHostException uhe) { 162 return "unknown-ip-address"; 163 } 164 } 165 166 } 167 | Popular Tags |