Removed DOCUMENT ME comments that were generated and applied source code
[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 org.springframework.core.io.ClassPathResource;
19
20 import org.springframework.jdbc.datasource.DriverManagerDataSource;
21
22 import org.wamblee.system.components.ORMappingConfig;
23 import org.wamblee.system.components.ORMappingConfig.DatabaseType;
24 import org.wamblee.system.core.AbstractComponent;
25 import org.wamblee.system.core.DefaultProvidedInterface;
26 import org.wamblee.system.core.DefaultRequiredInterface;
27 import org.wamblee.system.core.ProvidedInterface;
28 import org.wamblee.system.core.RequiredInterface;
29 import org.wamblee.system.core.Scope;
30 import org.wamblee.system.spring.SpringComponent;
31
32 import java.io.IOException;
33
34 import java.util.Map;
35 import java.util.Properties;
36 import java.util.TreeMap;
37
38 import javax.sql.DataSource;
39
40 /**
41  * 
42  * @author $author$
43  * @version $Revision$
44  */
45 public class DatasourceComponent extends AbstractComponent<DataSource> {
46     private static RequiredInterface PROPS = new DefaultRequiredInterface(
47         "dbprops", Properties.class);
48
49     private static ProvidedInterface DATASOURCE = new DefaultProvidedInterface(
50         "datasource", DataSource.class);
51
52     private static ProvidedInterface ORM_CONFIG = new DefaultProvidedInterface(
53         "ormconfig", ORMappingConfig.class);
54
55     /**
56      * Creates a new DatasourceComponent object.
57      * 
58      * 
59      */
60     public DatasourceComponent(String aName) throws IOException {
61         super(aName);
62
63         addRequiredInterface(PROPS);
64         addProvidedInterface(DATASOURCE);
65         addProvidedInterface(ORM_CONFIG);
66     }
67
68     @Override
69     protected DataSource doStart(Scope aScope) {
70         Properties dbProps = aScope.getInterfaceImplementation(PROPS
71             .getProvider(), Properties.class);
72         DriverManagerDataSource ds = new DriverManagerDataSource(dbProps
73             .getProperty("database.url"), dbProps
74             .getProperty("database.username"), dbProps
75             .getProperty("database.password"));
76         addInterface(DATASOURCE, ds, aScope);
77
78         DatabaseType type = DatabaseType.valueOf(dbProps
79             .getProperty("database.type"));
80
81         ORMappingConfig config = new ORMappingConfig(true, type);
82
83         addInterface(ORM_CONFIG, config, aScope);
84
85         return ds;
86     }
87
88     @Override
89     protected void doStop(DataSource aRuntime) {
90         // Empty.
91     }
92 }