Merge "remove mariadb superuser sql statements"
[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                 InputStream input = null;
61                 try {
62                         input = new FileInputStream("xacml.pap.properties");
63                         // load a properties file
64                         prop.load(input);
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", "")));
69                 }catch(Exception e){
70                         LOGGER.error("Exception Occured while loading properties file"+e);
71                 }finally{
72                         if(input != null){
73                                 try {
74                                         input.close();
75                                 } catch (IOException e) {
76                                         LOGGER.error("Exception Occured while clsoing the stream"+e);
77                                 }
78                         }
79                 }
80         }
81         
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());
89             return dataSource;
90         }
91         
92         @Autowired
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();
99         }
100         
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");
105                 return properties;
106         }
107         
108         @Autowired
109         @Bean(name = "transactionManager")
110         public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
111                 return new HibernateTransactionManager(sessionFactory);
112         }
113
114         public static String getDbDriver() {
115                 return dbDriver;
116         }
117
118         public static void setDbDriver(String dbDriver) {
119                 PAPRestConfig.dbDriver = dbDriver;
120         }
121
122         public static String getDbUrl() {
123                 return dbUrl;
124         }
125
126         public static void setDbUrl(String dbUrl) {
127                 PAPRestConfig.dbUrl = dbUrl;
128         }
129
130         public static String getDbUserName() {
131                 return dbUserName;
132         }
133
134         public static void setDbUserName(String dbUserName) {
135                 PAPRestConfig.dbUserName = dbUserName;
136         }
137
138         public static String getDbPassword() {
139                 return dbPassword;
140         }
141
142         public static void setDbPassword(String dbPassword) {   
143                 PAPRestConfig.dbPassword = CryptoUtils.decryptTxtNoExStr(dbPassword);
144         }
145         
146 }