<plug-in className="org.haree.struts.HibernatePlugIn"> <!-- 'path-to-config-file' is relative to the root of the class path. It MUST start with a '/'. The default is "/hibernate.cfg.xml --> <set-property property="configFilePath" value="path-to-config-file"/> <set-property property="storeInServletContext" value="true-or-false"/> </plug-in>
/** * Implements the <code>PlugIn</code> interface to configure the Hibernate * data persistence library. A configured * <code>net.sf.hibernate.SessionFactory</code> is stored in the * <code>ServletContext</code> of the web application unless the property * <code>storedInServletContext</code> is set to <code>false</code>. * * <p> * <plugin class="net.sf.hibernate.plugins.struts.HibernatePlugIn"> * <set-property name="configFilePath"" * value="path-to-config-file"/> * <set-property name="storedInServletContext"" * value="true-or-false"/> * </plugin> * * @author <a href="mailto:bhandy@users.sf.net">Bradley M. Handy</a> * @version 1.0 */ public class HibernatePlugIn implements PlugIn {
/** * the key under which the <code>SessionFactory</code> instance is stored * in the <code>ServletContext</code>. */ public static final String SESSION_FACTORY_KEY = SessionFactory.class.getName();
/** * indicates whether the <code>SessionFactory</code> instance will be stored * in the <code>ServletContext</code>, or not. */ private boolean _storedInServletContext =true;
/** * the path to the xml configuration file. the path should start with a * '/' character and be relative to the root of the class path. * (DEFAULT: "/hibernate.cfg.xml") */ private String _configFilePath ="/hibernate.cfg.xml";
/** * Initializes the <code>SessionFactory</code>. * @param servlet the <code>ActionServlet</code> instance under which the * plugin will run. * @param config the <code>ModuleConfig</code> for the module under which * the plugin will run. */ public void init(ActionServlet servlet, ModuleConfig config) throws ServletException { _servlet = servlet; _config = config;
initHibernate(); }
/** * Initializes Hibernate with the config file found at * <code>configFilePath</code>. */ private void initHibernate() throws ServletException { Configuration configuration =null; URL configFileURL =null; ServletContext context =null;
/** * Setter for property configFilePath. * @param configFilePath New value of property configFilePath. */ public void setConfigFilePath(String configFilePath) { if ((configFilePath ==null) || (configFilePath.trim().length() ==0)) { thrownew IllegalArgumentException( "configFilePath cannot be blank or null."); }
if (_log.isDebugEnabled()) { _log.debug("Setting 'configFilePath' to '" + configFilePath +"'"); }
_configFilePath = configFilePath; }
/** * Setter for property storedInServletContext. * @param storedInServletContext New value of property storedInServletContext. */ public void setStoredInServletContext(String storedInServletContext) { if ((storedInServletContext ==null) || (storedInServletContext.trim().length() ==0)) { storedInServletContext ="false"; }
if (_log.isDebugEnabled()) { _log.debug("Setting 'storedInServletContext' to '" + storedInServletContext +"'"); }