/*
* Copyright (c) 2012, Stephen Chin
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL STEPHEN CHIN OR ORACLE CORPORATION BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package steveonjava.server;
import org.hibernate.SessionFactory;
import org.hibernate.dialect.H2Dialect;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCache;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import javax.sql.DataSource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
@Configuration
@EnableCaching
@EnableWebMvc
@EnableTransactionManagement
public class ServicesConfiguration {
@Bean
public CustomerService customerService() throws Exception {
return new CustomerService(this.sessionFactory());
}
@Bean
@SuppressWarnings("deprecation")
public SessionFactory sessionFactory() throws Exception {
Properties props = new Properties();
Map<String, String> p = new HashMap<String, String>();
p.put(org.hibernate.cfg.Environment.HBM2DDL_AUTO, "create");
p.put(org.hibernate.cfg.Environment.HBM2DDL_IMPORT_FILES, "import_h2.sql");
p.put(org.hibernate.cfg.Environment.DIALECT, H2Dialect.class.getName());
p.put(org.hibernate.cfg.Environment.SHOW_SQL, "true");
Map<String, String> propsMap = p;
for (String k : propsMap.keySet())
props.setProperty(k, propsMap.get(k));
return new LocalSessionFactoryBuilder(dataSource())
.addAnnotatedClasses(Customer.class)
.addProperties(props)
.buildSessionFactory();
}
@Bean
public PlatformTransactionManager transactionManager() throws Exception {
return new HibernateTransactionManager(sessionFactory());
}
@Bean
public DataSource dataSource() throws Exception {
return new EmbeddedDatabaseBuilder()
.setName("crm")
.setType(EmbeddedDatabaseType.H2)
.build();
}
@Bean
public CacheManager cacheManager() throws Exception {
SimpleCacheManager scm = new SimpleCacheManager();
Cache cache = new ConcurrentMapCache("customers");
scm.setCaches(Arrays.asList(cache));
return scm;
}
}