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.net.MalformedURLException;
31  
32  import org.hibernate.HibernateException;
33  import org.jcontainer.dna.ConfigurationException;
34  import org.jcontainer.dna.Logger;
35  import org.objectledge.filesystem.FileSystem;
36  
37  /***
38   * The hibernate session factory component.
39   * 
40   * <p>
41   * An example configuration follows:
42   * </p>
43   * <pre>
44   * &lt;?xml version='1.0' encoding='utf-8'?>
45   * &lt;!DOCTYPE hibernate-configuration PUBLIC
46   *       &quot;-//Hibernate/Hibernate Configuration DTD 3.0//EN&quot;
47   *       &quot;http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd&quot;>
48   * &lt;hibernate-configuration>
49   *   &lt;session-factory>
50   *     &lt;!-- Database connection settings -->
51   *     &lt;property name="connection.driver_class">org.postgresql.Driver&lt;/property>
52   *     &lt;property name="connection.url">jdbc:postgresql://localhost/test_db&lt;/property>
53   *     &lt;property name="connection.username">test&lt;/property>
54   *     &lt;property name="connection.password">test&lt;/property>
55   * 
56   *     &lt;!-- JDBC connection pool (use the built-in) -->
57   *     &lt;property name="connection.pool_size">1&lt;/property>
58   * 
59   *     &lt;!-- SQL dialect -->
60   *     &lt;property name="dialect">org.hibernate.dialect.PostgreSQLDialect&lt;/property>
61   * 
62   *     &lt;!-- Echo all executed SQL to stdout -->
63   *     &lt;property name="show_sql">true&lt;/property>
64   * 
65   *     &lt;property name="query.substitutions">true 1, false 0&lt;/property>
66   * 
67   *     &lt;!--     Hibernate Security Service -->
68   *     &lt;mapping class="org.objectledge.security.hibernate.HibernatePermission"
69   *         resource="org/objectledge/security/hibernate/HibernatePermission.hbm.xml"/>
70   *     &lt;mapping class="org.objectledge.security.hibernate.HibernateResourceGroup"
71   *         resource="org/objectledge/security/hibernate/HibernateResourceGroup.hbm.xml"/>
72   *     &lt;mapping class="org.objectledge.security.hibernate.HibernateRole"
73   *         resource="org/objectledge/security/hibernate/HibernateRole.hbm.xml"/>
74   *     &lt;mapping class="org.objectledge.security.hibernate.HibernateRolePermission"
75   *         resource="org/objectledge/security/hibernate/HibernateRolePermission.hbm.xml"/>
76   *     &lt;mapping class="org.objectledge.security.hibernate.HibernateUser"
77   *         resource="org/objectledge/security/hibernate/HibernateUser.hbm.xml"/>
78   *     &lt;mapping class="org.objectledge.security.hibernate.HibernateUserGroupRole"
79   *         resource="org/objectledge/security/hibernate/HibernateUserGroupRole.hbm.xml"/>
80   *     &lt;mapping class="org.objectledge.security.hibernate.HibernateUserResourceGroupRo"
81   *         resource="org/objectledge/security/hibernate/HibernateUserResourceGroupRo.hbm.xml"/>
82   *   &lt;/session-factory>
83   * &lt;/hibernate-configuration>
84   *</pre>
85   * @author <a href="mailto:mgolebsk@elka.pw.edu.pl">Marcin Golebski</a>
86   * @version $Id: BasicHibernateSessionFactory.java,v 1.1 2006/01/17 13:40:27 zwierzem Exp $
87   */
88  public class BasicHibernateSessionFactory 
89  extends AbstractHibernateSessionFactory
90  {
91      public BasicHibernateSessionFactory(Logger logger, final FileSystem fs,
92          InterceptorFactory interceptorFactory)
93          throws ConfigurationException
94      {
95          super(logger, fs, new HibernateConfigurator()
96              {
97                  public void configure(org.hibernate.cfg.Configuration cfg) throws ConfigurationException
98                  {
99                      String xmlPath = "/config/org.objectledge.hibernate.HibernateSessionFactory.xml";  
100                     try
101                     {
102                         cfg.configure(fs.getResource(xmlPath));
103                     }
104                     catch(HibernateException e)
105                     {
106                         throw new ConfigurationException("cannot configure hibernate", e);
107                     }
108                     catch(MalformedURLException e)
109                     {
110                         throw new ConfigurationException("cannot access hibernate configuration file", e);
111                     }
112                 }
113             },
114             interceptorFactory);
115     }
116 
117     public BasicHibernateSessionFactory(Logger logger, final FileSystem fs)
118         throws ConfigurationException 
119     {
120         this(logger, fs, null);
121     }
122 }