55a2537c613d69c91305ad29dbe535f8187a5dd9
[utils] / security / src / test / java / org / wamblee / usermgt / hibernate / ExternalDatasourceComponent.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.ProvidedInterface;
30 import org.wamblee.system.core.RequiredInterface;
31 import org.wamblee.system.core.Scope;
32 import org.wamblee.system.spring.SpringComponent;
33 import org.wamblee.system.spring.component.ORMappingConfig;
34 import org.wamblee.system.spring.component.ORMappingConfig.DatabaseType;
35
36 public class ExternalDatasourceComponent extends AbstractComponent<DataSource> {
37
38     private static final String DATABASE_PROPERTIES = "properties/test.org.wamblee.security.database.properties";
39    
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     private Properties _dbProps;
46    
47     public ExternalDatasourceComponent(String aName) throws IOException {
48         this(aName, loadProperties());
49     }
50
51     private static Properties loadProperties() throws IOException {
52         Properties dbProps = new Properties();
53         dbProps.load(new ClassPathResource(DATABASE_PROPERTIES)
54                 .getInputStream());
55         return dbProps;
56     }
57     
58     public ExternalDatasourceComponent(String aName, Properties aProps) throws IOException {
59         super(aName);
60         _dbProps = aProps;
61         
62         addProvidedInterface(DATASOURCE);
63         addProvidedInterface(ORM_CONFIG);
64     }
65
66
67     @Override
68     protected DataSource doStart(Scope aScope) {
69         DriverManagerDataSource ds = new DriverManagerDataSource(
70                 _dbProps.getProperty("database.driver"),
71                 _dbProps.getProperty("database.url"), 
72                 _dbProps.getProperty("database.username"),
73                 _dbProps.getProperty("database.password"));
74        addInterface(DATASOURCE, ds, aScope);
75        DatabaseType type = DatabaseType.valueOf(_dbProps.getProperty("database.type"));
76        
77        ORMappingConfig config = new ORMappingConfig(true, type);
78        
79        addInterface(ORM_CONFIG, config, aScope);
80        
81        return ds; 
82     }
83
84     @Override
85     protected void doStop(DataSource aRuntime) {
86        // Empty.
87     }
88 }