SystemPropertyUtils

 
  1. /*
  2.  * Copyright 2002-2007 the original author or authors.
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *      http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16. package org.springframework.util;
  17. import org.apache.commons.logging.Log;
  18. import org.apache.commons.logging.LogFactory;
  19. /**
  20.  * Helper class for resolving placeholders in texts. Usually applied to file paths.
  21.  *
  22.  * <p>A text may contain <code>${...}</code> placeholders, to be resolved as
  23.  * system properties: e.g. <code>${user.dir}</code>.
  24.  *
  25.  * @author Juergen Hoeller
  26.  * @since 1.2.5
  27.  * @see #PLACEHOLDER_PREFIX
  28.  * @see #PLACEHOLDER_SUFFIX
  29.  * @see System#getProperty(String)
  30.  */
  31. public abstract class SystemPropertyUtils {
  32.     /** Prefix for system property placeholders: "${" */
  33.     public static final String PLACEHOLDER_PREFIX = "${";
  34.     /** Suffix for system property placeholders: "}" */
  35.     public static final String PLACEHOLDER_SUFFIX = "}";
  36.     private static final Log logger = LogFactory.getLog(SystemPropertyUtils.class);
  37.     /**
  38.      * Resolve ${...} placeholders in the given text,
  39.      * replacing them with corresponding system property values.
  40.      * @param text the String to resolve
  41.      * @return the resolved String
  42.      * @see #PLACEHOLDER_PREFIX
  43.      * @see #PLACEHOLDER_SUFFIX
  44.      */
  45.     public static String resolvePlaceholders(String text) {
  46.         StringBuffer buf = new StringBuffer(text);
  47.         int startIndex = buf.indexOf(PLACEHOLDER_PREFIX);
  48.         while (startIndex != -1) {
  49.             int endIndex = buf.indexOf(PLACEHOLDER_SUFFIX, startIndex + PLACEHOLDER_PREFIX.length());
  50.             if (endIndex != -1) {
  51.                 String placeholder = buf.substring(startIndex + PLACEHOLDER_PREFIX.length(), endIndex);
  52.                 int nextIndex = endIndex + PLACEHOLDER_SUFFIX.length();
  53.                 try {
  54.                     String propVal = System.getProperty(placeholder);
  55.                     if (propVal == null) {
  56.                         // Fall back to searching the system environment.
  57.                         propVal = System.getenv(placeholder);
  58.                     }
  59.                     if (propVal != null) {
  60.                         buf.replace(startIndex, endIndex + PLACEHOLDER_SUFFIX.length(), propVal);
  61.                         nextIndex = startIndex + propVal.length();
  62.                     }
  63.                     else {
  64.                         if (logger.isWarnEnabled()) {
  65.                             logger.warn("Could not resolve placeholder '" + placeholder + "' in [" + text +
  66.                                     "] as system property: neither system property nor environment variable found");
  67.                         }
  68.                     }
  69.                 }
  70.                 catch (Throwable ex) {
  71.                     if (logger.isWarnEnabled()) {
  72.                         logger.warn("Could not resolve placeholder '" + placeholder + "' in [" + text +
  73.                                 "] as system property: " + ex);
  74.                     }
  75.                 }
  76.                 startIndex = buf.indexOf(PLACEHOLDER_PREFIX, nextIndex);
  77.             }
  78.             else {
  79.                 startIndex = -1;
  80.             }
  81.         }
  82.         return buf.toString();
  83.     }
  84. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值