http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/bootstrap/src/main/java/org/qi4j/bootstrap/ApplicationAssembler.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/qi4j/bootstrap/ApplicationAssembler.java b/core/bootstrap/src/main/java/org/qi4j/bootstrap/ApplicationAssembler.java new file mode 100644 index 0000000..cb5dfda --- /dev/null +++ b/core/bootstrap/src/main/java/org/qi4j/bootstrap/ApplicationAssembler.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2009, Rickard Ãberg. All Rights Reserved. + * + * Licensed 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; + +/** + * 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/a789141d/core/bootstrap/src/main/java/org/qi4j/bootstrap/ApplicationAssemblerAdapter.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/qi4j/bootstrap/ApplicationAssemblerAdapter.java b/core/bootstrap/src/main/java/org/qi4j/bootstrap/ApplicationAssemblerAdapter.java new file mode 100644 index 0000000..1025f7a --- /dev/null +++ b/core/bootstrap/src/main/java/org/qi4j/bootstrap/ApplicationAssemblerAdapter.java @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2009, Rickard Ãberg. All Rights Reserved. + * + * Licensed 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; + +/** + * 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/a789141d/core/bootstrap/src/main/java/org/qi4j/bootstrap/ApplicationAssembly.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/qi4j/bootstrap/ApplicationAssembly.java b/core/bootstrap/src/main/java/org/qi4j/bootstrap/ApplicationAssembly.java new file mode 100644 index 0000000..b14d554 --- /dev/null +++ b/core/bootstrap/src/main/java/org/qi4j/bootstrap/ApplicationAssembly.java @@ -0,0 +1,110 @@ +/* + * Copyright 2008 Niclas Hedhman. All rights Reserved. + * Copyright 2012 Paul Merlin. + * + * Licensed 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; + +import org.qi4j.api.activation.Activator; +import org.qi4j.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/a789141d/core/bootstrap/src/main/java/org/qi4j/bootstrap/ApplicationAssemblyFactory.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/qi4j/bootstrap/ApplicationAssemblyFactory.java b/core/bootstrap/src/main/java/org/qi4j/bootstrap/ApplicationAssemblyFactory.java new file mode 100644 index 0000000..5d86bf9 --- /dev/null +++ b/core/bootstrap/src/main/java/org/qi4j/bootstrap/ApplicationAssemblyFactory.java @@ -0,0 +1,61 @@ +/* + * Copyright 2008 Niclas Hedhman. All rights Reserved. + * + * Licensed 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; + +/** + * Factory for creating new Zest 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/a789141d/core/bootstrap/src/main/java/org/qi4j/bootstrap/ApplicationModelFactory.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/qi4j/bootstrap/ApplicationModelFactory.java b/core/bootstrap/src/main/java/org/qi4j/bootstrap/ApplicationModelFactory.java new file mode 100644 index 0000000..c8ea46f --- /dev/null +++ b/core/bootstrap/src/main/java/org/qi4j/bootstrap/ApplicationModelFactory.java @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2009, Rickard Ãberg. All Rights Reserved. + * + * Licensed 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; + +import org.qi4j.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/a789141d/core/bootstrap/src/main/java/org/qi4j/bootstrap/ApplicationName.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/qi4j/bootstrap/ApplicationName.java b/core/bootstrap/src/main/java/org/qi4j/bootstrap/ApplicationName.java new file mode 100644 index 0000000..d2ca628 --- /dev/null +++ b/core/bootstrap/src/main/java/org/qi4j/bootstrap/ApplicationName.java @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2008, Rickard Ãberg. All Rights Reserved. + * + * Licensed 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; + +/** + * 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/a789141d/core/bootstrap/src/main/java/org/qi4j/bootstrap/Assembler.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/qi4j/bootstrap/Assembler.java b/core/bootstrap/src/main/java/org/qi4j/bootstrap/Assembler.java new file mode 100644 index 0000000..b2ffe34 --- /dev/null +++ b/core/bootstrap/src/main/java/org/qi4j/bootstrap/Assembler.java @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2007, Rickard Ãberg. All Rights Reserved. + * + * Licensed 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; + +/** + * 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 Zest 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.qi4j.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/a789141d/core/bootstrap/src/main/java/org/qi4j/bootstrap/AssemblerCollection.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/qi4j/bootstrap/AssemblerCollection.java b/core/bootstrap/src/main/java/org/qi4j/bootstrap/AssemblerCollection.java new file mode 100644 index 0000000..670d88b --- /dev/null +++ b/core/bootstrap/src/main/java/org/qi4j/bootstrap/AssemblerCollection.java @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2007, Rickard Ãberg. All Rights Reserved. + * + * Licensed 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; + +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 ); + } + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/bootstrap/src/main/java/org/qi4j/bootstrap/Assemblers.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/qi4j/bootstrap/Assemblers.java b/core/bootstrap/src/main/java/org/qi4j/bootstrap/Assemblers.java new file mode 100644 index 0000000..6fdcd80 --- /dev/null +++ b/core/bootstrap/src/main/java/org/qi4j/bootstrap/Assemblers.java @@ -0,0 +1,446 @@ +/* + * Copyright 2014 Paul Merlin. + * + * Licensed 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; + +/** + * Assembler adapters for common use cases (visibility, identity, configuration). + */ +public class Assemblers +{ + private Assemblers() + { + } + + /** + * Assembler with Visibility interface. + * @param <AssemblerType> Parameterized type of Assembler + */ + public interface Visible<AssemblerType> + extends Assembler + { + /** + * Set Visibility. + * @param visibility Visibility + * @return This Assembler instance + */ + AssemblerType visibleIn( org.qi4j.api.common.Visibility visibility ); + + /** + * Get Visibility. + * <p>Default to {@link org.qi4j.api.common.Visibility#module}.</p> + * @return Visibility + */ + org.qi4j.api.common.Visibility visibility(); + } + + /** + * Assembler with Identity interface. + * @param <AssemblerType> Parameterized type of Assembler + */ + public interface Identifiable<AssemblerType> + extends Assembler + { + /** + * Set Identity. + * @param identity Identity + * @return This Assembler instance + */ + AssemblerType identifiedBy( String identity ); + + /** + * @return {@literal true} if {@link #identity()} do not return null, {@literal false} otherwise + */ + boolean hasIdentity(); + + /** + * Get Identity. + * <p>Default to {@literal null}.</p> + * @return Identity + */ + String identity(); + } + + /** + * Assembler with Configuration interface. + * @param <AssemblerType> Parameterized type of Assembler + */ + public interface Configurable<AssemblerType> + extends Assembler + { + /** + * Set Configuration Module and Visibility. + * @param configModule Configuration Module + * @param configVisibility Configuration Visiblity + * @return This Assembler instance + */ + AssemblerType withConfig( ModuleAssembly configModule, + org.qi4j.api.common.Visibility configVisibility ); + + /** + * @return {@literal true} if {@link #configModule() ()} do not return null, {@literal false} otherwise + */ + boolean hasConfig(); + + /** + * Get Configuration Module. + * <p>Default to {@literal null}.</p> + * @return Configuration Module + */ + ModuleAssembly configModule(); + + /** + * Get Configuration Visibility. + * <p>Default to {@link org.qi4j.api.common.Visibility#module}.</p> + * @return Configuration Visibility + */ + org.qi4j.api.common.Visibility configVisibility(); + } + + /** + * Assembler with Visibility adapter. + * @param <AssemblerType> Parameterized type of Assembler + */ + public static abstract class Visibility<AssemblerType> + implements Visible<AssemblerType> + { + private org.qi4j.api.common.Visibility visibility = org.qi4j.api.common.Visibility.module; + + @Override + @SuppressWarnings( "unchecked" ) + public final AssemblerType visibleIn( org.qi4j.api.common.Visibility visibility ) + { + this.visibility = visibility; + return (AssemblerType) this; + } + + @Override + public final org.qi4j.api.common.Visibility visibility() + { + return visibility; + } + } + + /** + * Assembler with Identity adapter. + * @param <AssemblerType> Parameterized type of Assembler + */ + public static abstract class Identity<AssemblerType> + implements Identifiable<AssemblerType> + { + private String identity; + + @Override + @SuppressWarnings( "unchecked" ) + public final AssemblerType identifiedBy( String identity ) + { + this.identity = identity; + return (AssemblerType) this; + } + + @Override + public final boolean hasIdentity() + { + return identity != null; + } + + @Override + public final String identity() + { + return identity; + } + } + + /** + * Assembler with Configuration adapter. + * @param <AssemblerType> Parameterized type of Assembler + */ + public static abstract class Config<AssemblerType> + implements Configurable<AssemblerType> + { + private ModuleAssembly configModule = null; + private org.qi4j.api.common.Visibility configVisibility = org.qi4j.api.common.Visibility.module; + + @Override + @SuppressWarnings( "unchecked" ) + public final AssemblerType withConfig( ModuleAssembly configModule, + org.qi4j.api.common.Visibility configVisibility ) + { + this.configModule = configModule; + this.configVisibility = configVisibility; + return (AssemblerType) this; + } + + @Override + public final boolean hasConfig() + { + return configModule != null; + } + + @Override + public final ModuleAssembly configModule() + { + return configModule; + } + + @Override + public final org.qi4j.api.common.Visibility configVisibility() + { + return configVisibility; + } + } + + /** + * Assembler with Visibility and Identity adapter. + * @param <AssemblerType> Parameterized type of Assembler + */ + public static abstract class VisibilityIdentity<AssemblerType> + implements Visible<AssemblerType>, + Identifiable<AssemblerType> + { + private org.qi4j.api.common.Visibility visibility = org.qi4j.api.common.Visibility.module; + private String identity; + + @Override + @SuppressWarnings( "unchecked" ) + public final AssemblerType visibleIn( org.qi4j.api.common.Visibility visibility ) + { + this.visibility = visibility; + return (AssemblerType) this; + } + + @Override + public final org.qi4j.api.common.Visibility visibility() + { + return visibility; + } + + @Override + @SuppressWarnings( "unchecked" ) + public final AssemblerType identifiedBy( String identity ) + { + this.identity = identity; + return (AssemblerType) this; + } + + @Override + public final boolean hasIdentity() + { + return identity != null; + } + + @Override + public final String identity() + { + return identity; + } + } + + /** + * Assembler with Visibility and Configuration adapter. + * @param <AssemblerType> Parameterized type of Assembler + */ + public static abstract class VisibilityConfig<AssemblerType> + implements Visible<AssemblerType>, + Configurable<AssemblerType> + { + private org.qi4j.api.common.Visibility visibility = org.qi4j.api.common.Visibility.module; + private ModuleAssembly configModule = null; + private org.qi4j.api.common.Visibility configVisibility = org.qi4j.api.common.Visibility.module; + + @Override + @SuppressWarnings( "unchecked" ) + public final AssemblerType visibleIn( org.qi4j.api.common.Visibility visibility ) + { + this.visibility = visibility; + return (AssemblerType) this; + } + + @Override + public final org.qi4j.api.common.Visibility visibility() + { + return visibility; + } + + @Override + @SuppressWarnings( "unchecked" ) + public final AssemblerType withConfig( ModuleAssembly configModule, + org.qi4j.api.common.Visibility configVisibility ) + { + this.configModule = configModule; + this.configVisibility = configVisibility; + return (AssemblerType) this; + } + + @Override + public final boolean hasConfig() + { + return configModule != null; + } + + @Override + public final ModuleAssembly configModule() + { + return configModule; + } + + @Override + public final org.qi4j.api.common.Visibility configVisibility() + { + return configVisibility; + } + } + + /** + * Assembler with Identity and Configuration adapter. + * @param <AssemblerType> Parameterized type of Assembler + */ + public static abstract class IdentityConfig<AssemblerType> + implements Identifiable<AssemblerType>, + Configurable<AssemblerType> + { + private String identity; + private ModuleAssembly configModule = null; + private org.qi4j.api.common.Visibility configVisibility = org.qi4j.api.common.Visibility.module; + + @Override + @SuppressWarnings( "unchecked" ) + public final AssemblerType identifiedBy( String identity ) + { + this.identity = identity; + return (AssemblerType) this; + } + + @Override + public final boolean hasIdentity() + { + return identity != null; + } + + @Override + public final String identity() + { + return identity; + } + + @Override + @SuppressWarnings( "unchecked" ) + public final AssemblerType withConfig( ModuleAssembly configModule, + org.qi4j.api.common.Visibility configVisibility ) + { + this.configModule = configModule; + this.configVisibility = configVisibility; + return (AssemblerType) this; + } + + @Override + public final boolean hasConfig() + { + return configModule != null; + } + + @Override + public final ModuleAssembly configModule() + { + return configModule; + } + + @Override + public final org.qi4j.api.common.Visibility configVisibility() + { + return configVisibility; + } + } + + /** + * Assembler with Visibility, Identity and Configuation adapter. + * @param <AssemblerType> Parameterized type of Assembler + */ + public static abstract class VisibilityIdentityConfig<AssemblerType> + implements Visible<AssemblerType>, + Identifiable<AssemblerType>, + Configurable<AssemblerType> + { + private org.qi4j.api.common.Visibility visibility = org.qi4j.api.common.Visibility.module; + private String identity; + private ModuleAssembly configModule = null; + private org.qi4j.api.common.Visibility configVisibility = org.qi4j.api.common.Visibility.module; + + @Override + @SuppressWarnings( "unchecked" ) + public final AssemblerType visibleIn( org.qi4j.api.common.Visibility visibility ) + { + this.visibility = visibility; + return (AssemblerType) this; + } + + @Override + public final org.qi4j.api.common.Visibility visibility() + { + return visibility; + } + + @Override + @SuppressWarnings( "unchecked" ) + public final AssemblerType identifiedBy( String identity ) + { + this.identity = identity; + return (AssemblerType) this; + } + + @Override + public final boolean hasIdentity() + { + return identity != null; + } + + @Override + public final String identity() + { + return identity; + } + + @Override + @SuppressWarnings( "unchecked" ) + public final AssemblerType withConfig( ModuleAssembly configModule, + org.qi4j.api.common.Visibility configVisibility ) + { + this.configModule = configModule; + this.configVisibility = configVisibility; + return (AssemblerType) this; + } + + @Override + public final boolean hasConfig() + { + return configModule != null; + } + + @Override + public final ModuleAssembly configModule() + { + return configModule; + } + + @Override + public final org.qi4j.api.common.Visibility configVisibility() + { + return configVisibility; + } + } + +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/bootstrap/src/main/java/org/qi4j/bootstrap/AssemblyException.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/qi4j/bootstrap/AssemblyException.java b/core/bootstrap/src/main/java/org/qi4j/bootstrap/AssemblyException.java new file mode 100644 index 0000000..1c75cd9 --- /dev/null +++ b/core/bootstrap/src/main/java/org/qi4j/bootstrap/AssemblyException.java @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2007, Rickard Ãberg. All Rights Reserved. + * + * Licensed 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; + +/** + * Thrown by ModuleAssembly if the Assembler tries to make an invalid assembly. + */ +public class AssemblyException + extends Exception +{ + public AssemblyException() + { + } + + public AssemblyException( String string ) + { + super( string ); + } + + public AssemblyException( String string, Throwable throwable ) + { + super( string, throwable ); + } + + public AssemblyException( Throwable throwable ) + { + super( throwable ); + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/bootstrap/src/main/java/org/qi4j/bootstrap/AssemblySpecifications.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/qi4j/bootstrap/AssemblySpecifications.java b/core/bootstrap/src/main/java/org/qi4j/bootstrap/AssemblySpecifications.java new file mode 100644 index 0000000..66fafc7 --- /dev/null +++ b/core/bootstrap/src/main/java/org/qi4j/bootstrap/AssemblySpecifications.java @@ -0,0 +1,49 @@ +/* + * 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; + +import org.qi4j.api.type.HasTypes; +import org.qi4j.functional.Specification; +import org.qi4j.functional.Specifications; + +/** + * Utility specifications for Assemblies. + */ +public class AssemblySpecifications +{ + public static Specification<HasTypes> types( final Class... types ) + { + return new Specification<HasTypes>() + { + @Override + public boolean satisfiedBy( HasTypes item ) + { + + for( Class<?> type : item.types() ) + { + if( Specifications.in( types ).satisfiedBy( type ) ) + { + return true; + } + } + return false; + } + }; + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/bootstrap/src/main/java/org/qi4j/bootstrap/AssemblyVisitor.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/qi4j/bootstrap/AssemblyVisitor.java b/core/bootstrap/src/main/java/org/qi4j/bootstrap/AssemblyVisitor.java new file mode 100644 index 0000000..c1afb59 --- /dev/null +++ b/core/bootstrap/src/main/java/org/qi4j/bootstrap/AssemblyVisitor.java @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2009, Rickard Ãberg. All Rights Reserved. + * + * Licensed 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; + +/** + * Visitor interface to visit the whole or parts of an assembly. + * <p> + * Implement this interface and call visit() on ApplicationAssembly, LayerAssembly or ModuleAssembly. + * </p> + * <p> + * This can be used to, for example, add metadata to all entities, add concerns on composites, or similar. + * </p> + */ +public interface AssemblyVisitor<ThrowableType extends Throwable> +{ + public void visitApplication( ApplicationAssembly assembly ) + throws ThrowableType; + + public void visitLayer( LayerAssembly assembly ) + throws ThrowableType; + + public void visitModule( ModuleAssembly assembly ) + throws ThrowableType; + + public void visitComposite( TransientDeclaration declaration ) + throws ThrowableType; + + public void visitEntity( EntityDeclaration declaration ) + throws ThrowableType; + + public void visitService( ServiceDeclaration declaration ) + throws ThrowableType; + + public void visitImportedService( ImportedServiceDeclaration declaration ) + throws ThrowableType; + + public void visitValue( ValueDeclaration declaration ) + throws ThrowableType; + + public void visitObject( ObjectDeclaration declaration ) + throws ThrowableType; +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/bootstrap/src/main/java/org/qi4j/bootstrap/AssemblyVisitorAdapter.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/qi4j/bootstrap/AssemblyVisitorAdapter.java b/core/bootstrap/src/main/java/org/qi4j/bootstrap/AssemblyVisitorAdapter.java new file mode 100644 index 0000000..904bb5e --- /dev/null +++ b/core/bootstrap/src/main/java/org/qi4j/bootstrap/AssemblyVisitorAdapter.java @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2009, Rickard Ãberg. All Rights Reserved. + * + * Licensed 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; + +/** + * Base class for assembly visitors. Subclass and override + * the particular methods you are interested in. + */ +public class AssemblyVisitorAdapter<ThrowableType extends Throwable> + implements AssemblyVisitor<ThrowableType> +{ + @Override + public void visitApplication( ApplicationAssembly assembly ) + throws ThrowableType + { + } + + @Override + public void visitLayer( LayerAssembly assembly ) + throws ThrowableType + { + } + + @Override + public void visitModule( ModuleAssembly assembly ) + throws ThrowableType + { + } + + @Override + public void visitComposite( TransientDeclaration declaration ) + throws ThrowableType + { + } + + @Override + public void visitEntity( EntityDeclaration declaration ) + throws ThrowableType + { + } + + @Override + public void visitService( ServiceDeclaration declaration ) + throws ThrowableType + { + } + + @Override + public void visitImportedService( ImportedServiceDeclaration declaration ) + throws ThrowableType + { + } + + @Override + public void visitValue( ValueDeclaration declaration ) + throws ThrowableType + { + } + + @Override + public void visitObject( ObjectDeclaration declaration ) + throws ThrowableType + { + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/bootstrap/src/main/java/org/qi4j/bootstrap/AssociationDeclarations.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/qi4j/bootstrap/AssociationDeclarations.java b/core/bootstrap/src/main/java/org/qi4j/bootstrap/AssociationDeclarations.java new file mode 100644 index 0000000..59724ca --- /dev/null +++ b/core/bootstrap/src/main/java/org/qi4j/bootstrap/AssociationDeclarations.java @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2008, Rickard Ãberg. All Rights Reserved. + * + * Licensed 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; + +import java.lang.reflect.AccessibleObject; +import org.qi4j.api.common.MetaInfo; + +/** + * This provides declared {@link org.qi4j.api.association.Association} information that the runtime can use. + */ +public interface AssociationDeclarations +{ + MetaInfo metaInfoFor( AccessibleObject accessor ); +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/bootstrap/src/main/java/org/qi4j/bootstrap/BindingException.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/qi4j/bootstrap/BindingException.java b/core/bootstrap/src/main/java/org/qi4j/bootstrap/BindingException.java new file mode 100644 index 0000000..dbadbfd --- /dev/null +++ b/core/bootstrap/src/main/java/org/qi4j/bootstrap/BindingException.java @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2007, Rickard Ãberg. All Rights Reserved. + * + * Licensed 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; + +/** + * Thrown by the Zest runtime if a dependency can not be bound. + */ +public class BindingException + extends Exception +{ + public BindingException( String s ) + { + super( s ); + } + + public BindingException( String s, InvalidInjectionException ex ) + { + super( s, ex ); + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/bootstrap/src/main/java/org/qi4j/bootstrap/BootstrapException.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/qi4j/bootstrap/BootstrapException.java b/core/bootstrap/src/main/java/org/qi4j/bootstrap/BootstrapException.java new file mode 100644 index 0000000..f1f1cfd --- /dev/null +++ b/core/bootstrap/src/main/java/org/qi4j/bootstrap/BootstrapException.java @@ -0,0 +1,36 @@ +/* + * Copyright 2008 Niclas Hedhman. All rights Reserved. + * + * Licensed 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; + +/** + * This exception is thrown if no ApplicationFactory provider can be found + */ +public class BootstrapException + extends RuntimeException +{ + public BootstrapException( String message ) + { + super( message ); + } + + public BootstrapException( String message, Throwable cause ) + { + super( message, cause ); + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/bootstrap/src/main/java/org/qi4j/bootstrap/ClassScanner.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/qi4j/bootstrap/ClassScanner.java b/core/bootstrap/src/main/java/org/qi4j/bootstrap/ClassScanner.java new file mode 100644 index 0000000..70c2946 --- /dev/null +++ b/core/bootstrap/src/main/java/org/qi4j/bootstrap/ClassScanner.java @@ -0,0 +1,216 @@ +/* + * Copyright 2008-2011 Rickard Ãberg. All rights Reserved. + * + * Licensed 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; + +import java.io.File; +import java.io.IOException; +import java.lang.reflect.Modifier; +import java.net.URISyntaxException; +import java.net.URL; +import java.security.CodeSource; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; +import java.util.regex.Pattern; +import org.qi4j.functional.Function; +import org.qi4j.functional.Iterables; +import org.qi4j.functional.Specification; + +import static org.qi4j.functional.Iterables.filter; +import static org.qi4j.functional.Iterables.flatten; +import static org.qi4j.functional.Iterables.flattenIterables; +import static org.qi4j.functional.Iterables.iterable; +import static org.qi4j.functional.Iterables.map; + +/** + * Scan classpath for classes that matches given criteria. Useful for automated assemblies with lots of similar classes. + */ +public class ClassScanner +{ + /** + * Get all classes from the same package of the given class, and recursively in all subpackages. + * <p> + * This only works if the seed class is loaded from a file: URL. Jar files are possible as well. Abstract classes + * are not included in the results. For further filtering use e.g. Iterables.filter. + * </p> + * @param seedClass starting point for classpath scanning + * + * @return iterable of all concrete classes in the same package as the seedclass, and also all classes in subpackages. + */ + public static Iterable<Class<?>> findClasses( final Class<?> seedClass ) + { + CodeSource codeSource = seedClass.getProtectionDomain().getCodeSource(); + if( codeSource == null ) + { + return Iterables.empty(); + } + + URL location = codeSource.getLocation(); + + if( !location.getProtocol().equals( "file" ) ) + { + throw new IllegalArgumentException( "Can only enumerate classes from file system locations. URL is:" + location ); + } + + final File file; + try + { + file = new File( location.toURI().getPath() ); + } + catch( URISyntaxException e ) + { + throw new IllegalArgumentException( "The file location of codebase is invalid. Can not convert to URI. URL is:" + location ); + } + + if( file.getName().endsWith( ".jar" ) ) + { + try + { + final String packageName = seedClass.getPackage().getName().replace( '.', '/' ); + + JarFile jarFile = new JarFile( file ); + Iterable<JarEntry> entries = Iterables.iterable( jarFile.entries() ); + try + { + return Iterables.toList( filter( new ValidClass(), + map( new Function<JarEntry, Class<?>>() + { + @Override + public Class map( JarEntry jarEntry ) + { + String name = jarEntry.getName(); + name = name.substring( 0, name.length() - 6 ); + name = name.replace( '/', '.' ); + try + { + return seedClass.getClassLoader().loadClass( name ); + } + catch( ClassNotFoundException e ) + { + return null; + } + } + } + , filter( new Specification<JarEntry>() + { + @Override + public boolean satisfiedBy( JarEntry jarEntry ) + { + return jarEntry.getName() + .startsWith( packageName ) && jarEntry.getName() + .endsWith( ".class" ); + } + }, entries ) ) ) ); + } + finally + { + jarFile.close(); + } + } + catch( IOException e ) + { + throw new IllegalArgumentException( "Could not open jar file " + file, e ); + } + } + else + { + final File path = new File( file, seedClass.getPackage().getName().replace( '.', File.separatorChar ) ); + Iterable<File> files = findFiles( path, new Specification<File>() + { + @Override + public boolean satisfiedBy( File file ) + { + return file.getName().endsWith( ".class" ); + } + } ); + + return filter( new ValidClass(), + map( new Function<File, Class<?>>() + { + @Override + public Class<?> map( File f ) + { + String fileName = f.getAbsolutePath().substring( file.toString().length() + 1 ); + fileName = fileName.replace( File.separatorChar, '.' ) + .substring( 0, fileName.length() - 6 ); + try + { + return seedClass.getClassLoader().loadClass( fileName ); + } + catch( ClassNotFoundException e ) + { + return null; + } + } + }, files ) ); + } + } + + /** + * Useful specification for filtering classes based on a regular expression matching the class names. + * <p> + * Example: matches(".*Model") -> match only class names that end with Model + * </p> + * + * @param regex The regular expression to be matched. + * + * @return regex class name specification + */ + public static Specification<Class<?>> matches( String regex ) + { + final Pattern pattern = Pattern.compile( regex ); + + return new Specification<Class<?>>() + { + @Override + public boolean satisfiedBy( Class<?> aClass ) + { + return pattern.matcher( aClass.getName() ).matches(); + } + }; + } + + private static Iterable<File> findFiles( File directory, final Specification<File> filter ) + { + return flatten( filter( filter, iterable( directory.listFiles() ) ), + flattenIterables( map( new Function<File, Iterable<File>>() + { + @Override + public Iterable<File> map( File file ) + { + return findFiles( file, filter ); + } + }, filter( new Specification<File>() + { + @Override + public boolean satisfiedBy( File file ) + { + return file.isDirectory(); + } + }, iterable( directory.listFiles() ) ) ) ) ); + } + + private static class ValidClass + implements Specification<Class<?>> + { + @Override + public boolean satisfiedBy( Class<?> item ) + { + return ( item.isInterface() || !Modifier.isAbstract( item.getModifiers() ) ) && ( !item.isEnum() && !item.isAnonymousClass() ); + } + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/bootstrap/src/main/java/org/qi4j/bootstrap/ConfigurationAssembly.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/qi4j/bootstrap/ConfigurationAssembly.java b/core/bootstrap/src/main/java/org/qi4j/bootstrap/ConfigurationAssembly.java new file mode 100644 index 0000000..8f45332 --- /dev/null +++ b/core/bootstrap/src/main/java/org/qi4j/bootstrap/ConfigurationAssembly.java @@ -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. + * + */ +package org.qi4j.bootstrap; + +import org.qi4j.api.type.HasTypes; + +/** + * This represents the assembly information of a single ConfigurationComposite in a Module. + */ +public interface ConfigurationAssembly + extends HasTypes +{ +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/bootstrap/src/main/java/org/qi4j/bootstrap/ConfigurationDeclaration.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/qi4j/bootstrap/ConfigurationDeclaration.java b/core/bootstrap/src/main/java/org/qi4j/bootstrap/ConfigurationDeclaration.java new file mode 100644 index 0000000..ef10c35 --- /dev/null +++ b/core/bootstrap/src/main/java/org/qi4j/bootstrap/ConfigurationDeclaration.java @@ -0,0 +1,87 @@ +/* + * 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; + +import org.qi4j.api.common.Visibility; + +/** + * Fluent API for declaring configurations. Instances + * of this API are acquired by calling {@link ModuleAssembly#configurations(Class[])}. + */ +public interface ConfigurationDeclaration +{ + /** + * Set additional metainfo for this configuration declaration. + * + * @param info metainfo that can be retrieved from the CompositeDescriptor. + * + * @return This instance for a fluid DSL + */ + ConfigurationDeclaration setMetaInfo( Object info ); + + /** + * Set visibility for declared entities. + * + * @param visibility The {@link Visibility} that this ConfigurationComposite will have. + * + * @return This instance for a fluid DSL + */ + ConfigurationDeclaration visibleIn( Visibility visibility ); + + /** + * Declare additional concerns for these configurations. + * + * @param concerns The concerns that are to be added to the ConfigurationComposite beyond the statically declared ones. + * + * @return This instance for a fluid DSL + */ + ConfigurationDeclaration withConcerns( Class<?>... concerns ); + + /** + * Declare additional side-effects for these configurations. + * + * @param sideEffects The sideeffects that are to be added to the ConfigurationComposite beyond the statically declared ones. + * + * @return This instance for a fluid DSL + */ + ConfigurationDeclaration withSideEffects( Class<?>... sideEffects ); + + /** + * Declare additional mixins for these configurations. + * <p> + * This can be useful to override any default mixins from the configuration interface. + * </p> + * @param mixins The mixins that are to be added to the ConfigurationComposite beyond the statically declared ones. + * + * @return This instance for a fluid DSL + */ + ConfigurationDeclaration withMixins( Class<?>... mixins ); + + /** + * Declare additional interfaces for these declared interfaces. + * <p> + * This can be useful to add types that the Configuration should implement, but + * which you do not want to include in the entity interface itself. + * </p> + * @param types list of interfaces to add + * + * @return This instance for a fluid DSL + */ + ConfigurationDeclaration withTypes( Class<?>... types ); +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/bootstrap/src/main/java/org/qi4j/bootstrap/Energy4Java.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/qi4j/bootstrap/Energy4Java.java b/core/bootstrap/src/main/java/org/qi4j/bootstrap/Energy4Java.java new file mode 100644 index 0000000..d9cdc18 --- /dev/null +++ b/core/bootstrap/src/main/java/org/qi4j/bootstrap/Energy4Java.java @@ -0,0 +1,96 @@ +/* + * Copyright 2008-2011 Niclas Hedhman. All rights Reserved. + * + * Licensed 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; + +import org.qi4j.api.Qi4j; +import org.qi4j.api.structure.Application; +import org.qi4j.api.structure.ApplicationDescriptor; +import org.qi4j.spi.Qi4jSPI; + +/** + * Main bootstrap class for starting Zest and creating new applications. + * <p> + * Instantiate this and call one of the factory methods to get started. + * </p> + * <p> + * This class will use the Service Loader mechanism in Java to try to locate a runtime that implements + * the Qi4jRuntime interface. This avoids a direct dependency from the bootstrap to the runtime. + * </p> + */ +public final class Energy4Java +{ + private Qi4jRuntime runtime; + + public Energy4Java( RuntimeFactory runtimeFactory ) + { + this( runtimeFactory.createRuntime() ); + } + + public Energy4Java() + { + this( new RuntimeFactory.StandaloneApplicationRuntimeFactory().createRuntime() ); + } + + public Energy4Java( Qi4jRuntime runtime ) + { + if( runtime == null ) + { + throw new BootstrapException( "Can not create Zest without a Zest Runtime." ); + } + this.runtime = runtime; + } + + public ApplicationDescriptor newApplicationModel( ApplicationAssembler assembler ) + throws AssemblyException + { + ApplicationAssembly assembly = assembler.assemble( runtime.applicationAssemblyFactory() ); + + if( assembly == null ) + { + throw new AssemblyException( "Application assembler did not return any ApplicationAssembly" ); + } + + try + { + ApplicationModelFactory modelFactory = runtime.applicationModelFactory(); + return modelFactory.newApplicationModel( assembly ); + } + catch( RuntimeException e ) + { + throw new AssemblyException( "Unable to create Application Model.", e ); + } + } + + public Application newApplication( ApplicationAssembler assembler, Object... importedServiceInstances ) + throws AssemblyException + { + ApplicationDescriptor model = newApplicationModel( assembler ); + return model.newInstance( runtime.spi(), importedServiceInstances ); + } + + public Qi4jSPI spi() + { + return runtime.spi(); + } + + public Qi4j api() + { + return runtime.spi(); + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/bootstrap/src/main/java/org/qi4j/bootstrap/EntityAssembly.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/qi4j/bootstrap/EntityAssembly.java b/core/bootstrap/src/main/java/org/qi4j/bootstrap/EntityAssembly.java new file mode 100644 index 0000000..ef608ed --- /dev/null +++ b/core/bootstrap/src/main/java/org/qi4j/bootstrap/EntityAssembly.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.qi4j.bootstrap; + +import org.qi4j.api.type.HasTypes; + +/** + * This represents the assembly information of a single EntityComposite in a Module. + */ +public interface EntityAssembly + extends HasTypes +{ +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/bootstrap/src/main/java/org/qi4j/bootstrap/EntityDeclaration.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/qi4j/bootstrap/EntityDeclaration.java b/core/bootstrap/src/main/java/org/qi4j/bootstrap/EntityDeclaration.java new file mode 100644 index 0000000..fb00745 --- /dev/null +++ b/core/bootstrap/src/main/java/org/qi4j/bootstrap/EntityDeclaration.java @@ -0,0 +1,87 @@ +/* + * Copyright 2008 Niclas Hedhman. All rights Reserved. + * + * Licensed 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; + +import org.qi4j.api.common.Visibility; + +/** + * Fluent API for declaring entities. Instances + * of this API are acquired by calling {@link ModuleAssembly#entities(Class[])}. + */ +public interface EntityDeclaration +{ + /** + * Set additional metainfo for this entity declaration. + * + * @param info metainfo that can be retrieved from the EntityDescriptor. + * + * @return This instance for a fluid DSL + */ + EntityDeclaration setMetaInfo( Object info ); + + /** + * Set visibility for declared entities. + * + * @param visibility The {@link Visibility} that this EntityComposite will have. + * + * @return This instance for a fluid DSL + */ + EntityDeclaration visibleIn( Visibility visibility ); + + /** + * Declare additional concerns for these entities. + * + * @param concerns The concerns that are to be added to the EntityComposite beyond the statically declared ones. + * + * @return This instance for a fluid DSL + */ + EntityDeclaration withConcerns( Class<?>... concerns ); + + /** + * Declare additional side-effects for these entitites. + * + * @param sideEffects The sideeffects that are to be added to the EntityComposite beyond the statically declared ones. + * + * @return This instance for a fluid DSL + */ + EntityDeclaration withSideEffects( Class<?>... sideEffects ); + + /** + * Declare additional mixins for these entities. + * <p> + * This can be useful to override any default mixins from the entity interface. + * </p> + * @param mixins The mixins that are to be added to the EntityComposite beyond the statically declared ones. + * + * @return This instance for a fluid DSL + */ + EntityDeclaration withMixins( Class<?>... mixins ); + + /** + * Declare additional interfaces for these declared interfaces. + * <p> + * This can be useful to add types that the entities should implement, but + * which you do not want to include in the entity interface itself. + * </p> + * @param types list of interfaces to add + * + * @return This instance for a fluid DSL + */ + EntityDeclaration withTypes( Class<?>... types ); +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/bootstrap/src/main/java/org/qi4j/bootstrap/ImportedServiceAssembly.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/qi4j/bootstrap/ImportedServiceAssembly.java b/core/bootstrap/src/main/java/org/qi4j/bootstrap/ImportedServiceAssembly.java new file mode 100644 index 0000000..2d9320e --- /dev/null +++ b/core/bootstrap/src/main/java/org/qi4j/bootstrap/ImportedServiceAssembly.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.qi4j.bootstrap; + +import org.qi4j.api.type.HasTypes; + +/** + * This represents the assembly information of a single imported service in a Module. + */ +public interface ImportedServiceAssembly + extends HasTypes +{ +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/bootstrap/src/main/java/org/qi4j/bootstrap/ImportedServiceDeclaration.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/qi4j/bootstrap/ImportedServiceDeclaration.java b/core/bootstrap/src/main/java/org/qi4j/bootstrap/ImportedServiceDeclaration.java new file mode 100644 index 0000000..2d4b0a9 --- /dev/null +++ b/core/bootstrap/src/main/java/org/qi4j/bootstrap/ImportedServiceDeclaration.java @@ -0,0 +1,63 @@ +/* + * Copyright 2008 Niclas Hedhman. All rights Reserved. + * Copyright 2012, Paul Merlin. + * + * Licensed 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; + +import org.qi4j.api.activation.Activator; +import org.qi4j.api.common.Visibility; +import org.qi4j.api.service.ServiceImporter; +import org.qi4j.api.service.importer.InstanceImporter; +import org.qi4j.api.service.importer.NewObjectImporter; +import org.qi4j.api.service.importer.ServiceInstanceImporter; +import org.qi4j.api.service.importer.ServiceSelectorImporter; + +/** + * Fluent API for declaring imported services. Instances + * of this API are acquired by calling {@link ModuleAssembly#importedServices(Class[])}. + */ +public interface ImportedServiceDeclaration +{ + // Convenience constants for common service importers + public static final Class<? extends ServiceImporter> INSTANCE = InstanceImporter.class; + public static final Class<? extends ServiceImporter> NEW_OBJECT = NewObjectImporter.class; + public static final Class<? extends ServiceImporter> SERVICE_SELECTOR = ServiceSelectorImporter.class; + public static final Class<? extends ServiceImporter> SERVICE_IMPORTER = ServiceInstanceImporter.class; + + ImportedServiceDeclaration visibleIn( Visibility visibility ); + + ImportedServiceDeclaration importedBy( Class<? extends ServiceImporter> serviceImporterClass ); + + ImportedServiceDeclaration identifiedBy( String identity ); + + ImportedServiceDeclaration taggedWith( String... tags ); + + ImportedServiceDeclaration setMetaInfo( Object serviceAttribute ); + + ImportedServiceDeclaration importOnStartup(); + + /** + * Set the imported service activators. Activators are executed in order around + * the ServiceReference activation and passivation. + * + * @param activators the imported service activators + * @return the assembly + */ + @SuppressWarnings( { "unchecked","varargs" } ) + ImportedServiceDeclaration withActivators( Class<? extends Activator<?>>... activators ); +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/bootstrap/src/main/java/org/qi4j/bootstrap/InjectionException.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/qi4j/bootstrap/InjectionException.java b/core/bootstrap/src/main/java/org/qi4j/bootstrap/InjectionException.java new file mode 100644 index 0000000..bb0d782 --- /dev/null +++ b/core/bootstrap/src/main/java/org/qi4j/bootstrap/InjectionException.java @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2008, Rickard Ãberg. All Rights Reserved. + * + * Licensed 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; + +/** + * Thrown by the Zest runtime if a dependency can not be injected. + */ +public class InjectionException + extends RuntimeException +{ + public InjectionException( String s ) + { + super( s ); + } + + public InjectionException( String s, Throwable throwable ) + { + super( s, throwable ); + } + + public InjectionException( Throwable throwable ) + { + super( throwable ); + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/bootstrap/src/main/java/org/qi4j/bootstrap/InvalidInjectionException.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/qi4j/bootstrap/InvalidInjectionException.java b/core/bootstrap/src/main/java/org/qi4j/bootstrap/InvalidInjectionException.java new file mode 100644 index 0000000..d3f18d9 --- /dev/null +++ b/core/bootstrap/src/main/java/org/qi4j/bootstrap/InvalidInjectionException.java @@ -0,0 +1,36 @@ +/* + * 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; + +/** + * Thrown by the Zest runtime if a dependency injection declaration is invalid. + */ +public class InvalidInjectionException + extends Exception +{ + public InvalidInjectionException( String s ) + { + super( s ); + } + + public InvalidInjectionException( String s, Throwable throwable ) + { + super( s, throwable ); + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/bootstrap/src/main/java/org/qi4j/bootstrap/LayerAssembly.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/qi4j/bootstrap/LayerAssembly.java b/core/bootstrap/src/main/java/org/qi4j/bootstrap/LayerAssembly.java new file mode 100644 index 0000000..5880b3e --- /dev/null +++ b/core/bootstrap/src/main/java/org/qi4j/bootstrap/LayerAssembly.java @@ -0,0 +1,123 @@ +/* + * Copyright 2008 Niclas Hedhman. All rights Reserved. + * Copyright 2012 Paul Merlin. + * + * Licensed 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; + +import org.qi4j.api.activation.Activator; +import org.qi4j.api.structure.Layer; +import org.qi4j.functional.Specification; + +/** + * Fluid API for declaring a layer in an application. This is obtained by calling {@link ApplicationAssembly#layer(String)}. + */ +public interface LayerAssembly +{ + /** + * Get an assembly for a particular Module. If this is called many times with the same name, then the same module + * is affected. + * + * @param name The name of the Module to retrieve or create. + * + * @return The ModuleAssembly for the Module. + */ + ModuleAssembly module( String name ); + + ApplicationAssembly application(); + + String name(); + + LayerAssembly setName( String name ); + + LayerAssembly setMetaInfo( Object info ); + + LayerAssembly uses( LayerAssembly... layerAssembly ); + + /** + * Set the layer activators. Activators are executed in order around the + * Layer activation and passivation. + * + * @param activators the layer activators + * @return the assembly + */ + @SuppressWarnings( { "unchecked","varargs" } ) + LayerAssembly withActivators( Class<? extends Activator<Layer>>... activators ); + + <ThrowableType extends Throwable> void visit( AssemblyVisitor<ThrowableType> visitor ) + throws ThrowableType; + + /** + * Given a Specification for EntityAssembly's, returns a EntityDeclaration that can + * be used to work with all of the assemblies in this Layer matched by the specification. + * + * @param specification The Specification that specifies the EntityComposite types of interest. + * + * @return An EntityDeclaration for the specified EntityComposite types. + */ + EntityDeclaration entities( Specification<? super EntityAssembly> specification ); + + /** + * Given a Specification for ServiceAssembly's, returns a ServiceDeclaration that can + * be used to work with all of the assemblies in this Layer matched by the specification. + * + * @param specification The Specification that specifies the ServiceComposite types of interest. + * + * @return An ServiceDeclaration for the specified ServiceComposite types. + */ + ServiceDeclaration services( Specification<? super ServiceAssembly> specification ); + + /** + * Given a Specification for TransientAssembly's, returns a TransientDeclaration that can + * be used to work with all of the assemblies in this Layer matched by the specification. + * + * @param specification The Specification that specifies the TransientComposite types of interest. + * + * @return An TransientDeclaration for the specified TransientComposite types. + */ + TransientDeclaration transients( Specification<? super TransientAssembly> specification ); + + /** + * Given a Specification for ValueAssembly's, returns a ValueDeclaration that can + * be used to work with all of the assemblies in this Layer matched by the specification. + * + * @param specification The Specification that specifies the ValueComposite types of interest. + * + * @return An ValueDeclaration for the specified ValueComposite types. + */ + ValueDeclaration values( Specification<? super ValueAssembly> specification ); + + /** + * Given a Specification for ObjectAssembly's, returns a ObjectDeclaration that can + * be used to work with all of the assemblies in this Layer matched by the specification. + * + * @param specification The Specification that specifies the Object types of interest. + * + * @return An ObjectDeclaration for the specified Object types. + */ + ObjectDeclaration objects( Specification<? super ObjectAssembly> specification ); + + /** + * Given a Specification for ImportedServiceAssembly's, returns a ImportedServiceDeclaration that can + * be used to work with all of the assemblies in this Layer matched by the specification. + * + * @param specification The Specification that specifies the Imported Service types of interest. + * + * @return An ImportedServiceDeclaration for the specified Imported Service types. + */ + ImportedServiceDeclaration importedServices( Specification<? super ImportedServiceAssembly> specification ); +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/bootstrap/src/main/java/org/qi4j/bootstrap/LayerName.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/qi4j/bootstrap/LayerName.java b/core/bootstrap/src/main/java/org/qi4j/bootstrap/LayerName.java new file mode 100644 index 0000000..8d1b8bf --- /dev/null +++ b/core/bootstrap/src/main/java/org/qi4j/bootstrap/LayerName.java @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2008, Rickard Ãberg. All Rights Reserved. + * + * Licensed 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; + +/** + * Set the name of the layer + */ +public final class LayerName + implements Assembler +{ + private final String name; + + public LayerName( String name ) + { + this.name = name; + } + + @Override + public void assemble( ModuleAssembly module ) + throws AssemblyException + { + module.layer().setName( name ); + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/a789141d/core/bootstrap/src/main/java/org/qi4j/bootstrap/ManyAssociationDeclarations.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/qi4j/bootstrap/ManyAssociationDeclarations.java b/core/bootstrap/src/main/java/org/qi4j/bootstrap/ManyAssociationDeclarations.java new file mode 100644 index 0000000..f95a6ac --- /dev/null +++ b/core/bootstrap/src/main/java/org/qi4j/bootstrap/ManyAssociationDeclarations.java @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2008, Rickard Ãberg. All Rights Reserved. + * + * Licensed 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; + +import java.lang.reflect.AccessibleObject; +import org.qi4j.api.common.MetaInfo; + +/** + * This provides declared {@link org.qi4j.api.association.ManyAssociation} information that the runtime can use. + */ +public interface ManyAssociationDeclarations +{ + MetaInfo metaInfoFor( AccessibleObject accessor ); +} \ No newline at end of file
