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