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.springframework.core.io.ClassPathResource;
20 import org.springframework.jdbc.datasource.DriverManagerDataSource;
22 import org.wamblee.system.components.ORMappingConfig;
23 import org.wamblee.system.components.ORMappingConfig.DatabaseType;
24 import org.wamblee.system.core.AbstractComponent;
25 import org.wamblee.system.core.DefaultProvidedInterface;
26 import org.wamblee.system.core.DefaultRequiredInterface;
27 import org.wamblee.system.core.ProvidedInterface;
28 import org.wamblee.system.core.RequiredInterface;
29 import org.wamblee.system.core.Scope;
30 import org.wamblee.system.spring.SpringComponent;
32 import java.io.IOException;
35 import java.util.Properties;
36 import java.util.TreeMap;
38 import javax.sql.DataSource;
45 public class DatasourceComponent extends AbstractComponent<DataSource> {
46 private static RequiredInterface PROPS = new DefaultRequiredInterface(
47 "dbprops", Properties.class);
49 private static ProvidedInterface DATASOURCE = new DefaultProvidedInterface(
50 "datasource", DataSource.class);
52 private static ProvidedInterface ORM_CONFIG = new DefaultProvidedInterface(
53 "ormconfig", ORMappingConfig.class);
56 * Creates a new DatasourceComponent object.
60 public DatasourceComponent(String aName) throws IOException {
63 addRequiredInterface(PROPS);
64 addProvidedInterface(DATASOURCE);
65 addProvidedInterface(ORM_CONFIG);
69 protected DataSource doStart(Scope aScope) {
70 Properties dbProps = aScope.getInterfaceImplementation(PROPS
71 .getProvider(), Properties.class);
72 DriverManagerDataSource ds = new DriverManagerDataSource(dbProps
73 .getProperty("database.url"), dbProps
74 .getProperty("database.username"), dbProps
75 .getProperty("database.password"));
76 addInterface(DATASOURCE, ds, aScope);
78 DatabaseType type = DatabaseType.valueOf(dbProps
79 .getProperty("database.type"));
81 ORMappingConfig config = new ORMappingConfig(true, type);
83 addInterface(ORM_CONFIG, config, aScope);
89 protected void doStop(DataSource aRuntime) {