source code formatting.
[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 org.wamblee.support.persistence.Database;
19 import org.wamblee.support.persistence.DerbyDatabase;
20
21 import org.wamblee.system.components.ORMappingConfig.DatabaseType;
22 import org.wamblee.system.core.AbstractComponent;
23 import org.wamblee.system.core.DefaultProvidedInterface;
24 import org.wamblee.system.core.ProvidedInterface;
25 import org.wamblee.system.core.Scope;
26
27 import java.util.Properties;
28
29
30 /**
31  * DOCUMENT ME!
32  *
33  * @author $author$
34  * @version $Revision$
35   */
36 public class DatabaseComponent extends AbstractComponent<Database> {
37     /**
38      * DOCUMENT ME!
39      */
40     private static ProvidedInterface DB_PROPS = new DefaultProvidedInterface("dbProps",
41             Properties.class);
42
43     /**
44      * DOCUMENT ME!
45      */
46     private Database database;
47
48     /**
49      * Creates a new DatabaseComponent object.
50      *
51      * @param aName DOCUMENT ME!
52      * @param aDatabase DOCUMENT ME!
53      */
54     public DatabaseComponent(String aName, Database aDatabase) {
55         super(aName);
56         database = aDatabase;
57         addProvidedInterface(DB_PROPS);
58     }
59
60     /**
61      * DOCUMENT ME!
62      *
63      * @param aScope DOCUMENT ME!
64      *
65      * @return DOCUMENT ME!
66      */
67     @Override
68     protected Database doStart(Scope aScope) {
69         database.start();
70
71         Properties props = new Properties();
72
73         if (database instanceof DerbyDatabase) {
74             props.put("database.type", DatabaseType.DERBY.toString());
75         } else {
76             throw new IllegalArgumentException("Unknown database type "
77                 + database);
78         }
79
80         //props.put("database.driver", database.getDriverClassName());
81         props.put("database.url", database.getJdbcUrl());
82         props.put("database.username", database.getUsername());
83         props.put("database.password", database.getPassword());
84
85         addInterface(DB_PROPS, props, aScope);
86
87         return database;
88     }
89
90     /**
91      * DOCUMENT ME!
92      *
93      * @param aRuntime DOCUMENT ME!
94      */
95     @Override
96     protected void doStop(Database aRuntime) {
97         database.stop();
98     }
99 }