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