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.ServiceReference;
30 import org.osgi.framework.ServiceRegistration;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 public class DBLIBResourceActivator implements BundleActivator {
36 private static final String SDNC_CONFIG_DIR = "SDNC_CONFIG_DIR";
38 private static final String DBLIB_PROP_PATH = "/dblib.properties";
40 private ServiceRegistration registration = null;
42 private static final Logger LOG = LoggerFactory.getLogger(DBLIBResourceActivator.class);
45 public void start(BundleContext ctx) throws Exception {
46 LOG.info("entering DBLIBResourceActivator.start");
48 DbLibService jdbcDataSource = null;
50 Properties props = new Properties();
54 String propDir = System.getenv(SDNC_CONFIG_DIR);
55 if ((propDir == null) || (propDir.length() == 0)) {
56 propDir = "/opt/sdnc/data/properties";
58 file = new File(propDir + DBLIB_PROP_PATH);
60 propURL = file.toURI().toURL();
61 LOG.info("Using property file (1): " + file.toString());
63 propURL = ctx.getBundle().getResource("dblib.properties");
65 if (propURL == null) {
66 file = new File(DBLIB_PROP_PATH);
67 tmp = this.getClass().getResource(DBLIB_PROP_PATH);
68 // if(!file.exists()) {
70 throw new DblibConfigurationException("Missing configuration properties resource(3) : " + DBLIB_PROP_PATH);
72 propURL = tmp; //file.toURI().toURL();
73 LOG.info("Using property file (4): " + file.toString());
76 LOG.info("Using property file (2): " + propURL.toString());
82 props.load(propURL.openStream());
83 } catch (Exception e) {
84 throw new DblibConfigurationException("Could not load properties at URL " + propURL.toString(), e);
91 jdbcDataSource = DBResourceManager.create(props);
92 } catch (Exception exc) {
93 throw new DblibConfigurationException("Could not get initialize database", exc);
96 String regName = jdbcDataSource.getClass().getName();
98 LOG.info("Registering DBResourceManager service "+regName);
99 // registration = ctx.registerService(regName, jdbcDataSource, null);
100 registration = ctx.registerService(new String[] { regName, DbLibService.class.getName(), "javax.sql.DataSource" }, jdbcDataSource, null);
104 public void stop(BundleContext ctx) throws Exception {
105 LOG.info("entering DBLIBResourceActivator.stop");
106 if (registration != null)
109 ServiceReference sref = ctx.getServiceReference(DbLibService.class.getName());
112 LOG.warn("Could not find service reference for DBLIB service (" + DbLibService.class.getName() + ")");
114 DBResourceManager dblibSvc = (DBResourceManager) ctx.getService(sref);
115 if (dblibSvc == null) {
116 LOG.warn("Could not find service reference for DBLIB service (" + DbLibService.class.getName() + ")");
121 } catch(Throwable exc) {
122 LOG.warn("Cleanup", exc);
125 registration.unregister();
127 LOG.debug("Deregistering DBResourceManager service");