Update groupId to org.onap.ccsdk.sli
[ccsdk/sli/core.git] / dblib / provider / src / main / java / org / onap / ccsdk / sli / core / dblib / DBLIBResourceActivator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * onap
4  * ================================================================================
5  * Copyright (C) 2016 - 2017 ONAP
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.onap.ccsdk.sli.core.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.ServiceReference;
30 import org.osgi.framework.ServiceRegistration;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public class DBLIBResourceActivator implements BundleActivator {
35
36         private static final String SDNC_CONFIG_DIR = "SDNC_CONFIG_DIR";
37
38         private static final String DBLIB_PROP_PATH = "/dblib.properties";
39
40         private ServiceRegistration registration = null;
41
42         private static final Logger LOG = LoggerFactory.getLogger(DBLIBResourceActivator.class);
43
44         @Override
45         public void start(BundleContext ctx) throws Exception {
46                 LOG.info("entering DBLIBResourceActivator.start");
47                 
48                 DbLibService jdbcDataSource = null;
49                 // Read properties
50                 Properties props = new Properties();
51                 
52                 File file =  null;
53                 URL propURL = null;
54                 String propDir = System.getenv(SDNC_CONFIG_DIR);
55                 if ((propDir == null) || (propDir.length() == 0)) {
56                         propDir = "/opt/sdnc/data/properties";
57                 }
58                 file = new File(propDir + DBLIB_PROP_PATH);
59                 if(file.exists()) {
60                         propURL = file.toURI().toURL();
61                         LOG.info("Using property file (1): " + file.toString());
62                 } else {
63                         propURL = ctx.getBundle().getResource("dblib.properties");
64                         URL tmp = null;
65                         if (propURL == null) {
66                                 file = new File(DBLIB_PROP_PATH);
67                                 tmp = this.getClass().getResource(DBLIB_PROP_PATH);
68 //                              if(!file.exists()) {
69                                 if(tmp == null) {
70                                         throw new DblibConfigurationException("Missing configuration properties resource(3) : " + DBLIB_PROP_PATH);
71                                 } else {
72                                         propURL = tmp; //file.toURI().toURL();
73                                         LOG.info("Using property file (4): " + file.toString());
74                                 }
75                         } else {
76                                 LOG.info("Using property file (2): " + propURL.toString());
77                         }
78                 }
79
80                 
81                 try {
82                         props.load(propURL.openStream());
83                 } catch (Exception e) {
84                         throw new DblibConfigurationException("Could not load properties at URL " + propURL.toString(), e);
85
86                 }
87
88
89
90                 try {
91                         jdbcDataSource = DBResourceManager.create(props);
92                 } catch (Exception exc) {
93                         throw new DblibConfigurationException("Could not get initialize database", exc);
94                 }
95
96                 String regName = jdbcDataSource.getClass().getName();
97
98                 LOG.info("Registering DBResourceManager service "+regName);
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                         try {
108                                 ServiceReference sref = ctx.getServiceReference(DbLibService.class.getName());
109
110                                 if (sref == null) {
111                                         LOG.warn("Could not find service reference for DBLIB service (" + DbLibService.class.getName() + ")");
112                                 } else {
113                                         DBResourceManager dblibSvc = (DBResourceManager) ctx.getService(sref);
114                                         if (dblibSvc == null) {
115                                                 LOG.warn("Could not find service reference for DBLIB service (" + DbLibService.class.getName() + ")");
116                                         } else {
117                                                 dblibSvc.cleanUp();
118                                         }
119                                 }
120                         } catch(Throwable exc) {
121                                 LOG.warn("Cleanup", exc);
122                         }
123
124                         registration.unregister();
125                         registration = null;
126                         LOG.debug("Deregistering DBResourceManager service");
127                 }
128         }
129
130 }