diff options
Diffstat (limited to 'ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/PAPRestConfig.java')
-rw-r--r-- | ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/PAPRestConfig.java | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/PAPRestConfig.java b/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/PAPRestConfig.java index 801387380..bbacabab0 100644 --- a/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/PAPRestConfig.java +++ b/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/PAPRestConfig.java @@ -19,8 +19,24 @@ */ package org.openecomp.policy.pap.xacml.rest; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; + +import javax.annotation.PostConstruct; +import javax.sql.DataSource; + +import org.apache.tomcat.dbcp.dbcp2.BasicDataSource; +import org.hibernate.SessionFactory; +import org.openecomp.policy.common.logging.flexlogger.FlexLogger; +import org.openecomp.policy.common.logging.flexlogger.Logger; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; +import org.springframework.orm.hibernate4.HibernateTransactionManager; +import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder; import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @@ -30,6 +46,100 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter @EnableTransactionManagement @ComponentScan(basePackages = { "org.openecomp.*", "com.*" }) public class PAPRestConfig extends WebMvcConfigurerAdapter { + private static final Logger LOGGER = FlexLogger.getLogger(PAPRestConfig.class); + + private static String dbDriver = null; + private static String dbUrl = null; + private static String dbUserName = null; + private static String dbPassword = null; + + @PostConstruct + public void init(){ + Properties prop = new Properties(); + InputStream input = null; + try { + input = new FileInputStream("xacml.pap.properties"); + // load a properties file + prop.load(input); + setDbDriver(prop.getProperty("javax.persistence.jdbc.driver")); + setDbUrl(prop.getProperty("javax.persistence.jdbc.url")); + setDbUserName(prop.getProperty("javax.persistence.jdbc.user")); + setDbPassword(prop.getProperty("javax.persistence.jdbc.password")); + }catch(Exception e){ + LOGGER.error("Exception Occured while loading properties file"+e); + }finally{ + if(input != null){ + try { + input.close(); + } catch (IOException e) { + LOGGER.error("Exception Occured while clsoing the stream"+e); + } + } + } + } + + @Bean(name = "dataSource") + public DataSource getDataSource() { + BasicDataSource dataSource = new BasicDataSource(); + dataSource.setDriverClassName(PAPRestConfig.getDbDriver()); + dataSource.setUrl(PAPRestConfig.getDbUrl()); + dataSource.setUsername(PAPRestConfig.getDbUserName()); + dataSource.setPassword(PAPRestConfig.getDbPassword()); + return dataSource; + } + + @Autowired + @Bean(name = "sessionFactory") + public SessionFactory getSessionFactory(DataSource dataSource) { + LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource); + sessionBuilder.scanPackages("org.openecomp.*", "com.*"); + sessionBuilder.addProperties(getHibernateProperties()); + return sessionBuilder.buildSessionFactory(); + } + + private Properties getHibernateProperties() { + Properties properties = new Properties(); + properties.put("hibernate.show_sql", "true"); + properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect"); + return properties; + } + + @Autowired + @Bean(name = "transactionManager") + public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) { + return new HibernateTransactionManager(sessionFactory); + } + + public static String getDbDriver() { + return dbDriver; + } + + public static void setDbDriver(String dbDriver) { + PAPRestConfig.dbDriver = dbDriver; + } + + public static String getDbUrl() { + return dbUrl; + } + + public static void setDbUrl(String dbUrl) { + PAPRestConfig.dbUrl = dbUrl; + } + + public static String getDbUserName() { + return dbUserName; + } + + public static void setDbUserName(String dbUserName) { + PAPRestConfig.dbUserName = dbUserName; + } + public static String getDbPassword() { + return dbPassword; + } + public static void setDbPassword(String dbPassword) { + PAPRestConfig.dbPassword = dbPassword; + } + } |