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;
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.components.ORMappingConfig;
28 import org.wamblee.system.components.ORMappingConfig.DatabaseType;
29 import org.wamblee.system.core.AbstractComponent;
30 import org.wamblee.system.core.DefaultProvidedInterface;
31 import org.wamblee.system.core.DefaultRequiredInterface;
32 import org.wamblee.system.core.ProvidedInterface;
33 import org.wamblee.system.core.RequiredInterface;
34 import org.wamblee.system.core.Scope;
35 import org.wamblee.system.spring.SpringComponent;
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.url"),
60 dbProps.getProperty("database.username"),
61 dbProps.getProperty("database.password"));
62 addInterface(DATASOURCE, ds, aScope);
63 DatabaseType type = DatabaseType.valueOf(dbProps.getProperty("database.type"));
65 ORMappingConfig config = new ORMappingConfig(true, type);
67 addInterface(ORM_CONFIG, config, aScope);
73 protected void doStop(DataSource aRuntime) {