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 org.hibernate.SessionFactory;
20 import org.hibernate.dialect.DerbyDialect;
21 import org.hibernate.dialect.MySQLInnoDBDialect;
23 import org.springframework.orm.hibernate3.HibernateTemplate;
25 import org.springframework.transaction.PlatformTransactionManager;
27 import org.wamblee.persistence.hibernate.HibernateMappingFiles;
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;
38 import java.io.IOException;
40 import java.util.HashMap;
42 import java.util.Properties;
44 import javax.sql.DataSource;
51 public class HibernateComponent extends SpringComponent {
52 private static final String HIBERNATE_DIALECT_PROP = "hibernate.dialect";
54 private static final String HIBERNATE_SCHEMAUPDATE_PROP = "hibernate.schemaupdate";
56 private static final String HIBERNATE_PROPS_KEY = "hibernateProperties";
58 private static final String HIBERNATE_SPRING_CONFIG = "spring/org.wamblee.system.spring.component.hibernate.xml";
60 private final RequiredInterface CONFIG = new DefaultRequiredInterface(
61 "config", ORMappingConfig.class);
64 * Creates a new HibernateComponent object.
68 public HibernateComponent(String aName) throws IOException {
69 super(aName, new String[] { HIBERNATE_SPRING_CONFIG },
70 createProvided(), createRequired());
72 Properties props = new Properties();
73 addProperties(HIBERNATE_PROPS_KEY, props);
75 addRequiredInterface(CONFIG);
79 protected Scope doStart(Scope aExternalScope) {
80 ORMappingConfig config = aExternalScope.getInterfaceImplementation(
81 CONFIG.getProvider(), ORMappingConfig.class);
82 setProperty(HIBERNATE_SCHEMAUPDATE_PROP, "" + config.isSchemaUpdate());
84 DatabaseType db = config.getType();
85 String dialect = db.handleCases(new DatabaseType.Switch<String>() {
87 public String handleMySqlInnoDb() {
88 return MySQLInnoDBDialect.class.getName();
92 public String handleDerby() {
93 return DerbyDialect.class.getName();
97 getHibernateProperties().put(HIBERNATE_DIALECT_PROP, dialect);
99 return super.doStart(aExternalScope);
102 private Properties getHibernateProperties() {
103 return getProperties(HIBERNATE_PROPS_KEY);
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");
116 private static Map<String, ProvidedInterface> createProvided() {
117 Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
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));