2 * Copyright 2008 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.wamblee.system.spring.component;
18 import java.io.IOException;
19 import java.util.HashMap;
21 import java.util.Properties;
23 import javax.sql.DataSource;
25 import org.hibernate.SessionFactory;
26 import org.hibernate.dialect.MySQLInnoDBDialect;
27 import org.springframework.orm.hibernate3.HibernateTemplate;
28 import org.springframework.transaction.PlatformTransactionManager;
29 import org.wamblee.persistence.hibernate.HibernateMappingFiles;
30 import org.wamblee.system.core.DefaultProvidedInterface;
31 import org.wamblee.system.core.DefaultRequiredInterface;
32 import org.wamblee.system.core.ProvidedInterface;
33 import org.wamblee.system.core.RequiredInterface;
34 import org.wamblee.system.spring.SpringComponent;
36 public class HibernateComponent extends SpringComponent {
38 private static final String HIBERNATE_PROPS_KEY = "hibernateProperties";
39 private static final String HIBERNATE_SPRING_CONFIG = "spring/org.wamblee.system.spring.component.hibernate.xml";
41 public HibernateComponent(String aName) throws IOException {
42 super(aName, new String[] { HIBERNATE_SPRING_CONFIG},
43 createProvided(), createRequired());
45 Properties props = new Properties();
46 addProperties(HIBERNATE_PROPS_KEY, props);
48 props.put("hibernate.dialect", MySQLInnoDBDialect.class.getName());
49 setProperty("hibernate.schemaupdate", "true");
52 private Properties getHibernateProperties() {
53 return getProperties(HIBERNATE_PROPS_KEY);
56 private static Map<RequiredInterface, String> createRequired() {
57 Map<RequiredInterface,String> required = new HashMap<RequiredInterface, String>();
58 required.put(new DefaultRequiredInterface("datasource", DataSource.class), "dataSource");
59 required.put(new DefaultRequiredInterface("mappingFiles", HibernateMappingFiles.class),
60 "hibernateMappingFiles");
64 private static Map<String, ProvidedInterface> createProvided() {
65 Map<String,ProvidedInterface> provided = new HashMap<String,ProvidedInterface>();
67 provided.put("transactionManager", new DefaultProvidedInterface(
68 "transactionMgr", PlatformTransactionManager.class));
69 provided.put("sessionFactory", new DefaultProvidedInterface(
70 "sessionFactory", SessionFactory.class));
71 provided.put("org.springframework.orm.hibernate3.HibernateTemplate", new DefaultProvidedInterface(
72 "hibernateTemplate", HibernateTemplate.class));