68f94715e2231a606eb5a537688f5f6cb42d9fcd
[portal/sdk.git] /
1 /*-
2  * ================================================================================
3  * ECOMP Portal SDK
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property
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  * ================================================================================
19  */
20 package org.openecomp.portalsdk.analytics.system.fusion.adapter;
21
22 import java.sql.*;
23 import java.util.*;
24
25 import javax.naming.Context;
26 import javax.naming.InitialContext;
27 import javax.servlet.*;
28
29 import com.mchange.v2.c3p0.ComboPooledDataSource;
30
31 import org.hibernate.SessionFactory;
32 import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
33 import org.hibernate.engine.spi.SessionFactoryImplementor;
34 import org.hibernate.engine.spi.SessionImplementor;
35 import org.openecomp.portalsdk.core.FusionObject;
36 import org.openecomp.portalsdk.core.service.DataAccessService;
37 import org.openecomp.portalsdk.core.util.SystemProperties;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.context.annotation.Configuration;
40
41 public class FusionAdapter implements FusionObject {
42
43     public static final String LOCAL_SESSION_FACTORY_KEY = "local";
44
45
46     private ComboPooledDataSource dataSource;
47     private Map<String,ComboPooledDataSource> dataSourceMap;
48     
49     //private           SessionFactory sessionFactory;
50     private        ServletContext     servletContext;    
51
52     public FusionAdapter() {
53     }
54
55
56     public ServletContext getServletContext() {
57         return servletContext;
58     }
59
60     public void setServletContext(ServletContext servletContext) {
61         this.servletContext = servletContext;
62     }
63
64     /** Gets connection to the database **/
65     public  Connection getConnection() {
66         Connection connection = null;
67         try {
68            connection = getDataSource().getConnection();
69         } catch(Exception ex) {
70                 ex.printStackTrace();
71         }
72        return connection;
73     }
74
75
76     /** Gets connection to the database indicated via the session factory key **/
77     public synchronized Connection getConnection(String schemaId) {
78                 Connection connection = null;
79                 try {
80                         connection = getDataSourceMap().get(schemaId).getConnection();
81                 } catch (Exception e) {
82                         e.printStackTrace();
83                 }
84         
85                 return connection;
86     }
87
88
89     /** Releases connection to the database **/
90     public void releaseConnection(Connection conn) {
91         try {
92           conn.close();
93         }
94         catch (Exception e) {
95           e.printStackTrace();
96         }
97     }
98
99
100         public ComboPooledDataSource getDataSource() {
101                 return dataSource;
102         }
103
104
105         @Autowired
106         public void setDataSource(ComboPooledDataSource dataSource) {
107                 this.dataSource = dataSource;
108         }
109
110         public Map<String,ComboPooledDataSource> getDataSourceMap() {
111                 if(dataSourceMap==null)
112                         dataSourceMap = (Map<String,ComboPooledDataSource>)SpringContext.getApplicationContext().getBean("dataSourceMap");
113
114                 return dataSourceMap;
115         }
116
117         public void setdataSourceMap(Map<String,ComboPooledDataSource> dataSourceMap) {
118                 this.dataSourceMap = dataSourceMap;
119         }
120
121 }