View Javadoc

1   // 
2   // Copyright (c) 2003-2005, Caltha - Gajda, Krzewski, Mach, Potempski Sp.J. 
3   // All rights reserved. 
4   //   
5   // Redistribution and use in source and binary forms, with or without modification,  
6   // are permitted provided that the following conditions are met: 
7   //   
8   // * Redistributions of source code must retain the above copyright notice,  
9   // this list of conditions and the following disclaimer. 
10  // * Redistributions in binary form must reproduce the above copyright notice,  
11  // this list of conditions and the following disclaimer in the documentation  
12  // and/or other materials provided with the distribution. 
13  // * Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.  
14  // nor the names of its contributors may be used to endorse or promote products  
15  // derived from this software without specific prior written permission. 
16  // 
17  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"  
18  // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED  
19  // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
20  // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,  
21  // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,  
22  // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
23  // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
24  // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)  
25  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE  
26  // POSSIBILITY OF SUCH DAMAGE. 
27  //
28  package org.objectledge.hibernate;
29  
30  import java.io.IOException;
31  import java.util.HashMap;
32  
33  import org.hibernate.Interceptor;
34  import org.hibernate.Session;
35  import org.hibernate.SessionFactory;
36  import org.jcontainer.dna.ConfigurationException;
37  import org.jcontainer.dna.Logger;
38  import org.objectledge.filesystem.FileSystem;
39  import org.xml.sax.EntityResolver;
40  import org.xml.sax.InputSource;
41  import org.xml.sax.SAXException;
42  
43  /***
44   * The hibernate session factory component.
45   * 
46   * @author <a href="mailto:mgolebsk@elka.pw.edu.pl">Marcin Golebski</a>
47   * @author <a href="mailto:dgajda@caltha.pl">Damian Gajda</a>
48   * @version $Id: AbstractHibernateSessionFactory.java,v 1.2 2006/03/13 18:22:50 zwierzem Exp $
49   */
50  public abstract class AbstractHibernateSessionFactory 
51  implements HibernateSessionFactory
52  {
53      protected SessionFactory sessionFactory;
54      protected Interceptor interceptor;
55  
56      public AbstractHibernateSessionFactory(Logger logger, final FileSystem fs, HibernateConfigurator configurator,
57          InterceptorFactory interceptorFactory) throws ConfigurationException
58      {
59          logger.info("HibernateConfig starting...");
60          org.hibernate.cfg.Configuration cfg = new org.hibernate.cfg.Configuration();
61          cfg.setEntityResolver(new EntityResolver()
62          {
63              private HashMap<String, String> mapping = new HashMap<String, String>();
64              {
65                  mapping.put("-//Hibernate/Hibernate Configuration DTD 3.0//EN",
66                      "hibernate-configuration-3.0.dtd");
67                  mapping.put("-//Hibernate/Hibernate Mapping DTD 3.0//EN",
68                      "hibernate-mapping-3.0.dtd");
69              }
70              
71              public InputSource resolveEntity(String publicId, String systemId)
72              throws SAXException, IOException
73              {
74                  InputSource is = new InputSource(
75                       fs.getInputStream("/org/hibernate/"+mapping.get(publicId)));
76                  is.setPublicId(publicId);
77                  //is.setSystemId()
78                  return is;
79              }
80          });
81          
82          configurator.configure(cfg);
83         
84          sessionFactory = cfg.buildSessionFactory();
85  
86          if(interceptorFactory != null)
87          {
88              this.interceptor = interceptorFactory.createInterceptor(sessionFactory);
89          }       
90          logger.info("HibernateConfig started");
91      }
92  
93      public AbstractHibernateSessionFactory(Logger logger, FileSystem fs,
94          HibernateConfigurator configurator)
95          throws ConfigurationException 
96      {
97          this(logger, fs, configurator, null);
98      }
99  
100     /***
101      * Opens the hibernate session.
102      * 
103      * @return the newly open hibernate session.
104      */
105     public Session openHibernateSession()
106     {
107         if(interceptor != null)
108         {
109             return sessionFactory.openSession(interceptor);
110         }
111         return sessionFactory.openSession();
112     }
113 
114     public void start()
115     {
116         // nothing to do
117     }
118 
119     public void stop()
120     {
121         // nothing to do
122     }
123     
124     /***
125      * Configurator configures the hibernate session factory, to be implemented by concrete
126      * implementations.
127      */
128     protected interface HibernateConfigurator
129     {
130         /***
131          * Provides the configuration data and calls one of <code>configure()</code> methods on
132          * hibernate configuration object.
133          * 
134          * @param cfg the hibernate configuration object.
135          * @throws ConfigurationException thrown on configuration problems.
136          */
137         public void configure(org.hibernate.cfg.Configuration cfg) throws ConfigurationException;
138     }
139 }