You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airavata.apache.org by ma...@apache.org on 2020/04/23 00:29:43 UTC

[airavata] branch AIRAVATA-3126--Implement-compute-resource-reservation-addition created (now 6f35091)

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

machristie pushed a change to branch AIRAVATA-3126--Implement-compute-resource-reservation-addition
in repository https://gitbox.apache.org/repos/asf/airavata.git.


      at 6f35091  WIP

This branch includes the following new commits:

     new 6f35091  WIP

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[airavata] 01/01: WIP

Posted by ma...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

machristie pushed a commit to branch AIRAVATA-3126--Implement-compute-resource-reservation-addition
in repository https://gitbox.apache.org/repos/asf/airavata.git

commit 6f3509156e91ae2a389cb2143493dd27eac4d2b8
Author: Marcus Christie <ma...@apache.org>
AuthorDate: Fri Apr 17 11:01:40 2020 -0400

    WIP
---
 .../org/apache/airavata/common/utils/JPAUtils.java | 153 +++++++++++++++++++--
 .../core/utils/JPAUtil/AppCatalogJPAUtils.java     |  29 +---
 2 files changed, 140 insertions(+), 42 deletions(-)

diff --git a/modules/commons/src/main/java/org/apache/airavata/common/utils/JPAUtils.java b/modules/commons/src/main/java/org/apache/airavata/common/utils/JPAUtils.java
index 64395ff..03b5583 100644
--- a/modules/commons/src/main/java/org/apache/airavata/common/utils/JPAUtils.java
+++ b/modules/commons/src/main/java/org/apache/airavata/common/utils/JPAUtils.java
@@ -3,12 +3,24 @@ package org.apache.airavata.common.utils;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.function.Supplier;
 
+import javax.persistence.Cache;
+// import javax.persistence.EntityGraph;
+import javax.persistence.EntityManager;
 import javax.persistence.EntityManagerFactory;
 import javax.persistence.Persistence;
+import javax.persistence.PersistenceUnitUtil;
+// import javax.persistence.Query;
+// import javax.persistence.SynchronizationType;
+import javax.persistence.criteria.CriteriaBuilder;
+import javax.persistence.metamodel.Metamodel;
 
 import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
 import org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl;
+import org.apache.openjpa.jdbc.meta.MappingTool;
+import org.apache.openjpa.lib.util.Options;
+import org.apache.openjpa.persistence.ArgumentException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -17,6 +29,122 @@ import org.slf4j.LoggerFactory;
  */
 public class JPAUtils {
 
+    private static class EntityManagerFactoryWrapper implements EntityManagerFactory {
+
+        private final EntityManagerFactory factory;
+        private final JDBCConfig jdbcConfig;
+
+        EntityManagerFactoryWrapper(EntityManagerFactory factory, JDBCConfig jdbcConfig) {
+            this.factory = factory;
+            this.jdbcConfig = jdbcConfig;
+        }
+
+        @Override
+        public EntityManager createEntityManager() {
+            return wrapCreateEntityManager(() -> this.factory.createEntityManager());
+        }
+
+        @Override
+        public EntityManager createEntityManager(Map map) {
+            return wrapCreateEntityManager(() -> this.factory.createEntityManager(map));
+        }
+
+        // @Override
+        // public EntityManager createEntityManager(SynchronizationType synchronizationType) {
+        //     return wrapCreateEntityManager(() -> this.factory.createEntityManager(synchronizationType));
+        // }
+
+        // @Override
+        // public EntityManager createEntityManager(SynchronizationType synchronizationType, Map map) {
+        //     return wrapCreateEntityManager(() -> this.factory.createEntityManager(synchronizationType, map));
+        // }
+
+        private EntityManager wrapCreateEntityManager(Supplier<EntityManager> entityManagerSupplier) {
+
+            try {
+                return entityManagerSupplier.get();
+            } catch (ArgumentException e) {
+
+                Map<String, String> finalProperties = new HashMap<>(JPAUtils.DEFAULT_ENTITY_MANAGER_FACTORY_PROPERTIES);
+                finalProperties.putAll(JPAUtils.createConnectionProperties(this.jdbcConfig));
+                JDBCConfiguration jdbcConfiguration = new JDBCConfigurationImpl();
+                jdbcConfiguration.fromProperties(finalProperties);
+
+                Options options = new Options();
+                options.put("sqlFile", "migration.sql");
+                // If you want to generate the entire schema instead of just what is
+                // needed to bring the database up to date, use schemaAction=build
+                // options.put("schemaAction", "build");
+                options.put("foreignKeys", "true");
+                options.put("indexes", "true");
+                options.put("primaryKeys", "true");
+                try {
+                    MappingTool.run(jdbcConfiguration, new String[] {}, options, null);
+                } catch (Exception mappingToolEx) {
+                    logger.error("Failed to run MappingTool", mappingToolEx);
+                    throw new RuntimeException(
+                            "Failed to get EntityManager, then failed to run MappingTool to generate migration script",
+                            e);
+                }
+                throw new RuntimeException("Failed to get EntityManager, but successfully executed "
+                        + "MappingTool to generate migration script (to file named "
+                        + "migration.sql) in case the error was caused by the database "
+                        + "schema being out of date with the mappings", e);
+            }
+        }
+
+        @Override
+        public CriteriaBuilder getCriteriaBuilder() {
+            return this.factory.getCriteriaBuilder();
+        }
+
+        @Override
+        public Metamodel getMetamodel() {
+            return this.factory.getMetamodel();
+        }
+
+        @Override
+        public boolean isOpen() {
+            return this.factory.isOpen();
+        }
+
+        @Override
+        public void close() {
+            this.factory.close();
+        }
+
+        @Override
+        public Map<String, Object> getProperties() {
+            return this.factory.getProperties();
+        }
+
+        @Override
+        public Cache getCache() {
+            return this.factory.getCache();
+        }
+
+        @Override
+        public PersistenceUnitUtil getPersistenceUnitUtil() {
+            return this.factory.getPersistenceUnitUtil();
+        }
+
+        // @Override
+        // public void addNamedQuery(String name, Query query) {
+        //     this.factory.addNamedQuery(name, query);
+        // }
+
+        // @Override
+        // public <T> T unwrap(Class<T> cls) {
+        //     return this.factory.unwrap(cls);
+        // }
+
+        // @Override
+        // public <T> void addNamedEntityGraph(String graphName, EntityGraph<T> entityGraph) {
+        //     this.factory.addNamedEntityGraph(graphName, entityGraph);
+        // }
+
+    }
+
     private final static Logger logger = LoggerFactory.getLogger(JPAUtils.class);
     private final static Map<String, String> DEFAULT_ENTITY_MANAGER_FACTORY_PROPERTIES;
     static {
@@ -27,7 +155,8 @@ public class JPAUtils {
         properties.put("openjpa.RemoteCommitProvider", "sjvm");
         properties.put("openjpa.Log", "DefaultLevel=INFO, Runtime=INFO, Tool=INFO, SQL=INFO");
         // use the following to enable logging of all SQL statements
-        // properties.put("openjpa.Log", "DefaultLevel=INFO, Runtime=INFO, Tool=INFO, SQL=TRACE");
+        // properties.put("openjpa.Log", "DefaultLevel=INFO, Runtime=INFO, Tool=INFO,
+        // SQL=TRACE");
         properties.put("openjpa.jdbc.SynchronizeMappings", "validate");
         properties.put("openjpa.jdbc.QuerySQLCache", "false");
         properties.put("openjpa.DetachState", "all");
@@ -38,37 +167,33 @@ public class JPAUtils {
 
     /**
      * Create an {@link EntityManagerFactory} with the default settings.
+     * 
      * @param persistenceUnitName
      * @param jdbcConfig
      * @return {@link EntityManagerFactory}
      */
-    public static EntityManagerFactory getEntityManagerFactory(String persistenceUnitName, JDBCConfig jdbcConfig){
+    public static EntityManagerFactory getEntityManagerFactory(String persistenceUnitName, JDBCConfig jdbcConfig) {
 
         return getEntityManagerFactory(persistenceUnitName, jdbcConfig, Collections.emptyMap());
     }
 
     /**
-     * Create an {@link EntityManagerFactory}. The given properties will override the default properties.
+     * Create an {@link EntityManagerFactory}. The given properties will override
+     * the default properties.
+     * 
      * @param persistenceUnitName
      * @param jdbcConfig
      * @param properties
      * @return {@link EntityManagerFactory}
      */
-    public static EntityManagerFactory getEntityManagerFactory(String persistenceUnitName, JDBCConfig jdbcConfig, Map<String, String> properties) {
+    public static EntityManagerFactory getEntityManagerFactory(String persistenceUnitName, JDBCConfig jdbcConfig,
+            Map<String, String> properties) {
 
         Map<String, String> finalProperties = new HashMap<>(DEFAULT_ENTITY_MANAGER_FACTORY_PROPERTIES);
         finalProperties.putAll(createConnectionProperties(jdbcConfig));
         finalProperties.putAll(properties);
-        return Persistence.createEntityManagerFactory(persistenceUnitName, finalProperties);
-    }
-
-    public static JDBCConfiguration getJDBCConfiguration(JDBCConfig jdbcConfig) {
-
-        Map<String, String> finalProperties = new HashMap<>(DEFAULT_ENTITY_MANAGER_FACTORY_PROPERTIES);
-        finalProperties.putAll(createConnectionProperties(jdbcConfig));
-        JDBCConfiguration jdbcConfiguration = new JDBCConfigurationImpl();
-        jdbcConfiguration.fromProperties(finalProperties);
-        return jdbcConfiguration;
+        return new EntityManagerFactoryWrapper(
+                Persistence.createEntityManagerFactory(persistenceUnitName, finalProperties), jdbcConfig);
     }
 
     private static Map<String, String> createConnectionProperties(JDBCConfig jdbcConfig) {
diff --git a/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/utils/JPAUtil/AppCatalogJPAUtils.java b/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/utils/JPAUtil/AppCatalogJPAUtils.java
index 77ea71f..236b095 100644
--- a/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/utils/JPAUtil/AppCatalogJPAUtils.java
+++ b/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/utils/JPAUtil/AppCatalogJPAUtils.java
@@ -25,12 +25,6 @@ import javax.persistence.EntityManagerFactory;
 import org.apache.airavata.common.utils.JDBCConfig;
 import org.apache.airavata.common.utils.JPAUtils;
 import org.apache.airavata.registry.core.utils.AppCatalogJDBCConfig;
-import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
-import org.apache.openjpa.jdbc.meta.MappingTool;
-import org.apache.openjpa.lib.util.Options;
-import org.apache.openjpa.persistence.ArgumentException;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 public class AppCatalogJPAUtils {
 
@@ -40,29 +34,8 @@ public class AppCatalogJPAUtils {
     private static final JDBCConfig JDBC_CONFIG = new AppCatalogJDBCConfig();
     private static final EntityManagerFactory factory = JPAUtils.getEntityManagerFactory(PERSISTENCE_UNIT_NAME,
             JDBC_CONFIG);
-    private static final Logger logger = LoggerFactory.getLogger(AppCatalogJPAUtils.class);
 
     public static EntityManager getEntityManager() {
-        try {
-            return factory.createEntityManager();
-        } catch (ArgumentException e) {
-            // TODO: refactor this
-            JDBCConfiguration jdbcConfiguration = JPAUtils.getJDBCConfiguration(JDBC_CONFIG);
-            Options options = new Options();
-            options.put("sqlFile", "migration.sql");
-            // If you want to generate the entire schema instead of just what is
-            // needed to bring the database up to date, use schemaAction=build
-            // options.put("schemaAction", "build");
-            options.put("foreignKeys", "true");
-            options.put("indexes", "true");
-            options.put("primaryKeys", "true");
-            try {
-                MappingTool.run(jdbcConfiguration, new String[] {}, options, null);
-            } catch (Exception mappingToolEx) {
-                logger.error("Failed to run MappingTool", mappingToolEx);
-                throw new RuntimeException("Failed to get EntityManager, then failed to run MappingTool to generate migration script", e);
-            }
-            throw new RuntimeException("Failed to get EntityManager, but successfully executed MappingTool to generate migration script (to file named migration.sql) in case the error was caused by the database schema being out of date with the mappings", e);
-        }
+        return factory.createEntityManager();
     }
 }