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 java.util.List;
018import java.util.Objects;
019import java.util.stream.Collectors;
020
021import javax.persistence.metamodel.EntityType;
022
023import org.apache.tapestry5.hibernate.HibernateConfigurer;
024import org.apache.tapestry5.hibernate.HibernateSessionSource;
025import org.apache.tapestry5.ioc.annotations.PostInjection;
026import org.apache.tapestry5.ioc.internal.util.InternalUtils;
027import org.apache.tapestry5.ioc.services.RegistryShutdownHub;
028import org.hibernate.Session;
029import org.hibernate.SessionFactory;
030import org.hibernate.cfg.Configuration;
031import org.slf4j.Logger;
032
033public class HibernateSessionSourceImpl implements HibernateSessionSource
034{
035    private final SessionFactory sessionFactory;
036
037    private final Configuration configuration;
038
039    public HibernateSessionSourceImpl(Logger logger, List<HibernateConfigurer> hibernateConfigurers)
040    {
041        long startTime = System.currentTimeMillis();
042
043        configuration = new Configuration();
044
045        for (HibernateConfigurer configurer : hibernateConfigurers)
046            configurer.configure(configuration);
047
048        long configurationComplete = System.currentTimeMillis();
049
050        sessionFactory = configuration.buildSessionFactory();
051
052        long factoryCreated = System.currentTimeMillis();
053
054        logger.info(String.format("Hibernate startup: %,d ms to configure, %,d ms overall.", configurationComplete - startTime, factoryCreated - startTime));
055        
056        List<Class<?>> classes = sessionFactory.getMetamodel().getEntities().stream()
057                .map(EntityType::getJavaType)
058                .filter(Objects::nonNull)
059                .collect(Collectors.toList());        
060
061        logger.info(String.format("Configured Hibernate entities: %s", InternalUtils.joinSorted(classes)));
062    }
063
064    @PostInjection
065    public void listenForShutdown(RegistryShutdownHub hub)
066    {
067        hub.addRegistryShutdownListener(new Runnable()
068        {
069            @Override
070            public void run()
071            {
072                sessionFactory.close();
073            }
074        });
075    }
076
077    @Override
078    public Session create()
079    {
080        return sessionFactory.openSession();
081    }
082
083    @Override
084    public SessionFactory getSessionFactory()
085    {
086        return sessionFactory;
087    }
088
089    @Override
090    public Configuration getConfiguration()
091    {
092        return configuration;
093    }
094
095}