source code formatting.
[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  * DOCUMENT ME!
49  *
50  * @author $author$
51  * @version $Revision$
52  */
53 public class HibernateComponent extends SpringComponent {
54     /**
55      * DOCUMENT ME!
56      */
57     private static final String HIBERNATE_DIALECT_PROP = "hibernate.dialect";
58
59     /**
60      * DOCUMENT ME!
61      */
62     private static final String HIBERNATE_SCHEMAUPDATE_PROP = "hibernate.schemaupdate";
63
64     /**
65      * DOCUMENT ME!
66      */
67     private static final String HIBERNATE_PROPS_KEY = "hibernateProperties";
68
69     /**
70      * DOCUMENT ME!
71      */
72     private static final String HIBERNATE_SPRING_CONFIG = "spring/org.wamblee.system.spring.component.hibernate.xml";
73
74     /**
75      * DOCUMENT ME!
76      */
77     private final RequiredInterface CONFIG = new DefaultRequiredInterface("config",
78             ORMappingConfig.class);
79
80 /**
81      * Creates a new HibernateComponent object.
82      *
83      * @param aName DOCUMENT ME!
84      *
85      * @throws IOException DOCUMENT ME!
86      */
87     public HibernateComponent(String aName) throws IOException {
88         super(aName, new String[] { HIBERNATE_SPRING_CONFIG },
89             createProvided(), createRequired());
90
91         Properties props = new Properties();
92         addProperties(HIBERNATE_PROPS_KEY, props);
93
94         addRequiredInterface(CONFIG);
95     }
96
97     /**
98      * DOCUMENT ME!
99      *
100      * @param aExternalScope DOCUMENT ME!
101      *
102      * @return DOCUMENT ME!
103      */
104     @Override
105     protected Scope doStart(Scope aExternalScope) {
106         ORMappingConfig config = aExternalScope.getInterfaceImplementation(CONFIG
107                 .getProvider(), ORMappingConfig.class);
108         setProperty(HIBERNATE_SCHEMAUPDATE_PROP, "" + config.isSchemaUpdate());
109
110         DatabaseType db      = config.getType();
111         String       dialect = db.handleCases(new DatabaseType.Switch<String>() {
112                     @Override
113                     public String handleMySqlInnoDb() {
114                         return MySQLInnoDBDialect.class.getName();
115                     }
116
117                     @Override
118                     public String handleDerby() {
119                         return DerbyDialect.class.getName();
120                     }
121                 });
122
123         getHibernateProperties().put(HIBERNATE_DIALECT_PROP, dialect);
124
125         return super.doStart(aExternalScope);
126     }
127
128     /**
129      * DOCUMENT ME!
130      *
131      * @return DOCUMENT ME!
132      */
133     private Properties getHibernateProperties() {
134         return getProperties(HIBERNATE_PROPS_KEY);
135     }
136
137     /**
138      * DOCUMENT ME!
139      *
140      * @return DOCUMENT ME!
141      */
142     private static Map<RequiredInterface, String> createRequired() {
143         Map<RequiredInterface, String> required = new HashMap<RequiredInterface, String>();
144         required.put(new DefaultRequiredInterface("datasource", DataSource.class),
145             "dataSource");
146         required.put(new DefaultRequiredInterface("mappingFiles",
147                 HibernateMappingFiles.class), "hibernateMappingFiles");
148
149         return required;
150     }
151
152     /**
153      * DOCUMENT ME!
154      *
155      * @return DOCUMENT ME!
156      */
157     private static Map<String, ProvidedInterface> createProvided() {
158         Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
159
160         provided.put("transactionManager",
161             new DefaultProvidedInterface("transactionMgr",
162                 PlatformTransactionManager.class));
163         provided.put("sessionFactory",
164             new DefaultProvidedInterface("sessionFactory", SessionFactory.class));
165         provided.put("org.springframework.orm.hibernate3.HibernateTemplate",
166             new DefaultProvidedInterface("hibernateTemplate",
167                 HibernateTemplate.class));
168
169         return provided;
170     }
171 }