    001// Copyright 2008, 2009, 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
    015package org.apache.tapestry5.hibernate.modules;
    016
    017import org.apache.tapestry5.hibernate.*;
    018import org.apache.tapestry5.internal.hibernate.*;
    019import org.apache.tapestry5.ioc.MappedConfiguration;
    020import org.apache.tapestry5.ioc.OrderedConfiguration;
    021import org.apache.tapestry5.ioc.ScopeConstants;
    022import org.apache.tapestry5.ioc.ServiceBinder;
    023import org.apache.tapestry5.ioc.annotations.Local;
    024import org.apache.tapestry5.ioc.annotations.Marker;
    025import org.apache.tapestry5.ioc.annotations.Scope;
    026import org.apache.tapestry5.ioc.annotations.Symbol;
    027import org.apache.tapestry5.ioc.services.PerthreadManager;
    028import org.apache.tapestry5.ioc.services.PropertyShadowBuilder;
    029import org.hibernate.Session;
    030
    031import java.util.Collection;
    032
    033/**
    034 * Defines core services that support initialization of Hibernate and access to the Hibernate {@link
    035 * org.hibernate.Session}.
    036 */
    037@SuppressWarnings({"JavaDoc"})
    038@Marker(HibernateCore.class)
    039public class HibernateCoreModule
    040{
    041    public static void bind(ServiceBinder binder)
    042    {
    043        binder.bind(HibernateTransactionDecorator.class, HibernateTransactionDecoratorImpl.class);
    044        binder.bind(HibernateTransactionAdvisor.class, HibernateTransactionAdvisorImpl.class);
    045        binder.bind(HibernateConfigurer.class, DefaultHibernateConfigurer.class).withSimpleId();
    046        binder.bind(HibernateSessionSource.class, HibernateSessionSourceImpl.class);
    047    }
    048
    049
    050    public static void contributeFactoryDefaults(MappedConfiguration<String, String> configuration)
    051    {
    052        configuration.add(HibernateSymbols.DEFAULT_CONFIGURATION, "true");
    053        configuration.add(HibernateSymbols.EARLY_START_UP, "false");
    054    }
    055
    056    public static void contributeRegistryStartup(OrderedConfiguration<Runnable> configuration,
    057
    058                                                 @Symbol(HibernateSymbols.EARLY_START_UP)
    059                                                 final boolean earlyStartup,
    060
    061                                                 final HibernateSessionSource sessionSource)
    062    {
    063        configuration.add("HibernateStartup", new Runnable()
    064        {
    065            @Override
    066            public void run()
    067            {
    068                if (earlyStartup)
    069                    sessionSource.getConfiguration();
    070            }
    071        });
    072    }
    073
    074    public static HibernateEntityPackageManager buildHibernateEntityPackageManager(
    075            final Collection<String> packageNames)
    076    {
    077        return new HibernateEntityPackageManager()
    078        {
    079            @Override
    080            public Collection<String> getPackageNames()
    081            {
    082                return packageNames;
    083            }
    084        };
    085    }
    086
    087    /**
    088     * The session manager manages sessions on a per-thread/per-request basis. Any active transaction will be rolled
    089     * back at {@linkplain org.apache.tapestry5.ioc.Registry#cleanupThread() thread cleanup time}.  The thread is
    090     * cleaned up automatically in a Tapestry web application.
    091     */
    092    @Scope(ScopeConstants.PERTHREAD)
    093    public static HibernateSessionManager buildHibernateSessionManager(HibernateSessionSource sessionSource,
    094                                                                       PerthreadManager perthreadManager)
    095    {
    096        HibernateSessionManagerImpl service = new HibernateSessionManagerImpl(sessionSource);
    097
    098        perthreadManager.addThreadCleanupListener(service);
    099
    100        return service;
    101    }
    102
    103    public static Session buildSession(HibernateSessionManager sessionManager,
    104                                       PropertyShadowBuilder propertyShadowBuilder)
    105    {
    106        // Here's the thing: the tapestry.hibernate.Session class doesn't have to be per-thread,
    107        // since
    108        // it will invoke getSession() on the HibernateSessionManager service (which is per-thread).
    109        // On
    110        // first invocation per request,
    111        // this forces the HSM into existence (which creates the session and begins the
    112        // transaction).
    113        // Thus we don't actually create
    114        // a session until we first try to access it, then the session continues to exist for the
    115        // rest
    116        // of the request.
    117
    118        return propertyShadowBuilder.build(sessionManager, "session", Session.class);
    119    }
    120
    121    /**
    122     * Adds the following configurers: <dl> <dt>Default <dd> performs default hibernate configuration <dt>PackageName
    123     * <dd> loads entities by package name</dl>
    124     */
    125    public static void contributeHibernateSessionSource(OrderedConfiguration<HibernateConfigurer> config,
    126
    127                                                        @Local
    128                                                        HibernateConfigurer defaultHibernateConfigurer)
    129    {
    130        config.add("Default", defaultHibernateConfigurer);
    131        config.addInstance("PackageName", PackageNameHibernateConfigurer.class);
    132    }
    133}