1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package net.smartlab.web.config;
24
25 import java.net.URL;
26 import javax.naming.InitialContext;
27 import javax.naming.NamingException;
28 import javax.rmi.PortableRemoteObject;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.hibernate.SessionFactory;
32 import org.hibernate.cfg.Configuration;
33 import net.smartlab.config.Element;
34 import net.smartlab.config.XMLConfiguration;
35 import net.smartlab.web.BusinessObjectFactory;
36 import net.smartlab.web.Domain;
37
38
39
40
41
42
43
44
45
46 public class JNDIConfigurationStrategy implements FactoryConfigurationStrategy {
47
48
49
50
51 protected final Log logger = LogFactory.getLog(JNDIConfigurationStrategy.class);
52
53
54
55
56
57 public SessionFactory getSessionFactory(BusinessObjectFactory factory) {
58 synchronized (BusinessObjectFactory.class) {
59 String archive = Domain.getLastArchiveName(factory.getClass());
60 URL file = Domain.getResource(factory.getClass(), new String[] {"/META-INF/" + archive + ".hcf",
61 "/META-INF/smartweb.jar.hcf", "/META-INF/hibernate.conf.xml"});
62 try {
63 XMLConfiguration config = new XMLConfiguration(file);
64 Element sessionFactory = config.getElement("session-factory");
65 String jndi = sessionFactory.getAttribute("name");
66 try {
67
68 return (SessionFactory)PortableRemoteObject.narrow(new InitialContext().lookup(jndi),
69 SessionFactory.class);
70 } catch (NamingException ne) {
71 logger.info("getSessionFactory() - configure hibernate");
72 try {
73
74 Configuration hibernate = new Configuration().configure(file);
75 hibernate.buildSessionFactory();
76 return (SessionFactory)PortableRemoteObject.narrow(new InitialContext().lookup(jndi),
77 SessionFactory.class);
78 } catch (Exception e) {
79 logger.error("getSessionFactory() - error", e);
80 }
81 }
82 } catch (Exception e) {
83 logger.error("getSessionFactory() - error", e);
84 }
85 return null;
86 }
87 }
88 }