KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > hibernate > HibernateConfigurationImpl


1 package com.opensymphony.webwork.hibernate;
2
3 import com.opensymphony.util.ClassLoaderUtil;
4 import com.opensymphony.xwork.interceptor.component.Disposable;
5 import com.opensymphony.xwork.interceptor.component.Initializable;
6 import com.opensymphony.xwork.interceptor.component.ResourceAware;
7 import org.hibernate.SessionFactory;
8 import org.hibernate.cfg.Configuration;
9 import org.hibernate.tool.hbm2ddl.SchemaUpdate;
10
11 import java.net.URL JavaDoc;
12 import java.util.HashSet JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.Map JavaDoc;
15 import java.util.Set JavaDoc;
16
17 /**
18  * User: plightbo
19  * Date: Aug 9, 2005
20  * Time: 8:58:24 PM
21  */

22 public class HibernateConfigurationImpl implements HibernateConfiguration,
23         Initializable, Disposable, ResourceAware {
24     SessionFactory sessionFactory;
25     Set JavaDoc resources = new HashSet JavaDoc();
26
27     public void init() {
28         System.out.println("HibernateConfiguration.init");
29         URL JavaDoc url = ClassLoaderUtil.getResource("hibernate.cfg.xml", getClass());
30         Configuration configuration = new Configuration().configure(url);
31
32         resources.add("hibernate.cfg.xml");
33         for (Iterator JavaDoc iterator = configuration.getImports().entrySet().iterator(); iterator.hasNext();) {
34             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
35             String JavaDoc name = (String JavaDoc) entry.getKey();
36             resources.add(name.replaceAll("\\.", "/") + ".hbm.xml");
37         }
38
39         sessionFactory = configuration.buildSessionFactory();
40         new SchemaUpdate(configuration).execute(true, true);
41     }
42
43     public Set JavaDoc getDependentResources() {
44         return resources;
45     }
46
47     public SessionFactory getSessionFactory() {
48         return sessionFactory;
49     }
50
51     public void dispose() {
52         System.out.println("HibernateConfiguration.dispose");
53         sessionFactory.close();
54     }
55 }
56
Popular Tags