You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by ah...@apache.org on 2019/10/31 15:35:27 UTC

[isis] 01/02: ISIS-2158: have PersistenceSessionFactory managed by Spring

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

ahuber pushed a commit to branch v2
in repository https://gitbox.apache.org/repos/asf/isis.git

commit 28b7e3a9fb766fc76b5339e1c5de0baae91131ef
Author: Andi Huber <ah...@apache.org>
AuthorDate: Thu Oct 31 15:41:09 2019 +0100

    ISIS-2158: have PersistenceSessionFactory managed by Spring
    
    - allows to remove service loader 'IsisJsoRuntimePlugin'
---
 .../context/_Context_ThreadLocal_Singleton.java    | 99 ----------------------
 .../service/JdoPersistenceLifecycleService.java    | 17 +---
 .../isis/jdo/entities/JdoEntityTypeRegistry.java   |  8 +-
 .../org/apache/isis/jdo/IsisBootDataNucleus.java   |  2 +
 .../isis/jdo/jdosupport/IsisJdoSupportPlugin5.java | 11 +--
 .../persistence/PersistenceSessionFactory5.java    | 21 +++--
 ...e.isis.runtime.persistence.IsisJdoRuntimePlugin |  1 -
 .../runtime/persistence/IsisJdoRuntimePlugin.java  | 44 ----------
 .../persistence/PersistenceSessionFactory.java     | 13 +--
 9 files changed, 17 insertions(+), 199 deletions(-)

diff --git a/core/commons/src/main/java/org/apache/isis/commons/internal/context/_Context_ThreadLocal_Singleton.java b/core/commons/src/main/java/org/apache/isis/commons/internal/context/_Context_ThreadLocal_Singleton.java
deleted file mode 100644
index da0d98e..0000000
--- a/core/commons/src/main/java/org/apache/isis/commons/internal/context/_Context_ThreadLocal_Singleton.java
+++ /dev/null
@@ -1,99 +0,0 @@
-/*
- *  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.isis.commons.internal.context;
-
-import java.util.HashMap;
-import java.util.Map;
-
-import org.apache.isis.commons.internal.base._Casts;
-
-import static org.apache.isis.commons.internal.base._With.requires;
-
-import lombok.Value;
-import lombok.val;
-
-/**
- * <h1>- internal use only -</h1>
- * <p>
- * Package private mixin for _Context. 
- * Provides a context for storing and retrieving thread local object references.
- * </p>
- * <p>
- * <b>WARNING</b>: Do <b>NOT</b> use any of the classes provided by this package! <br/>
- * These may be changed or removed without notice!
- * </p>
- * @since 2.0
- */
-@Deprecated //TODO [2033] superseded by _Context_ThreadLocal 
-final class _Context_ThreadLocal_Singleton {
-
-    // -- MIXINS
-
-    static <T> void put(Class<? super T> type, T payload) {
-        requires(type, "type");
-        requires(payload, "payload");
-        THREAD_LOCAL_MAP.get().put(type, Payload.of(payload, null));
-    }
-
-    static <T> void put(Class<? super T> type, Object payload, Runnable onCleanup) {
-        requires(type, "type");
-        requires(payload, "payload");
-        requires(onCleanup, "onCleanup");
-        THREAD_LOCAL_MAP.get().put(type, Payload.of(payload, onCleanup));
-    }
-
-    static <T> T get(Class<? super T> type) {
-        val payload = THREAD_LOCAL_MAP.get().get(type);
-        if(payload!=null) {
-            return _Casts.uncheckedCast(payload.pojo);
-        }
-        return null;
-    }
-
-    static void cleanupThread() {
-        THREAD_LOCAL_MAP.get().forEach((key, payload)->payload.cleanUp());
-        THREAD_LOCAL_MAP.remove();
-    }
-
-    // -- HELPER
-
-    private _Context_ThreadLocal_Singleton(){}
-
-    @Value(staticConstructor="of")
-    private final static class Payload {
-        final Object pojo;
-        final Runnable onCleanup;
-        void cleanUp() {
-            if(onCleanup!=null) {
-                onCleanup.run();
-            }
-        }
-    }
-
-    /**
-     * Inheritable... allows to have concurrent computations utilizing the ForkJoinPool.
-     */
-    private final static ThreadLocal<Map<Class<?>, Payload>> THREAD_LOCAL_MAP = 
-            InheritableThreadLocal.withInitial(HashMap::new);
-
-
-
-
-}
diff --git a/core/plugins/jdo/common/src/main/java/org/apache/isis/jdo/datanucleus/service/JdoPersistenceLifecycleService.java b/core/plugins/jdo/common/src/main/java/org/apache/isis/jdo/datanucleus/service/JdoPersistenceLifecycleService.java
index dd6a1d8..a868a60 100644
--- a/core/plugins/jdo/common/src/main/java/org/apache/isis/jdo/datanucleus/service/JdoPersistenceLifecycleService.java
+++ b/core/plugins/jdo/common/src/main/java/org/apache/isis/jdo/datanucleus/service/JdoPersistenceLifecycleService.java
@@ -20,9 +20,7 @@ package org.apache.isis.jdo.datanucleus.service;
 
 import javax.annotation.PostConstruct;
 import javax.inject.Inject;
-import javax.inject.Singleton;
 
-import org.springframework.context.annotation.Bean;
 import org.springframework.context.event.EventListener;
 import org.springframework.stereotype.Service;
 
@@ -30,8 +28,6 @@ import org.apache.isis.commons.internal.context._Context;
 import org.apache.isis.commons.internal.exceptions._Exceptions;
 import org.apache.isis.config.registry.IsisBeanTypeRegistry;
 import org.apache.isis.metamodel.MetaModelContext;
-import org.apache.isis.metamodel.specloader.SpecificationLoader;
-import org.apache.isis.runtime.persistence.IsisJdoRuntimePlugin;
 import org.apache.isis.runtime.system.context.session.AppLifecycleEvent;
 import org.apache.isis.runtime.system.context.session.SessionLifecycleEvent;
 import org.apache.isis.runtime.system.persistence.PersistenceSession;
@@ -45,9 +41,7 @@ import lombok.extern.log4j.Log4j2;
 public class JdoPersistenceLifecycleService {
 
     @Inject private MetaModelContext metaModelContext;
-    @Inject private SpecificationLoader specificationLoader;
-    
-    private PersistenceSessionFactory persistenceSessionFactory;
+    @Inject private PersistenceSessionFactory persistenceSessionFactory;
 
     @PostConstruct
     public void postConstr() {
@@ -106,11 +100,6 @@ public class JdoPersistenceLifecycleService {
 
     }
 
-    @Bean @Singleton //XXX note: the resulting singleton is not life-cycle managed by Spring/CDI, nor are InjectionPoints resolved by Spring/CDI
-    public PersistenceSessionFactory producePersistenceSessionFactory() {
-        return persistenceSessionFactory;
-    }
-
     // -- HELPER
 
     private void openSession(IsisSession isisSession) {
@@ -140,13 +129,11 @@ public class JdoPersistenceLifecycleService {
     //	}
 
     private void create() {
-        persistenceSessionFactory = 
-                IsisJdoRuntimePlugin.get().getPersistenceSessionFactory();
         persistenceSessionFactory.init(metaModelContext);
     }
 
     private void init() {
-        persistenceSessionFactory.catalogNamedQueries(specificationLoader);
+        persistenceSessionFactory.catalogNamedQueries();
     }
 
     private void shutdown() {
diff --git a/core/plugins/jdo/common/src/main/java/org/apache/isis/jdo/entities/JdoEntityTypeRegistry.java b/core/plugins/jdo/common/src/main/java/org/apache/isis/jdo/entities/JdoEntityTypeRegistry.java
index e899f73..1e7d2e7 100644
--- a/core/plugins/jdo/common/src/main/java/org/apache/isis/jdo/entities/JdoEntityTypeRegistry.java
+++ b/core/plugins/jdo/common/src/main/java/org/apache/isis/jdo/entities/JdoEntityTypeRegistry.java
@@ -26,17 +26,15 @@ import javax.jdo.annotations.PersistenceCapable;
 
 import org.apache.isis.commons.internal.base._Lazy;
 import org.apache.isis.commons.internal.collections._Lists;
-import org.apache.isis.commons.internal.context._Context;
 import org.apache.isis.config.registry.IsisBeanTypeRegistry;
 import org.apache.isis.metamodel.JdoMetamodelUtil;
 
 import static org.apache.isis.commons.internal.base._NullSafe.stream;
 
-import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
 import lombok.val;
 
-@NoArgsConstructor(access = AccessLevel.PRIVATE)
+@NoArgsConstructor
 public class JdoEntityTypeRegistry {
 
     private final _Lazy<Set<String>> entityTypes = _Lazy.threadSafe(this::findEntityTypes);
@@ -45,10 +43,6 @@ public class JdoEntityTypeRegistry {
         return entityTypes.get();
     }
 
-    public static JdoEntityTypeRegistry current() {
-        return _Context.computeIfAbsent(JdoEntityTypeRegistry.class, JdoEntityTypeRegistry::new);
-    }
-
     // -- HELPER
 
     private Set<String> findEntityTypes() {
diff --git a/core/plugins/jdo/datanucleus-5/src/main/java/org/apache/isis/jdo/IsisBootDataNucleus.java b/core/plugins/jdo/datanucleus-5/src/main/java/org/apache/isis/jdo/IsisBootDataNucleus.java
index 883a59b..dadf67b 100644
--- a/core/plugins/jdo/datanucleus-5/src/main/java/org/apache/isis/jdo/IsisBootDataNucleus.java
+++ b/core/plugins/jdo/datanucleus-5/src/main/java/org/apache/isis/jdo/IsisBootDataNucleus.java
@@ -34,6 +34,7 @@ import org.apache.isis.jdo.jdosupport.mixins.Persistable_datanucleusIdLong;
 import org.apache.isis.jdo.metamodel.JdoProgrammingModelPlugin;
 import org.apache.isis.jdo.metrics.MetricsServiceDefault;
 import org.apache.isis.jdo.persistence.IsisPlatformTransactionManagerForJdo;
+import org.apache.isis.jdo.persistence.PersistenceSessionFactory5;
 
 @Configuration
 @Import({
@@ -43,6 +44,7 @@ import org.apache.isis.jdo.persistence.IsisPlatformTransactionManagerForJdo;
     MetricsServiceDefault.class,
     IsisJdoSupportDN5.class,
     IsisPlatformTransactionManagerForJdo.class,
+    PersistenceSessionFactory5.class
 })
 @ComponentScan(
         basePackageClasses= {
diff --git a/core/plugins/jdo/datanucleus-5/src/main/java/org/apache/isis/jdo/jdosupport/IsisJdoSupportPlugin5.java b/core/plugins/jdo/datanucleus-5/src/main/java/org/apache/isis/jdo/jdosupport/IsisJdoSupportPlugin5.java
index 6a83f90..7eb21b0 100644
--- a/core/plugins/jdo/datanucleus-5/src/main/java/org/apache/isis/jdo/jdosupport/IsisJdoSupportPlugin5.java
+++ b/core/plugins/jdo/datanucleus-5/src/main/java/org/apache/isis/jdo/jdosupport/IsisJdoSupportPlugin5.java
@@ -22,12 +22,9 @@ import java.lang.reflect.Method;
 
 import javax.annotation.Nullable;
 
-import org.apache.isis.jdo.persistence.PersistenceSessionFactory5;
 import org.apache.isis.metamodel.IsisJdoMetamodelPlugin;
-import org.apache.isis.runtime.persistence.IsisJdoRuntimePlugin;
-import org.apache.isis.runtime.system.persistence.PersistenceSessionFactory;
 
-public class IsisJdoSupportPlugin5 implements IsisJdoMetamodelPlugin, IsisJdoRuntimePlugin {
+public class IsisJdoSupportPlugin5 implements IsisJdoMetamodelPlugin {
 
     @Override
     public boolean isPersistenceEnhanced(@Nullable Class<?> cls) {
@@ -41,10 +38,4 @@ public class IsisJdoSupportPlugin5 implements IsisJdoMetamodelPlugin, IsisJdoRun
     public Method[] getMethodsProvidedByEnhancement() {
         return org.datanucleus.enhancement.Persistable.class.getDeclaredMethods();
     }
-
-    @Override
-    public PersistenceSessionFactory getPersistenceSessionFactory() {
-        return new PersistenceSessionFactory5();
-    }
-
 }
diff --git a/core/plugins/jdo/datanucleus-5/src/main/java/org/apache/isis/jdo/persistence/PersistenceSessionFactory5.java b/core/plugins/jdo/datanucleus-5/src/main/java/org/apache/isis/jdo/persistence/PersistenceSessionFactory5.java
index db74d19..c58a867 100644
--- a/core/plugins/jdo/datanucleus-5/src/main/java/org/apache/isis/jdo/persistence/PersistenceSessionFactory5.java
+++ b/core/plugins/jdo/datanucleus-5/src/main/java/org/apache/isis/jdo/persistence/PersistenceSessionFactory5.java
@@ -22,22 +22,21 @@ package org.apache.isis.jdo.persistence;
 import java.util.Map;
 import java.util.Objects;
 
-import javax.enterprise.inject.Vetoed;
+import javax.inject.Singleton;
 import javax.jdo.listener.StoreLifecycleListener;
 
 import org.datanucleus.PropertyNames;
 import org.datanucleus.api.jdo.JDOPersistenceManagerFactory;
+import org.springframework.stereotype.Service;
 
 import org.apache.isis.commons.internal.base._Blackhole;
 import org.apache.isis.commons.internal.base._Lazy;
-import org.apache.isis.commons.internal.components.ApplicationScopedComponent;
 import org.apache.isis.config.IsisConfiguration;
 import org.apache.isis.jdo.datanucleus.DataNucleusSettings;
 import org.apache.isis.jdo.datanucleus.JDOStateManagerForIsis;
 import org.apache.isis.jdo.entities.JdoEntityTypeRegistry;
 import org.apache.isis.jdo.lifecycles.JdoStoreLifecycleListenerForIsis;
 import org.apache.isis.metamodel.MetaModelContext;
-import org.apache.isis.metamodel.specloader.SpecificationLoader;
 import org.apache.isis.runtime.persistence.FixturesInstalledState;
 import org.apache.isis.runtime.persistence.FixturesInstalledStateHolder;
 import org.apache.isis.runtime.system.persistence.PersistenceSession;
@@ -54,9 +53,9 @@ import lombok.extern.log4j.Log4j2;
  * Factory for {@link PersistenceSession}.
  *
  */
-@Vetoed @Log4j2
+@Service @Singleton @Log4j2
 public class PersistenceSessionFactory5
-implements PersistenceSessionFactory, ApplicationScopedComponent, FixturesInstalledStateHolder {
+implements PersistenceSessionFactory, FixturesInstalledStateHolder {
 
     private final _Lazy<DataNucleusApplicationComponents5> applicationComponents = 
             _Lazy.threadSafe(this::createDataNucleusApplicationComponents);
@@ -64,6 +63,7 @@ implements PersistenceSessionFactory, ApplicationScopedComponent, FixturesInstal
     private StoreLifecycleListener storeLifecycleListener;
     private MetaModelContext metaModelContext;
     private IsisConfiguration configuration;
+    private final JdoEntityTypeRegistry jdoEntityTypeRegistry = new JdoEntityTypeRegistry();
 
     @Getter(onMethod=@__({@Override})) 
     @Setter(onMethod=@__({@Override})) 
@@ -95,9 +95,7 @@ implements PersistenceSessionFactory, ApplicationScopedComponent, FixturesInstal
         
         addDataNucleusPropertiesIfRequired(datanucleusProps);
         
-        System.out.println("############## " + datanucleusProps);
-
-        val classesToBePersisted = JdoEntityTypeRegistry.current().getEntityTypes();
+        val classesToBePersisted = jdoEntityTypeRegistry.getEntityTypes();
 
         return new DataNucleusApplicationComponents5(
                 configuration,
@@ -106,9 +104,10 @@ implements PersistenceSessionFactory, ApplicationScopedComponent, FixturesInstal
     }
 
     @Override
-    public void catalogNamedQueries(final SpecificationLoader specificationLoader) {
-        val classesToBePersisted = JdoEntityTypeRegistry.current().getEntityTypes();
-        DataNucleusApplicationComponents5.catalogNamedQueries(classesToBePersisted, specificationLoader);
+    public void catalogNamedQueries() {
+        val classesToBePersisted = jdoEntityTypeRegistry.getEntityTypes();
+        DataNucleusApplicationComponents5.catalogNamedQueries(classesToBePersisted, 
+                metaModelContext.getSpecificationLoader());
     }
 
     private static void addDataNucleusPropertiesIfRequired(Map<String, String> props) {
diff --git a/core/plugins/jdo/datanucleus-5/src/main/resources/META-INF/services/org.apache.isis.runtime.persistence.IsisJdoRuntimePlugin b/core/plugins/jdo/datanucleus-5/src/main/resources/META-INF/services/org.apache.isis.runtime.persistence.IsisJdoRuntimePlugin
deleted file mode 100644
index 4a6e9b3..0000000
--- a/core/plugins/jdo/datanucleus-5/src/main/resources/META-INF/services/org.apache.isis.runtime.persistence.IsisJdoRuntimePlugin
+++ /dev/null
@@ -1 +0,0 @@
-org.apache.isis.jdo.jdosupport.IsisJdoSupportPlugin5
\ No newline at end of file
diff --git a/core/runtime/src/main/java/org/apache/isis/runtime/persistence/IsisJdoRuntimePlugin.java b/core/runtime/src/main/java/org/apache/isis/runtime/persistence/IsisJdoRuntimePlugin.java
deleted file mode 100644
index c6af371..0000000
--- a/core/runtime/src/main/java/org/apache/isis/runtime/persistence/IsisJdoRuntimePlugin.java
+++ /dev/null
@@ -1,44 +0,0 @@
-/*
- *  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.isis.runtime.persistence;
-
-import org.apache.isis.commons.internal.context._Plugin;
-import org.apache.isis.runtime.system.persistence.PersistenceSessionFactory;
-
-public interface IsisJdoRuntimePlugin {
-
-    // -- INTERFACE
-
-    public PersistenceSessionFactory getPersistenceSessionFactory();
-
-    // -- LOOKUP
-
-    public static IsisJdoRuntimePlugin get() {
-        return _Plugin.getOrElse(IsisJdoRuntimePlugin.class,
-                ambiguousPlugins->{
-                    throw _Plugin.ambiguityNonRecoverable(IsisJdoRuntimePlugin.class, ambiguousPlugins);
-                },
-                ()->{
-                    throw _Plugin.absenceNonRecoverable(IsisJdoRuntimePlugin.class);
-                });
-    }
-
-
-
-}
diff --git a/core/runtime/src/main/java/org/apache/isis/runtime/system/persistence/PersistenceSessionFactory.java b/core/runtime/src/main/java/org/apache/isis/runtime/system/persistence/PersistenceSessionFactory.java
index 85a41b9..043ca1f 100644
--- a/core/runtime/src/main/java/org/apache/isis/runtime/system/persistence/PersistenceSessionFactory.java
+++ b/core/runtime/src/main/java/org/apache/isis/runtime/system/persistence/PersistenceSessionFactory.java
@@ -19,29 +19,18 @@
 package org.apache.isis.runtime.system.persistence;
 
 import org.apache.isis.metamodel.MetaModelContext;
-import org.apache.isis.metamodel.specloader.SpecificationLoader;
-import org.apache.isis.runtime.persistence.IsisJdoRuntimePlugin;
 import org.apache.isis.security.authentication.AuthenticationSession;
 
 public interface PersistenceSessionFactory {
 
-    // -- INTERFACE
-
     PersistenceSession createPersistenceSession(AuthenticationSession authenticationSession);
 
     void init(MetaModelContext metaModelContext);
 
-    void catalogNamedQueries(final SpecificationLoader specificationLoader);
+    void catalogNamedQueries();
 
     boolean isInitialized();
 
     void shutdown();
 
-    // -- FACTORY
-
-    static PersistenceSessionFactory get() {
-        return IsisJdoRuntimePlugin.get().getPersistenceSessionFactory();
-    }
-
-
 }