001 // Copyright 2008 Thiago H. de Paula Figueiredo
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 br.com.arsmachina.dao.hibernate.ioc;
016
017 import java.beans.PropertyVetoException;
018
019 import javax.sql.DataSource;
020
021 import org.hibernate.SessionFactory;
022 import org.hibernate.cfg.AnnotationConfiguration;
023 import org.springframework.config.java.annotation.Bean;
024 import org.springframework.config.java.annotation.Configuration;
025 import org.springframework.config.java.annotation.ExternalValue;
026 import org.springframework.config.java.annotation.Lazy;
027 import org.springframework.config.java.annotation.ResourceBundles;
028 import org.springframework.core.io.ClassPathResource;
029 import org.springframework.orm.hibernate3.HibernateTransactionManager;
030 import org.springframework.orm.hibernate3.LocalSessionFactoryBean;
031 import org.springframework.transaction.PlatformTransactionManager;
032
033 import com.mchange.v2.c3p0.ComboPooledDataSource;
034
035 /**
036 * Class that configures the persistence layer for Spring. Database info is read from a
037 * <code>datasource.properties</code> file in the root of the classpath. Hibernate configuration
038 * properties is read from a <code>hibernate.cfg.xml</code> file, also in the root of the
039 * classpath.
040 *
041 * @author Thiago H. de Paula Figueiredo
042 */
043 @Configuration(defaultLazy = Lazy.TRUE)
044 @ResourceBundles( { "classpath:/datasource" })
045 public class PersistenceConfiguration {
046
047 /**
048 * Location, in the classpath, of the Hibernate configuration file.
049 */
050 public static final String HIBERNATE_CONFIGURATION_FILE = "/hibernate.cfg.xml";
051
052 /**
053 * Property used to define the database url.
054 */
055 final public static String DATABASE_URL = "database.url";
056
057 /**
058 * Property used to define the database JDBC driver class name.
059 */
060 final public static String JDBC_DRIVER = "jdbc.driver";
061
062 /**
063 * Property used to define the database user name.
064 */
065 final public static String DATABASE_USERNAME = "database.username";
066
067 /**
068 * Property used to define the database user password.
069 */
070 final public static String DATABASE_PASSWORD = "database.password";
071
072 /**
073 * Single constructor of this class.
074 */
075 public PersistenceConfiguration() {
076 }
077
078 /**
079 * Creates a {@link DataSource}.
080 *
081 * @return a {@link DataSource}.
082 */
083 @Bean
084 public DataSource dataSource() {
085
086 ComboPooledDataSource dataSource = new ComboPooledDataSource();
087
088 dataSource.setJdbcUrl(getDatabaseURL());
089 dataSource.setUser(getDatabaseUsername());
090 dataSource.setPassword(getDatabasePassword());
091
092 try {
093 dataSource.setDriverClass(getJDBCDriver());
094 }
095 catch (PropertyVetoException e) {
096 throw new RuntimeException(e);
097 }
098
099 return dataSource;
100
101 }
102
103 /**
104 * Creates a {@link SessionFactory} that uses annotations and/or XML for mapping classes.
105 *
106 * @return a {@link SessionFactory}.
107 */
108 @Bean
109 public SessionFactory sessionFactory() {
110
111 LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();
112 factoryBean.setConfigurationClass(AnnotationConfiguration.class);
113 factoryBean.setConfigLocation(new ClassPathResource(HIBERNATE_CONFIGURATION_FILE));
114
115 try {
116 factoryBean.afterPropertiesSet();
117 }
118 catch (Exception e) {
119 throw new RuntimeException(e);
120 }
121
122 return (SessionFactory) factoryBean.getObject();
123
124 }
125
126 /**
127 * Creates the {@link PlatformTransactionManager} to be used by Spring.
128 *
129 * @return a {@link HibernateTransactionManager}.
130 */
131 @Bean
132 public PlatformTransactionManager transactionManager() {
133 return new HibernateTransactionManager(sessionFactory());
134 }
135
136 // @Bean(lazy = Lazy.FALSE)
137 // public OpenSessionInViewInterceptor openSessionInViewInterceptor() {
138 //
139 // final OpenSessionInViewInterceptor interceptor = new OpenSessionInViewInterceptor();
140 // interceptor.setSessionFactory(sessionFactory());
141 // interceptor.afterPropertiesSet();
142 //
143 // return interceptor;
144 //
145 // }
146
147 /**
148 * Returns the JDBC driver class name.
149 *
150 * @return a {@link String}.
151 */
152 @ExternalValue(DATABASE_URL)
153 public String getDatabaseURL() {
154 return "value not set";
155 }
156
157 /**
158 * Returns the JDBC driver class name.
159 *
160 * @return a {@link String}.
161 */
162 @ExternalValue(JDBC_DRIVER)
163 public String getJDBCDriver() {
164 return "value not set";
165 }
166
167 /**
168 * Returns the database user name.
169 *
170 * @return a {@link String}.
171 */
172 @ExternalValue(DATABASE_USERNAME)
173 public String getDatabaseUsername() {
174 return "value not set";
175 }
176
177 /**
178 * Returns the database user password.
179 *
180 * @return a {@link String}.
181 */
182 @ExternalValue(DATABASE_URL)
183 public String getDatabasePassword() {
184 return "value not set";
185 }
186
187 }