2 * Copyright 2008 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.usermgt.hibernate;
18 import java.io.IOException;
20 import java.util.Properties;
21 import java.util.TreeMap;
23 import javax.sql.DataSource;
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;
36 public class ExternalDatasourceComponent extends AbstractComponent<DataSource> {
38 private static final String DATABASE_PROPERTIES = "properties/test.org.wamblee.security.database.properties";
40 private static ProvidedInterface DATASOURCE = new DefaultProvidedInterface("datasource",
42 private static ProvidedInterface ORM_CONFIG = new DefaultProvidedInterface("ormconfig",
43 ORMappingConfig.class);
45 private Properties _dbProps;
47 public ExternalDatasourceComponent(String aName) throws IOException {
48 this(aName, loadProperties());
51 private static Properties loadProperties() throws IOException {
52 Properties dbProps = new Properties();
53 dbProps.load(new ClassPathResource(DATABASE_PROPERTIES)
58 public ExternalDatasourceComponent(String aName, Properties aProps) throws IOException {
62 addProvidedInterface(DATASOURCE);
63 addProvidedInterface(ORM_CONFIG);
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"));
77 ORMappingConfig config = new ORMappingConfig(true, type);
79 addInterface(ORM_CONFIG, config, aScope);
85 protected void doStop(DataSource aRuntime) {