View Javadoc

1   // Copyright 2008 Thiago H. de Paula Figueiredo
2   //
3   // Licensed under the Apache License, Version 2.0 (the "License");
4   // you may not use this file except in compliance with the License.
5   // You may obtain a copy of the License at
6   //
7   //     http://www.apache.org/licenses/LICENSE-2.0
8   //
9   // Unless required by applicable law or agreed to in writing, software
10  // distributed under the License is distributed on an "AS IS" BASIS,
11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  // See the License for the specific language governing permissions and
13  // limitations under the License.
14  
15  package br.com.arsmachina.dao.hibernate.ioc;
16  
17  import java.beans.PropertyVetoException;
18  
19  import javax.sql.DataSource;
20  
21  import org.hibernate.SessionFactory;
22  import org.hibernate.cfg.AnnotationConfiguration;
23  import org.springframework.config.java.annotation.Bean;
24  import org.springframework.config.java.annotation.Configuration;
25  import org.springframework.config.java.annotation.ExternalValue;
26  import org.springframework.config.java.annotation.Lazy;
27  import org.springframework.config.java.annotation.ResourceBundles;
28  import org.springframework.core.io.ClassPathResource;
29  import org.springframework.orm.hibernate3.HibernateTransactionManager;
30  import org.springframework.orm.hibernate3.LocalSessionFactoryBean;
31  import org.springframework.transaction.PlatformTransactionManager;
32  
33  import com.mchange.v2.c3p0.ComboPooledDataSource;
34  
35  /**
36   * Class that configures the persistence layer for Spring. Database info is read from a
37   * <code>datasource.properties</code> file in the root of the classpath. Hibernate configuration
38   * properties is read from a <code>hibernate.cfg.xml</code> file, also in the root of the
39   * classpath.
40   * 
41   * @author Thiago H. de Paula Figueiredo
42   */
43  @Configuration(defaultLazy = Lazy.TRUE)
44  @ResourceBundles( { "classpath:/datasource" })
45  public class PersistenceConfiguration {
46  
47  	/**
48  	 * Location, in the classpath, of the Hibernate configuration file.
49  	 */
50  	public static final String HIBERNATE_CONFIGURATION_FILE = "/hibernate.cfg.xml";
51  
52  	/**
53  	 * Property used to define the database url.
54  	 */
55  	final public static String DATABASE_URL = "database.url";
56  
57  	/**
58  	 * Property used to define the database JDBC driver class name.
59  	 */
60  	final public static String JDBC_DRIVER = "jdbc.driver";
61  
62  	/**
63  	 * Property used to define the database user name.
64  	 */
65  	final public static String DATABASE_USERNAME = "database.username";
66  
67  	/**
68  	 * Property used to define the database user password.
69  	 */
70  	final public static String DATABASE_PASSWORD = "database.password";
71  
72  	/**
73  	 * Single constructor of this class.
74  	 */
75  	public PersistenceConfiguration() {
76  	}
77  
78  	/**
79  	 * Creates a {@link DataSource}.
80  	 * 
81  	 * @return a {@link DataSource}.
82  	 */
83  	@Bean
84  	public DataSource dataSource() {
85  
86  		ComboPooledDataSource dataSource = new ComboPooledDataSource();
87  
88  		dataSource.setJdbcUrl(getDatabaseURL());
89  		dataSource.setUser(getDatabaseUsername());
90  		dataSource.setPassword(getDatabasePassword());
91  
92  		try {
93  			dataSource.setDriverClass(getJDBCDriver());
94  		}
95  		catch (PropertyVetoException e) {
96  			throw new RuntimeException(e);
97  		}
98  
99  		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 }