1 16 17 package org.springframework.util; 18 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 22 34 public abstract class SystemPropertyUtils { 35 36 37 public static final String PLACEHOLDER_PREFIX = "${"; 38 39 40 public static final String PLACEHOLDER_SUFFIX = "}"; 41 42 private static final Log logger = LogFactory.getLog(SystemPropertyUtils.class); 43 44 45 53 public static String resolvePlaceholders(String text) { 54 StringBuffer buf = new StringBuffer (text); 55 56 60 int startIndex = text.indexOf(PLACEHOLDER_PREFIX); 61 while (startIndex != -1) { 62 int endIndex = buf.toString().indexOf(PLACEHOLDER_SUFFIX, startIndex + PLACEHOLDER_PREFIX.length()); 63 if (endIndex != -1) { 64 String placeholder = buf.substring(startIndex + PLACEHOLDER_PREFIX.length(), endIndex); 65 int nextIndex = endIndex + PLACEHOLDER_SUFFIX.length(); 66 try { 67 String propVal = System.getProperty(placeholder); 68 if (propVal == null) { 69 propVal = System.getenv(placeholder); 71 } 72 if (propVal != null) { 73 buf.replace(startIndex, endIndex + PLACEHOLDER_SUFFIX.length(), propVal); 74 nextIndex = startIndex + propVal.length(); 75 } 76 else { 77 if (logger.isWarnEnabled()) { 78 logger.warn("Could not resolve placeholder '" + placeholder + "' in [" + text + 79 "] as system property: neither system property nor environment variable found"); 80 } 81 } 82 } 83 catch (Throwable ex) { 84 if (logger.isWarnEnabled()) { 85 logger.warn("Could not resolve placeholder '" + placeholder + "' in [" + text + 86 "] as system property: " + ex); 87 } 88 } 89 startIndex = buf.toString().indexOf(PLACEHOLDER_PREFIX, nextIndex); 90 } 91 else { 92 startIndex = -1; 93 } 94 } 95 96 return buf.toString(); 97 } 98 99 } 100 | Popular Tags |