364b250f1b82b9b30fce55cadd3320fe2e320842
[utils] / system / spring / src / main / java / org / wamblee / system / spring / component / HibernateComponent.java
1 /*
2  * Copyright 2008 the original author or authors.
3  * 
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
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
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.
15  */ 
16 package org.wamblee.system.spring.component;
17
18 import java.io.IOException;
19 import java.util.HashMap;
20 import java.util.Map;
21 import java.util.Properties;
22
23 import javax.sql.DataSource;
24
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;
35
36 public class HibernateComponent extends SpringComponent {
37
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";
40
41     public HibernateComponent(String aName) throws IOException { 
42         super(aName, new String[] { HIBERNATE_SPRING_CONFIG}, 
43                 createProvided(), createRequired()); 
44         
45         Properties props = new Properties(); 
46         addProperties(HIBERNATE_PROPS_KEY, props);
47    
48         props.put("hibernate.dialect", MySQLInnoDBDialect.class.getName());
49         setProperty("hibernate.schemaupdate", "true");
50     }
51     
52     private Properties getHibernateProperties() { 
53         return getProperties(HIBERNATE_PROPS_KEY);
54     }
55
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");
61         return required;
62     }
63
64     private static Map<String, ProvidedInterface> createProvided() {
65         Map<String,ProvidedInterface> provided = new HashMap<String,ProvidedInterface>(); 
66         
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));
73         return provided;
74     }
75 }