aboutsummaryrefslogtreecommitdiffstats
path: root/dblib/provider/src/main/java/org/openecomp/sdnc/sli/resource/dblib/DBLIBResourceActivator.java
blob: 85cf8341e4ad754d121565148a7d3c67ccfc3fa3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*-
 * ============LICENSE_START=======================================================
 * openecomp
 * ================================================================================
 * Copyright (C) 2016 - 2017 AT&T
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdnc.sli.resource.dblib;

import java.io.File;
import java.net.URL;
import java.util.Properties;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DBLIBResourceActivator implements BundleActivator {

	private static final String SDNC_CONFIG_DIR = "SDNC_CONFIG_DIR";

	private static final String DBLIB_PROP_PATH = "/dblib.properties";

	private ServiceRegistration registration = null;

	private static final Logger LOG = LoggerFactory.getLogger(DBLIBResourceActivator.class);

	@Override
	public void start(BundleContext ctx) throws Exception {
		LOG.info("entering DBLIBResourceActivator.start");
		
		DbLibService jdbcDataSource = null;
		// Read properties
		Properties props = new Properties();
		
		File file =  null;
		URL propURL = null;
		String propDir = System.getenv(SDNC_CONFIG_DIR);
		if ((propDir == null) || (propDir.length() == 0)) {
			propDir = "/opt/sdnc/data/properties";
		}
		file = new File(propDir + DBLIB_PROP_PATH);
		if(file.exists()) {
			propURL = file.toURI().toURL();
			LOG.info("Using property file (1): " + file.toString());
		} else {
			propURL = ctx.getBundle().getResource("dblib.properties");
			URL tmp = null;
			if (propURL == null) {
				file = new File(DBLIB_PROP_PATH);
				tmp = this.getClass().getResource(DBLIB_PROP_PATH);
//				if(!file.exists()) {
				if(tmp == null) {
					throw new DblibConfigurationException("Missing configuration properties resource(3) : " + DBLIB_PROP_PATH);
				} else {
					propURL = tmp; //file.toURI().toURL();
					LOG.info("Using property file (4): " + file.toString());
				}
			} else {
				LOG.info("Using property file (2): " + propURL.toString());
			}
		}

		
		try {
			props.load(propURL.openStream());
		} catch (Exception e) {
			throw new DblibConfigurationException("Could not load properties at URL " + propURL.toString(), e);

		}



		try {
			jdbcDataSource = DBResourceManager.create(props);
		} catch (Exception exc) {
			throw new DblibConfigurationException("Could not get initialize database", exc);
		}

		String regName = jdbcDataSource.getClass().getName();

		LOG.info("Registering DBResourceManager service "+regName);
//		registration = ctx.registerService(regName, jdbcDataSource, null);
		registration = ctx.registerService(new String[] { regName, DbLibService.class.getName(), "javax.sql.DataSource" }, jdbcDataSource, null);
	}

	@Override
	public void stop(BundleContext ctx) throws Exception {
		LOG.info("entering DBLIBResourceActivator.stop");
		if (registration != null)
		{
			registration.unregister();
			registration = null;
			LOG.debug("Deregistering DBResourceManager service");
		}
	}

}