001    // Copyright 2007, 2008, 2011 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    
015    package org.apache.tapestry5.internal.hibernate;
016    
017    import org.apache.tapestry5.hibernate.HibernateConfigurer;
018    import org.apache.tapestry5.hibernate.HibernateSessionSource;
019    import org.apache.tapestry5.ioc.annotations.PostInjection;
020    import org.apache.tapestry5.ioc.services.RegistryShutdownHub;
021    import org.hibernate.Session;
022    import org.hibernate.SessionFactory;
023    import org.hibernate.cfg.AnnotationConfiguration;
024    import org.hibernate.cfg.Configuration;
025    import org.slf4j.Logger;
026    
027    import java.util.List;
028    
029    public 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 AnnotationConfiguration();
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(HibernateCoreMessages.startupTiming(configurationComplete - startTime, factoryCreated - startTime));
051    
052            logger.info(HibernateCoreMessages.entityCatalog(sessionFactory.getAllClassMetadata().keySet()));
053        }
054    
055        @PostInjection
056        public void listenForShutdown(RegistryShutdownHub hub)
057        {
058            hub.addRegistryShutdownListener(new Runnable()
059            {
060                public void run()
061                {
062                    sessionFactory.close();
063                }
064            });
065        }
066    
067        public Session create()
068        {
069            return sessionFactory.openSession();
070        }
071    
072        public SessionFactory getSessionFactory()
073        {
074            return sessionFactory;
075        }
076    
077        public Configuration getConfiguration()
078        {
079            return configuration;
080        }
081    
082    }