4b256254ab0e00317d6d4927899321463b2dc8ab
[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.DerbyDialect;
27 import org.hibernate.dialect.MySQLInnoDBDialect;
28 import org.springframework.orm.hibernate3.HibernateTemplate;
29 import org.springframework.transaction.PlatformTransactionManager;
30 import org.wamblee.persistence.hibernate.HibernateMappingFiles;
31 import org.wamblee.system.components.ORMappingConfig;
32 import org.wamblee.system.components.ORMappingConfig.DatabaseType;
33 import org.wamblee.system.core.DefaultProvidedInterface;
34 import org.wamblee.system.core.DefaultRequiredInterface;
35 import org.wamblee.system.core.ProvidedInterface;
36 import org.wamblee.system.core.RequiredInterface;
37 import org.wamblee.system.core.Scope;
38 import org.wamblee.system.spring.SpringComponent;
39
40 public class HibernateComponent extends SpringComponent {
41
42     private static final String HIBERNATE_DIALECT_PROP = "hibernate.dialect";
43     private static final String HIBERNATE_SCHEMAUPDATE_PROP = "hibernate.schemaupdate";
44     private static final String HIBERNATE_PROPS_KEY = "hibernateProperties";
45     private static final String HIBERNATE_SPRING_CONFIG = "spring/org.wamblee.system.spring.component.hibernate.xml";
46     
47     private final RequiredInterface CONFIG = new DefaultRequiredInterface("config", ORMappingConfig.class);
48
49     public HibernateComponent(String aName) throws IOException { 
50         super(aName, new String[] { HIBERNATE_SPRING_CONFIG}, 
51                 createProvided(), createRequired()); 
52         
53         Properties props = new Properties(); 
54         addProperties(HIBERNATE_PROPS_KEY, props);
55    
56         addRequiredInterface(CONFIG);
57     }
58     
59     @Override
60     protected Scope doStart(Scope aExternalScope) {
61         
62         ORMappingConfig config = aExternalScope.getInterfaceImplementation(CONFIG.getProvider(), ORMappingConfig.class);
63         setProperty(HIBERNATE_SCHEMAUPDATE_PROP, "" + config.isSchemaUpdate());
64         
65         DatabaseType db = config.getType(); 
66         String dialect = db.handleCases(new DatabaseType.Switch<String>() { 
67             @Override
68             public String handleMySqlInnoDb() {
69                 return MySQLInnoDBDialect.class.getName(); 
70             }
71             @Override
72             public String handleDerby() {
73                 return DerbyDialect.class.getName(); 
74             }
75         });
76         getHibernateProperties().put(HIBERNATE_DIALECT_PROP, dialect);
77         
78         
79         return super.doStart(aExternalScope);
80     }
81     
82     private Properties getHibernateProperties() { 
83         return getProperties(HIBERNATE_PROPS_KEY);
84     }
85
86     private static Map<RequiredInterface, String> createRequired() {
87         Map<RequiredInterface,String> required = new HashMap<RequiredInterface, String>();
88         required.put(new DefaultRequiredInterface("datasource", DataSource.class), "dataSource");
89         required.put(new DefaultRequiredInterface("mappingFiles", HibernateMappingFiles.class), 
90                 "hibernateMappingFiles");
91         return required;
92     }
93
94     private static Map<String, ProvidedInterface> createProvided() {
95         Map<String,ProvidedInterface> provided = new HashMap<String,ProvidedInterface>(); 
96         
97         provided.put("transactionManager", new DefaultProvidedInterface(
98                 "transactionMgr", PlatformTransactionManager.class));
99         provided.put("sessionFactory", new DefaultProvidedInterface(
100                 "sessionFactory", SessionFactory.class));
101         provided.put("org.springframework.orm.hibernate3.HibernateTemplate", new DefaultProvidedInterface(
102                 "hibernateTemplate", HibernateTemplate.class));
103         return provided;
104     }
105 }