2 * Copyright 2005-2010 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.util.HashMap;
20 import java.util.Properties;
22 import javax.sql.DataSource;
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;
44 public class HibernateComponent extends SpringComponent {
45 private static final String HIBERNATE_DIALECT_PROP = "hibernate.dialect";
47 private static final String HIBERNATE_SCHEMAUPDATE_PROP = "hibernate.schemaupdate";
49 private static final String HIBERNATE_PROPS_KEY = "hibernateProperties";
51 private static final String HIBERNATE_SPRING_CONFIG = "spring/org.wamblee.system.spring.component.hibernate.xml";
53 private final RequiredInterface config = new DefaultRequiredInterface(
54 "config", ORMappingConfig.class);
57 * Creates a new HibernateComponent object.
61 public HibernateComponent(String aName) {
62 super(aName, new String[] { HIBERNATE_SPRING_CONFIG },
63 createProvided(), createRequired());
65 Properties props = new Properties();
66 addProperties(HIBERNATE_PROPS_KEY, props);
68 addRequiredInterface(config);
72 protected Scope doStart(Scope aExternalScope) {
73 ORMappingConfig orMappingConfig = aExternalScope.getInterfaceImplementation(
74 config.getProvider(), ORMappingConfig.class);
75 setProperty(HIBERNATE_SCHEMAUPDATE_PROP, "" + orMappingConfig.isSchemaUpdate());
77 DatabaseType db = orMappingConfig.getType();
78 String dialect = db.handleCases(new DatabaseType.Switch<String>() {
80 public String handleMySqlInnoDb() {
81 return MySQLInnoDBDialect.class.getName();
85 public String handleDerby() {
86 return DerbyDialect.class.getName();
90 getHibernateProperties().put(HIBERNATE_DIALECT_PROP, dialect);
92 return super.doStart(aExternalScope);
95 private Properties getHibernateProperties() {
96 return getProperties(HIBERNATE_PROPS_KEY);
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");
109 private static Map<String, ProvidedInterface> createProvided() {
110 Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
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));