类加载器使用工具类
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* 类加载器使用工具类
*
* @author wangwp
* @version $Id: ClassLoaderUtil.java,v 1.2 2008/09/10 08:52:25 jabberdev Exp $
*/
public abstract class ClassLoaderUtil {
private static final Log logger = LogFactory.getLog(ClassLoaderUtil.class);
private static final Properties EMPTY_PROPERTIES = new Properties();
/**
* 获得资源所在真实文件路径
*
* @param resource
* 资源
* @return
*/
public static String getPath(String resource) {
return getURL(resource).getPath();
}
public static URL getURL(String resource) {
return getClassLoader().getResource(resource);
}
/**
* 创建指定类的实例
*
* @param clazzName 类名
* @return
*/
public static Object getInstance(String clazzName){
try {
return loadClass(clazzName).newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
/**
* 根据指定的类名加载类
*
* @param name
* 类名
* @return
*/
public static Class<?> loadClass(String name) {
try {
ClassLoaderUtil.class.getClassLoader().loadClass(name);
return getClassLoader().loadClass(name);
} catch (ClassNotFoundException ex) {
throw new RuntimeException(ex);
}
}
public static ClassLoader getClassLoader() {
return Thread.currentThread().getContextClassLoader();
}
/**
* 将资源文件加载到输入流中
*
* @param resource
* 资源文件
* @return
*/
public static InputStream getStream(String resource) {
return getClassLoader().getResourceAsStream(resource);
}
/**
* 将资源文件转化为Properties对象
*
* @param resource
* 资源文件
* @return
*/
public static Properties getProperties(String resource) {
Properties properties = new Properties();
try {
InputStream is = getStream(resource);
if (is == null)
return EMPTY_PROPERTIES;
properties.load(is);
return properties;
} catch (IOException ex) {
return EMPTY_PROPERTIES;
}
}
/**
* 将资源文件转化为Reader
*
* @param resource
* 资源文件
* @return
*/
public static Reader getReader(String resource) {
InputStream is = getStream(resource);
if (is == null) {
return null;
}
return new BufferedReader(new InputStreamReader(is));
}
/**
* 将资源文件的内容转化为List实例
*
* @param resource
* 资源文件
* @return
*/
public static List<String> getList(String resource) {
List<String> list = new ArrayList<String>();
BufferedReader reader = (BufferedReader) getReader(resource);
if (null == reader) {
return list;
}
String line = "";
try {
while ((line = reader.readLine()) != null) {
list.add(line);
}
} catch (IOException ex) {
logger.error("将资源文件转化为list出现异常", ex);
} finally {
if (reader != null)
try {
reader.close();
} catch (IOException ex) {
logger.warn(ex.getMessage());
}
}
return list;
}
}