📄 config.java
字号:
package com.microvois.luence;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.CodeSource;
import java.security.ProtectionDomain;
import java.util.List;
import org.jdom.*;
import org.jdom.output.*;
import org.jdom.input.*;
import org.apache.log4j.Logger;
import org.apache.lucene.analysis.*;
import org.apache.lucene.analysis.standard.*;
import org.apache.lucene.document.*;
import org.apache.lucene.store.*;
import org.apache.lucene.search.*;
import org.apache.lucene.queryParser.*;
import org.apache.lucene.index.*;
public class Config
{
public static String StrDataDirectroy ="";
public static int nServerPort = 9090;
private static final String strconfig ="Config.xml";
private static final Logger log = Logger.getLogger(Config.class);
static
{
try
{
String strfile =getFullPathRelateClass("../../../", Config.class)+File.separator+strconfig;
if(loadConfig(strfile))
{
log.info("载入配置文件:"+strfile +" 成功...");
}
else
{
log.fatal("载入配置文件:"+strfile +" 失败...");
}
}catch(Exception se)
{
//se.printStackTrace();
//se.printStackTrace(log.);
log.fatal(se.toString());
}
}
/**
* load config from XML file .
*
* @param strfile : system configuration file .
* @return : true: sucess , false : fail .
*/
public static boolean loadConfig(String strfile )
{
boolean brv = false;
FileInputStream fi = null;
try
{
fi = new FileInputStream(strfile);
SAXBuilder sb = new SAXBuilder();
org.jdom.Document doc = sb.build(fi);
Element root = doc.getRootElement(); //得到根元素
List rootchilds = root.getChildren(); //得到根元素所有子元素的集合
for(int i=0;i<rootchilds.size();i++)
{
Element serviceele = (Element) rootchilds.get(i);
StrDataDirectroy = serviceele.getAttributeValue("datadir");
nServerPort =Integer.parseInt(serviceele.getAttributeValue("nport"));
}
brv = true;
}
catch(Exception e)
{
e.printStackTrace();
}
finally{
try{
fi.close();
}
catch(Exception e){
e.printStackTrace();
}
}
return brv;
}
/**
*
* @return
*/
public static String getPathFromClass(Class cls)
{
String path = null;
try
{
if (cls == null) {
throw new NullPointerException();
}
URL url = getClassLocationURL(cls);
if (url != null) {
path = url.getPath();
if ("jar".equalsIgnoreCase(url.getProtocol())) {
try {
path = new URL(path).getPath();
} catch (MalformedURLException e) {
}
int location = path.indexOf("!/");
if (location != -1) {
path = path.substring(0, location);
}
}
File file = new File(path);
path = file.getCanonicalPath();
}
}
catch(Exception se)
{}
return path;
}
public static String getFullPathRelateClass(String relatedPath, Class cls)
throws IOException {
String path = null;
if (relatedPath == null) {
throw new NullPointerException();
}
String clsPath = getPathFromClass(cls);
File clsFile = new File(clsPath);
String tempPath = clsFile.getParent() + File.separator + relatedPath;
File file = new File(tempPath);
path = file.getCanonicalPath();
return path;
}
/**
*/
private static URL getClassLocationURL(final Class cls) {
if (cls == null)
throw new IllegalArgumentException("null input: cls");
URL result = null;
final String clsAsResource = cls.getName().replace('.', '/').concat(
".class");
final ProtectionDomain pd = cls.getProtectionDomain();
// java.lang.Class contract does not specify
// if 'pd' can ever be null;
// it is not the case for Sun's implementations,
// but guard against null
// just in case:
if (pd != null) {
final CodeSource cs = pd.getCodeSource();
// 'cs' can be null depending on
// the classloader behavior:
if (cs != null)
result = cs.getLocation();
if (result != null) {
// Convert a code source location into
// a full class file location
// for some common cases:
if ("file".equals(result.getProtocol())) {
try {
if (result.toExternalForm().endsWith(".jar")
|| result.toExternalForm().endsWith(".zip"))
result = new URL("jar:".concat(
result.toExternalForm()).concat("!/")
.concat(clsAsResource));
else if (new File(result.getFile()).isDirectory())
result = new URL(result, clsAsResource);
} catch (MalformedURLException ignore) {
}
}
}
}
if (result == null) {
final ClassLoader clsLoader = cls.getClassLoader();
result = clsLoader != null ? clsLoader.getResource(clsAsResource)
: ClassLoader.getSystemResource(clsAsResource);
}
return result;
}
//for testting .
public static void main(String args[]) throws Exception
{
Thread.sleep(10000);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -