32634d0d70d2ba8806e7e8320b9d4dcfadc8d8b8
[portal/sdk.git] /
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal SDK
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.onap.portalsdk.analytics.system.fusion.adapter;
39
40 import java.sql.Connection;
41 import java.util.Map;
42
43 import javax.servlet.ServletContext;
44
45 import org.onap.portalsdk.core.domain.FusionObject;
46 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
47 import org.springframework.beans.factory.annotation.Autowired;
48
49 import com.mchange.v2.c3p0.ComboPooledDataSource;
50
51 public class FusionAdapter implements FusionObject {
52         
53         private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(FusionAdapter.class);
54
55     public static final String LOCAL_SESSION_FACTORY_KEY = "local";
56
57
58     private ComboPooledDataSource dataSource;
59     private Map<String,ComboPooledDataSource> dataSourceMap;
60     
61     //private           SessionFactory sessionFactory;
62     private        ServletContext     servletContext;    
63
64     public FusionAdapter() {
65     }
66
67
68     public ServletContext getServletContext() {
69         return servletContext;
70     }
71
72     public void setServletContext(ServletContext servletContext) {
73         this.servletContext = servletContext;
74     }
75
76     /** Gets connection to the database **/
77     public  Connection getConnection() {
78         Connection connection = null;
79         try {
80            connection = getDataSource().getConnection();
81         } catch(Exception ex) {
82                 logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage(), ex);
83         }
84        return connection;
85     }
86
87
88     /** Gets connection to the database indicated via the session factory key **/
89     public synchronized Connection getConnection(String schemaId) {
90                 Connection connection = null;
91                 try {
92                         connection = getDataSourceMap().get(schemaId).getConnection();
93                 } catch (Exception e) {
94                         logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), e);
95                 }
96         
97                 return connection;
98     }
99
100
101     /** Releases connection to the database **/
102     public void releaseConnection(Connection conn) {
103         try {
104           conn.close();
105         }
106         catch (Exception e) {
107           logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), e);
108         }
109     }
110
111
112         public ComboPooledDataSource getDataSource() {
113                 return dataSource;
114         }
115
116
117         @Autowired
118         public void setDataSource(ComboPooledDataSource dataSource) {
119                 this.dataSource = dataSource;
120         }
121
122         public Map<String,ComboPooledDataSource> getDataSourceMap() {
123                 if(dataSourceMap==null)
124                         dataSourceMap = (Map<String,ComboPooledDataSource>)SpringContext.getApplicationContext().getBean("dataSourceMap");
125
126                 return dataSourceMap;
127         }
128
129         public void setdataSourceMap(Map<String,ComboPooledDataSource> dataSourceMap) {
130                 this.dataSourceMap = dataSourceMap;
131         }
132
133 }