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.usermgt.hibernate;
18 import java.io.IOException;
20 import java.util.Properties;
21 import java.util.TreeMap;
23 import javax.sql.DataSource;
25 import org.springframework.core.io.ClassPathResource;
26 import org.springframework.jdbc.datasource.DriverManagerDataSource;
27 import org.wamblee.system.core.AbstractComponent;
28 import org.wamblee.system.core.DefaultProvidedInterface;
29 import org.wamblee.system.core.DefaultRequiredInterface;
30 import org.wamblee.system.core.ProvidedInterface;
31 import org.wamblee.system.core.RequiredInterface;
32 import org.wamblee.system.core.Scope;
33 import org.wamblee.system.spring.SpringComponent;
34 import org.wamblee.system.spring.component.ORMappingConfig;
35 import org.wamblee.system.spring.component.ORMappingConfig.DatabaseType;
37 public class DatasourceComponent extends AbstractComponent<DataSource> {
39 private static RequiredInterface PROPS = new DefaultRequiredInterface("dbprops", Properties.class);
40 private static ProvidedInterface DATASOURCE = new DefaultProvidedInterface("datasource",
42 private static ProvidedInterface ORM_CONFIG = new DefaultProvidedInterface("ormconfig",
43 ORMappingConfig.class);
45 public DatasourceComponent(String aName) throws IOException {
48 addRequiredInterface(PROPS);
49 addProvidedInterface(DATASOURCE);
50 addProvidedInterface(ORM_CONFIG);
55 protected DataSource doStart(Scope aScope) {
56 Properties dbProps = aScope.getInterfaceImplementation(
57 PROPS.getProvider(), Properties.class);
58 DriverManagerDataSource ds = new DriverManagerDataSource(
59 dbProps.getProperty("database.driver"),
60 dbProps.getProperty("database.url"),
61 dbProps.getProperty("database.username"),
62 dbProps.getProperty("database.password"));
63 addInterface(DATASOURCE, ds, aScope);
64 DatabaseType type = DatabaseType.valueOf(dbProps.getProperty("database.type"));
66 ORMappingConfig config = new ORMappingConfig(true, type);
68 addInterface(ORM_CONFIG, config, aScope);
74 protected void doStop(DataSource aRuntime) {