2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2016 - 2017 AT&T
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdnc.sli.resource.dblib;
25 import java.util.Properties;
27 import org.osgi.framework.BundleActivator;
28 import org.osgi.framework.BundleContext;
29 import org.osgi.framework.ServiceRegistration;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 public class DBLIBResourceActivator implements BundleActivator {
35 private static final String SDNC_CONFIG_DIR = "SDNC_CONFIG_DIR";
37 private static final String DBLIB_PROP_PATH = "/dblib.properties";
39 private ServiceRegistration registration = null;
41 private static final Logger LOG = LoggerFactory.getLogger(DBLIBResourceActivator.class);
44 public void start(BundleContext ctx) throws Exception {
45 LOG.info("entering DBLIBResourceActivator.start");
47 DbLibService jdbcDataSource = null;
49 Properties props = new Properties();
53 String propDir = System.getenv(SDNC_CONFIG_DIR);
54 if ((propDir == null) || (propDir.length() == 0)) {
55 propDir = "/opt/sdnc/data/properties";
57 file = new File(propDir + DBLIB_PROP_PATH);
59 propURL = file.toURI().toURL();
60 LOG.info("Using property file (1): " + file.toString());
62 propURL = ctx.getBundle().getResource("dblib.properties");
64 if (propURL == null) {
65 file = new File(DBLIB_PROP_PATH);
66 tmp = this.getClass().getResource(DBLIB_PROP_PATH);
67 // if(!file.exists()) {
69 throw new DblibConfigurationException("Missing configuration properties resource(3) : " + DBLIB_PROP_PATH);
71 propURL = tmp; //file.toURI().toURL();
72 LOG.info("Using property file (4): " + file.toString());
75 LOG.info("Using property file (2): " + propURL.toString());
81 props.load(propURL.openStream());
82 } catch (Exception e) {
83 throw new DblibConfigurationException("Could not load properties at URL " + propURL.toString(), e);
90 jdbcDataSource = DBResourceManager.create(props);
91 } catch (Exception exc) {
92 throw new DblibConfigurationException("Could not get initialize database", exc);
95 String regName = jdbcDataSource.getClass().getName();
97 LOG.info("Registering DBResourceManager service "+regName);
98 // registration = ctx.registerService(regName, jdbcDataSource, null);
99 registration = ctx.registerService(new String[] { regName, DbLibService.class.getName(), "javax.sql.DataSource" }, jdbcDataSource, null);
103 public void stop(BundleContext ctx) throws Exception {
104 LOG.info("entering DBLIBResourceActivator.stop");
105 if (registration != null)
107 registration.unregister();
109 LOG.debug("Deregistering DBResourceManager service");