解析oracle配置文件tnsnames.ora

package decrypt;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import ca.beq.util.win32.registry.RegistryKey;
import ca.beq.util.win32.registry.RegistryValue;
import ca.beq.util.win32.registry.RootKey;

public class TNSNamesReader {
	/**
	 * 得到ORACLE_HOME 此处用到一个第三方组件:jRegistryKey
	 * @return
	 * @author zhangzongwang Dec 22,2009
	 */
	public String getOracleHome() {
		String ORACLE_HOME = "";
		RegistryKey r = new RegistryKey(RootKey.HKEY_LOCAL_MACHINE,
				"SOFTWARE\\ORACLE");
		if (r.hasValue("ORACLE_HOME")) {
			RegistryValue v = r.getValue("ORACLE_HOME");
			ORACLE_HOME = v.getStringValue();
		}
		return ORACLE_HOME;
	}
	/**
	 * 得到tnsnames.ora路径
	 * @param OracleHome
	 * @return
	 * @author zhangzongwang Dec 22,2009
	 */
	public String getTNSNAMESPath(String OracleHome) {
		String oracleHomePath = this.getOracleHome();
		String tnsNamesOraFilePath = "";
		if (!oracleHomePath.equals("")) {
			tnsNamesOraFilePath = oracleHomePath
					+ "\\NETWORK\\ADMIN\\TNSNAMES.ORA";
			if (!(new File(tnsNamesOraFilePath).exists())) {
				tnsNamesOraFilePath = oracleHomePath
						+ "\\NET80\\ADMIN\\TNSNAMES.ORA";
			}
		}
		return tnsNamesOraFilePath;
	}
	/**
	 * 得到tnsnames.ora文件所有内容
	 * @param strTNSNAMESPath
	 * @return
	 * @author zhangzongwang Dec 22,2009
	 */
	public String getTNSNamesStr(String strTNSNAMESPath) {
		String output = ")";
		String fileLine;
		InputStream in = null;
		BufferedReader sr = null;
		try {
			in = new FileInputStream(strTNSNAMESPath);
			sr = new BufferedReader(new InputStreamReader(in));
			while ((fileLine = sr.readLine()) != null) {
				if (fileLine.length() > 0 && fileLine.indexOf("#") == -1) {
					output += fileLine.trim();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				sr.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		output = output.replaceAll(" ", "").toLowerCase();
		return output;
	}
	/**
	 * 解析
	 * @return
	 * @author zhangzongwang Dec 22,2009
	 */
	public List<Map<String, String>> loadTNSNames() {
		String OracleHome = this.getOracleHome();
		String tNSNamesStr = this.getTNSNamesStr(this
				.getTNSNAMESPath(OracleHome));
		Pattern ptn = Pattern
				.compile("(\\)\\w+|host|port|service_name)\\=(\\(|\\d+\\.\\d+\\.\\d+\\.\\d+|\\d+|\\w+)");
		Matcher match = ptn.matcher(tNSNamesStr);
		int start = 0;
		int i = 0;
		Map<String, String> map = new LinkedHashMap<String, String>();
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		while (match.find(start)) {
			String str = tNSNamesStr.substring(match.start(), match.end())
					.replace(")", "");
			String[] strs = str.split("=");
			if ("(".equals(strs[1])) {
				if (i != 0) {
					list.add(map);
					map = new LinkedHashMap<String, String>();
				}
				map.put("sid", strs[0]);
			} else {
				if (map.containsKey(strs[0])) {
					map.put(strs[0] + "1", strs[1]);
				} else {
					map.put(strs[0], strs[1]);
				}
			}
			start = match.end();
			i++;
		}
		list.add(map);
		return list;
	}
	/**
	 * 只要SID
	 * @return
	 * @author zhangzongwang Dec 22,2009
	 */
	public String[] loadTNSNameSID(){
		List<Map<String, String>> list = this.loadTNSNames();
		List<String> tsnNameSID = new ArrayList<String>();
	     for(Map<String,String> mp : list){	    	 
	    	 Set<String> set = mp.keySet();
	    	 Iterator<String> it = set.iterator();
	    	 while(it.hasNext()){
	    		 String key = it.next().toString();
	    		 String value = mp.get(key).toString();
	    		 if("sid".equals(key)){
	    			 tsnNameSID.add(value.toUpperCase());
	    		 }
	    	 }
	     }
	    String[] str = new String[tsnNameSID.size()];
		return tsnNameSID.toArray(str);
	}
}

你可能感兴趣的:(java,oracle)