code style improvements.
[utils] / system / spring / src / main / java / org / wamblee / system / spring / component / HibernateComponent.java
1 /*
2  * Copyright 2005-2010 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.util.HashMap;
19 import java.util.Map;
20 import java.util.Properties;
21
22 import javax.sql.DataSource;
23
24 import org.hibernate.SessionFactory;
25 import org.hibernate.dialect.DerbyDialect;
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.components.ORMappingConfig;
31 import org.wamblee.system.components.ORMappingConfig.DatabaseType;
32 import org.wamblee.system.core.DefaultProvidedInterface;
33 import org.wamblee.system.core.DefaultRequiredInterface;
34 import org.wamblee.system.core.ProvidedInterface;
35 import org.wamblee.system.core.RequiredInterface;
36 import org.wamblee.system.core.Scope;
37 import org.wamblee.system.spring.SpringComponent;
38
39 /**
40  * 
41  * @author $author$
42  * @version $Revision$
43  */
44 public class HibernateComponent extends SpringComponent {
45     private static final String HIBERNATE_DIALECT_PROP = "hibernate.dialect";
46
47     private static final String HIBERNATE_SCHEMAUPDATE_PROP = "hibernate.schemaupdate";
48
49     private static final String HIBERNATE_PROPS_KEY = "hibernateProperties";
50
51     private static final String HIBERNATE_SPRING_CONFIG = "spring/org.wamblee.system.spring.component.hibernate.xml";
52
53     private final RequiredInterface config = new DefaultRequiredInterface(
54         "config", ORMappingConfig.class);
55
56     /**
57      * Creates a new HibernateComponent object.
58      * 
59      * 
60      */
61     public HibernateComponent(String aName) {
62         super(aName, new String[] { HIBERNATE_SPRING_CONFIG },
63             createProvided(), createRequired());
64
65         Properties props = new Properties();
66         addProperties(HIBERNATE_PROPS_KEY, props);
67
68         addRequiredInterface(config);
69     }
70
71     @Override
72     protected Scope doStart(Scope aExternalScope) {
73         ORMappingConfig orMappingConfig = aExternalScope.getInterfaceImplementation(
74             config.getProvider(), ORMappingConfig.class);
75         setProperty(HIBERNATE_SCHEMAUPDATE_PROP, "" + orMappingConfig.isSchemaUpdate());
76
77         DatabaseType db = orMappingConfig.getType();
78         String dialect = db.handleCases(new DatabaseType.Switch<String>() {
79             @Override
80             public String handleMySqlInnoDb() {
81                 return MySQLInnoDBDialect.class.getName();
82             }
83
84             @Override
85             public String handleDerby() {
86                 return DerbyDialect.class.getName();
87             }
88         });
89
90         getHibernateProperties().put(HIBERNATE_DIALECT_PROP, dialect);
91
92         return super.doStart(aExternalScope);
93     }
94
95     private Properties getHibernateProperties() {
96         return getProperties(HIBERNATE_PROPS_KEY);
97     }
98
99     private static Map<RequiredInterface, String> createRequired() {
100         Map<RequiredInterface, String> required = new HashMap<RequiredInterface, String>();
101         required.put(new DefaultRequiredInterface("datasource",
102             DataSource.class), "dataSource");
103         required.put(new DefaultRequiredInterface("mappingFiles",
104             HibernateMappingFiles.class), "hibernateMappingFiles");
105
106         return required;
107     }
108
109     private static Map<String, ProvidedInterface> createProvided() {
110         Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
111
112         provided.put("transactionManager", new DefaultProvidedInterface(
113             "transactionMgr", PlatformTransactionManager.class));
114         provided.put("sessionFactory", new DefaultProvidedInterface(
115             "sessionFactory", SessionFactory.class));
116         provided.put("org.springframework.orm.hibernate3.HibernateTemplate",
117             new DefaultProvidedInterface("hibernateTemplate",
118                 HibernateTemplate.class));
119
120         return provided;
121     }
122 }