Java 类org.apache.log4j.lf5.util.Resource 实例源码

项目:full-hibernate-plugin-for-struts2    文件:HibernateSessionFactory.java   
/**
 * Create and test a Hibernate Core SessionFactory
 * @param file - Full qualified file name 
 * @return A new Hibernate Core SessionFactory
 * @throws IOException for File not found or corrupted
 * @throws HibernateException for invalid configuration
 * @throws SQLException for General SQL Problems (ie. network connection)
 */
public static SessionFactory createAndTestSessionFactory(String file) throws IOException, HibernateException, SQLException {
    Configuration configuration = null;
    try {
        configuration = (Configuration) Class.forName("org.hibernate.cfg.AnnotationConfiguration").newInstance();
        log.debug("Full Hibernate Plugin's Session Factory using Hibernate Annotation Configuration");
    } catch (Exception e) {
        configuration = new Configuration();
        log.debug("Full Hibernate Plugin's Session Factory using Hibernate XML Configuration");
    }
    configuration.configure(file);
    log.debug("Full Hibernate Plugin's Session Factory configuration file \""+file+"\" configured");

    String fileProperties = file.substring(1,file.length()).replace(".cfg", "").replace("xml", "properties");
    Resource resource = new Resource(fileProperties);
    if (resource.getURL()!=null) {
        String fullpath = resource.getURL().getPath().replace("%20", " ");
        FileInputStream fis = new FileInputStream(fullpath);
        Properties properties = new Properties();
        properties.load(fis);
        fis.close();
        configuration.addProperties(properties);
        log.debug("Full Hibernate Plugin's Session Factory property file \"/"+fileProperties+"\" configured");
    }

    SessionFactory sessionFactory = configuration.buildSessionFactory();

    Session testSession = sessionFactory.openSession();
    testSession.connection().getMetaData().getCatalogs();
    testSession.close();
    return sessionFactory;
}