aboutsummaryrefslogtreecommitdiffstats
path: root/dblib/provider/src/main/java/org/openecomp/sdnc/sli/resource/dblib/DBLIBResourceActivator.java
blob: 150a9a82fba23f0a3e2695a48cc43b386621fdb5 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*-
 * ============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.ServiceReference;
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(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)
		{
			try {
				ServiceReference sref = ctx.getServiceReference(DbLibService.class.getName());

				if (sref == null) {
					LOG.warn("Could not find service reference for DBLIB service ("	+ DbLibService.class.getName() + ")");
				} else {
					DBResourceManager dblibSvc = (DBResourceManager) ctx.getService(sref);
					if (dblibSvc == null) {
						LOG.warn("Could not find service reference for DBLIB service ("	+ DbLibService.class.getName() + ")");
					} else {
						dblibSvc.cleanUp();
					}
				}
			} catch(Throwable exc) {
				LOG.warn("Cleanup", exc);
			}

			registration.unregister();
			registration = null;
			LOG.debug("Deregistering DBResourceManager service");
		}
	}

}