You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airavata.apache.org by la...@apache.org on 2016/08/26 15:43:14 UTC

[19/50] [abbrv] airavata git commit: Registry java8 PoC

Registry java8 PoC


Project: http://git-wip-us.apache.org/repos/asf/airavata/repo
Commit: http://git-wip-us.apache.org/repos/asf/airavata/commit/db9e3b76
Tree: http://git-wip-us.apache.org/repos/asf/airavata/tree/db9e3b76
Diff: http://git-wip-us.apache.org/repos/asf/airavata/diff/db9e3b76

Branch: refs/heads/lahiru/AIRAVATA-2057
Commit: db9e3b76ee2694cdffeff7f56fa6ef4b3b375119
Parents: 1ecff76
Author: Shameera Rathnayaka <sh...@gmail.com>
Authored: Thu Aug 11 18:22:15 2016 -0400
Committer: Shameera Rathnayaka <sh...@gmail.com>
Committed: Fri Aug 12 16:34:04 2016 -0400

----------------------------------------------------------------------
 .../airavata/registry/core/Committer.java       |  29 ++++
 .../apache/airavata/registry/core/JPAUtil.java  |  57 +++++++
 .../catalog/resources/ExperimentResource.java   | 148 ++++++-------------
 3 files changed, 134 insertions(+), 100 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/airavata/blob/db9e3b76/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/Committer.java
----------------------------------------------------------------------
diff --git a/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/Committer.java b/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/Committer.java
new file mode 100644
index 0000000..99b504b
--- /dev/null
+++ b/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/Committer.java
@@ -0,0 +1,29 @@
+/*
+ *
+ * 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.airavata.registry.core;
+
+import org.apache.airavata.registry.cpi.RegistryException;
+
+@FunctionalInterface
+public interface Committer<T, R>  {
+
+   R commit(T t) throws RegistryException;
+}

http://git-wip-us.apache.org/repos/asf/airavata/blob/db9e3b76/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/JPAUtil.java
----------------------------------------------------------------------
diff --git a/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/JPAUtil.java b/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/JPAUtil.java
new file mode 100644
index 0000000..58988c6
--- /dev/null
+++ b/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/JPAUtil.java
@@ -0,0 +1,57 @@
+/*
+ *
+ * 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.airavata.registry.core;
+
+import org.apache.airavata.registry.core.experiment.catalog.ExpCatResourceUtils;
+import org.apache.airavata.registry.cpi.ExperimentCatalogException;
+import org.apache.airavata.registry.cpi.RegistryException;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.Persistence;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.function.Consumer;
+
+public class JPAUtil {
+
+    public static <R> R executeOnExpCatalog(Committer<EntityManager, R> committer) throws RegistryException, ExperimentCatalogException {
+        EntityManager entityManager = ExpCatResourceUtils.getEntityManager();
+        try {
+            entityManager.getTransaction().begin();
+            R r = committer.commit(entityManager);
+            entityManager.getTransaction().commit();
+            return  r;
+        }finally {
+            if(entityManager != null && entityManager.isOpen()){
+                if (entityManager.getTransaction().isActive()) {
+                    entityManager.getTransaction().rollback();
+                }
+                entityManager.close();
+            }
+        }
+
+    }
+
+    public static void test() throws ExperimentCatalogException, RegistryException {
+        executeOnExpCatalog((em) -> "");
+    }
+}

http://git-wip-us.apache.org/repos/asf/airavata/blob/db9e3b76/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/experiment/catalog/resources/ExperimentResource.java
----------------------------------------------------------------------
diff --git a/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/experiment/catalog/resources/ExperimentResource.java b/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/experiment/catalog/resources/ExperimentResource.java
index 174575f..7889839 100644
--- a/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/experiment/catalog/resources/ExperimentResource.java
+++ b/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/experiment/catalog/resources/ExperimentResource.java
@@ -22,6 +22,8 @@
 package org.apache.airavata.registry.core.experiment.catalog.resources;
 
 import org.apache.airavata.model.status.ExperimentState;
+import org.apache.airavata.registry.core.Committer;
+import org.apache.airavata.registry.core.JPAUtil;
 import org.apache.airavata.registry.core.experiment.catalog.ExpCatResourceUtils;
 import org.apache.airavata.registry.core.experiment.catalog.ExperimentCatResource;
 import org.apache.airavata.registry.core.experiment.catalog.ResourceType;
@@ -29,6 +31,7 @@ import org.apache.airavata.registry.core.experiment.catalog.model.*;
 import org.apache.airavata.registry.core.experiment.catalog.model.Process;
 import org.apache.airavata.registry.core.experiment.catalog.utils.QueryGenerator;
 import org.apache.airavata.registry.cpi.RegistryException;
+import org.apache.openjpa.persistence.Generator;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -264,120 +267,65 @@ public class ExperimentResource extends AbstractExpCatResource {
 
     
     public ExperimentCatResource get(ResourceType type, Object name) throws RegistryException{
-        EntityManager em = null;
         try {
-            em = ExpCatResourceUtils.getEntityManager();
-            em.getTransaction().begin();
-            QueryGenerator generator;
-            Query q;
             switch (type) {
                 case EXPERIMENT_STATUS:
-                    generator = new QueryGenerator(EXPERIMENT_STATUS);
-                    generator.setParameter(ExperimentStatusConstants.STATUS_ID, name);
-                    q = generator.selectQuery(em);
-                    ExperimentStatus status = (ExperimentStatus) q.getSingleResult();
-                    ExperimentStatusResource statusResource = (ExperimentStatusResource) Utils.getResource(ResourceType.EXPERIMENT_STATUS, status);
-                    em.getTransaction().commit();
-                    if (em.isOpen()) {
-                        if (em.getTransaction().isActive()){
-                            em.getTransaction().rollback();
-                        }
-                        em.close();
-                    }
-                    return statusResource;
+                    return JPAUtil.executeOnExpCatalog(entityManager -> {
+                        QueryGenerator generator = new QueryGenerator(EXPERIMENT_STATUS);
+                        generator.setParameter(ExperimentStatusConstants.STATUS_ID, name);
+                        Query q = generator.selectQuery(entityManager);
+                        ExperimentStatus status = (ExperimentStatus) q.getSingleResult();
+                        return (ExperimentStatusResource) Utils.getResource(ResourceType.EXPERIMENT_STATUS, status);
+                    });
                 case EXPERIMENT_ERROR:
-                    generator = new QueryGenerator(EXPERIMENT_ERROR);
-                    generator.setParameter(ExperimentErrorConstants.ERROR_ID, name);
-                    q = generator.selectQuery(em);
-                    ExperimentError experimentError = (ExperimentError) q.getSingleResult();
-                    ExperimentErrorResource processErrorResource = (ExperimentErrorResource) Utils.getResource(ResourceType.EXPERIMENT_ERROR, experimentError);
-                    em.getTransaction().commit();
-                    if (em.isOpen()) {
-                        if (em.getTransaction().isActive()){
-                            em.getTransaction().rollback();
-                        }
-                        em.close();
-                    }
-                    return processErrorResource;
+                    return JPAUtil.executeOnExpCatalog(entityManager -> {
+                        QueryGenerator generator = new QueryGenerator(EXPERIMENT_ERROR);
+                        generator.setParameter(ExperimentErrorConstants.ERROR_ID, name);
+                        Query q = generator.selectQuery(entityManager);
+                        ExperimentError experimentError = (ExperimentError) q.getSingleResult();
+                        return (ExperimentErrorResource) Utils.getResource(ResourceType.EXPERIMENT_ERROR, experimentError);
+                    });
                 case EXPERIMENT_INPUT:
-                    generator = new QueryGenerator(EXPERIMENT_INPUT);
-                    generator.setParameter(ExperimentInputConstants.INPUT_NAME, name);
-                    generator.setParameter(ExperimentInputConstants.EXPERIMENT_ID, experimentId);
-                    q = generator.selectQuery(em);
-                    ExperimentInput experimentInput = (ExperimentInput) q.getSingleResult();
-                    ExperimentInputResource experimentInputResource = (ExperimentInputResource) Utils.getResource(ResourceType.EXPERIMENT_INPUT, experimentInput);
-                    em.getTransaction().commit();
-                    if (em.isOpen()) {
-                        if (em.getTransaction().isActive()){
-                            em.getTransaction().rollback();
-                        }
-                        em.close();
-                    }
-                    return experimentInputResource;
+                    return JPAUtil.executeOnExpCatalog(entityManager -> {
+                        QueryGenerator generator = new QueryGenerator(EXPERIMENT_INPUT);
+                        generator.setParameter(ExperimentInputConstants.INPUT_NAME, name);
+                        generator.setParameter(ExperimentInputConstants.EXPERIMENT_ID, experimentId);
+                        Query q = generator.selectQuery(entityManager);
+                        ExperimentInput experimentInput = (ExperimentInput) q.getSingleResult();
+                        return (ExperimentInputResource) Utils.getResource(ResourceType.EXPERIMENT_INPUT, experimentInput);
+                    });
                 case EXPERIMENT_OUTPUT:
-                    generator = new QueryGenerator(EXPERIMENT_OUTPUT);
-                    generator.setParameter(ExperimentOutputConstants.OUTPUT_NAME, name);
-                    generator.setParameter(ExperimentInputConstants.EXPERIMENT_ID, experimentId);
-                    q = generator.selectQuery(em);
-                    ExperimentOutput experimentOutput = (ExperimentOutput) q.getSingleResult();
-                    ExperimentOutputResource outputResource = (ExperimentOutputResource) Utils.getResource(ResourceType.EXPERIMENT_OUTPUT, experimentOutput);
-                    em.getTransaction().commit();
-                    if (em.isOpen()) {
-                        if (em.getTransaction().isActive()){
-                            em.getTransaction().rollback();
-                        }
-                        em.close();
-                    }
-                    return outputResource;
+                    return JPAUtil.executeOnExpCatalog(entityManager -> {
+                        QueryGenerator generator = new QueryGenerator(EXPERIMENT_OUTPUT);
+                        generator.setParameter(ExperimentOutputConstants.OUTPUT_NAME, name);
+                        generator.setParameter(ExperimentInputConstants.EXPERIMENT_ID, experimentId);
+                        Query q = generator.selectQuery(entityManager);
+                        ExperimentOutput experimentOutput = (ExperimentOutput) q.getSingleResult();
+                        return (ExperimentOutputResource) Utils.getResource(ResourceType.EXPERIMENT_OUTPUT, experimentOutput);
+                    });
                 case USER_CONFIGURATION_DATA:
-                    generator = new QueryGenerator(USER_CONFIGURATION_DATA);
-                    generator.setParameter(UserConfigurationDataConstants.EXPERIMENT_ID, name);
-                    q = generator.selectQuery(em);
-                    UserConfigurationData configurationData = (UserConfigurationData) q.getSingleResult();
-                    UserConfigurationDataResource configurationDataResource = (UserConfigurationDataResource)
-                            Utils.getResource(ResourceType.USER_CONFIGURATION_DATA, configurationData);
-                    em.getTransaction().commit();
-                    if (em.isOpen()) {
-                        if (em.getTransaction().isActive()){
-                            em.getTransaction().rollback();
-                        }
-                        em.close();
-                    }
-                    return configurationDataResource;
+                    return JPAUtil.executeOnExpCatalog(entityManager -> {
+                        QueryGenerator generator = new QueryGenerator(USER_CONFIGURATION_DATA);
+                        generator.setParameter(UserConfigurationDataConstants.EXPERIMENT_ID, name);
+                        Query q = generator.selectQuery(entityManager);
+                        UserConfigurationData configurationData = (UserConfigurationData) q.getSingleResult();
+                        return (UserConfigurationDataResource)
+                                Utils.getResource(ResourceType.USER_CONFIGURATION_DATA, configurationData);
+                    });
                 case PROCESS:
-                    generator = new QueryGenerator(PROCESS);
-                    generator.setParameter(ProcessConstants.PROCESS_ID, name);
-                    q = generator.selectQuery(em);
-                    Process process = (Process) q.getSingleResult();
-                    ProcessResource processResource = (ProcessResource) Utils.getResource(ResourceType.PROCESS, process);
-                    em.getTransaction().commit();
-                    if (em.isOpen()) {
-                        if (em.getTransaction().isActive()){
-                            em.getTransaction().rollback();
-                        }
-                        em.close();
-                    }
-                    return processResource;
+                    return JPAUtil.executeOnExpCatalog(entityManager -> {
+                        QueryGenerator generator = new QueryGenerator(PROCESS);
+                        generator.setParameter(ProcessConstants.PROCESS_ID, name);
+                        Query q = generator.selectQuery(entityManager);
+                        Process process = (Process) q.getSingleResult();
+                        return (ProcessResource) Utils.getResource(ResourceType.PROCESS, process);
+                    });
                 default:
-                    em.getTransaction().commit();
-                    if (em.isOpen()) {
-                        if (em.getTransaction().isActive()){
-                            em.getTransaction().rollback();
-                        }
-                        em.close();
-                    }
                     logger.error("Unsupported resource type for experiment resource.", new IllegalArgumentException());
                     throw new IllegalArgumentException("Unsupported resource type for experiment resource.");
             }
         } catch (Exception e) {
             throw new RegistryException(e);
-        } finally {
-            if (em != null && em.isOpen()) {
-                if (em.getTransaction().isActive()){
-                    em.getTransaction().rollback();
-                }
-                em.close();
-            }
         }
     }