2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.context.annotation.Bean;
36 import org.springframework.context.annotation.ComponentScan;
37 import org.springframework.context.annotation.Configuration;
38 import org.springframework.orm.hibernate4.HibernateTransactionManager;
39 import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder;
40 import org.springframework.transaction.annotation.EnableTransactionManagement;
41 import org.springframework.web.servlet.config.annotation.EnableWebMvc;
42 import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
46 @EnableTransactionManagement
47 @ComponentScan(basePackages = { "org.onap.*", "com.*" })
48 public class PAPRestConfig extends WebMvcConfigurerAdapter {
49 private static final Logger LOGGER = FlexLogger.getLogger(PAPRestConfig.class);
51 private static String dbDriver = null;
52 private static String dbUrl = null;
53 private static String dbUserName = null;
54 private static String dbPassword = null;
58 Properties prop = new Properties();
59 InputStream input = null;
61 input = new FileInputStream("xacml.pap.properties");
62 // load a properties file
64 setDbDriver(prop.getProperty("javax.persistence.jdbc.driver"));
65 setDbUrl(prop.getProperty("javax.persistence.jdbc.url"));
66 setDbUserName(prop.getProperty("javax.persistence.jdbc.user"));
67 setDbPassword(prop.getProperty("javax.persistence.jdbc.password"));
69 LOGGER.error("Exception Occured while loading properties file"+e);
74 } catch (IOException e) {
75 LOGGER.error("Exception Occured while clsoing the stream"+e);
81 @Bean(name = "dataSource")
82 public DataSource getDataSource() {
83 BasicDataSource dataSource = new BasicDataSource();
84 dataSource.setDriverClassName(PAPRestConfig.getDbDriver());
85 dataSource.setUrl(PAPRestConfig.getDbUrl());
86 dataSource.setUsername(PAPRestConfig.getDbUserName());
87 dataSource.setPassword(PAPRestConfig.getDbPassword());
92 @Bean(name = "sessionFactory")
93 public SessionFactory getSessionFactory(DataSource dataSource) {
94 LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
95 sessionBuilder.scanPackages("org.onap.*", "com.*");
96 sessionBuilder.addProperties(getHibernateProperties());
97 return sessionBuilder.buildSessionFactory();
100 private Properties getHibernateProperties() {
101 Properties properties = new Properties();
102 properties.put("hibernate.show_sql", "true");
103 properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
108 @Bean(name = "transactionManager")
109 public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
110 return new HibernateTransactionManager(sessionFactory);
113 public static String getDbDriver() {
117 public static void setDbDriver(String dbDriver) {
118 PAPRestConfig.dbDriver = dbDriver;
121 public static String getDbUrl() {
125 public static void setDbUrl(String dbUrl) {
126 PAPRestConfig.dbUrl = dbUrl;
129 public static String getDbUserName() {
133 public static void setDbUserName(String dbUserName) {
134 PAPRestConfig.dbUserName = dbUserName;
137 public static String getDbPassword() {
141 public static void setDbPassword(String dbPassword) {
142 PAPRestConfig.dbPassword = dbPassword;