source code formatting.
[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  * DOCUMENT ME!
43  *
44  * @author $author$
45  * @version $Revision$
46  */
47 public class DatasourceComponent extends AbstractComponent<DataSource> {
48     /**
49      * DOCUMENT ME!
50      */
51     private static RequiredInterface PROPS = new DefaultRequiredInterface("dbprops",
52             Properties.class);
53
54     /**
55      * DOCUMENT ME!
56      */
57     private static ProvidedInterface DATASOURCE = new DefaultProvidedInterface("datasource",
58             DataSource.class);
59
60     /**
61      * DOCUMENT ME!
62      */
63     private static ProvidedInterface ORM_CONFIG = new DefaultProvidedInterface("ormconfig",
64             ORMappingConfig.class);
65
66 /**
67      * Creates a new DatasourceComponent object.
68      *
69      * @param aName DOCUMENT ME!
70      *
71      * @throws IOException DOCUMENT ME!
72      */
73     public DatasourceComponent(String aName) throws IOException {
74         super(aName);
75
76         addRequiredInterface(PROPS);
77         addProvidedInterface(DATASOURCE);
78         addProvidedInterface(ORM_CONFIG);
79     }
80
81     /**
82      * DOCUMENT ME!
83      *
84      * @param aScope DOCUMENT ME!
85      *
86      * @return DOCUMENT ME!
87      */
88     @Override
89     protected DataSource doStart(Scope aScope) {
90         Properties              dbProps = aScope.getInterfaceImplementation(PROPS
91                 .getProvider(), Properties.class);
92         DriverManagerDataSource ds      = new DriverManagerDataSource(dbProps
93                 .getProperty("database.url"),
94                 dbProps.getProperty("database.username"),
95                 dbProps.getProperty("database.password"));
96         addInterface(DATASOURCE, ds, aScope);
97
98         DatabaseType    type   = DatabaseType.valueOf(dbProps.getProperty(
99                     "database.type"));
100
101         ORMappingConfig config = new ORMappingConfig(true, type);
102
103         addInterface(ORM_CONFIG, config, aScope);
104
105         return ds;
106     }
107
108     /**
109      * DOCUMENT ME!
110      *
111      * @param aRuntime DOCUMENT ME!
112      */
113     @Override
114     protected void doStop(DataSource aRuntime) {
115         // Empty.
116     }
117 }