85cf8341e4ad754d121565148a7d3c67ccfc3fa3
[sdnc/core.git] / dblib / provider / src / main / java / org / openecomp / sdnc / sli / resource / dblib / DBLIBResourceActivator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openecomp
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdnc.sli.resource.dblib;
22
23 import java.io.File;
24 import java.net.URL;
25 import java.util.Properties;
26
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;
32
33 public class DBLIBResourceActivator implements BundleActivator {
34
35         private static final String SDNC_CONFIG_DIR = "SDNC_CONFIG_DIR";
36
37         private static final String DBLIB_PROP_PATH = "/dblib.properties";
38
39         private ServiceRegistration registration = null;
40
41         private static final Logger LOG = LoggerFactory.getLogger(DBLIBResourceActivator.class);
42
43         @Override
44         public void start(BundleContext ctx) throws Exception {
45                 LOG.info("entering DBLIBResourceActivator.start");
46                 
47                 DbLibService jdbcDataSource = null;
48                 // Read properties
49                 Properties props = new Properties();
50                 
51                 File file =  null;
52                 URL propURL = null;
53                 String propDir = System.getenv(SDNC_CONFIG_DIR);
54                 if ((propDir == null) || (propDir.length() == 0)) {
55                         propDir = "/opt/sdnc/data/properties";
56                 }
57                 file = new File(propDir + DBLIB_PROP_PATH);
58                 if(file.exists()) {
59                         propURL = file.toURI().toURL();
60                         LOG.info("Using property file (1): " + file.toString());
61                 } else {
62                         propURL = ctx.getBundle().getResource("dblib.properties");
63                         URL tmp = null;
64                         if (propURL == null) {
65                                 file = new File(DBLIB_PROP_PATH);
66                                 tmp = this.getClass().getResource(DBLIB_PROP_PATH);
67 //                              if(!file.exists()) {
68                                 if(tmp == null) {
69                                         throw new DblibConfigurationException("Missing configuration properties resource(3) : " + DBLIB_PROP_PATH);
70                                 } else {
71                                         propURL = tmp; //file.toURI().toURL();
72                                         LOG.info("Using property file (4): " + file.toString());
73                                 }
74                         } else {
75                                 LOG.info("Using property file (2): " + propURL.toString());
76                         }
77                 }
78
79                 
80                 try {
81                         props.load(propURL.openStream());
82                 } catch (Exception e) {
83                         throw new DblibConfigurationException("Could not load properties at URL " + propURL.toString(), e);
84
85                 }
86
87
88
89                 try {
90                         jdbcDataSource = DBResourceManager.create(props);
91                 } catch (Exception exc) {
92                         throw new DblibConfigurationException("Could not get initialize database", exc);
93                 }
94
95                 String regName = jdbcDataSource.getClass().getName();
96
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);
100         }
101
102         @Override
103         public void stop(BundleContext ctx) throws Exception {
104                 LOG.info("entering DBLIBResourceActivator.stop");
105                 if (registration != null)
106                 {
107                         registration.unregister();
108                         registration = null;
109                         LOG.debug("Deregistering DBResourceManager service");
110                 }
111         }
112
113 }