// print version info
System.out.println("BI: version info");
VersionInfo.init(ctx.getInitParameter("system.version-file-name"));
System.out.println(VersionInfo.getVersionInfo());
/**
* 设置系统的工作目录
* @param key
* system property key to use, as expected in Log4j configuration
* (for example: "demo.root", used as
* "${demo.root}/WEB-INF/demo.log")
*/
public static void setWorkingDirSystemProperty(String key)
{
System.setProperty(key, new File("").getAbsolutePath());
}
/**
* 替换${},得到真实的路径参数
* @param text 要替换的文本
* @return String [替换后的文本]
*/
public static String resolvePlaceholders(String text)
{
StringBuffer buf = new StringBuffer(text);
String placeholder = null;
int nextIndex = 0;
String propVal = null;
int startIndex = buf.indexOf(PLACEHOLDER_PREFIX);
while (startIndex != -1)
{
int endIndex = buf.indexOf(PLACEHOLDER_SUFFIX, startIndex
+ PLACEHOLDER_PREFIX.length());
if (endIndex != -1)
{
placeholder = buf.substring(startIndex
+ PLACEHOLDER_PREFIX.length(), endIndex);
nextIndex = endIndex + PLACEHOLDER_SUFFIX.length();
try
{
propVal = System.getProperty(placeholder);
if (propVal == null)
{
// Fall back to searching the system environment.
propVal = System.getenv(placeholder);
}
if (propVal != null)
{
buf.replace(startIndex, endIndex
+ PLACEHOLDER_SUFFIX.length(), propVal);
nextIndex = startIndex + propVal.length();
}
else
{
System.err.println("Could not resolve placeholder ‘"
+ placeholder
+ "‘ in ["
+ text
+ "] as system property: neither system property nor environment variable found");
}
}
catch (Throwable ex)
{
System.err.println("Could not resolve placeholder ‘"
+ placeholder + "‘ in [" + text
+ "] as system property: " + ex);
}
startIndex = buf.indexOf(PLACEHOLDER_PREFIX, nextIndex);
}
else
{
startIndex = -1;
}
}
return buf.toString();
}
/**
* 根据文件的上下文路径获得URL对象
* 可以通过类路径,URL,绝对路径,相对路径来获得
* @param resourceLocation 文件的上下文路径
* @return URL [URL对象]
* @exception throws [FileNotFoundException] [文件没有被找到异常]
*/
public static URL getURL(String resourceLocation)
throws FileNotFoundException
{
if (null == resourceLocation)
{
throw new IllegalArgumentException(
"Resource location must not be null");
}
// 通过类路径classpath加载log4j得到路径
if (resourceLocation.startsWith(CLASSPATH_URL_PREFIX))
{
// 得到类的相对路径
String path = resourceLocation.substring(CLASSPATH_URL_PREFIX.length());
URL url = getDefaultClassLoader().getResource(path);
if (url == null)
{
String description = "class path resource [" + path + "]";
throw new FileNotFoundException(
description
+ " cannot be resolved to URL because it does not exist");
}
return url;
}
try
{
// 直接通过相对加载
return new URL(resourceLocation);
}
catch (MalformedURLException ex)
{
// no URL -> treat as file path
try
{
// 通过文件路径加载
return new File(resourceLocation).toURI().toURL();
}
catch (MalformedURLException ex2)
{
throw new FileNotFoundException("Resource location ["
+ resourceLocation
+ "] is neither a URL not a well-formed file path");
}
}
}
/**
* 根据文件的上下文路径获得文件对象
* 可以通过类路径,URL,绝对路径,相对路径来获得
* @param resourceLocation 文件的上下文路径
* @return File [文件对象]
* @exception throws [FileNotFoundException] [文件没有被找到异常]
*/
public static File getFile(String resourceLocation)
throws FileNotFoundException
{
if (null == resourceLocation)
{
throw new IllegalArgumentException(
"Resource location must not be null");
}
// 通过类路径
if (resourceLocation.startsWith(CLASSPATH_URL_PREFIX))
{
String path = resourceLocation.substring(CLASSPATH_URL_PREFIX.length());
String description = "class path resource [" + path + "]";
URL url = getDefaultClassLoader().getResource(path);
if (url == null)
{
throw new FileNotFoundException(description
+ " cannot be resolved to absolute file path "
+ "because it does not reside in the file system");
}
return getFile(url, description);
}
try
{
// try URL
return getFile(new URL(resourceLocation));
}
catch (MalformedURLException ex)
{
// no URL -> treat as file path
return new File(resourceLocation);
}
}
/**
* 根据URL获得具体的文件
* @param resourceUrl 文件的URL路径
* @param description 文件描述
* @return File [文件对象]
* @exception throws [FileNotFoundException] [文件没有被找到异常]
*/
public static File getFile(URL resourceUrl, String description)
throws FileNotFoundException
{
// Assert.notNull(resourceUrl, "Resource URL must not be null");
if (null == resourceUrl)
{
throw new IllegalArgumentException("Resource URL must not be null");
}
if (!URL_PROTOCOL_FILE.equals(resourceUrl.getProtocol()))
{
throw new FileNotFoundException(description
+ " cannot be resolved to absolute file path "
+ "because it does not reside in the file system: "
+ resourceUrl);
}
try
{
return new File(toURI(resourceUrl).getSchemeSpecificPart());
}
catch (URISyntaxException ex)
{
// Fallback for URLs that are not valid URIs (should hardly ever
// happen).
return new File(resourceUrl.getFile());
}
}
/**
* 根据URI获得具体的文件
* @param resourceUri 文件的URI路径
* @param description 文件描述
* @return File [文件对象]
* @exception throws [FileNotFoundException] [文件没有被找到异常]
*/
public static File getFile(URI resourceUri, String description)
throws FileNotFoundException
{
// Assert.notNull(resourceUri, "Resource URI must not be null");
if (null == resourceUri)
{
throw new IllegalArgumentException("Resource URI must not be null");
}
if (!URL_PROTOCOL_FILE.equals(resourceUri.getScheme()))
{
throw new FileNotFoundException(description
+ " cannot be resolved to absolute file path "
+ "because it does not reside in the file system: "
+ resourceUri);
}
return new File(resourceUri.getSchemeSpecificPart());
}
/**
* 将URL转换成URI对象
* @param url url对象
* @return URI [URI对象]
* @exception throws [URISyntaxException ] [URI转换异常]
*/
public static URI toURI(URL url) throws URISyntaxException
{
return toURI(url.toString());
}
/**
* 根据文件路径获得URI对象
* @param location 文件路径
* @return URI [URI路径]
* @exception throws [URISyntaxException] [URI转换异常]
*/
public static URI toURI(String location) throws URISyntaxException
{
// 进行路径中的空格替换
return new URI(replace(location, " ", "%20"));
}
/**
* 获得类装载器
* @return ClassLoader [类装载器]
*/
public static ClassLoader getDefaultClassLoader()
{
ClassLoader cl = null;
try
{
cl = Thread.currentThread().getContextClassLoader();
}
catch (Throwable ex)
{
// Cannot access thread context ClassLoader - falling back to system
// class loader...
}
if (cl == null)
{
// No thread context class loader -> use class loader of this class.
cl = Log4jConfigurer.class.getClassLoader();
}
return cl;
}
/**
* 字符串替换
* @param inString 要替换的字符串
* @param oldPattern 替换前的字符
* @param newPattern 替换后的字符
* @return String [替换后的字符串]
*/
public static String replace(String inString, String oldPattern,
String newPattern)
{
if (!hasLength(inString) || !hasLength(oldPattern)
|| newPattern == null)
{
return inString;
}
StringBuffer sbuf = new StringBuffer();
// output StringBuffer we‘ll build up
int pos = 0; // our position in the old string
int index = inString.indexOf(oldPattern);
// the index of an occurrence we‘ve found, or -1
int patLen = oldPattern.length();
while (index >= 0)
{
sbuf.append(inString.substring(pos, index));
sbuf.append(newPattern);
pos = index + patLen;
index = inString.indexOf(oldPattern, pos);
}
sbuf.append(inString.substring(pos));
// remember to append any characters to the right of a match
return sbuf.toString();
}
/**
* 新增配置项资源文件
*
* @param config
* the configuration to add
*/
public void addConfiguration(Configuration config) {
if (!configList.contains(config)) {
// As the inMemoryConfiguration contains all manually added keys,
// we must make sure that it is always last. "Normal", non composed
// configuration add their keys at the end of the configuration and
// we want to mimic this behaviour.
configList.add(configList.indexOf(inMemoryConfiguration), config);
// Make sure that you can‘t remove the inMemoryConfiguration from
// the CompositeConfiguration object
if (!config.equals(inMemoryConfiguration)) {
configList.remove(config);
}
}
/**
* 获取当前加载的配置数量
*
* @return the number of configuration
*/
public int getNumberOfConfigurations() {
return configList.size();
}
/**
* 清空所有已加载到内存中的配置信息
*/
public void clear() {
configList.clear();
// recreate the in memory configuration
inMemoryConfiguration = new BaseConfiguration();
((BaseConfiguration) inMemoryConfiguration)
.setThrowExceptionOnMissing(isThrowExceptionOnMissing());
((BaseConfiguration) inMemoryConfiguration).setListDelimiter(getListDelimiter());
((BaseConfiguration) inMemoryConfiguration)
.setDelimiterParsingDisabled(isDelimiterParsingDisabled());
configList.add(inMemoryConfiguration);
}
boolean isEmpty = true;
for (Iterator i = configList.iterator(); i.hasNext();) {
Configuration config = (Configuration) i.next();
if (!config.isEmpty()) {
return false;
}
}
return isEmpty;
}
protected void clearPropertyDirect(String key) {
for (Iterator i = configList.iterator(); i.hasNext();) {
Configuration config = (Configuration) i.next();
config.clearProperty(key);
}
}
public boolean containsKey(String key) {
for (Iterator i = configList.iterator(); i.hasNext();) {
Configuration config = (Configuration) i.next();
if (config.containsKey(key)) {
return true;
}
}
return false;
}
public List getList(String key, List defaultValue) {
List list = new ArrayList();
// add all elements from the first configuration containing the requested key
Iterator it = configList.iterator();
while (it.hasNext() && list.isEmpty()) {
Configuration config = (Configuration) it.next();
if (config != inMemoryConfiguration && config.containsKey(key)) {
appendListProperty(list, config, key);
}
}
// add all elements from the in memory configuration
appendListProperty(list, inMemoryConfiguration, key);
if (list.isEmpty()) {
return defaultValue;
}
ListIterator lit = list.listIterator();
while (lit.hasNext()) {
lit.set(interpolate(lit.next()));
}
return list;
}
public String[] getStringArray(String key) {
List list = getList(key);
// transform property values into strings
String[] tokens = new String[list.size()];
for (int i = 0; i < tokens.length; i++) {
tokens[i] = String.valueOf(list.get(i));
}
return tokens;
}
/**
* Return the configuration at the specified index.
*
* @param index
* The index of the configuration to retrieve
* @return the configuration at this index
*/
public Configuration getConfiguration(int index) {
return (Configuration) configList.get(index);
}
/**
* Returns the "in memory configuration". In this configuration changes are
* stored.
*
* @return the in memory configuration
*/
public Configuration getInMemoryConfiguration() {
return inMemoryConfiguration;
}
/**
* Returns a copy of this object. This implementation will create a deep clone, i.e.
* all configurations contained in this composite will also be cloned. This only works
* if all contained configurations support cloning; otherwise a runtime exception will
* be thrown. Registered event handlers won‘t get cloned.
*
* @return the copy
* @since 1.3
*/
public Object clone() {
/**
* Sets a flag whether added values for string properties should be checked for the
* list delimiter. This implementation ensures that the in memory configuration is
* correctly initialized.
*
* @param delimiterParsingDisabled
* the new value of the flag
* @since 1.4
*/
public void setDelimiterParsingDisabled(boolean delimiterParsingDisabled) {
/**
* Sets the character that is used as list delimiter. This implementation ensures that
* the in memory configuration is correctly initialized.
*
* @param listDelimiter
* the new list delimiter character
* @since 1.4
*/
public void setListDelimiter(char listDelimiter) {
/**
* Returns the configuration source, in which the specified key is defined. This
* method will iterate over all existing child configurations and check whether they
* contain the specified key. The following constellations are possible:
* <ul>
* <li>If exactly one child configuration contains the key, this configuration is
* returned as the source configuration. This may be the
* <em>in memory configuration</em> (this has to be explicitly checked by the
* calling application).</li>
* <li>If none of the child configurations contain the key, <b>null</b> is returned.</li>
* <li>If the key is contained in multiple child configurations or if the key is
* <b>null</b>, a <code>IllegalArgumentException</code> is thrown. In this case the
* source configuration cannot be determined.</li>
* </ul>
*
* @param key
* the key to be checked
* @return the source configuration of this key
* @throws IllegalArgumentException
* if the source configuration cannot be determined
* @since 1.5
*/
public Configuration getSource(String key) {
if (key == null) {
throw new IllegalArgumentException("Key must not be null!");
}
Configuration source = null;
for (Iterator it = configList.iterator(); it.hasNext();) {
Configuration conf = (Configuration) it.next();
if (conf.containsKey(key)) {
if (source != null) {
throw new IllegalArgumentException("The key " + key
+ " is defined by multiple sources!");
}
source = conf;
}
}
return source;
}
/**
* Adds the value of a property to the given list. This method is used by
* <code>getList()</code> for gathering property values from the child
* configurations.
*
* @param dest
* the list for collecting the data
* @param config
* the configuration to query
* @param key
* the key of the property
*/
private static void appendListProperty(List dest, Configuration config, String key) {
Object value = config.getProperty(key);
if (value != null) {
if (value instanceof Collection) {
dest.addAll((Collection) value);
} else {
dest.add(value);
}
}
}
}
/**
* 保留浮点数小数位数
* @param d 数值
* @param len 小数位数
* @return
*/
public static String doubleFormat(double d, int len)
{
BigDecimal b = new BigDecimal(Double.toString(d));
return decFormat(b, len);
}
public static String decFormat(BigDecimal d, int len)
{
DecimalFormat df = new DecimalFormat("0.0000000000000");
String result = df.format(d);
int index = result.indexOf(".") + 1; // 索引从1开始
result = result.substring(0, index + len); // 保留3位小数点
return result;
}
}
/**
* 保留浮点数小数位数
* @param d 数值
* @param len 小数位数
* @return
*/
public static String doubleFormat(double d, int len)
{
BigDecimal b = new BigDecimal(Double.toString(d));
return decFormat(b, len);
}
public static String decFormat(BigDecimal d, int len)
{
DecimalFormat df = new DecimalFormat("0.0000000000000");
String result = df.format(d);
int index = result.indexOf(".") + 1; // 索引从1开始
result = result.substring(0, index + len); // 保留3位小数点
return result;
}
}