polygene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From paulmer...@apache.org
Subject polygene-java git commit: Move default IdentityGenerator & Serialization from all modules to a support layer
Date Mon, 22 May 2017 15:34:14 GMT
Repository: polygene-java
Updated Branches:
  refs/heads/pm/bootstrap/support-layer [created] 8f5a5b254


Move default IdentityGenerator & Serialization from all modules to a support layer


Project: http://git-wip-us.apache.org/repos/asf/polygene-java/repo
Commit: http://git-wip-us.apache.org/repos/asf/polygene-java/commit/8f5a5b25
Tree: http://git-wip-us.apache.org/repos/asf/polygene-java/tree/8f5a5b25
Diff: http://git-wip-us.apache.org/repos/asf/polygene-java/diff/8f5a5b25

Branch: refs/heads/pm/bootstrap/support-layer
Commit: 8f5a5b2543af91dd720fb3904081a01928d617f0
Parents: c7f705b
Author: Paul Merlin <paulmerlin@apache.org>
Authored: Mon May 22 17:33:23 2017 +0200
Committer: Paul Merlin <paulmerlin@apache.org>
Committed: Mon May 22 17:33:23 2017 +0200

----------------------------------------------------------------------
 .../api/activation/ActivationEventsTest.java    |  9 ++
 .../activation/PassivationExceptionTest.java    |  8 ++
 .../DefaultIdentityGeneratorAssembler.java      |  7 +-
 .../DefaultSerializationAssembler.java          |  7 +-
 .../bootstrap/ApplicationModelFactoryImpl.java  | 96 ++++++++++++++++++++
 .../runtime/bootstrap/ModuleAssemblyImpl.java   |  8 +-
 .../tools/model/VisitableDetailTest.java        |  4 +
 7 files changed, 126 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/polygene-java/blob/8f5a5b25/core/api/src/test/java/org/apache/polygene/api/activation/ActivationEventsTest.java
----------------------------------------------------------------------
diff --git a/core/api/src/test/java/org/apache/polygene/api/activation/ActivationEventsTest.java
b/core/api/src/test/java/org/apache/polygene/api/activation/ActivationEventsTest.java
index b5c09f7..b867190 100644
--- a/core/api/src/test/java/org/apache/polygene/api/activation/ActivationEventsTest.java
+++ b/core/api/src/test/java/org/apache/polygene/api/activation/ActivationEventsTest.java
@@ -22,6 +22,7 @@ package org.apache.polygene.api.activation;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
+import org.apache.polygene.api.structure.Layer;
 import org.junit.Test;
 import org.apache.polygene.api.activation.ActivationEvent.EventType;
 import org.apache.polygene.api.mixin.Mixins;
@@ -259,6 +260,14 @@ public class ActivationEventsTest
         @Override
         public void onEvent( ActivationEvent event )
         {
+            // Ignore polygene-support layer events
+            if( ( event.source() instanceof Layer
+                  && ( (Layer) event.source() ).name().equals( "polygene-support"
) )
+                || ( event.source() instanceof Module
+                     && ( (Module) event.source() ).layer().name().equals( "polygene-support"
) ) )
+            {
+                return;
+            }
             events.add( event );
         }
 

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/8f5a5b25/core/api/src/test/java/org/apache/polygene/api/activation/PassivationExceptionTest.java
----------------------------------------------------------------------
diff --git a/core/api/src/test/java/org/apache/polygene/api/activation/PassivationExceptionTest.java
b/core/api/src/test/java/org/apache/polygene/api/activation/PassivationExceptionTest.java
index ba7bca9..ba79fdb 100644
--- a/core/api/src/test/java/org/apache/polygene/api/activation/PassivationExceptionTest.java
+++ b/core/api/src/test/java/org/apache/polygene/api/activation/PassivationExceptionTest.java
@@ -201,6 +201,14 @@ public class PassivationExceptionTest
             {
                 return;
             }
+            // Ignore polygene-support layer events
+            if( ( event.source() instanceof Layer
+                  && ( (Layer) event.source() ).name().equals( "polygene-support"
) )
+                || ( event.source() instanceof Module
+                     && ( (Module) event.source() ).layer().name().equals( "polygene-support"
) ) )
+            {
+                return;
+            }
             switch( event.type() )
             {
                 case PASSIVATING:

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/8f5a5b25/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/identity/DefaultIdentityGeneratorAssembler.java
----------------------------------------------------------------------
diff --git a/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/identity/DefaultIdentityGeneratorAssembler.java
b/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/identity/DefaultIdentityGeneratorAssembler.java
index 923f3cd..6edf964 100644
--- a/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/identity/DefaultIdentityGeneratorAssembler.java
+++ b/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/identity/DefaultIdentityGeneratorAssembler.java
@@ -21,11 +21,11 @@ package org.apache.polygene.bootstrap.identity;
 
 import org.apache.polygene.api.identity.IdentityGenerator;
 import org.apache.polygene.api.identity.UuidGeneratorMixin;
-import org.apache.polygene.bootstrap.Assembler;
+import org.apache.polygene.bootstrap.Assemblers;
 import org.apache.polygene.bootstrap.ModuleAssembly;
 
 public class DefaultIdentityGeneratorAssembler
-    implements Assembler
+    extends Assemblers.Visibility<DefaultIdentityGeneratorAssembler>
 {
     public static final String IDENTITY = "default-identity-generator";
 
@@ -34,6 +34,7 @@ public class DefaultIdentityGeneratorAssembler
     {
         module.services( IdentityGenerator.class )
               .withMixins( UuidGeneratorMixin.class )
-              .identifiedBy( IDENTITY );
+              .identifiedBy( IDENTITY )
+              .visibleIn( visibility() );
     }
 }

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/8f5a5b25/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/serialization/DefaultSerializationAssembler.java
----------------------------------------------------------------------
diff --git a/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/serialization/DefaultSerializationAssembler.java
b/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/serialization/DefaultSerializationAssembler.java
index 048764d..9a38517 100644
--- a/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/serialization/DefaultSerializationAssembler.java
+++ b/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/serialization/DefaultSerializationAssembler.java
@@ -21,7 +21,7 @@ import org.apache.polygene.api.serialization.Converters;
 import org.apache.polygene.api.serialization.Deserializer;
 import org.apache.polygene.api.serialization.Serialization;
 import org.apache.polygene.api.serialization.Serializer;
-import org.apache.polygene.bootstrap.Assembler;
+import org.apache.polygene.bootstrap.Assemblers;
 import org.apache.polygene.bootstrap.ModuleAssembly;
 import org.apache.polygene.serialization.javaxjson.JavaxJsonAdapters;
 import org.apache.polygene.serialization.javaxjson.JavaxJsonFactories;
@@ -31,7 +31,7 @@ import org.apache.polygene.spi.serialization.JsonSerialization;
 import org.apache.polygene.spi.serialization.JsonSerializer;
 
 public class DefaultSerializationAssembler
-    implements Assembler
+    extends Assemblers.Visibility<DefaultSerializationAssembler>
 {
     public static final String IDENTITY = "default-serialization";
 
@@ -47,6 +47,7 @@ public class DefaultSerializationAssembler
                           JavaxJsonAdapters.class,
                           JavaxJsonFactories.class )
               .identifiedBy( IDENTITY )
-              .taggedWith( Serialization.Format.JSON );
+              .taggedWith( Serialization.Format.JSON )
+              .visibleIn( visibility() );
     }
 }

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/8f5a5b25/core/runtime/src/main/java/org/apache/polygene/runtime/bootstrap/ApplicationModelFactoryImpl.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/polygene/runtime/bootstrap/ApplicationModelFactoryImpl.java
b/core/runtime/src/main/java/org/apache/polygene/runtime/bootstrap/ApplicationModelFactoryImpl.java
index d2c788c..b990ca7 100644
--- a/core/runtime/src/main/java/org/apache/polygene/runtime/bootstrap/ApplicationModelFactoryImpl.java
+++ b/core/runtime/src/main/java/org/apache/polygene/runtime/bootstrap/ApplicationModelFactoryImpl.java
@@ -25,15 +25,20 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import org.apache.polygene.api.common.Visibility;
 import org.apache.polygene.api.composite.ModelDescriptor;
+import org.apache.polygene.api.identity.IdentityGenerator;
 import org.apache.polygene.api.structure.ApplicationDescriptor;
 import org.apache.polygene.api.structure.Layer;
 import org.apache.polygene.api.util.HierarchicalVisitor;
 import org.apache.polygene.bootstrap.ApplicationAssembly;
 import org.apache.polygene.bootstrap.ApplicationModelFactory;
+import org.apache.polygene.bootstrap.Assembler;
 import org.apache.polygene.bootstrap.AssemblyException;
 import org.apache.polygene.bootstrap.BindingException;
 import org.apache.polygene.bootstrap.LayerAssembly;
+import org.apache.polygene.bootstrap.identity.DefaultIdentityGeneratorAssembler;
+import org.apache.polygene.bootstrap.serialization.DefaultSerializationAssembler;
 import org.apache.polygene.runtime.activation.ActivatorsModel;
 import org.apache.polygene.runtime.composite.CompositeMethodModel;
 import org.apache.polygene.runtime.injection.InjectedFieldModel;
@@ -43,6 +48,11 @@ import org.apache.polygene.runtime.structure.ApplicationModel;
 import org.apache.polygene.runtime.structure.LayerModel;
 import org.apache.polygene.runtime.structure.ModuleModel;
 import org.apache.polygene.runtime.structure.UsedLayersModel;
+import org.apache.polygene.spi.serialization.JsonSerialization;
+
+import static java.util.Collections.emptyList;
+import static java.util.stream.Collectors.toList;
+import static java.util.stream.StreamSupport.stream;
 
 /**
  * Factory for Applications.
@@ -50,6 +60,20 @@ import org.apache.polygene.runtime.structure.UsedLayersModel;
 public final class ApplicationModelFactoryImpl
     implements ApplicationModelFactory
 {
+    public static final String SUPPORT_LAYER_NAME = "polygene-support";
+    public static final String SPI_DEFAULTS_MODULE_NAME = "spi-defaults";
+
+    private static final HashMap<Class, Assembler> SPI_DEFAULTS_ASSEMBLERS;
+
+    static
+    {
+        SPI_DEFAULTS_ASSEMBLERS = new HashMap<>( 2 );
+        SPI_DEFAULTS_ASSEMBLERS.put( IdentityGenerator.class,
+                                     new DefaultIdentityGeneratorAssembler().visibleIn( Visibility.application
) );
+        SPI_DEFAULTS_ASSEMBLERS.put( JsonSerialization.class,
+                                     new DefaultSerializationAssembler().visibleIn( Visibility.application
) );
+    }
+
     @Override
     public ApplicationDescriptor newApplicationModel( ApplicationAssembly assembly )
     {
@@ -62,6 +86,11 @@ public final class ApplicationModelFactoryImpl
 
         buildAllLayers( helper, maps, layerAssemblies, layerModels );
         populateUsedLayerModels( maps, layerAssemblies );
+        assembleSupportLayer( helper, maps, applicationAssembly, layerModels );
+
+        assert layerModels.size() == maps.mapAssemblyModel.size();
+        assert layerModels.size() == maps.mapModelAssembly.size();
+        assert layerModels.size() == maps.mapUsedLayers.size();
 
         ApplicationModel applicationModel = buildApplicationModel( applicationAssembly, layerModels
);
         bindApplicationModel( applicationModel );
@@ -123,6 +152,68 @@ public final class ApplicationModelFactoryImpl
         }
     }
 
+    private void assembleSupportLayer( AssemblyHelper helper, AssemblyMaps maps,
+                                       ApplicationAssemblyImpl applicationAssembly, List<LayerModel>
layerModels )
+    {
+        Map<Class, List<LayerModel>> spiDefaultsTargetLayers = findSpiDefaultsTargetLayers(
layerModels );
+        if( !spiDefaultsTargetLayers.isEmpty() )
+        {
+            // Assemble layer
+            LayerAssemblyImpl runtimeLayerAssembly = (LayerAssemblyImpl) applicationAssembly
+                .layer( SUPPORT_LAYER_NAME );
+            List<ModuleModel> runtimeModuleModels = new ArrayList<>();
+            LayerModel runtimeLayerModel = new LayerModel(
+                runtimeLayerAssembly.name(),
+                runtimeLayerAssembly.metaInfo(),
+                new UsedLayersModel( emptyList() ),
+                new ActivatorsModel<>( runtimeLayerAssembly.activators() ),
+                runtimeModuleModels );
+
+            // Assemble module
+            ModuleAssemblyImpl spiDefaultsAssembly = (ModuleAssemblyImpl) runtimeLayerAssembly
+                .module( SPI_DEFAULTS_MODULE_NAME );
+            spiDefaultsTargetLayers
+                .entrySet().stream()
+                .map( entry -> SPI_DEFAULTS_ASSEMBLERS.get( entry.getKey() ) )
+                .forEach( spiDefaultAssembler -> spiDefaultAssembler.assemble( spiDefaultsAssembly
) );
+            runtimeModuleModels.add( spiDefaultsAssembly.assembleModule( runtimeLayerModel,
helper ) );
+
+            layerModels.add( runtimeLayerModel );
+            maps.addModel( runtimeLayerAssembly, runtimeLayerModel );
+
+            // Wire layer uses
+            spiDefaultsTargetLayers
+                .values().stream()
+                .flatMap( List::stream )
+                .map( maps::usedLayersOf )
+                .forEach( usedLayers -> usedLayers.add( runtimeLayerModel ) );
+        }
+    }
+
+    private Map<Class, List<LayerModel>> findSpiDefaultsTargetLayers( List<LayerModel>
layerModels )
+    {
+        Map<Class, List<LayerModel>> spiTargetLayers = new HashMap<>( SPI_DEFAULTS_ASSEMBLERS.size()
);
+        for( Class spiServiceClass : SPI_DEFAULTS_ASSEMBLERS.keySet() )
+        {
+            List<LayerModel> targetLayers = layerModels
+                .stream()
+                // their modules
+                .flatMap( layerModel -> stream( layerModel.modules().spliterator(), false
) )
+                // where no service of the spi service type is visible
+                .filter( moduleModel -> moduleModel.findVisibleServiceTypes()
+                                                   .noneMatch( descriptor -> descriptor.hasType(
spiServiceClass ) ) )
+                // their layers
+                .map( ModuleModel::layer ).map( LayerModel.class::cast )
+                .distinct()
+                .collect( toList() );
+            if( !targetLayers.isEmpty() )
+            {
+                spiTargetLayers.put( spiServiceClass, targetLayers );
+            }
+        }
+        return spiTargetLayers;
+    }
+
 
     private ApplicationModel buildApplicationModel( ApplicationAssemblyImpl applicationAssembly,
                                                     List<LayerModel> layerModels )
@@ -179,6 +270,11 @@ public final class ApplicationModelFactoryImpl
             }
             return mapUsedLayers.get( assembly );
         }
+
+        List<LayerModel> usedLayersOf( LayerModel model )
+        {
+            return usedLayersOf( assemblyOf( model ) );
+        }
     }
 
     private static class BindingVisitor

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/8f5a5b25/core/runtime/src/main/java/org/apache/polygene/runtime/bootstrap/ModuleAssemblyImpl.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/polygene/runtime/bootstrap/ModuleAssemblyImpl.java
b/core/runtime/src/main/java/org/apache/polygene/runtime/bootstrap/ModuleAssemblyImpl.java
index abd76e1..24ecb7f 100644
--- a/core/runtime/src/main/java/org/apache/polygene/runtime/bootstrap/ModuleAssemblyImpl.java
+++ b/core/runtime/src/main/java/org/apache/polygene/runtime/bootstrap/ModuleAssemblyImpl.java
@@ -38,9 +38,7 @@ import org.apache.polygene.api.composite.TransientComposite;
 import org.apache.polygene.api.entity.EntityComposite;
 import org.apache.polygene.api.identity.HasIdentity;
 import org.apache.polygene.api.identity.Identity;
-import org.apache.polygene.api.identity.IdentityGenerator;
 import org.apache.polygene.api.identity.StringIdentity;
-import org.apache.polygene.api.serialization.Serialization;
 import org.apache.polygene.api.service.DuplicateServiceIdentityException;
 import org.apache.polygene.api.structure.Module;
 import org.apache.polygene.api.type.HasEqualOrAssignableFromType;
@@ -69,8 +67,6 @@ import org.apache.polygene.bootstrap.TransientAssembly;
 import org.apache.polygene.bootstrap.TransientDeclaration;
 import org.apache.polygene.bootstrap.ValueAssembly;
 import org.apache.polygene.bootstrap.ValueDeclaration;
-import org.apache.polygene.bootstrap.identity.DefaultIdentityGeneratorAssembler;
-import org.apache.polygene.bootstrap.serialization.DefaultSerializationAssembler;
 import org.apache.polygene.bootstrap.unitofwork.DefaultUnitOfWorkAssembler;
 import org.apache.polygene.runtime.activation.ActivatorsModel;
 import org.apache.polygene.runtime.composite.TransientModel;
@@ -121,10 +117,8 @@ final class ModuleAssemblyImpl
 
     static
     {
-        DEFAULT_ASSEMBLERS = new HashMap<>( 3 );
+        DEFAULT_ASSEMBLERS = new HashMap<>( 1 );
         DEFAULT_ASSEMBLERS.put( UnitOfWorkFactory.class, new DefaultUnitOfWorkAssembler()
);
-        DEFAULT_ASSEMBLERS.put( IdentityGenerator.class, new DefaultIdentityGeneratorAssembler()
);
-        DEFAULT_ASSEMBLERS.put( Serialization.class, new DefaultSerializationAssembler()
);
     }
 
     ModuleAssemblyImpl(LayerAssembly layerAssembly, String name)

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/8f5a5b25/tools/model-detail/src/test/java/org/apache/polygene/tools/model/VisitableDetailTest.java
----------------------------------------------------------------------
diff --git a/tools/model-detail/src/test/java/org/apache/polygene/tools/model/VisitableDetailTest.java
b/tools/model-detail/src/test/java/org/apache/polygene/tools/model/VisitableDetailTest.java
index d6af619..0981b9e 100644
--- a/tools/model-detail/src/test/java/org/apache/polygene/tools/model/VisitableDetailTest.java
+++ b/tools/model-detail/src/test/java/org/apache/polygene/tools/model/VisitableDetailTest.java
@@ -85,6 +85,10 @@ public class VisitableDetailTest
                     // Leaving Structure
                     "visitLeave( ModuleName )",
                     "visitLeave( LayerName )",
+                    "visitEnter( polygene-support )",
+                    "visitEnter( spi-defaults )",
+                    "visitLeave( spi-defaults )",
+                    "visitLeave( polygene-support )",
                     "visitLeave( UnderTestApp )"
                 )
             )


Mime
View raw message