• Java J2EE读取配置文件


    package com;
    
    
    
    
     
    
    
    
    
    import org.slf4j.Logger;
    
    
    
    
    import org.slf4j.LoggerFactory;
    
    
    
    
     
    
    
    
    
    import javax.naming.InitialContext;
    
    
    
    
    import java.io.File;
    
    
    
    
    import java.io.FileFilter;
    
    
    
    
    import java.io.FileReader;
    
    
    
    
    import java.util.Iterator;
    
    
    
    
    import java.util.Map;
    
    
    
    
    import java.util.Map.Entry;
    
    
    
    
    import java.util.Properties;
    
    
    
    
    import java.util.concurrent.ConcurrentHashMap;
    
    
    
    
     
    
    
    
    
    /**
    
    
    
    
     * 读取配置文件
    
    
    
    
     */
    
    
    
    
    public class ReadProperty {
    
    
    
    
     
    
    
    
    
    	private static final Logger logger = LoggerFactory.getLogger(Globals.class);
    
    
    
    
    	
    
    
    
    
    	private static String confHome = null;
    
    
    
    
     
    
    
    
    
    	//并发,线程安全的map
    
    
    
    
    	private static Map<String, String> confProperties = new ConcurrentHashMap<>();
    
    
    
    
    	private static Map<String, File> confFiles = new ConcurrentHashMap<>();
    
    
    
    
     
    
    
    
    
    	//加载成功
    
    
    
    
    	private static boolean loadingSuccess = true;
    
    
    
    
    	
    
    
    
    
     
    
    
    
    
        /**
    
    
    
    
         * 加载配置文件
    
    
    
    
    	 */
    
    
    
    
    	private synchronized static void loadProperties() {
    
    
    
    
     
    
    
    
    
    		//如果没有加载成功,返回
    
    
    
    
    		if ( !loadingSuccess ) {
    
    
    
    
                return;
    
    
    
    
    		}
    
    
    
    
     
    
    
    
    
    		//如果加载的文件是空的
    
    
    
    
    		if (confProperties.isEmpty()) {
    
    
    
    
     
    
    
    
    
    			//如果电脑环境变量中为空
    
    
    
    
    			if (confHome == null) {
    
    
    
    
    	        	confHome = System.getProperty("CONF_HOME");
    
    
    
    
    	        }
    
    
    
    
     
    
    
    
    
    	        //如果web.xml中没配
    
    
    
    
    	        if (confHome == null) {
    
    
    
    
    	            try {
    
    
    
    
    	                InitialContext context = new InitialContext();
    
    
    
    
    	                confHome = (String)context.lookup("java:comp/env/CONF_HOME");
    
    
    
    
    	            } catch(Exception e) {
    
    
    
    
    	            	logger.warn("Can not find jini name {}", "java:comp/env/CONF_HOME");
    
    
    
    
    	            }
    
    
    
    
    	        }
    
    
    
    
     
    
    
    
    
    	        //如果还是为空,就找本机路径下的ProtectionDomain/CodeSource/Location/getFile/WEB-INF/文件夹/conf
    
    
    
    
    	        if (confHome == null) {
    
    
    
    
    	        	confHome = (new InitWebPath()).getRootPath() + "WEB-INF" + File.separator + "conf";
    
    
    
    
    	        }
    
    
    
    
     
    
    
    
    
    	        //是否是文件夹
    
    
    
    
    		   try {
    
    
    
    
    			   File dirFile = new File(confHome);
    
    
    
    
    			   if(!dirFile.exists() || (!dirFile.isDirectory())){ 
    
    
    
    
    				   logger.warn("Can not find home or is not directory!
    {}", confHome);
    
    
    
    
    				   loadingSuccess = false;
    
    
    
    
    				   return;
    
    
    
    
    			   }
    
    
    
    
     
    
    
    
    
    			   //获取所有文件后缀是.properties的文件名
    
    
    
    
    			   File[] files = dirFile.listFiles(new FileFilter() {
    
    
    
    
    				   @Override
    
    
    
    
    				   public boolean accept(File file) {
    
    
    
    
    					   String fileName = file.getName();
    
    
    
    
    					   int pos = fileName.lastIndexOf(".properties");//最后一个匹配的 db.xml和db.xml.xml
    
    
    
    
    					   
    
    
    
    
    					   if (pos != -1) {  
    
    
    
    
    						   confFiles.put(fileName.substring(0, pos), file);//文件名与文件关联 key value 
    
    
    
    
    						   return true;
    
    
    
    
    					   } else {
    
    
    
    
    						   pos = fileName.lastIndexOf(".xml");
    
    
    
    
    						   confFiles.put(fileName.substring(0, pos), file);
    
    
    
    
    						   return false;
    
    
    
    
    					   }
    
    
    
    
    				   }
    
    
    
    
    			   }
    
    
    
    
    			   );
    
    
    
    
     
    
    
    
    
    			   //迭代文件,读取key value
    
    
    
    
    			   for(File file : files) {
    
    
    
    
    				   Properties fileProperties = new Properties();
    
    
    
    
    				   fileProperties.load(new FileReader(file));
    
    
    
    
    				   Iterator<Entry<Object, Object>> iterProp = fileProperties.entrySet().iterator();
    
    
    
    
    				   while(iterProp.hasNext()) {
    
    
    
    
    					   Entry<Object, Object> row = iterProp.next();
    
    
    
    
    					   Object key = row.getKey();
    
    
    
    
    					   Object value = row.getValue();
    
    
    
    
    					   if (null!=key && null!=value) {
    
    
    
    
    						   confProperties.put(key.toString(), value.toString());
    
    
    
    
    					   }
    
    
    
    
    				   }
    
    
    
    
    			   }
    
    
    
    
    		   } catch(Exception e) {
    
    
    
    
    			   loadingSuccess = false;
    
    
    
    
    		   }
    
    
    
    
    		}
    
    
    
    
    	}
    
    
    
    
     
    
    
    
    
    	/**
    
    
    
    
    	 * 读取配置文件信息
    
    
    
    
    	 * @param name key
    
    
    
    
    	 * @return value
    
    
    
    
    	 */
    
    
    
    
    	public static String getProperty(String name) {
    
    
    
    
    		if (confProperties.isEmpty()) {
    
    
    
    
    			loadProperties();
    
    
    
    
    		}
    
    
    
    
    		return confProperties.get(name);
    
    
    
    
    	}
    
    
    
    
    	
    
    
    
    
    	
    
    
    
    
    	static class InitWebPath{
    
    
    
    
    		public String getRootPath() {
    
    
    
    
    			String url = InitWebPath.class.getProtectionDomain().getCodeSource().getLocation().getFile();
    
    
    
    
    			String filePath = "";
    
    
    
    
    			try {
    
    
    
    
    				filePath = java.net.URLDecoder.decode(url, "utf-8");
    
    
    
    
    			} catch (Exception e) {
    
    
    
    
    				logger.error(e.getMessage(), e);
    
    
    
    
    			}
    
    
    
    
     
    
    
    
    
    			final String fileFlag = "file:";
    
    
    
    
    			if (filePath.startsWith(fileFlag)) {
    
    
    
    
    				filePath = filePath.substring(fileFlag.length());
    
    
    
    
    			}
    
    
    
    
    			final String applicationFlag = "WEB-INF";
    
    
    
    
    			return filePath.substring(0, filePath.lastIndexOf(applicationFlag));
    
    
    
    
    		}
    
    
    
    
    	}
    
    
    
    
    }
    
  • 相关阅读:
    1-13Object类之toString方法
    jvm源码解读--16 锁_开头
    jvm源码解读--16 cas 用法解析
    jvm源码解读--15 oop对象详解
    jvm源码解读--14 defNewGeneration.cpp gc标记复制之后,进行空间清理
    jvm源码解读--13 gc_root中的栈中oop的mark 和copy 过程分析
    Error: Could not find or load main class ***
    使用javah 给.class类编译jni_helloworld.h文件头
    jvm源码解读--12 invokspecial指令的解读
    jvm源码解读--11 ldc指令的解读
  • 原文地址:https://www.cnblogs.com/firstdream/p/9810127.html
Copyright © 2020-2023  润新知