    001// Copyright 2007-2013 The Apache Software Foundation
    002//
    003// Licensed under the Apache License, Version 2.0 (the "License");
    004// you may not use this file except in compliance with the License.
    005// You may obtain a copy of the License at
    006//
    007// http://www.apache.org/licenses/LICENSE-2.0
    008//
    009// Unless required by applicable law or agreed to in writing, software
    010// distributed under the License is distributed on an "AS IS" BASIS,
    011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    012// See the License for the specific language governing permissions and
    013// limitations under the License.
    014
    015package org.apache.tapestry5.hibernate.internal;
    016
    017import org.apache.tapestry5.hibernate.HibernateConfigurer;
    018import org.apache.tapestry5.hibernate.HibernateSessionSource;
    019import org.apache.tapestry5.ioc.annotations.PostInjection;
    020import org.apache.tapestry5.ioc.internal.util.InternalUtils;
    021import org.apache.tapestry5.ioc.services.RegistryShutdownHub;
    022import org.hibernate.Session;
    023import org.hibernate.SessionFactory;
    024import org.hibernate.cfg.Configuration;
    025import org.slf4j.Logger;
    026
    027import java.util.List;
    028
    029public class HibernateSessionSourceImpl implements HibernateSessionSource
    030{
    031    private final SessionFactory sessionFactory;
    032
    033    private final Configuration configuration;
    034
    035    public HibernateSessionSourceImpl(Logger logger, List<HibernateConfigurer> hibernateConfigurers)
    036    {
    037        long startTime = System.currentTimeMillis();
    038
    039        configuration = new Configuration();
    040
    041        for (HibernateConfigurer configurer : hibernateConfigurers)
    042            configurer.configure(configuration);
    043
    044        long configurationComplete = System.currentTimeMillis();
    045
    046        sessionFactory = configuration.buildSessionFactory();
    047
    048        long factoryCreated = System.currentTimeMillis();
    049
    050        logger.info(String.format("Hibernate startup: %,d ms to configure, %,d ms overall.", configurationComplete - startTime, factoryCreated - startTime));
    051
    052        logger.info(String.format("Configured Hibernate entities: %s", InternalUtils.joinSorted(sessionFactory.getAllClassMetadata().keySet())));
    053    }
    054
    055    @PostInjection
    056    public void listenForShutdown(RegistryShutdownHub hub)
    057    {
    058        hub.addRegistryShutdownListener(new Runnable()
    059        {
    060            @Override
    061            public void run()
    062            {
    063                sessionFactory.close();
    064            }
    065        });
    066    }
    067
    068    @Override
    069    public Session create()
    070    {
    071        return sessionFactory.openSession();
    072    }
    073
    074    @Override
    075    public SessionFactory getSessionFactory()
    076    {
    077        return sessionFactory;
    078    }
    079
    080    @Override
    081    public Configuration getConfiguration()
    082    {
    083        return configuration;
    084    }
    085
    086}