類加載器使用工具類

類加載器使用工具類


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;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章