code style improvements.
[utils] / system / spring / src / main / java / org / wamblee / system / spring / component / DatasourceComponent.java
1 /*
2  * Copyright 2005-2010 the original author or authors.
3  * 
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
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
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.
15  */ 
16 package org.wamblee.system.spring.component;
17
18 import java.util.Properties;
19
20 import javax.sql.DataSource;
21
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;
31
32 /**
33  * 
34  * @author $author$
35  * @version $Revision$
36  */
37 public class DatasourceComponent extends AbstractComponent<DataSource> {
38     private static RequiredInterface PROPS = new DefaultRequiredInterface(
39         "dbprops", Properties.class);
40
41     private static ProvidedInterface DATASOURCE = new DefaultProvidedInterface(
42         "datasource", DataSource.class);
43
44     private static ProvidedInterface ORM_CONFIG = new DefaultProvidedInterface(
45         "ormconfig", ORMappingConfig.class);
46
47     /**
48      * Creates a new DatasourceComponent object.
49      * 
50      * 
51      */
52     public DatasourceComponent(String aName) {
53         super(aName);
54
55         addRequiredInterface(PROPS);
56         addProvidedInterface(DATASOURCE);
57         addProvidedInterface(ORM_CONFIG);
58     }
59
60     @Override
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);
69
70         DatabaseType type = DatabaseType.valueOf(dbProps
71             .getProperty("database.type"));
72
73         ORMappingConfig config = new ORMappingConfig(true, type);
74
75         addInterface(ORM_CONFIG, config, aScope);
76
77         return ds;
78     }
79
80     @Override
81     protected void doStop(DataSource aRuntime) {
82         // Empty.
83     }
84 }