3ac51bbed5c1a4603816b65bf1d461d441eaf008
[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.FusionObject;
46 import org.springframework.beans.factory.annotation.Autowired;
47
48 import com.mchange.v2.c3p0.ComboPooledDataSource;
49
50 public class FusionAdapter implements FusionObject {
51
52     public static final String LOCAL_SESSION_FACTORY_KEY = "local";
53
54
55     private ComboPooledDataSource dataSource;
56     private Map<String,ComboPooledDataSource> dataSourceMap;
57     
58     //private           SessionFactory sessionFactory;
59     private        ServletContext     servletContext;    
60
61     public FusionAdapter() {
62     }
63
64
65     public ServletContext getServletContext() {
66         return servletContext;
67     }
68
69     public void setServletContext(ServletContext servletContext) {
70         this.servletContext = servletContext;
71     }
72
73     /** Gets connection to the database **/
74     public  Connection getConnection() {
75         Connection connection = null;
76         try {
77            connection = getDataSource().getConnection();
78         } catch(Exception ex) {
79                 ex.printStackTrace();
80         }
81        return connection;
82     }
83
84
85     /** Gets connection to the database indicated via the session factory key **/
86     public synchronized Connection getConnection(String schemaId) {
87                 Connection connection = null;
88                 try {
89                         connection = getDataSourceMap().get(schemaId).getConnection();
90                 } catch (Exception e) {
91                         e.printStackTrace();
92                 }
93         
94                 return connection;
95     }
96
97
98     /** Releases connection to the database **/
99     public void releaseConnection(Connection conn) {
100         try {
101           conn.close();
102         }
103         catch (Exception e) {
104           e.printStackTrace();
105         }
106     }
107
108
109         public ComboPooledDataSource getDataSource() {
110                 return dataSource;
111         }
112
113
114         @Autowired
115         public void setDataSource(ComboPooledDataSource dataSource) {
116                 this.dataSource = dataSource;
117         }
118
119         public Map<String,ComboPooledDataSource> getDataSourceMap() {
120                 if(dataSourceMap==null)
121                         dataSourceMap = (Map<String,ComboPooledDataSource>)SpringContext.getApplicationContext().getBean("dataSourceMap");
122
123                 return dataSourceMap;
124         }
125
126         public void setdataSourceMap(Map<String,ComboPooledDataSource> dataSourceMap) {
127                 this.dataSourceMap = dataSourceMap;
128         }
129
130 }