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 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;
53 public class HibernateComponent extends SpringComponent {
57 private static final String HIBERNATE_DIALECT_PROP = "hibernate.dialect";
62 private static final String HIBERNATE_SCHEMAUPDATE_PROP = "hibernate.schemaupdate";
67 private static final String HIBERNATE_PROPS_KEY = "hibernateProperties";
72 private static final String HIBERNATE_SPRING_CONFIG = "spring/org.wamblee.system.spring.component.hibernate.xml";
77 private final RequiredInterface CONFIG = new DefaultRequiredInterface("config",
78 ORMappingConfig.class);
81 * Creates a new HibernateComponent object.
83 * @param aName DOCUMENT ME!
85 * @throws IOException DOCUMENT ME!
87 public HibernateComponent(String aName) throws IOException {
88 super(aName, new String[] { HIBERNATE_SPRING_CONFIG },
89 createProvided(), createRequired());
91 Properties props = new Properties();
92 addProperties(HIBERNATE_PROPS_KEY, props);
94 addRequiredInterface(CONFIG);
100 * @param aExternalScope DOCUMENT ME!
102 * @return DOCUMENT ME!
105 protected Scope doStart(Scope aExternalScope) {
106 ORMappingConfig config = aExternalScope.getInterfaceImplementation(CONFIG
107 .getProvider(), ORMappingConfig.class);
108 setProperty(HIBERNATE_SCHEMAUPDATE_PROP, "" + config.isSchemaUpdate());
110 DatabaseType db = config.getType();
111 String dialect = db.handleCases(new DatabaseType.Switch<String>() {
113 public String handleMySqlInnoDb() {
114 return MySQLInnoDBDialect.class.getName();
118 public String handleDerby() {
119 return DerbyDialect.class.getName();
123 getHibernateProperties().put(HIBERNATE_DIALECT_PROP, dialect);
125 return super.doStart(aExternalScope);
131 * @return DOCUMENT ME!
133 private Properties getHibernateProperties() {
134 return getProperties(HIBERNATE_PROPS_KEY);
140 * @return DOCUMENT ME!
142 private static Map<RequiredInterface, String> createRequired() {
143 Map<RequiredInterface, String> required = new HashMap<RequiredInterface, String>();
144 required.put(new DefaultRequiredInterface("datasource", DataSource.class),
146 required.put(new DefaultRequiredInterface("mappingFiles",
147 HibernateMappingFiles.class), "hibernateMappingFiles");
155 * @return DOCUMENT ME!
157 private static Map<String, ProvidedInterface> createProvided() {
158 Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
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));