2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * ============LICENSE_END=========================================================
20 package org.onap.policy.pap.xacml.rest;
22 import java.io.FileInputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.Properties;
27 import javax.annotation.PostConstruct;
28 import javax.sql.DataSource;
30 import org.apache.tomcat.dbcp.dbcp2.BasicDataSource;
31 import org.hibernate.SessionFactory;
32 import org.onap.policy.common.logging.flexlogger.FlexLogger;
33 import org.onap.policy.common.logging.flexlogger.Logger;
34 import org.onap.policy.utils.CryptoUtils;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.context.annotation.Bean;
37 import org.springframework.context.annotation.ComponentScan;
38 import org.springframework.context.annotation.Configuration;
39 import org.springframework.orm.hibernate4.HibernateTransactionManager;
40 import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder;
41 import org.springframework.transaction.annotation.EnableTransactionManagement;
42 import org.springframework.web.servlet.config.annotation.EnableWebMvc;
43 import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
47 @EnableTransactionManagement
48 @ComponentScan(basePackages = { "org.onap.*", "com.*" })
49 public class PAPRestConfig extends WebMvcConfigurerAdapter {
50 private static final Logger LOGGER = FlexLogger.getLogger(PAPRestConfig.class);
52 private static String dbDriver = null;
53 private static String dbUrl = null;
54 private static String dbUserName = null;
55 private static String dbPassword = null;
59 Properties prop = new Properties();
60 InputStream input = null;
62 input = new FileInputStream("xacml.pap.properties");
63 // load a properties file
65 setDbDriver(prop.getProperty("javax.persistence.jdbc.driver"));
66 setDbUrl(prop.getProperty("javax.persistence.jdbc.url"));
67 setDbUserName(prop.getProperty("javax.persistence.jdbc.user"));
68 setDbPassword( CryptoUtils.decryptTxtNoExStr(prop.getProperty("javax.persistence.jdbc.password", "")));
70 LOGGER.error("Exception Occured while loading properties file"+e);
75 } catch (IOException e) {
76 LOGGER.error("Exception Occured while clsoing the stream"+e);
82 @Bean(name = "dataSource")
83 public DataSource getDataSource() {
84 BasicDataSource dataSource = new BasicDataSource();
85 dataSource.setDriverClassName(PAPRestConfig.getDbDriver());
86 dataSource.setUrl(PAPRestConfig.getDbUrl());
87 dataSource.setUsername(PAPRestConfig.getDbUserName());
88 dataSource.setPassword(PAPRestConfig.getDbPassword());
93 @Bean(name = "sessionFactory")
94 public SessionFactory getSessionFactory(DataSource dataSource) {
95 LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
96 sessionBuilder.scanPackages("org.onap.*", "com.*");
97 sessionBuilder.addProperties(getHibernateProperties());
98 return sessionBuilder.buildSessionFactory();
101 private Properties getHibernateProperties() {
102 Properties properties = new Properties();
103 properties.put("hibernate.show_sql", "true");
104 properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
109 @Bean(name = "transactionManager")
110 public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
111 return new HibernateTransactionManager(sessionFactory);
114 public static String getDbDriver() {
118 public static void setDbDriver(String dbDriver) {
119 PAPRestConfig.dbDriver = dbDriver;
122 public static String getDbUrl() {
126 public static void setDbUrl(String dbUrl) {
127 PAPRestConfig.dbUrl = dbUrl;
130 public static String getDbUserName() {
134 public static void setDbUserName(String dbUserName) {
135 PAPRestConfig.dbUserName = dbUserName;
138 public static String getDbPassword() {
142 public static void setDbPassword(String dbPassword) {
143 PAPRestConfig.dbPassword = CryptoUtils.decryptTxtNoExStr(dbPassword);