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 java.util.Properties;
20 import javax.sql.DataSource;
22 import org.springframework.jdbc.datasource.DriverManagerDataSource;
23 import org.wamblee.system.components.ORMappingConfig;
24 import org.wamblee.system.components.ORMappingConfig.DatabaseType;
25 import org.wamblee.system.core.AbstractComponent;
26 import org.wamblee.system.core.DefaultProvidedInterface;
27 import org.wamblee.system.core.DefaultRequiredInterface;
28 import org.wamblee.system.core.ProvidedInterface;
29 import org.wamblee.system.core.RequiredInterface;
30 import org.wamblee.system.core.Scope;
37 public class DatasourceComponent extends AbstractComponent<DataSource> {
38 private static RequiredInterface PROPS = new DefaultRequiredInterface(
39 "dbprops", Properties.class);
41 private static ProvidedInterface DATASOURCE = new DefaultProvidedInterface(
42 "datasource", DataSource.class);
44 private static ProvidedInterface ORM_CONFIG = new DefaultProvidedInterface(
45 "ormconfig", ORMappingConfig.class);
48 * Creates a new DatasourceComponent object.
52 public DatasourceComponent(String aName) {
55 addRequiredInterface(PROPS);
56 addProvidedInterface(DATASOURCE);
57 addProvidedInterface(ORM_CONFIG);
61 protected DataSource doStart(Scope aScope) {
62 Properties dbProps = aScope.getInterfaceImplementation(PROPS
63 .getProvider(), Properties.class);
64 DriverManagerDataSource ds = new DriverManagerDataSource(dbProps
65 .getProperty("database.url"), dbProps
66 .getProperty("database.username"), dbProps
67 .getProperty("database.password"));
68 addInterface(DATASOURCE, ds, aScope);
70 DatabaseType type = DatabaseType.valueOf(dbProps
71 .getProperty("database.type"));
73 ORMappingConfig config = new ORMappingConfig(true, type);
75 addInterface(ORM_CONFIG, config, aScope);
81 protected void doStop(DataSource aRuntime) {