You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@polygene.apache.org by pa...@apache.org on 2015/09/22 16:52:29 UTC
zest-java git commit: ZEST-125 Port LayeredApplicationAssembler
enhancements from develop
Repository: zest-java
Updated Branches:
refs/heads/support/2.1 33337d86d -> efcf237c4
ZEST-125 Port LayeredApplicationAssembler enhancements from develop
Extracted from 9304d0086af9036b6ce14d578badae251e211dff
Project: http://git-wip-us.apache.org/repos/asf/zest-java/repo
Commit: http://git-wip-us.apache.org/repos/asf/zest-java/commit/efcf237c
Tree: http://git-wip-us.apache.org/repos/asf/zest-java/tree/efcf237c
Diff: http://git-wip-us.apache.org/repos/asf/zest-java/diff/efcf237c
Branch: refs/heads/support/2.1
Commit: efcf237c41aacb4a9618c7f0a5891e7e48f439a0
Parents: 33337d8
Author: Paul Merlin <pa...@nosphere.org>
Authored: Tue Sep 22 16:48:04 2015 +0200
Committer: Paul Merlin <pa...@nosphere.org>
Committed: Tue Sep 22 16:48:04 2015 +0200
----------------------------------------------------------------------
.../layered/IllegalLayerAssemblerException.java | 31 +++++
.../layered/LayeredApplicationAssembler.java | 137 +++++++++++++++----
.../LayeredApplicationAssemblerTest.java | 1 +
3 files changed, 139 insertions(+), 30 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/zest-java/blob/efcf237c/core/bootstrap/src/main/java/org/qi4j/bootstrap/layered/IllegalLayerAssemblerException.java
----------------------------------------------------------------------
diff --git a/core/bootstrap/src/main/java/org/qi4j/bootstrap/layered/IllegalLayerAssemblerException.java b/core/bootstrap/src/main/java/org/qi4j/bootstrap/layered/IllegalLayerAssemblerException.java
new file mode 100644
index 0000000..8107864
--- /dev/null
+++ b/core/bootstrap/src/main/java/org/qi4j/bootstrap/layered/IllegalLayerAssemblerException.java
@@ -0,0 +1,31 @@
+/*
+ * 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.qi4j.bootstrap.layered;
+
+import org.qi4j.bootstrap.AssemblyException;
+
+public class IllegalLayerAssemblerException extends AssemblyException
+{
+ public IllegalLayerAssemblerException( String message )
+ {
+ super( message );
+ }
+}
http://git-wip-us.apache.org/repos/asf/zest-java/blob/efcf237c/core/bootstrap/src/main/java/org/qi4j/bootstrap/layered/LayeredApplicationAssembler.java
----------------------------------------------------------------------
diff --git a/core/bootstrap/src/main/java/org/qi4j/bootstrap/layered/LayeredApplicationAssembler.java b/core/bootstrap/src/main/java/org/qi4j/bootstrap/layered/LayeredApplicationAssembler.java
index b399711..39d3bcf 100644
--- a/core/bootstrap/src/main/java/org/qi4j/bootstrap/layered/LayeredApplicationAssembler.java
+++ b/core/bootstrap/src/main/java/org/qi4j/bootstrap/layered/LayeredApplicationAssembler.java
@@ -20,6 +20,8 @@ package org.qi4j.bootstrap.layered;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import org.qi4j.api.activation.ActivationException;
@@ -36,13 +38,15 @@ import org.qi4j.bootstrap.LayerAssembly;
public abstract class LayeredApplicationAssembler
implements ApplicationAssembler
{
- protected Application application;
- protected String name;
- protected String version;
+ protected final Energy4Java zest;
+ protected final String name;
+ protected final String version;
+
private final Application.Mode mode;
- private ApplicationAssembly assembly;
+ private final HashMap<Class<? extends LayerAssembler>, LayerAssembler> assemblers = new HashMap<>();
- private HashMap<Class<? extends LayerAssembler>, LayerAssembler> assemblers = new HashMap<>();
+ private ApplicationAssembly assembly;
+ protected Application application;
public LayeredApplicationAssembler( String name, String version, Application.Mode mode )
throws AssemblyException
@@ -50,10 +54,15 @@ public abstract class LayeredApplicationAssembler
this.name = name;
this.version = version;
this.mode = mode;
- Energy4Java qi4j = new Energy4Java();
- ApplicationDescriptor model = qi4j.newApplicationModel( this );
+ zest = new Energy4Java();
+ }
+
+ public void initialize()
+ throws AssemblyException
+ {
+ ApplicationDescriptor model = zest.newApplicationModel( this );
onModelCreated( model );
- instantiateApplication( qi4j, model );
+ instantiateApplication( zest, model );
}
public ApplicationAssembly assembly()
@@ -68,15 +77,15 @@ public abstract class LayeredApplicationAssembler
* The default implementation simply calls;
* </p>
* <pre><code>
- * application = model.newInstance( qi4j.spi() );
+ * application = model.newInstance( zest.spi() );
* </code></pre>
*
- * @param qi4j The Zest runtime engine.
+ * @param zest The Zest runtime engine.
* @param model The application model descriptor.
*/
- protected void instantiateApplication( Energy4Java qi4j, ApplicationDescriptor model )
+ protected void instantiateApplication( Energy4Java zest, ApplicationDescriptor model )
{
- application = model.newInstance( qi4j.spi() );
+ application = model.newInstance( zest.spi() );
}
/**
@@ -89,7 +98,7 @@ public abstract class LayeredApplicationAssembler
* expected to take place.
* </p>
*
- * @param model
+ * @param model The model that has just been created.
*/
protected void onModelCreated( ApplicationDescriptor model )
{
@@ -137,15 +146,10 @@ public abstract class LayeredApplicationAssembler
setNameIfPresent( layerAssemblerClass, classname );
LayerAssembly layer = assembly.layer( classname );
- LayerAssembler layerAssembler = instantiateAssembler( layerAssemblerClass, layer );
+ LayerAssembler layerAssembler = instantiateLayerAssembler( layerAssemblerClass, layer );
assemblers.put( layerAssemblerClass, layerAssembler );
- LayerAssembly assembly = layerAssembler.assemble( layer );
- if( assembly == null )
- {
- // Assume that people forgot, and let's not require a "return layer", since we can do that ourselves.
- return layer;
- }
- return assembly;
+ assembleLayer( layerAssembler, layer );
+ return layer;
}
catch( Exception e )
{
@@ -153,23 +157,96 @@ public abstract class LayeredApplicationAssembler
}
}
- private LayerAssembler instantiateAssembler( Class<? extends LayerAssembler> layerAssemblerClass,
- LayerAssembly layer
+ protected void assembleLayer( LayerAssembler layerAssembler, LayerAssembly layer )
+ throws AssemblyException
+ {
+ layerAssembler.assemble( layer );
+ }
+
+ protected <T extends LayerAssembler> LayerAssembler instantiateLayerAssembler( Class<T> layerAssemblerClass,
+ LayerAssembly layer
+ )
+ throws InstantiationException, IllegalAccessException, InvocationTargetException, IllegalLayerAssemblerException
+ {
+ LayerAssembler assembler = createWithFactoryMethod( layerAssemblerClass, layer );
+ if( assembler != null )
+ {
+ return assembler;
+ }
+ assembler = createWithConstructor( layerAssemblerClass, layer );
+ if( assembler != null )
+ {
+ return assembler;
+ }
+ throw new IllegalLayerAssemblerException( "No matching factory method nor constructor found in " + layerAssemblerClass );
+ }
+
+ private LayerAssembler createWithFactoryMethod( Class<? extends LayerAssembler> layerAssemblerClass,
+ LayerAssembly layer
)
- throws InstantiationException, IllegalAccessException, java.lang.reflect.InvocationTargetException
+ throws InvocationTargetException, IllegalAccessException
{
- LayerAssembler layerAssembler;
try
{
- Constructor<? extends LayerAssembler> assemblyConstructor = layerAssemblerClass.getConstructor( LayerAssembly.class );
- layerAssembler = assemblyConstructor.newInstance( layer );
+ Method factoryMethod = layerAssemblerClass.getDeclaredMethod( "create", LayerAssembly.class );
+ factoryMethod.setAccessible( true );
+ int modifiers = factoryMethod.getModifiers();
+ if( Modifier.isStatic( modifiers ) && LayerAssembler.class.isAssignableFrom( factoryMethod.getReturnType() ) )
+ {
+ return (LayerAssembler) factoryMethod.invoke( null, layer );
+ }
+ }
+ catch( NoSuchMethodException e )
+ {
+ try
+ {
+ Method factoryMethod = layerAssemblerClass.getDeclaredMethod( "create" );
+ factoryMethod.setAccessible( true );
+ int modifiers = factoryMethod.getModifiers();
+ if( Modifier.isStatic( modifiers ) && LayerAssembler.class.isAssignableFrom( factoryMethod.getReturnType() ) )
+ {
+ return (LayerAssembler) factoryMethod.invoke( null );
+ }
+ }
+ catch( NoSuchMethodException e1 )
+ {
+ }
+ }
+ return null;
+ }
+
+ private LayerAssembler createWithConstructor( Class<? extends LayerAssembler> layerAssemblerClass,
+ LayerAssembly assembly
+ )
+ throws IllegalAccessException, InvocationTargetException, InstantiationException
+ {
+ try
+ {
+ Constructor<? extends LayerAssembler> constructor = layerAssemblerClass.getConstructor( LayerAssembly.class );
+ if( constructor != null )
+ {
+ constructor.setAccessible( true );
+ return constructor.newInstance( assembly );
+ }
}
catch( NoSuchMethodException e )
{
- // Use default constructor then.
- layerAssembler = layerAssemblerClass.newInstance();
+ try
+ {
+ Constructor<? extends LayerAssembler> constructor = layerAssemblerClass.getDeclaredConstructor();
+ if( constructor != null )
+ {
+ constructor.setAccessible( true );
+ System.out.println(constructor);
+ return constructor.newInstance();
+ }
+ }
+ catch( NoSuchMethodException e1 )
+ {
+ return null;
+ }
}
- return layerAssembler;
+ return null;
}
static void setNameIfPresent( Class<?> clazz, String classname )
http://git-wip-us.apache.org/repos/asf/zest-java/blob/efcf237c/core/bootstrap/src/test/java/org/qi4j/bootstrap/assembly/LayeredApplicationAssemblerTest.java
----------------------------------------------------------------------
diff --git a/core/bootstrap/src/test/java/org/qi4j/bootstrap/assembly/LayeredApplicationAssemblerTest.java b/core/bootstrap/src/test/java/org/qi4j/bootstrap/assembly/LayeredApplicationAssemblerTest.java
index b1212ae..8cfb178 100644
--- a/core/bootstrap/src/test/java/org/qi4j/bootstrap/assembly/LayeredApplicationAssemblerTest.java
+++ b/core/bootstrap/src/test/java/org/qi4j/bootstrap/assembly/LayeredApplicationAssemblerTest.java
@@ -33,6 +33,7 @@ public class LayeredApplicationAssemblerTest
throws AssemblyException, ActivationException
{
TestApplication assembler = new TestApplication( "Test Application", "1.0.1", Application.Mode.test );
+ assembler.initialize();
assembler.start();
assertThat( assembler.application().name(), equalTo("Test Application") );