/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package com.xpn.xwiki.store.hibernate;

import java.io.InputStream;

import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.xwiki.component.annotation.Component;

import com.xpn.xwiki.util.Util;

/**
 * Default implementation for {@link HibernateSessionFactory}.
 * 
 * @version $Id$
 * @since 2.0M1
 * @todo this was coded by Artem. Find out why we need this as a component.
 */
@Component
public class DefaultHibernateSessionFactory implements HibernateSessionFactory
{
    /**
     * Hibernate configuration object.
     */
    private Configuration configuration = new Configuration()
    {
        private static final long serialVersionUID = 1L;

        // there is no #configure(InputStream) so we use #configure(String) and override #getConfigurationInputStream
        @Override
        protected InputStream getConfigurationInputStream(String resource) throws HibernateException
        {
            InputStream stream = Util.getResourceAsStream(resource);
            if (stream == null) {
                throw new HibernateException("Can't find [" + resource + "] for hibernate configuration");
            }
            return stream;
        }
    };

    /**
     * Real Hibernate session factory.
     */
    private SessionFactory sessionFactory;

    /**
     * {@inheritDoc}
     * @see HibernateSessionFactory#getConfiguration()
     */
    public Configuration getConfiguration()
    {
        return this.configuration;
    }

    /**
     * {@inheritDoc}
     * @see HibernateSessionFactory#getSessionFactory()
     */
    public SessionFactory getSessionFactory()
    {
        return this.sessionFactory;
    }

    /**
     * {@inheritDoc}
     * @see HibernateSessionFactory#setSessionFactory(SessionFactory)
     */
    public void setSessionFactory(SessionFactory sessionFactory)
    {
        this.sessionFactory = sessionFactory;
    }
}

@@ -942,27 +942,69 @@
 java.io.
-InputStream
+File;%0Aimport java.io.InputStream;%0Aimport java.net.URL
 ;%0A%0Aimpor
@@ -1309,16 +1309,20 @@
 1%0A *
- @todo t
+/%0A// TODO: T
 his 
@@ -1383,20 +1383,16 @@
 ponent.%0A
- */%0A
 @Compone
@@ -1656,16 +1656,2600 @@
  = 1L;%0A%0A
+        /**%0A         * Whether the Hibernate Configuration has alreayd been initialized or not. We do this so that the%0A         * Hibernate %7B@link org.hibernate.cfg.Configuration#configure()%7D methods can be called several times in a%0A         * row without causing some Duplicate Mapping errors, see our overridden%0A         * %7B@link #getConfigurationInputStream(String)%7D below.%0A         */%0A        private boolean isConfigurationInitialized;%0A%0A        /**%0A         * %7B@inheritDoc%7D%0A         * @see org.hibernate.cfg.Configuration#configure()%0A         */%0A        @Override public Configuration configure() throws HibernateException%0A        %7B%0A            Configuration configuration;%0A            if (this.isConfigurationInitialized) %7B%0A                configuration = this;%0A            %7D else %7B%0A                configuration = super.configure();%0A                this.isConfigurationInitialized = true;%0A            %7D%0A            return configuration;%0A        %7D%0A%0A        /**%0A         * %7B@inheritDoc%7D%0A         * @see org.hibernate.cfg.Configuration#configure(String) %0A         */%0A        @Override public Configuration configure(String resource) throws HibernateException%0A        %7B%0A            Configuration configuration;%0A            if (this.isConfigurationInitialized) %7B%0A                configuration = this;%0A            %7D else %7B%0A                configuration = super.configure(resource);%0A                this.isConfigurationInitialized = true;%0A            %7D%0A            return configuration;%0A        %7D%0A%0A        /**%0A         * %7B@inheritDoc%7D%0A         * @see org.hibernate.cfg.Configuration#configure(java.net.URL)%0A         */%0A        @Override public Configuration configure(URL url) throws HibernateException%0A        %7B%0A            Configuration configuration;%0A            if (this.isConfigurationInitialized) %7B%0A                configuration = this;%0A            %7D else %7B%0A                configuration = super.configure(url);%0A                this.isConfigurationInitialized = true;%0A            %7D%0A            return configuration;%0A        %7D%0A%0A        /**%0A         * %7B@inheritDoc%7D%0A         * @see org.hibernate.cfg.Configuration#configure(java.io.File) %0A         */%0A        @Override public Configuration configure(File configFile) throws HibernateException%0A        %7B%0A            Configuration configuration;%0A            if (this.isConfigurationInitialized) %7B%0A                configuration = this;%0A            %7D else %7B%0A                configuration = super.configure(configFile);%0A                this.isConfigurationInitialized = true;%0A            %7D%0A            return configuration;%0A        %7D%0A%0A
         

Make XWiki support recovering from a connection problem with the database