1
2
3
4
5
6
7 package ch.ledcom.hephaistos.dao;
8
9 import java.net.URL;
10
11 import javax.servlet.ServletContext;
12 import javax.servlet.ServletException;
13
14 import net.sf.hibernate.SessionFactory;
15 import net.sf.hibernate.cfg.Configuration;
16
17 import org.apache.commons.logging.Log;
18 import org.apache.commons.logging.LogFactory;
19 import org.apache.struts.action.ActionServlet;
20 import org.apache.struts.action.PlugIn;
21 import org.apache.struts.config.ModuleConfig;
22
23 /***
24 * Implements the <code>PlugIn</code> interface to configure the Hibernate
25 * data persistence library. A configured
26 * <code>net.sf.hibernate.SessionFactory</code> is stored in the
27 * <code>ServletContext</code> of the web application unless the property
28 * <code>storedInServletContext</code> is set to <code>false</code>.
29 *
30 * <p>
31 * <plugin class="net.sf.hibernate.plugins.struts.HibernatePlugIn">
32 * <set-property name="configFilePath""
33 * value="path-to-config-file"/>
34 * <set-property name="storedInServletContext""
35 * value="true-or-false"/>
36 * </plugin>
37 *
38 * @author <a href="mailto:bhandy@users.sf.net">Bradley M. Handy</a>
39 * @version 1.0
40 */
41 public class HibernatePlugIn implements PlugIn {
42
43 /***
44 * the key under which the <code>SessionFactory</code> instance is stored
45 * in the <code>ServletContext</code>.
46 */
47 public static final String SESSION_FACTORY_KEY
48 = SessionFactory.class.getName();
49
50 private static Log logger = LogFactory.getLog(HibernatePlugIn.class);
51
52 /***
53 * indicates whether the <code>SessionFactory</code> instance will be stored
54 * in the <code>ServletContext</code>, or not.
55 */
56 private boolean _storedInServletContext = true;
57
58 /***
59 * the path to the xml configuration file. the path should start with a
60 * '/' character and be relative to the root of the class path.
61 * (DEFAULT: "/hibernate.cfg.xml")
62 */
63 private String _configFilePath = "/hibernate.cfg.xml";
64
65 private ActionServlet _servlet = null;
66 private ModuleConfig _config = null;
67 private SessionFactory _factory = null;
68
69 /***
70 * Destroys the <code>SessionFactory</code> instance.
71 */
72 public void destroy() {
73 _servlet = null;
74 _config = null;
75
76 try {
77 logger.debug("Destroying SessionFactory...");
78
79 _factory.close();
80
81 logger.debug("SessionFactory destroyed...");
82 } catch (Exception e) {
83 logger.error("Unable to destroy SessionFactory...(exception ignored)",
84 e);
85 }
86 }
87
88 /***
89 * Initializes the <code>SessionFactory</code>.
90 * @param servlet the <code>ActionServlet</code> instance under which the
91 * plugin will run.
92 * @param config the <code>ModuleConfig</code> for the module under which
93 * the plugin will run.
94 */
95 public void init(ActionServlet servlet, ModuleConfig config)
96 throws ServletException {
97 _servlet = servlet;
98 _config = config;
99
100 initHibernate();
101 }
102
103 /***
104 * Initializes Hibernate with the config file found at
105 * <code>configFilePath</code>.
106 */
107 private void initHibernate() throws ServletException {
108 Configuration configuration = null;
109 URL configFileURL = null;
110 ServletContext context = null;
111
112 try {
113 configFileURL = HibernatePlugIn.class.getResource(_configFilePath);
114
115 context = _servlet.getServletContext();
116
117 if (logger.isDebugEnabled()) {
118 logger.debug("Initializing Hibernate from "
119 + _configFilePath + "...");
120 }
121
122 configuration = (new Configuration()).configure(configFileURL);
123 _factory = configuration.buildSessionFactory();
124
125 if (_storedInServletContext) {
126 logger.debug("Storing SessionFactory in ServletContext, using key : " + SESSION_FACTORY_KEY);
127
128 context.setAttribute(SESSION_FACTORY_KEY, _factory);
129 }
130
131 } catch (Throwable t) {
132 logger.error("Exception while initializing Hibernate.");
133 logger.error("Rethrowing exception...", t);
134
135 throw (new ServletException(t));
136 }
137 }
138
139 /***
140 * Setter for property configFilePath.
141 * @param configFilePath New value of property configFilePath.
142 */
143 public void setConfigFilePath(String configFilePath) {
144 if ((configFilePath == null) || (configFilePath.trim().length() == 0)) {
145 throw new IllegalArgumentException(
146 "configFilePath cannot be blank or null.");
147 }
148
149 if (logger.isDebugEnabled()) {
150 logger.debug("Setting 'configFilePath' to '"
151 + configFilePath + "'...");
152 }
153
154 _configFilePath = configFilePath;
155 }
156
157 /***
158 * Setter for property storedInServletContext.
159 * @param storedInServletContext New value of property storedInServletContext.
160 */
161 public void setStoredInServletContext(String storedInServletContext) {
162 if ((storedInServletContext == null)
163 || (storedInServletContext.trim().length() == 0)) {
164 storedInServletContext = "false";
165 }
166
167 if (logger.isDebugEnabled()) {
168 logger.debug("Setting 'storedInServletContext' to '"
169 + storedInServletContext + "'...");
170 }
171
172 _storedInServletContext
173 = new Boolean(storedInServletContext).booleanValue();
174 }
175
176 }