USE TRY WITH RESOURCES
[policy/engine.git] / ONAP-PAP-REST / src / main / java / org / onap / policy / pap / xacml / rest / PAPRestConfig.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-REST
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
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  * ============LICENSE_END=========================================================
19  */
20 package org.onap.policy.pap.xacml.rest;
21
22 import java.io.FileInputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.Properties;
26
27 import javax.annotation.PostConstruct;
28 import javax.sql.DataSource;
29
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;
44
45 @Configuration
46 @EnableWebMvc
47 @EnableTransactionManagement
48 @ComponentScan(basePackages = { "org.onap.*", "com.*" })
49 public class PAPRestConfig extends WebMvcConfigurerAdapter {
50     private static final Logger LOGGER  = FlexLogger.getLogger(PAPRestConfig.class);
51
52     private static String dbDriver = null;
53     private static String dbUrl = null;
54     private static String dbUserName = null;
55     private static String dbPassword = null;
56
57     @PostConstruct
58     public void init(){
59         Properties prop = new Properties();
60         try(InputStream input = new FileInputStream("xacml.pap.properties")) {
61             // load a properties file
62             prop.load(input);
63             setDbDriver(prop.getProperty("javax.persistence.jdbc.driver"));
64             setDbUrl(prop.getProperty("javax.persistence.jdbc.url"));
65             setDbUserName(prop.getProperty("javax.persistence.jdbc.user"));
66             setDbPassword( CryptoUtils.decryptTxtNoExStr(prop.getProperty("javax.persistence.jdbc.password", "")));
67         }catch(Exception e){
68             LOGGER.error("Exception Occured while loading properties file"+e);
69         }
70     }
71
72     @Bean(name = "dataSource")
73     public DataSource getDataSource() {
74         BasicDataSource dataSource = new BasicDataSource();
75         dataSource.setDriverClassName(PAPRestConfig.getDbDriver());
76         dataSource.setUrl(PAPRestConfig.getDbUrl());
77         dataSource.setUsername(PAPRestConfig.getDbUserName());
78         dataSource.setPassword(PAPRestConfig.getDbPassword());
79         return dataSource;
80     }
81
82     @Autowired
83     @Bean(name = "sessionFactory")
84     public SessionFactory getSessionFactory(DataSource dataSource) {
85         LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
86         sessionBuilder.scanPackages("org.onap.*", "com.*");
87         sessionBuilder.addProperties(getHibernateProperties());
88         return sessionBuilder.buildSessionFactory();
89     }
90
91     private Properties getHibernateProperties() {
92         Properties properties = new Properties();
93         properties.put("hibernate.show_sql", "true");
94         properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
95         return properties;
96     }
97
98     @Autowired
99     @Bean(name = "transactionManager")
100     public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
101         return new HibernateTransactionManager(sessionFactory);
102     }
103
104     public static String getDbDriver() {
105         return dbDriver;
106     }
107
108     public static void setDbDriver(String dbDriver) {
109         PAPRestConfig.dbDriver = dbDriver;
110     }
111
112     public static String getDbUrl() {
113         return dbUrl;
114     }
115
116     public static void setDbUrl(String dbUrl) {
117         PAPRestConfig.dbUrl = dbUrl;
118     }
119
120     public static String getDbUserName() {
121         return dbUserName;
122     }
123
124     public static void setDbUserName(String dbUserName) {
125         PAPRestConfig.dbUserName = dbUserName;
126     }
127
128     public static String getDbPassword() {
129         return dbPassword;
130     }
131
132     public static void setDbPassword(String dbPassword) {
133         PAPRestConfig.dbPassword = CryptoUtils.decryptTxtNoExStr(dbPassword);
134     }
135
136 }