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