summaryrefslogtreecommitdiffstats
path: root/ecomp-sdk/epsdk-core/src/main/java/org/openecomp/portalsdk/core/conf/HibernateConfiguration.java
blob: 8a3459d1fb88de88698ff566aae57e0eaea209e9 (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
/*-
 * ================================================================================
 * 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 {

	private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(HibernateConfiguration.class);

	@Autowired
	private HibernateMappingLocatable hbMappingLocatable;

	@Autowired
	private DataSource dataSource;

	@Bean
	public LocalSessionFactoryBean sessionFactory() {
		LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
		sessionFactory.setDataSource(dataSource);
		sessionFactory.setPackagesToScan(hbMappingLocatable.getPackagesToScan());
		sessionFactory.setHibernateProperties(getHibernateProperties());
		sessionFactory.setMappingLocations(hbMappingLocatable.getMappingLocations());
		return sessionFactory;
	}

	/**
	 * Builds a properties object with Hibernate properties in te system.properties file.
	 * 
	 * @return Properties object
	 */
	private Properties getHibernateProperties() {
		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;
	}

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

		try {
			conn = dataSource.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;
	}

	@Bean
	@Autowired
	public HibernateTransactionManager transactionManager(SessionFactory s) {
		HibernateTransactionManager txManager = new HibernateTransactionManager();
		txManager.setSessionFactory(s);
		return txManager;
	}

}