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 2017/11/09 17:54:41 UTC

[isis] 08/09: ISIS-1754 context aware class-loading for Isis

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

ahuber pushed a commit to branch dev/2.0.0/ISIS-1767-jee-7
in repository https://gitbox.apache.org/repos/asf/isis.git

commit ab2b0697c7b1104610cb25c22062ba8b6d8c29f2
Author: Andi Huber <ah...@apache.org>
AuthorDate: Mon Oct 30 17:29:14 2017 +0100

    ISIS-1754 context aware class-loading for Isis
---
 .../core/runtime/services/ServiceInstantiator.java |   4 +-
 .../core/runtime/system/context/IsisContext.java   | 149 ++++++++++++---------
 .../IsisComponentProvider.java                     |  26 ++--
 3 files changed, 107 insertions(+), 72 deletions(-)

diff --git a/core/runtime/src/main/java/org/apache/isis/core/runtime/services/ServiceInstantiator.java b/core/runtime/src/main/java/org/apache/isis/core/runtime/services/ServiceInstantiator.java
index 3303118..38ee52f 100644
--- a/core/runtime/src/main/java/org/apache/isis/core/runtime/services/ServiceInstantiator.java
+++ b/core/runtime/src/main/java/org/apache/isis/core/runtime/services/ServiceInstantiator.java
@@ -42,6 +42,7 @@ import org.apache.isis.core.commons.lang.ArrayExtensions;
 import org.apache.isis.core.commons.lang.MethodExtensions;
 import org.apache.isis.core.metamodel.services.ServicesInjector;
 import org.apache.isis.core.metamodel.specloader.classsubstitutor.JavassistEnhanced;
+import org.apache.isis.core.runtime.system.context.IsisContext;
 
 import javassist.util.proxy.MethodFilter;
 import javassist.util.proxy.MethodHandler;
@@ -110,8 +111,7 @@ public final class ServiceInstantiator {
     private Class<?> loadClass(final String className) {
         try {
             LOG.debug("loading class for service: " + className);
-            //return Thread.currentThread().getContextClassLoader().loadClass(className);
-            return Class.forName(className);
+            return IsisContext.getClassLoader().loadClass(className);
         } catch (final ClassNotFoundException ex) {
             throw new InitialisationException(String.format("Cannot find class '%s' for service", className));
         }
diff --git a/core/runtime/src/main/java/org/apache/isis/core/runtime/system/context/IsisContext.java b/core/runtime/src/main/java/org/apache/isis/core/runtime/system/context/IsisContext.java
index dd63f0f..f26b6d9 100644
--- a/core/runtime/src/main/java/org/apache/isis/core/runtime/system/context/IsisContext.java
+++ b/core/runtime/src/main/java/org/apache/isis/core/runtime/system/context/IsisContext.java
@@ -25,74 +25,103 @@ import org.slf4j.LoggerFactory;
 import org.apache.isis.core.commons.exceptions.IsisException;
 import org.apache.isis.core.metamodel.specloader.validator.MetaModelInvalidException;
 import org.apache.isis.core.runtime.system.session.IsisSessionFactoryBuilder;
+import org.apache.log4j.BasicConfigurator;
 import org.apache.isis.core.runtime.system.session.IsisSession;
 import org.apache.isis.core.runtime.system.session.IsisSessionFactory;
 
 /**
- * Simply a static field holding the {@link IsisSessionFactory} singleton, and conveneince methods to obtain the
+ * Simply a static field holding the {@link IsisSessionFactory} singleton, and convenience methods to obtain the
  * current {@link IsisSession}, along with application-scoped components and also any transaction-scoped components.
  */
 public final class IsisContext {
 
-    private static final Logger LOG = LoggerFactory.getLogger(IsisContext.class);
-
-    private IsisContext(){
-        throw new IllegalStateException("Never instantiated");
-    }
-
-    //region > metaModelInvalidExceptionIfAny (static)
-    /**
-     * Populated only if the metamodel was found to be invalid
-     */
-    private static MetaModelInvalidException metamodelInvalidException;
-
-    public static MetaModelInvalidException getMetaModelInvalidExceptionIfAny() {
-        return IsisContext.metamodelInvalidException;
-    }
-    public static void setMetaModelInvalidException(final MetaModelInvalidException metaModelInvalid) {
-        IsisContext.metamodelInvalidException = metaModelInvalid;
-    }
-    //endregion
-
-    //region > sessionFactory (static)
-
-    private static IsisSessionFactory sessionFactory;
-
-    public static IsisSessionFactory getSessionFactory() {
-        return sessionFactory;
-    }
-
-
-    /**
-     * Intended to be called only by {@link IsisSessionFactoryBuilder}.
-     */
-    public static void setSessionFactory(final IsisSessionFactory sessionFactory) {
-        if (IsisContext.sessionFactory != null) {
-            throw new IsisException("SessionFactory already set up");
-        }
-        IsisContext.sessionFactory = sessionFactory;
-    }
-    
-    /**
-     * Resets
-     * @deprecated replaced by {@link #destroy()}
-     * 
-     */
-    @Deprecated
-    public static void testReset() {
-    	destroy();
-    }
-    
-    /**
-     * Destroys this context and clears any state associated with it. 
-     * It marks the end of IsisContext's lifecycle. Subsequent calls have no effect. 
-     */
-    public static void destroy() {
-        sessionFactory = null;
-        metamodelInvalidException = null;
-    }
-
-    //endregion
+	private static final Logger log = LoggerFactory.getLogger(IsisContext.class);
+
+	// -- CONSTRUCTOR
+
+	private IsisContext(){
+		throw new IllegalStateException("Never instantiated");
+	}
+
+	// -- META MODEL VALIDATION
+
+	/**
+	 * Populated only if the metamodel was found to be invalid
+	 */
+	private static MetaModelInvalidException metamodelInvalidException;
+
+	public static MetaModelInvalidException getMetaModelInvalidExceptionIfAny() {
+		return IsisContext.metamodelInvalidException;
+	}
+	public static void setMetaModelInvalidException(final MetaModelInvalidException metaModelInvalid) {
+		IsisContext.metamodelInvalidException = metaModelInvalid;
+	}
+
+	// -- SESSION FACTORY
+
+	private static IsisSessionFactory sessionFactory;
+
+	public static IsisSessionFactory getSessionFactory() {
+		return sessionFactory;
+	}
+
+	/**
+	 * Intended to be called only by {@link IsisSessionFactoryBuilder}.
+	 */
+	public static void setSessionFactory(final IsisSessionFactory sessionFactory) {
+		if (IsisContext.sessionFactory != null) {
+			throw new IsisException("SessionFactory already set up");
+		}
+		IsisContext.sessionFactory = sessionFactory;
+	}
+
+	// -- LIFE CYCLING
+
+	/**
+	 * Resets
+	 * @deprecated replaced by {@link #destroy()}
+	 * 
+	 */
+	@Deprecated
+	public static void testReset() {
+		destroy();
+	}
+
+	/**
+	 * life-cycle exit point
+	 * <p>
+	 * Destroys this context and clears any state associated with it. 
+	 * It marks the end of IsisContext's life-cycle. Subsequent calls have no effect.
+	 * </p> 
+	 */
+	public static void destroy() {
+		sessionFactory = null;
+		metamodelInvalidException = null;
+		log.info("destroyed");
+		resetLogging();
+	}
+
+	// -- CLASS LOADING
+
+	/**
+	 * 
+	 * @return the framework's default class-loader
+	 */
+	public static ClassLoader getClassLoader() {
+		//TODO requires skinny-war testing on glassfish, payara, wildfly, tomee, ...
+		return Thread.currentThread().getContextClassLoader();
+	}
+
+	// -- HELPER
+
+	/**
+	 * TODO [andi-huber] not sure if required, initial idea was to force log4j
+	 * re-configuration on a undeply/deploy cycle
+	 */
+	private static void resetLogging() {
+		org.apache.log4j.Logger.getRootLogger().removeAllAppenders();
+		BasicConfigurator.resetConfiguration();
+	}
 
 
 }
diff --git a/core/runtime/src/main/java/org/apache/isis/core/runtime/systemusinginstallers/IsisComponentProvider.java b/core/runtime/src/main/java/org/apache/isis/core/runtime/systemusinginstallers/IsisComponentProvider.java
index 2557cb2..83d854e 100644
--- a/core/runtime/src/main/java/org/apache/isis/core/runtime/systemusinginstallers/IsisComponentProvider.java
+++ b/core/runtime/src/main/java/org/apache/isis/core/runtime/systemusinginstallers/IsisComponentProvider.java
@@ -30,15 +30,6 @@ import java.util.stream.Stream;
 import javax.annotation.Nullable;
 import javax.jdo.annotations.PersistenceCapable;
 
-import com.google.common.base.Function;
-import com.google.common.base.Joiner;
-import com.google.common.collect.Iterables;
-import com.google.common.collect.Lists;
-import com.google.common.collect.Sets;
-
-import org.reflections.Reflections;
-import org.reflections.vfs.Vfs;
-
 import org.apache.isis.applib.AppManifest;
 import org.apache.isis.applib.annotation.DomainObject;
 import org.apache.isis.applib.annotation.DomainService;
@@ -64,9 +55,20 @@ import org.apache.isis.core.runtime.services.ServicesInstallerFromConfiguration;
 import org.apache.isis.core.runtime.services.ServicesInstallerFromConfigurationAndAnnotation;
 import org.apache.isis.core.runtime.system.IsisSystemException;
 import org.apache.isis.core.runtime.system.SystemConstants;
+import org.apache.isis.core.runtime.system.context.IsisContext;
 import org.apache.isis.objectstore.jdo.service.RegisterEntities;
 import org.apache.isis.progmodels.dflt.JavaReflectorHelper;
 import org.apache.isis.progmodels.dflt.ProgrammingModelFacetsJava5;
+import org.reflections.Configuration;
+import org.reflections.Reflections;
+import org.reflections.util.ConfigurationBuilder;
+import org.reflections.vfs.Vfs;
+
+import com.google.common.base.Function;
+import com.google.common.base.Joiner;
+import com.google.common.collect.Iterables;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Sets;
 
 /**
  * 
@@ -140,7 +142,11 @@ public abstract class IsisComponentProvider {
 
         Vfs.setDefaultURLTypes(ClassDiscoveryServiceUsingReflections.getUrlTypes());
 
-        final Reflections reflections = new Reflections(packages);
+        final Configuration reflectionsConfig = new ConfigurationBuilder()
+        		.addClassLoader(IsisContext.getClassLoader())
+        		.forPackages(packages.toArray(new String[] {})); 
+        final Reflections reflections = new Reflections(reflectionsConfig);
+
         final Set<Class<?>> domainServiceTypes = reflections.getTypesAnnotatedWith(DomainService.class);
         final Set<Class<?>> persistenceCapableTypes = findPersistenceCapableTypes(reflections);
         final Set<Class<? extends FixtureScript>> fixtureScriptTypes = reflections.getSubTypesOf(FixtureScript.class);

-- 
To stop receiving notification emails like this one, please contact
"commits@isis.apache.org" <co...@isis.apache.org>.