All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.j2mvc.framework.config.PropsConfig Maven / Gradle / Ivy

Go to download

强烈建议使用J2mvc 2.1以后的版本。 version 2.1.01 1.更换JSON依赖包. version 2.1.02 1.移除com.j2mvc.StringUtils.getUtf8()方法调用. 更改为getCharset() version 2.1.03 1.更新JNDI连接设置 version 2.1.04 1.修改works.xml配置url-pkg-prefixes改为pkg

There is a newer version: 2.1.12
Show newest version
package com.j2mvc.framework.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.net.URL;

import org.apache.log4j.Logger;
import com.j2mvc.framework.i18n.I18n;
import com.j2mvc.util.OSType;
import com.j2mvc.util.PropertiesConfiguration;

public class PropsConfig {

	final static Logger log = Logger.getLogger(I18n.class);
	static String path = "/conf";

	public static void init(){

		String dir = "";
		if(OSType.OSinfo.isWindows())
			dir = System.getProperty("user.dir");
		else {
			URL url =  Config.class.getResource("/");
			if(url!=null){
				dir = url.getPath();
			}
		}
		String source = dir+"/.."+path;
		File file = new File(source);
		if(!file.exists()){
			// 获取Jar包路径
			URL url = Config.class.getProtectionDomain().getCodeSource().getLocation();
			if(url!=null){
				String jarPath  = url.getPath();
				File jarFile = new File(jarPath);
				source = jarFile.getParent()+"/.."+path;
				file = new File(source);
				if(!file.exists()){
					source = jarFile.getParent()+path;
					file = new File(source);
				}
			}
		}
		if(!file.exists()){
			// 文件不存在
			file = new File(Config.class.getResource(path).getFile());
			source = file.getAbsolutePath();
		}
		log.info(" init config >> "+source);

		File[] files = file.listFiles();
		for(File f:files){
			String fileName = f.getName();
			String ext = f.getName().substring(fileName.lastIndexOf(".")+1,fileName.length());
			if("properties".equalsIgnoreCase(ext)){
				init(source+"/"+fileName);
			}
		}
	}
	/**
	 * 
	 * @param fileName
	 */
	public static void init(String fileName) {
		PropertiesConfiguration configuration = new PropertiesConfiguration(fileName);
		Config.props.putAll(configuration.map());
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy