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.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;
40 public class HibernateComponent extends SpringComponent {
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";
47 private final RequiredInterface CONFIG = new DefaultRequiredInterface("config", ORMappingConfig.class);
49 public HibernateComponent(String aName) throws IOException {
50 super(aName, new String[] { HIBERNATE_SPRING_CONFIG},
51 createProvided(), createRequired());
53 Properties props = new Properties();
54 addProperties(HIBERNATE_PROPS_KEY, props);
56 addRequiredInterface(CONFIG);
60 protected Scope doStart(Scope aExternalScope) {
62 ORMappingConfig config = aExternalScope.getInterfaceImplementation(CONFIG.getProvider(), ORMappingConfig.class);
63 setProperty(HIBERNATE_SCHEMAUPDATE_PROP, "" + config.isSchemaUpdate());
65 DatabaseType db = config.getType();
66 String dialect = db.handleCases(new DatabaseType.Switch<String>() {
68 public String handleMySqlInnoDb() {
69 return MySQLInnoDBDialect.class.getName();
72 public String handleDerby() {
73 return DerbyDialect.class.getName();
76 getHibernateProperties().put(HIBERNATE_DIALECT_PROP, dialect);
79 return super.doStart(aExternalScope);
82 private Properties getHibernateProperties() {
83 return getProperties(HIBERNATE_PROPS_KEY);
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");
94 private static Map<String, ProvidedInterface> createProvided() {
95 Map<String,ProvidedInterface> provided = new HashMap<String,ProvidedInterface>();
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));