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 public void run() 066 { 067 if (earlyStartup) 068 sessionSource.getConfiguration(); 069 } 070 }); 071 } 072 073 public static HibernateEntityPackageManager buildHibernateEntityPackageManager( 074 final Collection<String> packageNames) 075 { 076 return new HibernateEntityPackageManager() 077 { 078 public Collection<String> getPackageNames() 079 { 080 return packageNames; 081 } 082 }; 083 } 084 085 /** 086 * The session manager manages sessions on a per-thread/per-request basis. Any active transaction will be rolled 087 * back at {@linkplain org.apache.tapestry5.ioc.Registry#cleanupThread() thread cleanup time}. The thread is 088 * cleaned up automatically in a Tapestry web application. 089 */ 090 @Scope(ScopeConstants.PERTHREAD) 091 public static HibernateSessionManager buildHibernateSessionManager(HibernateSessionSource sessionSource, 092 PerthreadManager perthreadManager) 093 { 094 HibernateSessionManagerImpl service = new HibernateSessionManagerImpl(sessionSource); 095 096 perthreadManager.addThreadCleanupListener(service); 097 098 return service; 099 } 100 101 public static Session buildSession(HibernateSessionManager sessionManager, 102 PropertyShadowBuilder propertyShadowBuilder) 103 { 104 // Here's the thing: the tapestry.hibernate.Session class doesn't have to be per-thread, 105 // since 106 // it will invoke getSession() on the HibernateSessionManager service (which is per-thread). 107 // On 108 // first invocation per request, 109 // this forces the HSM into existence (which creates the session and begins the 110 // transaction). 111 // Thus we don't actually create 112 // a session until we first try to access it, then the session continues to exist for the 113 // rest 114 // of the request. 115 116 return propertyShadowBuilder.build(sessionManager, "session", Session.class); 117 } 118 119 /** 120 * Adds the following configurers: <dl> <dt>Default <dd> performs default hibernate configuration <dt>PackageName 121 * <dd> loads entities by package name</dl> 122 */ 123 public static void contributeHibernateSessionSource(OrderedConfiguration<HibernateConfigurer> config, 124 125 @Local 126 HibernateConfigurer defaultHibernateConfigurer) 127 { 128 config.add("Default", defaultHibernateConfigurer); 129 config.addInstance("PackageName", PackageNameHibernateConfigurer.class); 130 } 131}