You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@fineract.apache.org by ju...@apache.org on 2023/02/09 07:26:59 UTC

[fineract-cn-postgresql] 10/23: EclipseLink Configuration extending JpaBaseConfiguration

This is an automated email from the ASF dual-hosted git repository.

juhan pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/fineract-cn-postgresql.git

commit df172c6601f4723d4319a20f8318719e439539ad
Author: Ebenezer Graham <eg...@alustudent.com>
AuthorDate: Sun Jul 21 21:36:44 2019 +0400

    EclipseLink Configuration extending JpaBaseConfiguration
    
    Using org.ecplise.persistence.jpa to avoid security exception
    
    The JpaConfiguration Class already contains an LEMFB.
---
 build.gradle                                       |  38 +++--
 .../config/EclipseLinkJpaConfiguration.java        |  96 +++++++++++++
 .../config/PostgreSQLJavaConfiguration.java        | 156 ++++++++-------------
 .../cn/postgresql/util/PostgreSQLConstants.java    |   1 +
 4 files changed, 184 insertions(+), 107 deletions(-)

diff --git a/build.gradle b/build.gradle
index 8a5cb92..20be6d6 100644
--- a/build.gradle
+++ b/build.gradle
@@ -18,6 +18,12 @@
 buildscript {
     repositories {
         jcenter()
+        maven {
+            url "https://plugins.gradle.org/m2/"
+        }
+    }
+    dependencies {
+        classpath "gradle.plugin.org.jamgo:eclipselink-plugin:0.2.2"
     }
 }
 
@@ -25,16 +31,21 @@ plugins {
     id 'com.github.hierynomus.license' version '0.13.1'
     id("org.nosphere.apache.rat") version "0.3.1"
     id "com.jfrog.artifactory" version "4.9.5"
+ //   id "org.jamgo.eclipselink-plugin" version "0.2.2"
 }
 
 group 'org.apache.fineract.cn'
 version '0.1.0-BUILD-SNAPSHOT'
 
 ext.versions = [
-        springcontext        : '4.3.3.RELEASE',
-        springboot           : '1.4.1.RELEASE',
-        findbugs             : '3.0.1',
-        frameworklang        : '0.1.0-BUILD-SNAPSHOT'
+        springcontext: '4.3.3.RELEASE',
+        springboot   : '1.4.1.RELEASE',
+        findbugs     : '3.0.1',
+        frameworklang: '0.1.0-BUILD-SNAPSHOT',
+        bonecp       : '0.8.0.RELEASE',
+        flyway       : '4.0.1',
+        postgresql   : '42.2.5',
+        eclipselink  : '2.7.4',
 ]
 
 apply plugin: 'java'
@@ -42,6 +53,7 @@ apply plugin: 'idea'
 apply plugin: 'maven'
 apply plugin: 'maven-publish'
 apply plugin: 'license'
+apply plugin: "org.jamgo.eclipselink-plugin"
 
 tasks.withType(JavaCompile) {
     sourceCompatibility = JavaVersion.VERSION_1_8
@@ -59,11 +71,16 @@ dependencies {
             [group: 'org.springframework', name: 'spring-context', version: versions.springcontext],
             [group: 'com.google.code.findbugs', name: 'jsr305', version: versions.findbugs],
             [group: 'org.apache.fineract.cn', name: 'lang', version: versions.frameworklang],
-            [group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: versions.springboot],
-            [group: 'com.jolbox', name: 'bonecp', version: '0.8.0.RELEASE'],
-            [group: 'org.flywaydb', name: 'flyway-core', version: '4.0.1'],
-            [group: 'org.postgresql', name: 'postgresql', version: '42.2.5']
+            [group: 'com.jolbox', name: 'bonecp', version: versions.bonecp],
+            [group: 'org.flywaydb', name: 'flyway-core', version: versions.flyway],
+            [group: 'org.postgresql', name: 'postgresql', version: versions.postgresql],
+            [group: 'org.eclipse.persistence', name: 'org.eclipse.persistence.jpa', version: versions.eclipselink]
     )
+    compile(group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: versions.springboot)
+            {
+                exclude group: 'org.hibernate', module: 'hibernate-entitymanager'
+                exclude group: 'org.hibernate', module: 'hibernate-core'
+            }
     testCompile(
             [group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: versions.springboot]
     )
@@ -94,11 +111,11 @@ artifactory {
         }
 
         defaults {
-            publications ('postgresqlPublication')
+            publications('postgresqlPublication')
         }
     }
 }
-artifactoryPublish.dependsOn('clean','publishToMavenLocal')
+artifactoryPublish.dependsOn('clean', 'publishToMavenLocal')
 
 license {
     header rootProject.file('HEADER')
@@ -121,3 +138,4 @@ rat {
     ]
     plainOutput = true
 }
+
diff --git a/src/main/java/org/apache/fineract/cn/postgresql/config/EclipseLinkJpaConfiguration.java b/src/main/java/org/apache/fineract/cn/postgresql/config/EclipseLinkJpaConfiguration.java
new file mode 100644
index 0000000..62ff39f
--- /dev/null
+++ b/src/main/java/org/apache/fineract/cn/postgresql/config/EclipseLinkJpaConfiguration.java
@@ -0,0 +1,96 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.fineract.cn.postgresql.config;
+
+import org.eclipse.persistence.config.BatchWriting;
+import org.eclipse.persistence.config.PersistenceUnitProperties;
+import org.eclipse.persistence.config.TargetDatabase;
+import org.eclipse.persistence.logging.SessionLog;
+import org.springframework.beans.factory.ObjectProvider;
+import org.springframework.boot.autoconfigure.domain.EntityScan;
+import org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration;
+import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
+import org.springframework.orm.jpa.JpaTransactionManager;
+import org.springframework.orm.jpa.vendor.AbstractJpaVendorAdapter;
+import org.springframework.orm.jpa.vendor.Database;
+import org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter;
+import org.springframework.transaction.PlatformTransactionManager;
+import org.springframework.transaction.annotation.EnableTransactionManagement;
+import org.springframework.transaction.jta.JtaTransactionManager;
+
+import javax.persistence.EntityManagerFactory;
+import javax.sql.DataSource;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author Ebenezer Graham
+ */
+
+@Configuration
+@EnableTransactionManagement
+@EntityScan({
+		"org.apache.fineract.cn.**.repository",
+		"org.apache.fineract.cn.postgresql.util"
+})
+public class EclipseLinkJpaConfiguration extends JpaBaseConfiguration {
+	
+	
+	protected EclipseLinkJpaConfiguration(DataSource dataSource, JpaProperties properties, ObjectProvider<JtaTransactionManager> jtaTransactionManagerProvider) {
+		super(dataSource, properties, jtaTransactionManagerProvider);
+	}
+	
+	@Bean
+	public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
+		final JpaTransactionManager transactionManager = new JpaTransactionManager();
+		transactionManager.setEntityManagerFactory(emf);
+		return transactionManager;
+	}
+	
+	@Bean
+	public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
+		return new PersistenceExceptionTranslationPostProcessor();
+	}
+	
+	@Bean
+	protected AbstractJpaVendorAdapter createJpaVendorAdapter() {
+		EclipseLinkJpaVendorAdapter vendorAdapter = new EclipseLinkJpaVendorAdapter();
+		vendorAdapter.setDatabasePlatform("org.eclipse.persistence.platform.database.PostgreSQLPlatform");
+		vendorAdapter.setShowSql(true); // Todo: remove sql log
+		vendorAdapter.setDatabase(Database.POSTGRESQL);
+		vendorAdapter.setGenerateDdl(false);
+		return vendorAdapter;
+	}
+	
+	@Bean
+	protected Map<String, Object> getVendorProperties() {
+		HashMap<String, Object> properties = new HashMap<>();
+		properties.put(PersistenceUnitProperties.WEAVING, "static");
+		properties.put(PersistenceUnitProperties.WEAVING_EAGER, "true");
+		properties.put(PersistenceUnitProperties.TARGET_DATABASE, TargetDatabase.PostgreSQL);
+		properties.put(PersistenceUnitProperties.BATCH_WRITING, BatchWriting.JDBC);
+		properties.put(PersistenceUnitProperties.LOGGING_LEVEL, SessionLog.ALL_LABEL);// Todo: Reduce log level after test
+		properties.put(PersistenceUnitProperties.LOGGING_PARAMETERS, "true");
+		properties.put(PersistenceUnitProperties.DEPLOY_ON_STARTUP, "false");
+		return properties;
+	}
+}
diff --git a/src/main/java/org/apache/fineract/cn/postgresql/config/PostgreSQLJavaConfiguration.java b/src/main/java/org/apache/fineract/cn/postgresql/config/PostgreSQLJavaConfiguration.java
index 09d7a9f..7a92b0a 100644
--- a/src/main/java/org/apache/fineract/cn/postgresql/config/PostgreSQLJavaConfiguration.java
+++ b/src/main/java/org/apache/fineract/cn/postgresql/config/PostgreSQLJavaConfiguration.java
@@ -27,113 +27,75 @@ import org.apache.fineract.cn.postgresql.util.PostgreSQLConstants;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
+import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
+import org.springframework.context.annotation.*;
 import org.springframework.core.env.Environment;
-import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
-import org.springframework.orm.jpa.JpaTransactionManager;
-import org.springframework.orm.jpa.JpaVendorAdapter;
 import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
-import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
-import org.springframework.transaction.PlatformTransactionManager;
-import org.springframework.transaction.annotation.EnableTransactionManagement;
+import org.springframework.orm.jpa.vendor.AbstractJpaVendorAdapter;
 
-import javax.persistence.EntityManagerFactory;
 import javax.sql.DataSource;
 import java.util.Properties;
 
 @SuppressWarnings("WeakerAccess")
 @Configuration
 @ConditionalOnProperty(prefix = "postgresql", name = "enabled", matchIfMissing = true)
-@EnableTransactionManagement
 @EnableApplicationName
+@Import(EclipseLinkJpaConfiguration.class)
 public class PostgreSQLJavaConfiguration {
-
-  private final Environment env;
-
-  @Autowired
-  public PostgreSQLJavaConfiguration(final Environment env) {
-    super();
-    this.env = env;
-  }
-
-  @Bean(name = PostgreSQLConstants.LOGGER_NAME)
-  public Logger logger() {
-    return LoggerFactory.getLogger(PostgreSQLConstants.LOGGER_NAME);
-  }
-
-  @Bean
-  public LocalContainerEntityManagerFactoryBean entityManagerFactory(final DataSource dataSource) {
-    final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
-    em.setPersistenceUnitName("metaPU");
-    em.setDataSource(dataSource);
-    em.setPackagesToScan("org.apache.fineract.cn.**.repository");
-
-    final JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
-    em.setJpaVendorAdapter(vendorAdapter);
-    em.setJpaProperties(additionalProperties());
-
-    return em;
-  }
-
-  @Bean
-  public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
-    final JpaTransactionManager transactionManager = new JpaTransactionManager();
-    transactionManager.setEntityManagerFactory(emf);
-    return transactionManager;
-  }
-
-  @Bean
-  public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
-    return new PersistenceExceptionTranslationPostProcessor();
-  }
-
-  @Bean
-  public FlywayFactoryBean flywayFactoryBean(final ApplicationName applicationName) {
-    return new FlywayFactoryBean(applicationName);
-  }
-
-  @Bean
-  public MetaDataSourceWrapper metaDataSourceWrapper() {
-
-    final BoneCPDataSource boneCPDataSource = new BoneCPDataSource();
-    boneCPDataSource.setDriverClass(
-            this.env.getProperty(PostgreSQLConstants.POSTGRESQL_DRIVER_CLASS_PROP, PostgreSQLConstants.POSTGRESQL_DRIVER_CLASS_DEFAULT));
-    boneCPDataSource.setJdbcUrl(JdbcUrlBuilder
-            .create(JdbcUrlBuilder.DatabaseType.POSTGRESQL)
-            .host(this.env.getProperty(PostgreSQLConstants.POSTGRESQL_HOST_PROP, PostgreSQLConstants.POSTGRESQL_HOST_DEFAULT))
-            .port(this.env.getProperty(PostgreSQLConstants.POSTGRESQL_PORT_PROP, PostgreSQLConstants.POSTGRESQL_PORT_DEFAULT))
-            .instanceName(this.env.getProperty(PostgreSQLConstants.POSTGRESQL_DATABASE_NAME_PROP, PostgreSQLConstants.POSTGRESQL_DATABASE_NAME_DEFAULT))
-            .build());
-    boneCPDataSource.setUsername(
-            this.env.getProperty(PostgreSQLConstants.POSTGRESQL_USER_PROP, PostgreSQLConstants.POSTGRESQL_USER_DEFAULT));
-    boneCPDataSource.setPassword(
-            this.env.getProperty(PostgreSQLConstants.POSTGRESQL_PASSWORD_PROP, PostgreSQLConstants.POSTGRESQL_PASSWORD_DEFAULT));
-    boneCPDataSource.setIdleConnectionTestPeriodInMinutes(
-            Long.valueOf(this.env.getProperty(PostgreSQLConstants.BONECP_IDLE_CONNECTION_TEST_PROP, PostgreSQLConstants.BONECP_IDLE_CONNECTION_TEST_DEFAULT)));
-    boneCPDataSource.setIdleMaxAgeInMinutes(
-            Long.valueOf(this.env.getProperty(PostgreSQLConstants.BONECP_IDLE_MAX_AGE_PROP, PostgreSQLConstants.BONECP_IDLE_MAX_AGE_DEFAULT)));
-    boneCPDataSource.setMaxConnectionsPerPartition(
-            Integer.valueOf(this.env.getProperty(PostgreSQLConstants.BONECP_MAX_CONNECTION_PARTITION_PROP, PostgreSQLConstants.BONECP_MAX_CONNECTION_PARTITION_DEFAULT)));
-    boneCPDataSource.setMinConnectionsPerPartition(
-            Integer.valueOf(this.env.getProperty(PostgreSQLConstants.BONECP_MIN_CONNECTION_PARTITION_PROP, PostgreSQLConstants.BONECP_MIN_CONNECTION_PARTITION_DEFAULT)));
-    boneCPDataSource.setPartitionCount(
-            Integer.valueOf(this.env.getProperty(PostgreSQLConstants.BONECP_PARTITION_COUNT_PROP, PostgreSQLConstants.BONECP_PARTITION_COUNT_DEFAULT)));
-    boneCPDataSource.setAcquireIncrement(
-            Integer.valueOf(this.env.getProperty(PostgreSQLConstants.BONECP_ACQUIRE_INCREMENT_PROP, PostgreSQLConstants.BONECP_ACQUIRE_INCREMENT_DEFAULT)));
-    boneCPDataSource.setStatementsCacheSize(
-            Integer.valueOf(this.env.getProperty(PostgreSQLConstants.BONECP_STATEMENT_CACHE_PROP, PostgreSQLConstants.BONECP_STATEMENT_CACHE_DEFAULT)));
-
-    final Properties driverProperties = new Properties();
-    driverProperties.setProperty("useServerPrepStmts", "false");
-    boneCPDataSource.setDriverProperties(driverProperties);
-    return new MetaDataSourceWrapper(boneCPDataSource);
-  }
-
-  private Properties additionalProperties() {
-    final Properties properties = new Properties();
-    properties.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
-    return properties;
-  }
+	
+	private final Environment env;
+
+	@Autowired
+	protected PostgreSQLJavaConfiguration(Environment env) {
+		super();
+		this.env = env;
+	}
+	
+	@Bean(name = PostgreSQLConstants.LOGGER_NAME)
+	public Logger logger() {
+		return LoggerFactory.getLogger(PostgreSQLConstants.LOGGER_NAME);
+	}
+	
+	@Bean
+	public FlywayFactoryBean flywayFactoryBean(final ApplicationName applicationName) {
+		return new FlywayFactoryBean(applicationName);
+	}
+	
+	@Bean
+	public MetaDataSourceWrapper metaDataSourceWrapper() {
+		final BoneCPDataSource boneCPDataSource = new BoneCPDataSource();
+		boneCPDataSource.setDriverClass(
+				this.env.getProperty(PostgreSQLConstants.POSTGRESQL_DRIVER_CLASS_PROP, PostgreSQLConstants.POSTGRESQL_DRIVER_CLASS_DEFAULT));
+		boneCPDataSource.setJdbcUrl(JdbcUrlBuilder
+				.create(JdbcUrlBuilder.DatabaseType.POSTGRESQL)
+				.host(this.env.getProperty(PostgreSQLConstants.POSTGRESQL_HOST_PROP, PostgreSQLConstants.POSTGRESQL_HOST_DEFAULT))
+				.port(this.env.getProperty(PostgreSQLConstants.POSTGRESQL_PORT_PROP, PostgreSQLConstants.POSTGRESQL_PORT_DEFAULT))
+				.instanceName(this.env.getProperty(PostgreSQLConstants.POSTGRESQL_DATABASE_NAME_PROP, PostgreSQLConstants.POSTGRESQL_DATABASE_NAME_DEFAULT))
+				.build());
+		boneCPDataSource.setUsername(
+				this.env.getProperty(PostgreSQLConstants.POSTGRESQL_USER_PROP, PostgreSQLConstants.POSTGRESQL_USER_DEFAULT));
+		boneCPDataSource.setPassword(
+				this.env.getProperty(PostgreSQLConstants.POSTGRESQL_PASSWORD_PROP, PostgreSQLConstants.POSTGRESQL_PASSWORD_DEFAULT));
+		boneCPDataSource.setIdleConnectionTestPeriodInMinutes(
+				Long.valueOf(this.env.getProperty(PostgreSQLConstants.BONECP_IDLE_CONNECTION_TEST_PROP, PostgreSQLConstants.BONECP_IDLE_CONNECTION_TEST_DEFAULT)));
+		boneCPDataSource.setIdleMaxAgeInMinutes(
+				Long.valueOf(this.env.getProperty(PostgreSQLConstants.BONECP_IDLE_MAX_AGE_PROP, PostgreSQLConstants.BONECP_IDLE_MAX_AGE_DEFAULT)));
+		boneCPDataSource.setMaxConnectionsPerPartition(
+				Integer.valueOf(this.env.getProperty(PostgreSQLConstants.BONECP_MAX_CONNECTION_PARTITION_PROP, PostgreSQLConstants.BONECP_MAX_CONNECTION_PARTITION_DEFAULT)));
+		boneCPDataSource.setMinConnectionsPerPartition(
+				Integer.valueOf(this.env.getProperty(PostgreSQLConstants.BONECP_MIN_CONNECTION_PARTITION_PROP, PostgreSQLConstants.BONECP_MIN_CONNECTION_PARTITION_DEFAULT)));
+		boneCPDataSource.setPartitionCount(
+				Integer.valueOf(this.env.getProperty(PostgreSQLConstants.BONECP_PARTITION_COUNT_PROP, PostgreSQLConstants.BONECP_PARTITION_COUNT_DEFAULT)));
+		boneCPDataSource.setAcquireIncrement(
+				Integer.valueOf(this.env.getProperty(PostgreSQLConstants.BONECP_ACQUIRE_INCREMENT_PROP, PostgreSQLConstants.BONECP_ACQUIRE_INCREMENT_DEFAULT)));
+		boneCPDataSource.setStatementsCacheSize(
+				Integer.valueOf(this.env.getProperty(PostgreSQLConstants.BONECP_STATEMENT_CACHE_PROP, PostgreSQLConstants.BONECP_STATEMENT_CACHE_DEFAULT)));
+		
+		final Properties driverProperties = new Properties();
+		driverProperties.setProperty("useServerPrepStmts", "false");
+		boneCPDataSource.setDriverProperties(driverProperties);
+		return new MetaDataSourceWrapper(boneCPDataSource);
+	}
 }
diff --git a/src/main/java/org/apache/fineract/cn/postgresql/util/PostgreSQLConstants.java b/src/main/java/org/apache/fineract/cn/postgresql/util/PostgreSQLConstants.java
index 824c03a..e1af537 100644
--- a/src/main/java/org/apache/fineract/cn/postgresql/util/PostgreSQLConstants.java
+++ b/src/main/java/org/apache/fineract/cn/postgresql/util/PostgreSQLConstants.java
@@ -26,6 +26,7 @@ public interface PostgreSQLConstants {
   String POSTGRESQL_DRIVER_CLASS_DEFAULT = "org.postgresql.Driver";
   String POSTGRESQL_DATABASE_NAME_PROP = "postgresql.database";
   String POSTGRESQL_DATABASE_NAME_DEFAULT = "seshat";
+  String POSTGRESQL_DATABASE_NAME = "postgres";
   String POSTGRESQL_HOST_PROP = "postgresql.host";
   String POSTGRESQL_HOST_DEFAULT = "localhost";
   String POSTGRESQL_PORT_PROP = "postgresql.port";