10e679589d95a1c2bd0ca91d3b94c9822eb21367
[utils] / system / general / src / test / java / org / wamblee / system / components / DatabaseComponent.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.components;
17
18 import java.util.Properties;
19
20 import org.wamblee.support.persistence.Database;
21 import org.wamblee.support.persistence.DerbyDatabase;
22 import org.wamblee.system.components.ORMappingConfig.DatabaseType;
23 import org.wamblee.system.core.AbstractComponent;
24 import org.wamblee.system.core.DefaultProvidedInterface;
25 import org.wamblee.system.core.ProvidedInterface;
26 import org.wamblee.system.core.Scope;
27
28 public class DatabaseComponent extends AbstractComponent<Database> {
29     
30     private static ProvidedInterface DB_PROPS = new DefaultProvidedInterface("dbProps", Properties.class);
31     
32     private Database database; 
33     
34     public DatabaseComponent(String aName, Database aDatabase) { 
35         super(aName); 
36         database = aDatabase;
37         addProvidedInterface(DB_PROPS);
38     }
39
40     @Override
41     protected Database doStart(Scope aScope) {
42         database.start(); 
43         
44         Properties props = new Properties();
45         if ( database instanceof DerbyDatabase ) { 
46             props.put("database.type", DatabaseType.DERBY.toString());
47         } else { 
48             throw new IllegalArgumentException("Unknown database type " + database); 
49         }
50         //props.put("database.driver", database.getDriverClassName());
51         props.put("database.url", database.getJdbcUrl());
52         props.put("database.username", database.getUsername());
53         props.put("database.password", database.getPassword());
54
55         addInterface(DB_PROPS, props, aScope);
56         return database; 
57     }
58
59     @Override
60     protected void doStop(Database aRuntime) {
61         database.stop();
62     }
63
64 }