summaryrefslogtreecommitdiffstats
path: root/ecomp-sdk/quantum/src/main/java/org/openecomp/portalsdk/core/conf/HibernateConfiguration.java
blob: b70ced484b4a4173105fd08ec5edd101afdba702 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*-
 * ================================================================================
 * eCOMP Portal SDK
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ================================================================================
 */
package org.openecomp.portalsdk.core.conf;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.sql.DataSource;

import org.hibernate.SessionFactory;
import org.openecomp.portalsdk.core.logging.format.AlarmSeverityEnum;
import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
import org.openecomp.portalsdk.core.util.SystemProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.mchange.v2.c3p0.ComboPooledDataSource;

@Configuration
@EnableTransactionManagement
public class HibernateConfiguration {
	
	@Autowired
	private SystemProperties systemProperties;
	
	@Autowired
	private HibernateMappingLocatable mappingLocate;
	
	EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(HibernateConfiguration.class);
	
	DataSource dataSource;
	

    @Bean
    public LocalSessionFactoryBean sessionFactory() {
    	LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(getDataSource());
        sessionFactory.setPackagesToScan(new String[] { "org.openecomp"});
        sessionFactory.setHibernateProperties(hibernateProperties());
        sessionFactory.setMappingLocations(mappingLocate.getMappingLocations());
        return sessionFactory;
     }

     
   
     
	@SuppressWarnings("rawtypes")
	@Bean
	public Map dataSourceMap() throws Exception {
		Connection conn = null;
		Statement stmt = null;
		Map<String, ComboPooledDataSource> dataSourceMap = new HashMap<String, ComboPooledDataSource>();

		try {
			conn = getDataSource().getConnection();
			stmt = conn.createStatement();
			String sql;
			sql = "SELECT schema_id,datasource_type,connection_url,user_name,password,driver_class,min_pool_size,max_pool_size,idle_connection_test_period FROM schema_info";
			ResultSet rs = stmt.executeQuery(sql);

			while (rs.next()) {
				ComboPooledDataSource dataSource = new ComboPooledDataSource();
				dataSource.setDriverClass(rs.getString("driver_class"));
				dataSource.setJdbcUrl(rs.getString("connection_url"));
				dataSource.setUser(rs.getString("user_name"));						
				dataSource.setPassword(rs.getString("password"));						    	
				dataSource.setMinPoolSize(rs.getInt("min_pool_size"));
				dataSource.setMaxPoolSize(rs.getInt("max_pool_size"));
				dataSource.setIdleConnectionTestPeriod(rs.getInt("idle_connection_test_period"));
				dataSourceMap.put(rs.getString("schema_id"), dataSource);
			}
			rs.close();
			stmt.close();
			conn.close();
		} catch (Exception e) {
			logger.error(EELFLoggerDelegate.errorLogger, "Error initializing database, verify database settings in properties file: " + e.getMessage(),AlarmSeverityEnum.CRITICAL);
			e.printStackTrace();
			dataSourceMap = null;
			throw e;
		} finally {
			try {
				if (stmt != null)
					stmt.close();
			} catch (SQLException se2) {}
			try {
				if (conn != null)
					conn.close();
			} catch (SQLException se) {	se.printStackTrace();}
		}

		return dataSourceMap;
	}

	private Properties hibernateProperties() {
        Properties properties = new Properties();
        properties.put("hibernate.dialect", SystemProperties.getProperty(SystemProperties.HB_DIALECT));
        properties.put("hibernate.show_sql", SystemProperties.getProperty(SystemProperties.HB_SHOW_SQL));
        return properties;        
    }
     
    @Bean
    @Autowired
    public HibernateTransactionManager transactionManager(SessionFactory s) {
       HibernateTransactionManager txManager = new HibernateTransactionManager();
       txManager.setSessionFactory(s);
       return txManager;
    }

	public SystemProperties getSystemProperties() {
		return systemProperties;
	}

	public void setSystemProperties(SystemProperties systemProperties) {
		this.systemProperties = systemProperties;
	}




	public DataSource getDataSource() {
		return dataSource;
	}


	@Autowired
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}
    
}