a8d53d4644f6b2c8957acafba6478b62c94ade1e
[utils] / system / spring / src / main / java / org / wamblee / system / spring / component / DatasourceComponent.java
1 /*
2  * Copyright 2008 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.io.IOException;
19 import java.util.Map;
20 import java.util.Properties;
21 import java.util.TreeMap;
22
23 import javax.sql.DataSource;
24
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;
36
37 public class DatasourceComponent extends AbstractComponent<DataSource> {
38
39     private static RequiredInterface PROPS = new DefaultRequiredInterface("dbprops", Properties.class);
40     private static ProvidedInterface DATASOURCE =  new DefaultProvidedInterface("datasource",
41             DataSource.class);
42     private static ProvidedInterface ORM_CONFIG = new DefaultProvidedInterface("ormconfig",
43             ORMappingConfig.class);
44    
45     public DatasourceComponent(String aName) throws IOException {
46         super(aName); 
47         
48         addRequiredInterface(PROPS);
49         addProvidedInterface(DATASOURCE);
50         addProvidedInterface(ORM_CONFIG);
51     }
52
53
54     @Override
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"));
64        
65        ORMappingConfig config = new ORMappingConfig(true, type);
66        
67        addInterface(ORM_CONFIG, config, aScope);
68        
69        return ds; 
70     }
71
72     @Override
73     protected void doStop(DataSource aRuntime) {
74        // Empty.
75     }
76 }