2 * Copyright 2008 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.system.spring.component;
18 import java.io.IOException;
19 import java.util.HashMap;
21 import java.util.Properties;
23 import javax.sql.DataSource;
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.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 import org.wamblee.system.spring.component.ORMappingConfig.DatabaseType;
39 public class HibernateComponent extends SpringComponent {
41 private static final String HIBERNATE_DIALECT_PROP = "hibernate.dialect";
42 private static final String HIBERNATE_SCHEMAUPDATE_PROP = "hibernate.schemaupdate";
43 private static final String HIBERNATE_PROPS_KEY = "hibernateProperties";
44 private static final String HIBERNATE_SPRING_CONFIG = "spring/org.wamblee.system.spring.component.hibernate.xml";
46 private final RequiredInterface CONFIG = new DefaultRequiredInterface("config", ORMappingConfig.class);
48 public HibernateComponent(String aName) throws IOException {
49 super(aName, new String[] { HIBERNATE_SPRING_CONFIG},
50 createProvided(), createRequired());
52 Properties props = new Properties();
53 addProperties(HIBERNATE_PROPS_KEY, props);
55 addRequiredInterface(CONFIG);
59 protected Scope doStart(Scope aExternalScope) {
61 ORMappingConfig config = aExternalScope.getInterfaceImplementation(CONFIG.getProvider(), ORMappingConfig.class);
62 setProperty(HIBERNATE_SCHEMAUPDATE_PROP, "" + config.isSchemaUpdate());
64 DatabaseType db = config.getType();
65 String dialect = db.handleCases(new DatabaseType.Switch<String>() {
67 public String handleMySqlInnoDb() {
68 return MySQLInnoDBDialect.class.getName();
71 public String handleDerby() {
72 return DerbyDialect.class.getName();
75 getHibernateProperties().put(HIBERNATE_DIALECT_PROP, MySQLInnoDBDialect.class.getName());
78 return super.doStart(aExternalScope);
81 private Properties getHibernateProperties() {
82 return getProperties(HIBERNATE_PROPS_KEY);
85 private static Map<RequiredInterface, String> createRequired() {
86 Map<RequiredInterface,String> required = new HashMap<RequiredInterface, String>();
87 required.put(new DefaultRequiredInterface("datasource", DataSource.class), "dataSource");
88 required.put(new DefaultRequiredInterface("mappingFiles", HibernateMappingFiles.class),
89 "hibernateMappingFiles");
93 private static Map<String, ProvidedInterface> createProvided() {
94 Map<String,ProvidedInterface> provided = new HashMap<String,ProvidedInterface>();
96 provided.put("transactionManager", new DefaultProvidedInterface(
97 "transactionMgr", PlatformTransactionManager.class));
98 provided.put("sessionFactory", new DefaultProvidedInterface(
99 "sessionFactory", SessionFactory.class));
100 provided.put("org.springframework.orm.hibernate3.HibernateTemplate", new DefaultProvidedInterface(
101 "hibernateTemplate", HibernateTemplate.class));