Property files are now being read by the property component
[utils] / security / src / test / java / org / wamblee / usermgt / hibernate / 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.usermgt.hibernate;
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.core.AbstractComponent;
28 import org.wamblee.system.core.DefaultProvidedInterface;
29 import org.wamblee.system.core.DefaultRequiredInterface;
30 import org.wamblee.system.core.ProvidedInterface;
31 import org.wamblee.system.core.RequiredInterface;
32 import org.wamblee.system.core.Scope;
33 import org.wamblee.system.spring.SpringComponent;
34 import org.wamblee.system.spring.component.ORMappingConfig;
35 import org.wamblee.system.spring.component.ORMappingConfig.DatabaseType;
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.driver"),
60                 dbProps.getProperty("database.url"), 
61                 dbProps.getProperty("database.username"),
62                 dbProps.getProperty("database.password"));
63        addInterface(DATASOURCE, ds, aScope);
64        DatabaseType type = DatabaseType.valueOf(dbProps.getProperty("database.type"));
65        
66        ORMappingConfig config = new ORMappingConfig(true, type);
67        
68        addInterface(ORM_CONFIG, config, aScope);
69        
70        return ds; 
71     }
72
73     @Override
74     protected void doStop(DataSource aRuntime) {
75        // Empty.
76     }
77 }