http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/ValueAssembly.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/ValueAssembly.java b/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/ValueAssembly.java new file mode 100644 index 0000000..83352d0 --- /dev/null +++ b/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/ValueAssembly.java @@ -0,0 +1,30 @@ +/* + * 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.polygene.bootstrap; + +import org.apache.polygene.api.type.HasTypes; + +/** + * This represents the assembly information of a single ValueComposite in a Module. + */ +public interface ValueAssembly + extends HasTypes +{ +}
http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/ValueDeclaration.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/ValueDeclaration.java b/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/ValueDeclaration.java new file mode 100644 index 0000000..415335d --- /dev/null +++ b/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/ValueDeclaration.java @@ -0,0 +1,41 @@ +/* + * 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.polygene.bootstrap; + +import org.apache.polygene.api.common.Visibility; + +/** + * Fluent API for declaring values + */ +public interface ValueDeclaration +{ + ValueDeclaration setMetaInfo( Object info ); + + ValueDeclaration visibleIn( Visibility visibility ); + + ValueDeclaration withConcerns( Class<?>... concerns ); + + ValueDeclaration withSideEffects( Class<?>... sideEffects ); + + ValueDeclaration withMixins( Class<?>... mixins ); + + ValueDeclaration withTypes( Class<?>... roles ); +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/ZestRuntime.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/ZestRuntime.java b/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/ZestRuntime.java new file mode 100644 index 0000000..c5c5386 --- /dev/null +++ b/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/ZestRuntime.java @@ -0,0 +1,38 @@ +/* + * 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.polygene.bootstrap; + +import org.apache.polygene.api.PolygeneAPI; +import org.apache.polygene.spi.PolygeneSPI; + +/** + * This interface has to be implemented by Polygene runtimes. + */ +public interface PolygeneRuntime +{ + ApplicationAssemblyFactory applicationAssemblyFactory(); + + ApplicationModelFactory applicationModelFactory(); + + PolygeneAPI api(); + + PolygeneSPI spi(); +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/builder/ApplicationBuilder.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/builder/ApplicationBuilder.java b/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/builder/ApplicationBuilder.java new file mode 100644 index 0000000..313be1e --- /dev/null +++ b/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/builder/ApplicationBuilder.java @@ -0,0 +1,303 @@ +/* + * 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.polygene.bootstrap.builder; + +import java.io.InputStream; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Scanner; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; +import org.apache.polygene.api.activation.ActivationEventListener; +import org.apache.polygene.api.activation.ActivationEventListenerRegistration; +import org.apache.polygene.api.activation.ActivationException; +import org.apache.polygene.api.activation.ApplicationPassivationThread; +import org.apache.polygene.api.structure.Application; +import org.apache.polygene.api.structure.ApplicationDescriptor; +import org.apache.polygene.bootstrap.ApplicationAssembler; +import org.apache.polygene.bootstrap.ApplicationAssembly; +import org.apache.polygene.bootstrap.ApplicationAssemblyFactory; +import org.apache.polygene.bootstrap.AssemblyException; +import org.apache.polygene.bootstrap.Energy4Java; +import org.apache.polygene.bootstrap.LayerAssembly; + +/** + * Application Builder. + */ +public class ApplicationBuilder + implements ActivationEventListenerRegistration +{ + private final String applicationName; + private String applicationVersion; + private Application.Mode applicationMode; + private final List<Object> metaInfos = new ArrayList<>(); + private boolean passivationShutdownHook; + private final Map<String, LayerDeclaration> layers = new HashMap<>(); + private final List<ActivationEventListener> activationListeners = new ArrayList<>(); + + public ApplicationBuilder( String applicationName ) + { + this.applicationName = applicationName; + } + + public ApplicationBuilder version( String version ) + { + applicationVersion = version; + return this; + } + + public ApplicationBuilder mode( Application.Mode mode ) + { + applicationMode = mode; + return this; + } + + public ApplicationBuilder metaInfo( Object... metaInfos ) + { + for( Object metaInfo : metaInfos ) + { + this.metaInfos.add( metaInfo ); + } + return this; + } + + /** + * Register a JVM shutdown hook that passivate the Application. + * + * The hook is registered after activating the Application and before {@link #afterActivation()}. + * + * @return This builder + */ + public ApplicationBuilder withPassivationShutdownHook() + { + this.passivationShutdownHook = true; + return this; + } + + /** + * Create and activate a new Application. + * @return Activated Application + * @throws AssemblyException if the assembly failed + * @throws ActivationException if the activation failed + */ + public Application newApplication() + throws AssemblyException, ActivationException + { + Energy4Java zest = new Energy4Java(); + ApplicationDescriptor model = zest.newApplicationModel( new ApplicationAssembler() + { + @Override + public ApplicationAssembly assemble( ApplicationAssemblyFactory factory ) + throws AssemblyException + { + ApplicationAssembly assembly = factory.newApplicationAssembly(); + assembly.setName( applicationName ); + if( applicationVersion != null ) + { + assembly.setVersion( applicationVersion ); + } + if( applicationMode != null ) + { + assembly.setMode( applicationMode ); + } + for( Object metaInfo : metaInfos ) + { + assembly.setMetaInfo( metaInfo ); + } + HashMap<String, LayerAssembly> createdLayers = new HashMap<>(); + for( Map.Entry<String, LayerDeclaration> entry : layers.entrySet() ) + { + LayerAssembly layer = entry.getValue().createLayer( assembly ); + createdLayers.put( entry.getKey(), layer ); + } + for( LayerDeclaration layer : layers.values() ) + { + layer.initialize( createdLayers ); + } + return assembly; + } + } ); + Application application = model.newInstance( zest.api() ); + for( ActivationEventListener activationListener : activationListeners ) + { + application.registerActivationEventListener( activationListener ); + } + beforeActivation(); + application.activate(); + if( passivationShutdownHook ) + { + Runtime.getRuntime().addShutdownHook( new ApplicationPassivationThread( application ) ); + } + afterActivation(); + return application; + } + + /** + * Called before application activation. + */ + protected void beforeActivation() + { + } + + /** + * Called after application activation. + */ + protected void afterActivation() + { + } + + @Override + public void registerActivationEventListener( ActivationEventListener listener ) + { + activationListeners.add( listener ); + } + + @Override + public void deregisterActivationEventListener( ActivationEventListener listener ) + { + activationListeners.remove( listener ); + } + + /** + * Declare Layer. + * @param layerName Name of the Layer + * @return Layer declaration for the given name, new if did not already exists + */ + public LayerDeclaration withLayer( String layerName ) + { + LayerDeclaration layerDeclaration = layers.get( layerName ); + if( layerDeclaration != null ) + { + return layerDeclaration; + } + layerDeclaration = new LayerDeclaration( layerName ); + layers.put( layerName, layerDeclaration ); + return layerDeclaration; + } + + /** + * Load an ApplicationBuilder from a JSON String. + * @param json JSON String + * @return Application Builder loaded from JSON + * @throws JSONException if unable to read JSON + * @throws AssemblyException if unable to declare the assembly + */ + public static ApplicationBuilder fromJson( String json ) + throws JSONException, AssemblyException + { + JSONObject root = new JSONObject( json ); + return fromJson( root ); + } + + /** + * Load an ApplicationBuilder from a JSON InputStream. + * @param json JSON input + * @return Application Builder loaded from JSON + * @throws JSONException if unable to read JSON + * @throws AssemblyException if unable to declare the assembly + */ + public static ApplicationBuilder fromJson( InputStream json ) + throws JSONException, AssemblyException + { + String jsonString = new Scanner( json, "UTF-8" ).useDelimiter( "\\A" ).next(); + return fromJson( jsonString ); + } + + /** + * Load an ApplicationBuilder from a JSONObject. + * @param root JSON object + * @return Application Builder loaded from JSON + * @throws JSONException if unable to read JSON + * @throws AssemblyException if unable to declare the assembly + */ + public static ApplicationBuilder fromJson( JSONObject root ) + throws JSONException, AssemblyException + { + String applicationName = root.getString( "name" ); + ApplicationBuilder builder = new ApplicationBuilder( applicationName ); + builder.configureWithJson( root ); + return builder; + } + + /** Configures the application struucture from a JSON document. + * + * @param root The JSON document root. + * @throws JSONException if the JSON document isn't valid. + * @throws AssemblyException if probelms in the Assemblers provided in the JSON document. + */ + protected void configureWithJson( JSONObject root ) + throws JSONException, AssemblyException + { + JSONArray layers = root.optJSONArray( "layers" ); + if( layers != null ) + { + for( int i = 0; i < layers.length(); i++ ) + { + JSONObject layerObject = layers.getJSONObject( i ); + String layerName = layerObject.getString( "name" ); + LayerDeclaration layerDeclaration = withLayer( layerName ); + JSONArray using = layerObject.optJSONArray( "uses" ); + if( using != null ) + { + for( int j = 0; j < using.length(); j++ ) + { + layerDeclaration.using( using.getString( j ) ); + } + } + JSONArray modules = layerObject.optJSONArray( "modules" ); + if( modules != null ) + { + for( int k = 0; k < modules.length(); k++ ) + { + JSONObject moduleObject = modules.getJSONObject( k ); + String moduleName = moduleObject.getString( "name" ); + ModuleDeclaration moduleDeclaration = layerDeclaration.withModule( moduleName ); + JSONArray assemblers = moduleObject.optJSONArray( "assemblers" ); + if( assemblers != null ) + { + for( int m = 0; m < assemblers.length(); m++ ) + { + String string = assemblers.getString( m ); + moduleDeclaration.withAssembler( string ); + } + } + } + } + } + } + } + + /** + * {@literal main} method that read JSON from STDIN. + * <p>Passivation exceptions are written to STDERR if any.</p> + * @param args Unused + * @throws JSONException if unable to read JSON + * @throws AssemblyException if the assembly failed + * @throws ActivationException if the activation failed + */ + public static void main( String[] args ) + throws JSONException, ActivationException, AssemblyException + { + fromJson( System.in ).withPassivationShutdownHook().newApplication(); + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/builder/LayerDeclaration.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/builder/LayerDeclaration.java b/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/builder/LayerDeclaration.java new file mode 100644 index 0000000..2bc14f0 --- /dev/null +++ b/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/builder/LayerDeclaration.java @@ -0,0 +1,111 @@ +/* + * 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.polygene.bootstrap.builder; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.StreamSupport; +import org.apache.polygene.bootstrap.ApplicationAssembly; +import org.apache.polygene.bootstrap.AssemblyException; +import org.apache.polygene.bootstrap.LayerAssembly; +import org.apache.polygene.bootstrap.ModuleAssembly; + +/** + * Provides declared {@link org.apache.polygene.api.structure.Layer} information that the {@link ApplicationBuilder} can use. + */ +public class LayerDeclaration +{ + private final String layerName; + private final List<String> using = new ArrayList<>(); + private final Map<String, ModuleDeclaration> modules = new HashMap<>(); + private LayerAssembly layer; + + LayerDeclaration( String layerName ) + { + this.layerName = layerName; + } + + /** + * Declare using layer. + * @param layerName Used layer name + * @return This Layer declaration + */ + public LayerDeclaration using( String layerName ) + { + this.using.add( layerName ); + return this; + } + + /** + * Declare using layers. + * @param layerNames Used layers names + * @return This Layer declaration + */ + public LayerDeclaration using( Iterable<String> layerNames ) + { + StreamSupport.stream( layerNames.spliterator(), false ) + .forEach( using::add ); + return this; + } + + /** + * Declare Module. + * @param moduleName Name of the Module + * @return Module declaration for the given name, new if did not already exists + */ + public ModuleDeclaration withModule( String moduleName ) + { + ModuleDeclaration module = modules.get( moduleName ); + if( module != null ) + { + return module; + } + module = new ModuleDeclaration( moduleName ); + modules.put( moduleName, module ); + return module; + } + + LayerAssembly createLayer( ApplicationAssembly application ) + { + layer = application.layer( layerName ); + layer.setName( layerName ); + for( ModuleDeclaration module : modules.values() ) + { + ModuleAssembly assembly = module.createModule( layer ); + } + return layer; + } + + void initialize( HashMap<String, LayerAssembly> createdLayers ) + throws AssemblyException + { + for( String uses : using ) + { + LayerAssembly usedLayer = createdLayers.get( uses ); + layer.uses( usedLayer ); + } + for( ModuleDeclaration module : modules.values() ) + { + module.initialize(); + } + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/builder/ModuleDeclaration.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/builder/ModuleDeclaration.java b/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/builder/ModuleDeclaration.java new file mode 100644 index 0000000..fb20c26 --- /dev/null +++ b/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/builder/ModuleDeclaration.java @@ -0,0 +1,168 @@ +/* + * 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.polygene.bootstrap.builder; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.StreamSupport; +import org.apache.polygene.bootstrap.Assembler; +import org.apache.polygene.bootstrap.AssemblyException; +import org.apache.polygene.bootstrap.LayerAssembly; +import org.apache.polygene.bootstrap.ModuleAssembly; + +import static java.util.stream.Collectors.toList; +import static org.apache.polygene.api.util.Classes.isAssignableFrom; + +/** + * Provides declared {@link org.apache.polygene.api.structure.Module} information that the {@link ApplicationBuilder} can use. + */ +public class ModuleDeclaration +{ + private final String moduleName; + private final List<Assembler> assemblers = new ArrayList<>(); + private ModuleAssembly module; + + public ModuleDeclaration( String moduleName ) + { + this.moduleName = moduleName; + } + + /** + * Declare Assembler. + * @param assembler Assembler instance + * @return This Module declaration + */ + public ModuleDeclaration withAssembler( Assembler assembler ) + { + assemblers.add( assembler ); + return this; + } + + /** + * Declare Assembler. + * @param classname Assembler class name + * @return This Module declaration + * @throws AssemblyException if unable to load class, not an Assembler or unable to instanciate + */ + public ModuleDeclaration withAssembler( String classname ) + throws AssemblyException + { + Class<? extends Assembler> clazz = loadClass( classname ); + return withAssembler( clazz ); + } + + /** + * Declare Assembler. + * @param assemblerClass Assembler class + * @return This Module declaration + * @throws AssemblyException not an Assembler or if unable to instanciate + */ + public ModuleDeclaration withAssembler( Class<?> assemblerClass ) + throws AssemblyException + { + Assembler assembler = createAssemblerInstance( assemblerClass ); + assemblers.add( assembler ); + return this; + } + + /** + * Declare Assemblers. + * <p>Declare several Assemblers from an Iterable of Class objects.</p> + * <p>Typically used along {@link org.apache.polygene.bootstrap.ClassScanner}.</p> + * @param assemblerClasses Assembler classes + * @return This Module declaration + * @throws AssemblyException if one of the Class is not an Assembler or unable to instantiate + */ + public ModuleDeclaration withAssemblers( Iterable<Class<?>> assemblerClasses ) + throws AssemblyException + { + List<Class<?>> notAssemblers = StreamSupport.stream( assemblerClasses.spliterator(), false ) + .filter( isAssignableFrom( Assembler.class ).negate() ) + .collect( toList() ); + if( !notAssemblers.isEmpty() ) + { + throw new AssemblyException( + "Classes " + notAssemblers + " are not implementing " + Assembler.class.getName() + ); + } + for( Class<?> assemblerClass : assemblerClasses ) + { + withAssembler( assemblerClass ); + } + return this; + } + + ModuleAssembly createModule( LayerAssembly layer ) + { + module = layer.module( moduleName ); + return module; + } + + void initialize() + throws AssemblyException + { + for( Assembler assembler : assemblers ) + { + assembler.assemble( module ); + } + } + + @SuppressWarnings( "unchecked" ) + private Class<? extends Assembler> loadClass( String classname ) + throws AssemblyException + { + Class<?> clazz; + try + { + clazz = getClass().getClassLoader().loadClass( classname ); + } + catch( Exception e ) + { + throw new AssemblyException( "Unable to load class " + classname, e ); + } + if( !Assembler.class.isAssignableFrom( clazz ) ) + { + throw new AssemblyException( + "Class " + classname + " is not implementing " + Assembler.class.getName() + ); + } + //noinspection unchecked + return (Class<? extends Assembler>) clazz; + } + + private Assembler createAssemblerInstance( Class<?> assemblerClass ) + throws AssemblyException + { + if( !Assembler.class.isAssignableFrom( assemblerClass ) ) + { + throw new AssemblyException( + "Class " + assemblerClass + " is not implementing " + Assembler.class.getName() + ); + } + try + { + return (Assembler) assemblerClass.newInstance(); + } + catch( Exception e ) + { + throw new AssemblyException( "Unable to instantiate " + assemblerClass, e ); + } + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/builder/package.html ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/builder/package.html b/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/builder/package.html new file mode 100644 index 0000000..6c1ff92 --- /dev/null +++ b/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/builder/package.html @@ -0,0 +1,28 @@ +<!-- + ~ 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. + ~ + ~ + --> +<html> + <body> + <h2>Application Builder.</h2> + <p> + Generic ApplicationBuilder, which simplifies the creation of Application Structure, either by a fluid DSL + or from a JSON file. + </p> + </body> +</html> http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/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 new file mode 100644 index 0000000..e50ec45 --- /dev/null +++ b/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/identity/DefaultIdentityGeneratorAssembler.java @@ -0,0 +1,38 @@ +/* + * 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.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.AssemblyException; +import org.apache.polygene.bootstrap.ModuleAssembly; + +public class DefaultIdentityGeneratorAssembler + implements Assembler +{ + + @Override + public void assemble(ModuleAssembly module) + throws AssemblyException + { + module.services(IdentityGenerator.class).withMixins(UuidGeneratorMixin.class); + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/layered/IllegalLayerAssemblerException.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/layered/IllegalLayerAssemblerException.java b/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/layered/IllegalLayerAssemblerException.java new file mode 100644 index 0000000..9c843d1 --- /dev/null +++ b/core/bootstrap/src/main/java/org/apache/polygene/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.apache.polygene.bootstrap.layered; + +import org.apache.polygene.bootstrap.AssemblyException; + +public class IllegalLayerAssemblerException extends AssemblyException +{ + public IllegalLayerAssemblerException( String message ) + { + super( message ); + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/layered/LayerAssembler.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/layered/LayerAssembler.java b/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/layered/LayerAssembler.java new file mode 100644 index 0000000..1d91d88 --- /dev/null +++ b/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/layered/LayerAssembler.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.polygene.bootstrap.layered; + +import org.apache.polygene.bootstrap.AssemblyException; +import org.apache.polygene.bootstrap.LayerAssembly; + +public interface LayerAssembler +{ + LayerAssembly assemble( LayerAssembly layer ) + throws AssemblyException; +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/layered/LayeredApplicationAssembler.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/layered/LayeredApplicationAssembler.java b/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/layered/LayeredApplicationAssembler.java new file mode 100644 index 0000000..58e5a9e --- /dev/null +++ b/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/layered/LayeredApplicationAssembler.java @@ -0,0 +1,297 @@ +/* + * 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.polygene.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.apache.polygene.api.activation.ActivationException; +import org.apache.polygene.api.activation.PassivationException; +import org.apache.polygene.api.structure.Application; +import org.apache.polygene.api.structure.ApplicationDescriptor; +import org.apache.polygene.bootstrap.ApplicationAssembler; +import org.apache.polygene.bootstrap.ApplicationAssembly; +import org.apache.polygene.bootstrap.ApplicationAssemblyFactory; +import org.apache.polygene.bootstrap.AssemblyException; +import org.apache.polygene.bootstrap.Energy4Java; +import org.apache.polygene.bootstrap.LayerAssembly; + +public abstract class LayeredApplicationAssembler + implements ApplicationAssembler +{ + protected final Energy4Java zest; + protected final String name; + protected final String version; + + private final Application.Mode mode; + private final HashMap<Class<? extends LayerAssembler>, LayerAssembler> assemblers = new HashMap<>(); + + private ApplicationAssembly assembly; + protected ApplicationDescriptor model; + protected Application application; + + public LayeredApplicationAssembler( String name, String version, Application.Mode mode ) + throws AssemblyException + { + this.name = name; + this.version = version; + this.mode = mode; + zest = new Energy4Java(); + } + + public void initialize() + throws AssemblyException + { + model = zest.newApplicationModel( this ); + onModelCreated( model ); + instantiateApplication( zest, model ); + } + + public ApplicationAssembly assembly() + { + return assembly; + } + + /** + * This method is called from the <code>initialize</code> method to instantiate the Polygene application from the + * application model. + * + * <p> + * The default implementation simply calls; + * </p> + * <pre><code> + * application = model.newInstance( polygene.spi() ); + * </code></pre> + * + * @param zest The Polygene runtime engine. + * @param model The application model descriptor. + */ + protected void instantiateApplication( Energy4Java zest, ApplicationDescriptor model ) + { + application = model.newInstance( zest.spi() ); + } + + /** + * This method is called after the Application Model has been created, before the instantiation of the Polygene + * application. + * + * <p> + * The default implementation does nothing. Applications may have advanced features to inspect or + * modify the model prior to instantiation, and this is the place where such advanced manipulation is + * expected to take place. + * </p> + * + * @param model The model that has just been created. + */ + protected void onModelCreated( ApplicationDescriptor model ) + { + } + + public ApplicationDescriptor model() + { + return model; + } + + public Application application() + { + return application; + } + + public void start() + throws ActivationException + { + application.activate(); + } + + public void stop() + throws PassivationException + { + application.passivate(); + } + + @Override + public ApplicationAssembly assemble( ApplicationAssemblyFactory applicationFactory ) + throws AssemblyException + { + assembly = applicationFactory.newApplicationAssembly(); + assembly.setName( name ); + assembly.setVersion( version ); + assembly.setMode( mode ); + assembleLayers( assembly ); + return assembly; + } + + protected LayerAssembly createLayer( Class<? extends LayerAssembler> layerAssemblerClass ) + throws IllegalArgumentException + { + try + { + String classname = layerAssemblerClass.getSimpleName(); + if( classname.endsWith( "Layer" ) ) + { + classname = classname.substring( 0, classname.length() - 5 ) + " Layer"; + } + setNameIfPresent( layerAssemblerClass, classname ); + LayerAssembly layer = assembly.layer( classname ); + + LayerAssembler layerAssembler = instantiateLayerAssembler( layerAssemblerClass, layer ); + assemblers.put( layerAssemblerClass, layerAssembler ); + assembleLayer( layerAssembler, layer ); + return layer; + } + catch( Exception e ) + { + throw new IllegalArgumentException( "Unable to instantiate layer with " + layerAssemblerClass.getSimpleName(), e ); + } + } + + 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 InvocationTargetException, IllegalAccessException + { + try + { + 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 ) + { + 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 null; + } + + static void setNameIfPresent( Class<?> clazz, String classname ) + throws IllegalAccessException + { + try + { + Field field = clazz.getDeclaredField( "NAME" ); + if( Modifier.isStatic( field.getModifiers() ) ) + { + field.setAccessible( true ); + field.set( null, classname ); + } + } + catch( Exception e ) + { + // Ignore and consider normal. + } + } + + @SuppressWarnings( "unchecked" ) + protected <T extends LayerAssembler> T assemblerOf( Class<T> layerAssemblerClass ) + { + return (T) assemblers.get( layerAssemblerClass ); + } + + /** + * Called from the <code>assemble</code> method to assemble the layers in the application. + * + * <p> + * This method must be implemented, and is typically a list of LayerAssembler instantiations, followed + * by {@link LayerAssembly#uses(LayerAssembly...)} declarations. + * </p> + * + * @param assembly Application assembly + * @throws AssemblyException on invalid assembly + */ + protected abstract void assembleLayers( ApplicationAssembly assembly ) + throws AssemblyException; +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/layered/LayeredLayerAssembler.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/layered/LayeredLayerAssembler.java b/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/layered/LayeredLayerAssembler.java new file mode 100644 index 0000000..9783de5 --- /dev/null +++ b/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/layered/LayeredLayerAssembler.java @@ -0,0 +1,90 @@ +/* + * 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.polygene.bootstrap.layered; + +import java.lang.reflect.Constructor; +import java.util.HashMap; +import org.apache.polygene.bootstrap.LayerAssembly; +import org.apache.polygene.bootstrap.ModuleAssembly; + +public abstract class LayeredLayerAssembler + implements LayerAssembler +{ + private HashMap<Class<? extends ModuleAssembler>, ModuleAssembler> assemblers = new HashMap<>(); + + protected ModuleAssembly createModule( LayerAssembly layer, Class<? extends ModuleAssembler> moduleAssemblerClass ) + { + try + { + ModuleAssembler moduleAssembler = instantiateAssembler( layer, moduleAssemblerClass ); + String moduleName = createModuleName( moduleAssemblerClass ); + LayeredApplicationAssembler.setNameIfPresent( moduleAssemblerClass, moduleName ); + ModuleAssembly module = layer.module( moduleName ); + assemblers.put( moduleAssemblerClass, moduleAssembler ); + ModuleAssembly assembly = moduleAssembler.assemble( layer, module ); + if( assembly == null ) + { + return module; + } + return assembly; + } + catch( Exception e ) + { + throw new IllegalArgumentException( "Unable to instantiate module with " + moduleAssemblerClass.getSimpleName(), e ); + } + } + + protected String createModuleName( Class<? extends ModuleAssembler> modulerAssemblerClass ) + { + String moduleName = modulerAssemblerClass.getSimpleName(); + if( moduleName.endsWith( "Module" ) ) + { + moduleName = moduleName.substring( 0, moduleName.length() - 6 ) + " Module"; + } + return moduleName; + } + + protected ModuleAssembler instantiateAssembler( LayerAssembly layer, + Class<? extends ModuleAssembler> modulerAssemblerClass + ) + throws InstantiationException, IllegalAccessException, java.lang.reflect.InvocationTargetException, NoSuchMethodException + { + ModuleAssembler moduleAssembler; + try + { + Constructor<? extends ModuleAssembler> assemblyConstructor = modulerAssemblerClass.getDeclaredConstructor( ModuleAssembly.class ); + assemblyConstructor.setAccessible( true ); + moduleAssembler = assemblyConstructor.newInstance( layer ); + } + catch( NoSuchMethodException e ) + { + Constructor<? extends ModuleAssembler> assemblyConstructor = modulerAssemblerClass.getDeclaredConstructor(); + assemblyConstructor.setAccessible( true ); + moduleAssembler = assemblyConstructor.newInstance(); + } + return moduleAssembler; + } + + @SuppressWarnings( "unchecked" ) + protected <T extends ModuleAssembler> T assemblerOf( Class<T> moduleAssemblerType ) + { + return (T) assemblers.get( moduleAssemblerType ); + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/layered/ModuleAssembler.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/layered/ModuleAssembler.java b/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/layered/ModuleAssembler.java new file mode 100644 index 0000000..739c3ad --- /dev/null +++ b/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/layered/ModuleAssembler.java @@ -0,0 +1,30 @@ +/* + * 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.polygene.bootstrap.layered; + +import org.apache.polygene.bootstrap.AssemblyException; +import org.apache.polygene.bootstrap.LayerAssembly; +import org.apache.polygene.bootstrap.ModuleAssembly; + +public interface ModuleAssembler +{ + ModuleAssembly assemble( LayerAssembly layer, ModuleAssembly module ) + throws AssemblyException; +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/layered/package.html ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/layered/package.html b/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/layered/package.html new file mode 100644 index 0000000..0b8300b --- /dev/null +++ b/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/layered/package.html @@ -0,0 +1,24 @@ +<!-- + ~ 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. + ~ + ~ + --> +<html> + <body> + <h2>Layered Assembly.</h2> + </body> +</html> http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/package.html ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/package.html b/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/package.html new file mode 100644 index 0000000..7c867b8 --- /dev/null +++ b/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/package.html @@ -0,0 +1,24 @@ +<!-- + ~ 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. + ~ + ~ + --> +<html> + <body> + <h2>Assembly and Bootstrap API.</h2> + </body> +</html> http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/unitofwork/DefaultUnitOfWorkAssembler.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/unitofwork/DefaultUnitOfWorkAssembler.java b/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/unitofwork/DefaultUnitOfWorkAssembler.java new file mode 100644 index 0000000..6383b75 --- /dev/null +++ b/core/bootstrap/src/main/java/org/apache/polygene/bootstrap/unitofwork/DefaultUnitOfWorkAssembler.java @@ -0,0 +1,55 @@ +/* + * 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.polygene.bootstrap.unitofwork; + +import org.apache.polygene.api.unitofwork.UnitOfWork; +import org.apache.polygene.api.unitofwork.UnitOfWorkFactory; +import org.apache.polygene.bootstrap.Assembler; +import org.apache.polygene.bootstrap.AssemblyException; +import org.apache.polygene.bootstrap.ModuleAssembly; + +public class DefaultUnitOfWorkAssembler + implements Assembler +{ + @Override + public void assemble( ModuleAssembly module ) + throws AssemblyException + { + Class factoryMixin = loadMixinClass( "org.apache.polygene.runtime.unitofwork.UnitOfWorkFactoryMixin" ); + module.services( UnitOfWorkFactory.class ).withMixins( factoryMixin ); + + Class uowMixin = loadMixinClass( "org.apache.polygene.runtime.unitofwork.ModuleUnitOfWork" ); + module.transients( UnitOfWork.class ).withMixins( uowMixin ); + } + + private Class<?> loadMixinClass( String name ) + throws AssemblyException + { + try + { + return getClass().getClassLoader().loadClass( name ); + } + catch( ClassNotFoundException e ) + { + throw new AssemblyException( "Default UnitOfWorkFactory mixin is not present in the system." ); + } + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/bootstrap/src/main/java/org/apache/zest/bootstrap/ApplicationAssembler.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/apache/zest/bootstrap/ApplicationAssembler.java b/core/bootstrap/src/main/java/org/apache/zest/bootstrap/ApplicationAssembler.java deleted file mode 100644 index a2dbb67..0000000 --- a/core/bootstrap/src/main/java/org/apache/zest/bootstrap/ApplicationAssembler.java +++ /dev/null @@ -1,39 +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.zest.bootstrap; - -/** - * Implement this interface to create the root class that - * is responsible for assembling your entire application. - * - * Model introspectors will instantiate this class and call assemble - * to create the application, which will then be visited to get - * information about its structure. - * - * Application deployment servers will instantiate this, call assemble, - * and then activate the created application, which will be the runtime - * instance that forms your application. - */ -public interface ApplicationAssembler -{ - ApplicationAssembly assemble( ApplicationAssemblyFactory applicationFactory ) - throws AssemblyException; -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/bootstrap/src/main/java/org/apache/zest/bootstrap/ApplicationAssemblerAdapter.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/apache/zest/bootstrap/ApplicationAssemblerAdapter.java b/core/bootstrap/src/main/java/org/apache/zest/bootstrap/ApplicationAssemblerAdapter.java deleted file mode 100644 index 519f47b..0000000 --- a/core/bootstrap/src/main/java/org/apache/zest/bootstrap/ApplicationAssemblerAdapter.java +++ /dev/null @@ -1,49 +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.zest.bootstrap; - -/** - * Helper base class for application assemblers that - * want to either create applications using only one layer/module, - * or that wants to create pancake-layered applications. - */ -public class ApplicationAssemblerAdapter - implements ApplicationAssembler -{ - private final Assembler[][][] assemblers; - - protected ApplicationAssemblerAdapter( Assembler assembler ) - { - this.assemblers = new Assembler[][][]{ { { assembler } } }; - } - - protected ApplicationAssemblerAdapter( Assembler[][][] assemblers ) - { - this.assemblers = assemblers; - } - - @Override - public ApplicationAssembly assemble( ApplicationAssemblyFactory applicationFactory ) - throws AssemblyException - { - return applicationFactory.newApplicationAssembly( assemblers ); - } -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/bootstrap/src/main/java/org/apache/zest/bootstrap/ApplicationAssembly.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/apache/zest/bootstrap/ApplicationAssembly.java b/core/bootstrap/src/main/java/org/apache/zest/bootstrap/ApplicationAssembly.java deleted file mode 100644 index 8219f0e..0000000 --- a/core/bootstrap/src/main/java/org/apache/zest/bootstrap/ApplicationAssembly.java +++ /dev/null @@ -1,111 +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.zest.bootstrap; - -import org.apache.zest.api.activation.Activator; -import org.apache.zest.api.structure.Application; - -/** - * An application assembly. This can be used by Assemblers to programmatically - * set the name of the application and create new layers. - */ -public interface ApplicationAssembly -{ - /** - * Create a new layer assembly - * - * @param name of the new layer - * - * @return a LayerAssembly instance - */ - LayerAssembly layer( String name ); - - /** - * Get an assembly for a particular Module. If this is called many times with the same names, then the same module - * is affected. - * - * @param layerName The name of the Layer - * @param moduleName The name of the Module to retrieve or create. - * @return The ModuleAssembly for the Module. - */ - ModuleAssembly module( String layerName, String moduleName ); - - /** - * Get the currently set name of the application - * - * @return the name of the application - */ - String name(); - - /** - * Get the currently set mode of the application - * - * @return the application mode - */ - Application.Mode mode(); - - /** - * Set the name of the application - * - * @param name of the application - * - * @return the assembly - */ - ApplicationAssembly setName( String name ); - - /** - * Set the version of the application. This can be in any format, but - * most likely will follow the Dewey format, i.e. x.y.z. - * - * @param version of the application - * - * @return the assembly - */ - ApplicationAssembly setVersion( String version ); - - /** - * Set the application mode. This will be set to "production" by default. You can - * set the system property "mode" to either "development", "satisfiedBy" or "production" - * to explicitly set the mode. If that is not an option, then call this method - * during assembly to set the mode. The mode may then be queried by assemblers, - * and they may assemble the application differentlly depending on this setting. - * - * @param mode the application mode - * - * @return the assembly - */ - ApplicationAssembly setMode( Application.Mode mode ); - - ApplicationAssembly setMetaInfo( Object info ); - - /** - * Set the application activators. Activators are executed in order around the - * Application activation and passivation. - * - * @param activators the application activators - * @return the assembly - */ - @SuppressWarnings( { "unchecked","varargs" } ) - ApplicationAssembly withActivators( Class<? extends Activator<Application>>... activators ); - - <ThrowableType extends Throwable> void visit( AssemblyVisitor<ThrowableType> visitor ) - throws ThrowableType; -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/bootstrap/src/main/java/org/apache/zest/bootstrap/ApplicationAssemblyFactory.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/apache/zest/bootstrap/ApplicationAssemblyFactory.java b/core/bootstrap/src/main/java/org/apache/zest/bootstrap/ApplicationAssemblyFactory.java deleted file mode 100644 index 0b9a3d5..0000000 --- a/core/bootstrap/src/main/java/org/apache/zest/bootstrap/ApplicationAssemblyFactory.java +++ /dev/null @@ -1,63 +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.zest.bootstrap; - -/** - * Factory for creating new Polygene application assemblies. Typically - * you will implement one or more Assemblers, wrap them in an ApplicationAssembler, - * which then uses this factory to assemble and create applications. - */ -public interface ApplicationAssemblyFactory -{ - /** - * Create a new application with one layer and one module. - * - * @param assembler the assembler for the single module - * - * @return the application instance - * - * @throws AssemblyException if the application could not be assembled - */ - ApplicationAssembly newApplicationAssembly( Assembler assembler ) - throws AssemblyException; - - /** - * Create a new application with the same amount of layers - * as the first array size, with modules according to the second array size, - * and then use the third array for assemblers of each module. This gives you - * a simple way to create "pancake" layered applications. - * - * @param assemblers the set of assemblers for the application - * - * @return the application instance - * - * @throws AssemblyException if the application could not be assembled - */ - ApplicationAssembly newApplicationAssembly( Assembler[][][] assemblers ) - throws AssemblyException; - - /** - * Create a new ApplicationAssembly that can be used for the above method. - * - * @return a new ApplicationAssembly - */ - ApplicationAssembly newApplicationAssembly(); -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/bootstrap/src/main/java/org/apache/zest/bootstrap/ApplicationModelFactory.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/apache/zest/bootstrap/ApplicationModelFactory.java b/core/bootstrap/src/main/java/org/apache/zest/bootstrap/ApplicationModelFactory.java deleted file mode 100644 index a8d72be..0000000 --- a/core/bootstrap/src/main/java/org/apache/zest/bootstrap/ApplicationModelFactory.java +++ /dev/null @@ -1,33 +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.zest.bootstrap; - -import org.apache.zest.api.structure.ApplicationDescriptor; - -/** - * Factory for ApplicationModelSPI's. Takes an ApplicationAssembly, executes it, - * and builds an application model from it, which can then be instantiated and activated. - */ -public interface ApplicationModelFactory -{ - ApplicationDescriptor newApplicationModel( ApplicationAssembly assembly ) - throws AssemblyException; -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/bootstrap/src/main/java/org/apache/zest/bootstrap/ApplicationName.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/apache/zest/bootstrap/ApplicationName.java b/core/bootstrap/src/main/java/org/apache/zest/bootstrap/ApplicationName.java deleted file mode 100644 index a045eb3..0000000 --- a/core/bootstrap/src/main/java/org/apache/zest/bootstrap/ApplicationName.java +++ /dev/null @@ -1,42 +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.zest.bootstrap; - -/** - * Set the name of the application - */ -public final class ApplicationName - implements Assembler -{ - private String name; - - public ApplicationName( String name ) - { - this.name = name; - } - - @Override - public void assemble( ModuleAssembly module ) - throws AssemblyException - { - module.layer().application().setName( name ); - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/bootstrap/src/main/java/org/apache/zest/bootstrap/Assembler.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/apache/zest/bootstrap/Assembler.java b/core/bootstrap/src/main/java/org/apache/zest/bootstrap/Assembler.java deleted file mode 100644 index f7809f0..0000000 --- a/core/bootstrap/src/main/java/org/apache/zest/bootstrap/Assembler.java +++ /dev/null @@ -1,50 +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.zest.bootstrap; - -/** - * ModuleAssemblies are configured by Assemblers. This - * is the interface you would implement in order to provide - * all configuration and additional metainfo that is needed - * to instantiate a Polygene application. - */ -public interface Assembler -{ - /** - * Assemblers receive a callback to the ModuleAssembly - * they are supposed to configure. They can use this - * to register objects, composites, services etc. and - * the additional metadata that may exist for these - * artifacts. - * <p> - * An Assembler may create new Modules by calling - * {@link org.apache.zest.bootstrap.ModuleAssembly#layer()} and - * then {@link LayerAssembly#module(String)} (String)}. - * This allows an Assembler to bootstrap an entire Layer with - * more Modules. - * </p> - * @param module the Module to assemble - * - * @throws AssemblyException thrown if the assembler tries to do something illegal - */ - void assemble( ModuleAssembly module ) - throws AssemblyException; -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/bootstrap/src/main/java/org/apache/zest/bootstrap/AssemblerCollection.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/apache/zest/bootstrap/AssemblerCollection.java b/core/bootstrap/src/main/java/org/apache/zest/bootstrap/AssemblerCollection.java deleted file mode 100644 index e222efb..0000000 --- a/core/bootstrap/src/main/java/org/apache/zest/bootstrap/AssemblerCollection.java +++ /dev/null @@ -1,76 +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.zest.bootstrap; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; - -/** - * Assembler that delegates to a collection of Assemblers. - * <p> - * Makes it easy to collect and compose assemblers into bigger assemblers. - * </p> - */ -public final class AssemblerCollection - implements Assembler -{ - Collection<Assembler> assemblers; - - public AssemblerCollection( Assembler... assemblers ) - { - this.assemblers = Arrays.asList( assemblers ); - } - - @SafeVarargs - public AssemblerCollection( Class<? extends Assembler>... assemblyClasses ) - throws AssemblyException - { - assemblers = new ArrayList<>(); - for( Class<? extends Assembler> assemblyClass : assemblyClasses ) - { - try - { - Assembler assembler = assemblyClass.newInstance(); - assemblers.add( assembler ); - } - catch( Exception e ) - { - throw new AssemblyException( "Could not instantiate assembly with class " + assemblyClass.getName(), e ); - } - } - } - - public AssemblerCollection( Collection<Assembler> assemblers ) - { - this.assemblers = assemblers; - } - - @Override - public void assemble( ModuleAssembly module ) - throws AssemblyException - { - for( Assembler assembler : assemblers ) - { - assembler.assemble( module ); - } - } -}
