You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@polygene.apache.org by ni...@apache.org on 2015/07/31 04:58:57 UTC
[17/81] [abbrv] [partial] zest-java git commit: First round of
changes to move to org.apache.zest namespace.
http://git-wip-us.apache.org/repos/asf/zest-java/blob/061ddaa0/core/runtime/src/main/java/org/apache/zest/runtime/association/ManyAssociationModel.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/association/ManyAssociationModel.java b/core/runtime/src/main/java/org/apache/zest/runtime/association/ManyAssociationModel.java
new file mode 100644
index 0000000..fe35926
--- /dev/null
+++ b/core/runtime/src/main/java/org/apache/zest/runtime/association/ManyAssociationModel.java
@@ -0,0 +1,265 @@
+/*
+ * Copyright (c) 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.apache.zest.runtime.association;
+
+import java.lang.reflect.AccessibleObject;
+import java.lang.reflect.Field;
+import java.lang.reflect.Member;
+import java.lang.reflect.Method;
+import java.lang.reflect.Type;
+import java.lang.reflect.TypeVariable;
+import java.util.List;
+import org.apache.zest.api.association.AssociationDescriptor;
+import org.apache.zest.api.association.GenericAssociationInfo;
+import org.apache.zest.api.association.ManyAssociation;
+import org.apache.zest.api.common.MetaInfo;
+import org.apache.zest.api.common.QualifiedName;
+import org.apache.zest.api.constraint.ConstraintViolation;
+import org.apache.zest.api.constraint.ConstraintViolationException;
+import org.apache.zest.api.entity.Aggregated;
+import org.apache.zest.api.entity.EntityReference;
+import org.apache.zest.api.entity.Queryable;
+import org.apache.zest.api.property.Immutable;
+import org.apache.zest.api.util.Classes;
+import org.apache.zest.bootstrap.BindingException;
+import org.apache.zest.functional.Function2;
+import org.apache.zest.functional.Visitable;
+import org.apache.zest.functional.Visitor;
+import org.apache.zest.runtime.composite.ValueConstraintsInstance;
+import org.apache.zest.runtime.model.Binder;
+import org.apache.zest.runtime.model.Resolution;
+import org.apache.zest.runtime.structure.ModuleUnitOfWork;
+import org.apache.zest.runtime.unitofwork.BuilderEntityState;
+import org.apache.zest.spi.entity.EntityState;
+
+import static org.apache.zest.functional.Iterables.empty;
+import static org.apache.zest.functional.Iterables.first;
+
+/**
+ * Model for a ManyAssociation.
+ *
+ * <p>Equality is based on the ManyAssociation accessor object (associated type and name), not on the QualifiedName.</p>
+ */
+public final class ManyAssociationModel
+ implements AssociationDescriptor, AssociationInfo, Binder, Visitable<ManyAssociationModel>
+{
+ private final ValueConstraintsInstance associationConstraints;
+ private final MetaInfo metaInfo;
+ private Type type;
+ private final AccessibleObject accessor;
+ private QualifiedName qualifiedName;
+ private final ValueConstraintsInstance constraints;
+ private boolean queryable;
+ private boolean immutable;
+ private boolean aggregated;
+ private AssociationInfo builderInfo;
+
+ public ManyAssociationModel( AccessibleObject accessor,
+ ValueConstraintsInstance valueConstraintsInstance,
+ ValueConstraintsInstance associationConstraintsInstance,
+ MetaInfo metaInfo
+ )
+ {
+ this.metaInfo = metaInfo;
+ this.constraints = valueConstraintsInstance;
+ this.associationConstraints = associationConstraintsInstance;
+ this.accessor = accessor;
+ initialize();
+ }
+
+ private void initialize()
+ {
+ this.type = GenericAssociationInfo.associationTypeOf( accessor );
+ this.qualifiedName = QualifiedName.fromAccessor( accessor );
+ this.immutable = metaInfo.get( Immutable.class ) != null;
+ this.aggregated = metaInfo.get( Aggregated.class ) != null;
+
+ final Queryable queryable = accessor.getAnnotation( Queryable.class );
+ this.queryable = queryable == null || queryable.value();
+ }
+
+ @Override
+ public <T> T metaInfo( Class<T> infoType )
+ {
+ return metaInfo.get( infoType );
+ }
+
+ @Override
+ public QualifiedName qualifiedName()
+ {
+ return qualifiedName;
+ }
+
+ @Override
+ public Type type()
+ {
+ return type;
+ }
+
+ @Override
+ public boolean isImmutable()
+ {
+ return immutable;
+ }
+
+ @Override
+ public boolean isAggregated()
+ {
+ return aggregated;
+ }
+
+ @Override
+ public AccessibleObject accessor()
+ {
+ return accessor;
+ }
+
+ @Override
+ public boolean queryable()
+ {
+ return queryable;
+ }
+
+ public AssociationInfo getBuilderInfo()
+ {
+ return builderInfo;
+ }
+
+ public <T> ManyAssociation<T> newInstance( final ModuleUnitOfWork uow, EntityState state )
+ {
+ return new ManyAssociationInstance<>( state instanceof BuilderEntityState ? builderInfo : this, new Function2<EntityReference, Type, Object>()
+ {
+ @Override
+ public Object map( EntityReference entityReference, Type type )
+ {
+ return uow.get( Classes.RAW_CLASS.map( type ), entityReference.identity() );
+ }
+ }, state.manyAssociationValueOf( qualifiedName ) );
+ }
+
+ @Override
+ public void checkConstraints( Object composite )
+ throws ConstraintViolationException
+ {
+ if( constraints != null )
+ {
+ List<ConstraintViolation> violations = constraints.checkConstraints( composite );
+ if( !violations.isEmpty() )
+ {
+ Iterable<Class<?>> empty = empty();
+ throw new ConstraintViolationException( "", empty, (Member) accessor, violations );
+ }
+ }
+ }
+
+ public void checkAssociationConstraints( ManyAssociation manyAssociation )
+ throws ConstraintViolationException
+ {
+ if( associationConstraints != null )
+ {
+ List<ConstraintViolation> violations = associationConstraints.checkConstraints( manyAssociation );
+ if( !violations.isEmpty() )
+ {
+ Iterable<Class<?>> empty = empty();
+ throw new ConstraintViolationException( "", empty, (Member) accessor, violations );
+ }
+ }
+ }
+
+ @Override
+ public <ThrowableType extends Throwable> boolean accept( Visitor<? super ManyAssociationModel, ThrowableType> visitor )
+ throws ThrowableType
+ {
+ return visitor.visit( this );
+ }
+
+ @Override
+ public void bind( Resolution resolution )
+ throws BindingException
+ {
+ builderInfo = new AssociationInfo()
+ {
+ @Override
+ public boolean isImmutable()
+ {
+ return false;
+ }
+
+ @Override
+ public QualifiedName qualifiedName()
+ {
+ return qualifiedName;
+ }
+
+ @Override
+ public Type type()
+ {
+ return type;
+ }
+
+ @Override
+ public void checkConstraints( Object value )
+ throws ConstraintViolationException
+ {
+ ManyAssociationModel.this.checkConstraints( value );
+ }
+ };
+
+ if( type instanceof TypeVariable )
+ {
+ Class mainType = first( resolution.model().types() );
+ type = Classes.resolveTypeVariable( (TypeVariable) type, ( (Member) accessor ).getDeclaringClass(), mainType );
+ }
+ }
+
+ @Override
+ public boolean equals( Object o )
+ {
+ if( this == o )
+ {
+ return true;
+ }
+ if( o == null || getClass() != o.getClass() )
+ {
+ return false;
+ }
+
+ ManyAssociationModel that = (ManyAssociationModel) o;
+
+ return accessor.equals( that.accessor );
+ }
+
+ @Override
+ public int hashCode()
+ {
+ return accessor.hashCode();
+ }
+
+ @Override
+ public String toString()
+ {
+ if( accessor instanceof Field )
+ {
+ return ( (Field) accessor ).toGenericString();
+ }
+ else
+ {
+ return ( (Method) accessor ).toGenericString();
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/zest-java/blob/061ddaa0/core/runtime/src/main/java/org/apache/zest/runtime/association/ManyAssociationsModel.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/association/ManyAssociationsModel.java b/core/runtime/src/main/java/org/apache/zest/runtime/association/ManyAssociationsModel.java
new file mode 100644
index 0000000..220d3ec
--- /dev/null
+++ b/core/runtime/src/main/java/org/apache/zest/runtime/association/ManyAssociationsModel.java
@@ -0,0 +1,123 @@
+/*
+ * Copyright (c) 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.apache.zest.runtime.association;
+
+import java.lang.reflect.AccessibleObject;
+import java.lang.reflect.Member;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import org.apache.zest.api.association.AssociationDescriptor;
+import org.apache.zest.api.association.ManyAssociation;
+import org.apache.zest.api.common.QualifiedName;
+import org.apache.zest.functional.HierarchicalVisitor;
+import org.apache.zest.functional.VisitableHierarchy;
+import org.apache.zest.runtime.structure.ModuleUnitOfWork;
+import org.apache.zest.runtime.value.ValueStateInstance;
+import org.apache.zest.spi.entity.EntityState;
+
+/**
+ * Model for ManyAssociations.
+ */
+public final class ManyAssociationsModel
+ implements VisitableHierarchy<ManyAssociationsModel, ManyAssociationModel>
+{
+ private final Map<AccessibleObject, ManyAssociationModel> mapAccessorAssociationModel = new LinkedHashMap<>();
+
+ public ManyAssociationsModel()
+ {
+ }
+
+ public Iterable<ManyAssociationModel> manyAssociations()
+ {
+ return mapAccessorAssociationModel.values();
+ }
+
+ public void addManyAssociation( ManyAssociationModel model )
+ {
+ mapAccessorAssociationModel.put( model.accessor(), model );
+ }
+
+ @Override
+ public <ThrowableType extends Throwable> boolean accept( HierarchicalVisitor<? super ManyAssociationsModel, ? super ManyAssociationModel, ThrowableType> visitor )
+ throws ThrowableType
+ {
+ if( visitor.visitEnter( this ) )
+ {
+ for( ManyAssociationModel associationModel : mapAccessorAssociationModel.values() )
+ {
+ if( !associationModel.accept( visitor ) )
+ {
+ break;
+ }
+ }
+ }
+ return visitor.visitLeave( this );
+ }
+
+ public <T> ManyAssociation<T> newInstance( AccessibleObject accessor,
+ EntityState entityState,
+ ModuleUnitOfWork uow )
+ {
+ return mapAccessorAssociationModel.get( accessor ).newInstance( uow, entityState );
+ }
+
+ public ManyAssociationModel getManyAssociation( AccessibleObject accessor )
+ throws IllegalArgumentException
+ {
+ ManyAssociationModel manyAssociationModel = mapAccessorAssociationModel.get( accessor );
+ if( manyAssociationModel == null )
+ {
+ throw new IllegalArgumentException( "No many-association found with name:" + ( (Member) accessor ).getName() );
+ }
+ return manyAssociationModel;
+ }
+
+ public AssociationDescriptor getManyAssociationByName( String name )
+ throws IllegalArgumentException
+ {
+ for( ManyAssociationModel associationModel : mapAccessorAssociationModel.values() )
+ {
+ if( associationModel.qualifiedName().name().equals( name ) )
+ {
+ return associationModel;
+ }
+ }
+ throw new IllegalArgumentException( "No many-association found with name:" + name );
+ }
+
+ public AssociationDescriptor getManyAssociationByQualifiedName( QualifiedName name )
+ throws IllegalArgumentException
+ {
+ for( ManyAssociationModel associationModel : mapAccessorAssociationModel.values() )
+ {
+ if( associationModel.qualifiedName().equals( name ) )
+ {
+ return associationModel;
+ }
+ }
+ throw new IllegalArgumentException( "No many-association found with qualified name:" + name );
+ }
+
+ public void checkConstraints( ValueStateInstance state )
+ {
+ for( ManyAssociationModel manyAssociationModel : mapAccessorAssociationModel.values() )
+ {
+ manyAssociationModel.checkAssociationConstraints( state.manyAssociationFor( manyAssociationModel.accessor() ) );
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/zest-java/blob/061ddaa0/core/runtime/src/main/java/org/apache/zest/runtime/association/NamedAssociationInstance.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/association/NamedAssociationInstance.java b/core/runtime/src/main/java/org/apache/zest/runtime/association/NamedAssociationInstance.java
new file mode 100644
index 0000000..46e8b87
--- /dev/null
+++ b/core/runtime/src/main/java/org/apache/zest/runtime/association/NamedAssociationInstance.java
@@ -0,0 +1,237 @@
+/*
+ * Copyright (c) 2011-2013, Niclas Hedhman. All Rights Reserved.
+ * Copyright (c) 2014, Paul Merlin. 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.apache.zest.runtime.association;
+
+import java.lang.reflect.Type;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import org.apache.zest.api.association.AssociationDescriptor;
+import org.apache.zest.api.association.NamedAssociation;
+import org.apache.zest.api.association.NamedAssociationWrapper;
+import org.apache.zest.api.entity.EntityReference;
+import org.apache.zest.api.entity.Identity;
+import org.apache.zest.api.util.NullArgumentException;
+import org.apache.zest.functional.Function;
+import org.apache.zest.functional.Function2;
+import org.apache.zest.spi.entity.NamedAssociationState;
+
+import static org.apache.zest.functional.Iterables.map;
+
+public class NamedAssociationInstance<T>
+ extends AbstractAssociationInstance<T>
+ implements NamedAssociation<T>
+{
+
+ private final NamedAssociationState namedAssociationState;
+
+ public NamedAssociationInstance( AssociationInfo associationInfo,
+ Function2<EntityReference, Type, Object> associationFunction,
+ NamedAssociationState namedAssociationState
+ )
+ {
+ super( associationInfo, associationFunction );
+ this.namedAssociationState = namedAssociationState;
+ }
+
+ @Override
+ public Iterator<String> iterator()
+ {
+ return namedAssociationState.iterator();
+ }
+
+ @Override
+ public int count()
+ {
+ return namedAssociationState.count();
+ }
+
+ @Override
+ public boolean containsName( String name )
+ {
+ return namedAssociationState.containsName( name );
+ }
+
+ @Override
+ public boolean put( String name, T entity )
+ {
+ NullArgumentException.validateNotNull( "entity", entity );
+ checkImmutable();
+ checkType( entity );
+ associationInfo.checkConstraints( entity );
+ return namedAssociationState.put( name, new EntityReference( ( (Identity) entity ).identity().get() ) );
+ }
+
+ @Override
+ public boolean remove( String name )
+ {
+ checkImmutable();
+ return namedAssociationState.remove( name );
+ }
+
+ @Override
+ public T get( String name )
+ {
+ return getEntity( namedAssociationState.get( name ) );
+ }
+
+ @Override
+ public String nameOf( T entity )
+ {
+ return namedAssociationState.nameOf( getEntityReference( entity ) );
+ }
+
+ @Override
+ public Map<String, T> toMap()
+ {
+ Map<String, T> map = new HashMap<>();
+ for( String name : namedAssociationState )
+ {
+ map.put( name, getEntity( namedAssociationState.get( name ) ) );
+ }
+ return map;
+ }
+
+ @Override
+ public Iterable<EntityReference> references()
+ {
+ return map( new Function<String, EntityReference>()
+ {
+ @Override
+ public EntityReference map( String name )
+ {
+ return namedAssociationState.get( name );
+ }
+ }, namedAssociationState );
+ }
+
+ @Override
+ public EntityReference referenceOf( String name )
+ {
+ return namedAssociationState.get( name );
+ }
+
+ public Iterable<Map.Entry<String, EntityReference>> getEntityReferences()
+ {
+ return map( new Function<String, Map.Entry<String, EntityReference>>()
+ {
+ @Override
+ public Map.Entry<String, EntityReference> map( final String key )
+ {
+ final EntityReference value = namedAssociationState.get( key );
+ return new Map.Entry<String, EntityReference>()
+ {
+ @Override
+ public String getKey()
+ {
+ return key;
+ }
+
+ @Override
+ public EntityReference getValue()
+ {
+ return value;
+ }
+
+ @Override
+ public EntityReference setValue( EntityReference value )
+ {
+ throw new UnsupportedOperationException( "Immutable Map" );
+ }
+
+ @Override
+ public boolean equals( Object o )
+ {
+ if( o instanceof Map.Entry )
+ {
+ Map.Entry other = (Map.Entry) o;
+ return key.equals( other.getKey() );
+ }
+ return false;
+ }
+
+ @Override
+ public int hashCode()
+ {
+ return 997 * key.hashCode() + 981813497;
+ }
+ };
+ }
+ }, namedAssociationState );
+ }
+
+
+ @Override
+ public boolean equals( Object o )
+ {
+ if( this == o )
+ {
+ return true;
+ }
+ if( o == null || getClass() != o.getClass() )
+ {
+ return false;
+ }
+ NamedAssociation<?> that = (NamedAssociation) o;
+ // Unwrap if needed
+ while( that instanceof NamedAssociationWrapper )
+ {
+ that = ( (NamedAssociationWrapper) that ).next();
+ }
+ // Descriptor equality
+ NamedAssociationInstance<?> thatInstance = (NamedAssociationInstance) that;
+ AssociationDescriptor thatDescriptor = (AssociationDescriptor) thatInstance.associationInfo();
+ if( !associationInfo.equals( thatDescriptor ) )
+ {
+ return false;
+ }
+ // State equality
+ if( namedAssociationState.count() != thatInstance.namedAssociationState.count() )
+ {
+ return false;
+ }
+ for( String name : namedAssociationState )
+ {
+ if( !thatInstance.namedAssociationState.containsName( name ) )
+ {
+ return false;
+ }
+ EntityReference thisReference = namedAssociationState.get( name );
+ EntityReference thatReference = thatInstance.namedAssociationState.get( name );
+ if( !thisReference.equals( thatReference ) )
+ {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ @Override
+ public int hashCode()
+ {
+ int hash = associationInfo.hashCode() * 31; // Descriptor
+ for( String name : namedAssociationState )
+ {
+ hash += name.hashCode();
+ hash += namedAssociationState.get( name ).hashCode() * 7; // State
+ }
+ return hash;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/zest-java/blob/061ddaa0/core/runtime/src/main/java/org/apache/zest/runtime/association/NamedAssociationModel.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/association/NamedAssociationModel.java b/core/runtime/src/main/java/org/apache/zest/runtime/association/NamedAssociationModel.java
new file mode 100644
index 0000000..d4dd6a6
--- /dev/null
+++ b/core/runtime/src/main/java/org/apache/zest/runtime/association/NamedAssociationModel.java
@@ -0,0 +1,266 @@
+/*
+ * Copyright (c) 2011-2013, Niclas Hedhman. All Rights Reserved.
+ * Copyright (c) 2014, Paul Merlin. 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.apache.zest.runtime.association;
+
+import java.lang.reflect.AccessibleObject;
+import java.lang.reflect.Field;
+import java.lang.reflect.Member;
+import java.lang.reflect.Method;
+import java.lang.reflect.Type;
+import java.lang.reflect.TypeVariable;
+import java.util.List;
+import org.apache.zest.api.association.AssociationDescriptor;
+import org.apache.zest.api.association.GenericAssociationInfo;
+import org.apache.zest.api.association.NamedAssociation;
+import org.apache.zest.api.common.MetaInfo;
+import org.apache.zest.api.common.QualifiedName;
+import org.apache.zest.api.constraint.ConstraintViolation;
+import org.apache.zest.api.constraint.ConstraintViolationException;
+import org.apache.zest.api.entity.Aggregated;
+import org.apache.zest.api.entity.EntityReference;
+import org.apache.zest.api.entity.Queryable;
+import org.apache.zest.api.property.Immutable;
+import org.apache.zest.api.util.Classes;
+import org.apache.zest.bootstrap.BindingException;
+import org.apache.zest.functional.Function2;
+import org.apache.zest.functional.Visitable;
+import org.apache.zest.functional.Visitor;
+import org.apache.zest.runtime.composite.ValueConstraintsInstance;
+import org.apache.zest.runtime.model.Binder;
+import org.apache.zest.runtime.model.Resolution;
+import org.apache.zest.runtime.structure.ModuleUnitOfWork;
+import org.apache.zest.runtime.unitofwork.BuilderEntityState;
+import org.apache.zest.spi.entity.EntityState;
+
+import static org.apache.zest.functional.Iterables.empty;
+import static org.apache.zest.functional.Iterables.first;
+
+/**
+ * Model for a NamedAssociation.
+ *
+ * <p>Equality is based on the NamedAssociation accessor object (associated type and name), not on the QualifiedName.</p>
+ */
+public final class NamedAssociationModel
+ implements AssociationDescriptor, AssociationInfo, Binder, Visitable<NamedAssociationModel>
+{
+ private final ValueConstraintsInstance associationConstraints;
+ private final MetaInfo metaInfo;
+ private Type type;
+ private final AccessibleObject accessor;
+ private QualifiedName qualifiedName;
+ private final ValueConstraintsInstance constraints;
+ private boolean queryable;
+ private boolean immutable;
+ private boolean aggregated;
+ private AssociationInfo builderInfo;
+
+ public NamedAssociationModel( AccessibleObject accessor,
+ ValueConstraintsInstance valueConstraintsInstance,
+ ValueConstraintsInstance associationConstraintsInstance,
+ MetaInfo metaInfo
+ )
+ {
+ this.metaInfo = metaInfo;
+ this.constraints = valueConstraintsInstance;
+ this.associationConstraints = associationConstraintsInstance;
+ this.accessor = accessor;
+ initialize();
+ }
+
+ private void initialize()
+ {
+ this.type = GenericAssociationInfo.associationTypeOf( accessor );
+ this.qualifiedName = QualifiedName.fromAccessor( accessor );
+ this.immutable = metaInfo.get( Immutable.class ) != null;
+ this.aggregated = metaInfo.get( Aggregated.class ) != null;
+
+ final Queryable queryable = accessor.getAnnotation( Queryable.class );
+ this.queryable = queryable == null || queryable.value();
+ }
+
+ @Override
+ public <T> T metaInfo( Class<T> infoType )
+ {
+ return metaInfo.get( infoType );
+ }
+
+ @Override
+ public QualifiedName qualifiedName()
+ {
+ return qualifiedName;
+ }
+
+ @Override
+ public Type type()
+ {
+ return type;
+ }
+
+ @Override
+ public boolean isImmutable()
+ {
+ return immutable;
+ }
+
+ @Override
+ public boolean isAggregated()
+ {
+ return aggregated;
+ }
+
+ @Override
+ public AccessibleObject accessor()
+ {
+ return accessor;
+ }
+
+ @Override
+ public boolean queryable()
+ {
+ return queryable;
+ }
+
+ public AssociationInfo getBuilderInfo()
+ {
+ return builderInfo;
+ }
+
+ public <T> NamedAssociation<T> newInstance( final ModuleUnitOfWork uow, EntityState state )
+ {
+ return new NamedAssociationInstance<>( state instanceof BuilderEntityState ? builderInfo : this, new Function2<EntityReference, Type, Object>()
+ {
+ @Override
+ public Object map( EntityReference entityReference, Type type )
+ {
+ return uow.get( Classes.RAW_CLASS.map( type ), entityReference.identity() );
+ }
+ }, state.namedAssociationValueOf( qualifiedName ) );
+ }
+
+ @Override
+ public void checkConstraints( Object composite )
+ throws ConstraintViolationException
+ {
+ if( constraints != null )
+ {
+ List<ConstraintViolation> violations = constraints.checkConstraints( composite );
+ if( !violations.isEmpty() )
+ {
+ Iterable<Class<?>> empty = empty();
+ throw new ConstraintViolationException( "", empty, (Member) accessor, violations );
+ }
+ }
+ }
+
+ public void checkAssociationConstraints( NamedAssociation association )
+ throws ConstraintViolationException
+ {
+ if( associationConstraints != null )
+ {
+ List<ConstraintViolation> violations = associationConstraints.checkConstraints( association );
+ if( !violations.isEmpty() )
+ {
+ Iterable<Class<?>> empty = empty();
+ throw new ConstraintViolationException( "", empty, (Member) accessor, violations );
+ }
+ }
+ }
+
+ @Override
+ public <ThrowableType extends Throwable> boolean accept( Visitor<? super NamedAssociationModel, ThrowableType> visitor )
+ throws ThrowableType
+ {
+ return visitor.visit( this );
+ }
+
+ @Override
+ public void bind( Resolution resolution )
+ throws BindingException
+ {
+ builderInfo = new AssociationInfo()
+ {
+ @Override
+ public boolean isImmutable()
+ {
+ return false;
+ }
+
+ @Override
+ public QualifiedName qualifiedName()
+ {
+ return qualifiedName;
+ }
+
+ @Override
+ public Type type()
+ {
+ return type;
+ }
+
+ @Override
+ public void checkConstraints( Object value )
+ throws ConstraintViolationException
+ {
+ NamedAssociationModel.this.checkConstraints( value );
+ }
+ };
+
+ if( type instanceof TypeVariable )
+ {
+ Class mainType = first( resolution.model().types() );
+ type = Classes.resolveTypeVariable( (TypeVariable) type, ( (Member) accessor ).getDeclaringClass(), mainType );
+ }
+ }
+
+ @Override
+ public boolean equals( Object o )
+ {
+ if( this == o )
+ {
+ return true;
+ }
+ if( o == null || getClass() != o.getClass() )
+ {
+ return false;
+ }
+
+ NamedAssociationModel that = (NamedAssociationModel) o;
+
+ return accessor.equals( that.accessor );
+ }
+
+ @Override
+ public int hashCode()
+ {
+ return accessor.hashCode();
+ }
+
+ @Override
+ public String toString()
+ {
+ if( accessor instanceof Field )
+ {
+ return ( (Field) accessor ).toGenericString();
+ }
+ else
+ {
+ return ( (Method) accessor ).toGenericString();
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/zest-java/blob/061ddaa0/core/runtime/src/main/java/org/apache/zest/runtime/association/NamedAssociationsModel.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/association/NamedAssociationsModel.java b/core/runtime/src/main/java/org/apache/zest/runtime/association/NamedAssociationsModel.java
new file mode 100644
index 0000000..87722b0
--- /dev/null
+++ b/core/runtime/src/main/java/org/apache/zest/runtime/association/NamedAssociationsModel.java
@@ -0,0 +1,128 @@
+/*
+ * Copyright (c) 2011-2013, Niclas Hedhman. All Rights Reserved.
+ * Copyright (c) 2014, Paul Merlin. 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.apache.zest.runtime.association;
+
+import java.lang.reflect.AccessibleObject;
+import java.lang.reflect.Member;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import org.apache.zest.api.association.AssociationDescriptor;
+import org.apache.zest.api.association.NamedAssociation;
+import org.apache.zest.api.common.QualifiedName;
+import org.apache.zest.functional.HierarchicalVisitor;
+import org.apache.zest.functional.VisitableHierarchy;
+import org.apache.zest.runtime.structure.ModuleUnitOfWork;
+import org.apache.zest.runtime.value.ValueStateInstance;
+import org.apache.zest.spi.entity.EntityState;
+
+/**
+ * Model for NamedAssociations.
+ */
+public final class NamedAssociationsModel
+ implements VisitableHierarchy<NamedAssociationsModel, NamedAssociationModel>
+{
+ private final Map<AccessibleObject, NamedAssociationModel> mapAccessorAssociationModel = new LinkedHashMap<>();
+
+ public NamedAssociationsModel()
+ {
+ }
+
+ public Iterable<NamedAssociationModel> namedAssociations()
+ {
+ return mapAccessorAssociationModel.values();
+ }
+
+ public void addNamedAssociation( NamedAssociationModel model )
+ {
+ mapAccessorAssociationModel.put( model.accessor(), model );
+ }
+
+ @Override
+ public <ThrowableType extends Throwable> boolean accept( HierarchicalVisitor<? super NamedAssociationsModel, ? super NamedAssociationModel, ThrowableType> visitor )
+ throws ThrowableType
+ {
+ if( visitor.visitEnter( this ) )
+ {
+ for( NamedAssociationModel associationModel : mapAccessorAssociationModel.values() )
+ {
+ if( !associationModel.accept( visitor ) )
+ {
+ break;
+ }
+ }
+ }
+ return visitor.visitLeave( this );
+ }
+
+ public <T> NamedAssociation<T> newInstance( AccessibleObject accessor,
+ EntityState entityState,
+ ModuleUnitOfWork uow )
+ {
+ return mapAccessorAssociationModel.get( accessor ).newInstance( uow, entityState );
+ }
+
+ public NamedAssociationModel getNamedAssociation( AccessibleObject accessor )
+ throws IllegalArgumentException
+ {
+ if( false )
+ {
+ return (NamedAssociationModel) getNamedAssociationByName( QualifiedName.fromAccessor( accessor ).name() );
+ }
+ NamedAssociationModel namedAssociationModel = mapAccessorAssociationModel.get( accessor );
+ if( namedAssociationModel == null )
+ {
+ throw new IllegalArgumentException( "No named-association found with name:" + ( (Member) accessor ).getName() );
+ }
+ return namedAssociationModel;
+ }
+
+ public AssociationDescriptor getNamedAssociationByName( String name )
+ throws IllegalArgumentException
+ {
+ for( NamedAssociationModel associationModel : mapAccessorAssociationModel.values() )
+ {
+ if( associationModel.qualifiedName().name().equals( name ) )
+ {
+ return associationModel;
+ }
+ }
+ throw new IllegalArgumentException( "No named-association found with name:" + name );
+ }
+
+ public AssociationDescriptor getNamedAssociationByQualifiedName( QualifiedName name )
+ throws IllegalArgumentException
+ {
+ for( NamedAssociationModel associationModel : mapAccessorAssociationModel.values() )
+ {
+ if( associationModel.qualifiedName().equals( name ) )
+ {
+ return associationModel;
+ }
+ }
+ throw new IllegalArgumentException( "No named-association found with qualified name:" + name );
+ }
+
+ public void checkConstraints( ValueStateInstance state )
+ {
+ for( NamedAssociationModel associationModel : mapAccessorAssociationModel.values() )
+ {
+ associationModel.checkAssociationConstraints( state.namedAssociationFor( associationModel.accessor() ) );
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/zest-java/blob/061ddaa0/core/runtime/src/main/java/org/apache/zest/runtime/bootstrap/AndAppliesToFilter.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/bootstrap/AndAppliesToFilter.java b/core/runtime/src/main/java/org/apache/zest/runtime/bootstrap/AndAppliesToFilter.java
new file mode 100644
index 0000000..76c6207
--- /dev/null
+++ b/core/runtime/src/main/java/org/apache/zest/runtime/bootstrap/AndAppliesToFilter.java
@@ -0,0 +1,41 @@
+/*
+ * 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.apache.zest.runtime.bootstrap;
+
+import java.lang.reflect.Method;
+import org.apache.zest.api.common.AppliesToFilter;
+
+/**
+ * JAVADOC
+ */
+final class AndAppliesToFilter
+ implements AppliesToFilter
+{
+ private final AppliesToFilter left;
+ private final AppliesToFilter right;
+
+ AndAppliesToFilter( AppliesToFilter left, AppliesToFilter right )
+ {
+ this.left = left;
+ this.right = right;
+ }
+
+ @Override
+ public boolean appliesTo( Method method, Class<?> mixin, Class<?> compositeType, Class<?> fragmentClass )
+ {
+ return left.appliesTo( method, mixin, compositeType, fragmentClass ) &&
+ right.appliesTo( method, mixin, compositeType, fragmentClass );
+ }
+}
http://git-wip-us.apache.org/repos/asf/zest-java/blob/061ddaa0/core/runtime/src/main/java/org/apache/zest/runtime/bootstrap/AnnotationAppliesToFilter.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/bootstrap/AnnotationAppliesToFilter.java b/core/runtime/src/main/java/org/apache/zest/runtime/bootstrap/AnnotationAppliesToFilter.java
new file mode 100644
index 0000000..622eecb
--- /dev/null
+++ b/core/runtime/src/main/java/org/apache/zest/runtime/bootstrap/AnnotationAppliesToFilter.java
@@ -0,0 +1,41 @@
+/*
+ * 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.apache.zest.runtime.bootstrap;
+
+import java.lang.reflect.Method;
+import org.apache.zest.api.common.AppliesToFilter;
+
+/**
+ * JAVADOC
+ */
+final class AnnotationAppliesToFilter
+ implements AppliesToFilter
+{
+ @SuppressWarnings( "raw" )
+ private final Class annotationType;
+
+ @SuppressWarnings( "raw" )
+ AnnotationAppliesToFilter( Class type )
+ {
+ this.annotationType = type;
+ }
+
+ @Override
+ @SuppressWarnings( "unchecked" )
+ public boolean appliesTo( Method method, Class<?> mixin, Class<?> compositeType, Class<?> fragmentClass )
+ {
+ return method.getAnnotation( annotationType ) != null;
+ }
+}
http://git-wip-us.apache.org/repos/asf/zest-java/blob/061ddaa0/core/runtime/src/main/java/org/apache/zest/runtime/bootstrap/ApplicationAssemblyFactoryImpl.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/bootstrap/ApplicationAssemblyFactoryImpl.java b/core/runtime/src/main/java/org/apache/zest/runtime/bootstrap/ApplicationAssemblyFactoryImpl.java
new file mode 100644
index 0000000..7b1c7a4
--- /dev/null
+++ b/core/runtime/src/main/java/org/apache/zest/runtime/bootstrap/ApplicationAssemblyFactoryImpl.java
@@ -0,0 +1,73 @@
+/*
+ * 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.apache.zest.runtime.bootstrap;
+
+import org.apache.zest.bootstrap.ApplicationAssembly;
+import org.apache.zest.bootstrap.ApplicationAssemblyFactory;
+import org.apache.zest.bootstrap.Assembler;
+import org.apache.zest.bootstrap.AssemblyException;
+import org.apache.zest.bootstrap.LayerAssembly;
+import org.apache.zest.bootstrap.ModuleAssembly;
+
+/**
+ * Factory for ApplicationAssembly.
+ */
+public final class ApplicationAssemblyFactoryImpl
+ implements ApplicationAssemblyFactory
+{
+ @Override
+ public ApplicationAssembly newApplicationAssembly( Assembler assembler )
+ throws AssemblyException
+ {
+ return newApplicationAssembly( new Assembler[][][]{ { { assembler } } } );
+ }
+
+ @Override
+ public ApplicationAssembly newApplicationAssembly( Assembler[][][] assemblers )
+ throws AssemblyException
+ {
+ ApplicationAssembly applicationAssembly = newApplicationAssembly();
+
+ // Build all layers bottom-up
+ LayerAssembly below = null;
+ for( int layer = assemblers.length - 1; layer >= 0; layer-- )
+ {
+ // Create Layer
+ LayerAssembly layerAssembly = applicationAssembly.layer( "Layer " + ( layer + 1 ) );
+ for( int module = 0; module < assemblers[ layer ].length; module++ )
+ {
+ // Create Module
+ ModuleAssembly moduleAssembly = layerAssembly.module( "Module " + ( module + 1 ) );
+ for( Assembler assembler : assemblers[ layer ][ module ] )
+ {
+ // Register Assembler
+ assembler.assemble( moduleAssembly );
+ }
+ }
+ if( below != null )
+ {
+ layerAssembly.uses( below ); // Link layers
+ }
+ below = layerAssembly;
+ }
+ return applicationAssembly;
+ }
+
+ @Override
+ public ApplicationAssembly newApplicationAssembly()
+ {
+ return new ApplicationAssemblyImpl();
+ }
+}
http://git-wip-us.apache.org/repos/asf/zest-java/blob/061ddaa0/core/runtime/src/main/java/org/apache/zest/runtime/bootstrap/ApplicationAssemblyImpl.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/bootstrap/ApplicationAssemblyImpl.java b/core/runtime/src/main/java/org/apache/zest/runtime/bootstrap/ApplicationAssemblyImpl.java
new file mode 100644
index 0000000..8fe7f7d
--- /dev/null
+++ b/core/runtime/src/main/java/org/apache/zest/runtime/bootstrap/ApplicationAssemblyImpl.java
@@ -0,0 +1,152 @@
+/*
+ * Copyright (c) 2007, Rickard Öberg. All Rights Reserved.
+ * Copyright (c) 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.apache.zest.runtime.bootstrap;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import org.apache.zest.api.activation.Activator;
+import org.apache.zest.api.common.MetaInfo;
+import org.apache.zest.api.structure.Application;
+import org.apache.zest.bootstrap.ApplicationAssembly;
+import org.apache.zest.bootstrap.AssemblyVisitor;
+import org.apache.zest.bootstrap.LayerAssembly;
+import org.apache.zest.bootstrap.ModuleAssembly;
+
+/**
+ * The representation of an entire application. From
+ * this you can set information about the application
+ * and create LayerAssemblies.
+ */
+public final class ApplicationAssemblyImpl
+ implements ApplicationAssembly
+{
+ private final Map<String, LayerAssemblyImpl> layerAssemblies = new LinkedHashMap<>();
+ private String name = "Application";
+ private String version = "1.0"; // Default version
+ private Application.Mode mode;
+ private final MetaInfo metaInfo = new MetaInfo();
+ private final List<Class<? extends Activator<Application>>> activators = new ArrayList<>();
+
+ public ApplicationAssemblyImpl()
+ {
+ mode = Application.Mode.valueOf( System.getProperty( "mode", "production" ) );
+ }
+
+ @Override
+ public LayerAssembly layer( String name )
+ {
+ if( name != null )
+ {
+ LayerAssemblyImpl existing = layerAssemblies.get( name );
+ if( existing != null )
+ {
+ return existing;
+ }
+ }
+ LayerAssemblyImpl layerAssembly = new LayerAssemblyImpl( this, name );
+ layerAssemblies.put( name, layerAssembly );
+ return layerAssembly;
+ }
+
+ @Override
+ public ModuleAssembly module( String layerName, String moduleName )
+ {
+ return layer( layerName ).module( moduleName );
+ }
+
+ @Override
+ public ApplicationAssembly setName( String name )
+ {
+ this.name = name;
+ return this;
+ }
+
+ @Override
+ public ApplicationAssembly setVersion( String version )
+ {
+ this.version = version;
+ return this;
+ }
+
+ @Override
+ public ApplicationAssembly setMode( Application.Mode mode )
+ {
+ this.mode = mode;
+ return this;
+ }
+
+ @Override
+ public ApplicationAssembly setMetaInfo( Object info )
+ {
+ metaInfo.set( info );
+ return this;
+ }
+
+ @Override
+ @SafeVarargs
+ public final ApplicationAssembly withActivators( Class<? extends Activator<Application>>... activators )
+ {
+ this.activators.addAll( Arrays.asList( activators ) );
+ return this;
+ }
+
+ @Override
+ public <ThrowableType extends Throwable> void visit( AssemblyVisitor<ThrowableType> visitor )
+ throws ThrowableType
+ {
+ visitor.visitApplication( this );
+ for( LayerAssemblyImpl layerAssembly : layerAssemblies.values() )
+ {
+ layerAssembly.visit( visitor );
+ }
+ }
+
+ public Collection<LayerAssemblyImpl> layerAssemblies()
+ {
+ return layerAssemblies.values();
+ }
+
+ public List<Class<? extends Activator<Application>>> activators()
+ {
+ return activators;
+ }
+
+ public MetaInfo metaInfo()
+ {
+ return metaInfo;
+ }
+
+ @Override
+ public String name()
+ {
+ return name;
+ }
+
+ public String version()
+ {
+ return version;
+ }
+
+ @Override
+ public Application.Mode mode()
+ {
+ return mode;
+ }
+}
http://git-wip-us.apache.org/repos/asf/zest-java/blob/061ddaa0/core/runtime/src/main/java/org/apache/zest/runtime/bootstrap/ApplicationModelFactoryImpl.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/bootstrap/ApplicationModelFactoryImpl.java b/core/runtime/src/main/java/org/apache/zest/runtime/bootstrap/ApplicationModelFactoryImpl.java
new file mode 100644
index 0000000..d1eb738
--- /dev/null
+++ b/core/runtime/src/main/java/org/apache/zest/runtime/bootstrap/ApplicationModelFactoryImpl.java
@@ -0,0 +1,192 @@
+/*
+ * Copyright (c) 2007, Rickard Öberg. All Rights Reserved.
+ * Copyright (c) 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.apache.zest.runtime.bootstrap;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import org.apache.zest.api.composite.ModelDescriptor;
+import org.apache.zest.api.structure.Application;
+import org.apache.zest.api.structure.ApplicationDescriptor;
+import org.apache.zest.api.structure.Layer;
+import org.apache.zest.bootstrap.ApplicationAssembly;
+import org.apache.zest.bootstrap.ApplicationModelFactory;
+import org.apache.zest.bootstrap.AssemblyException;
+import org.apache.zest.bootstrap.BindingException;
+import org.apache.zest.bootstrap.LayerAssembly;
+import org.apache.zest.functional.HierarchicalVisitor;
+import org.apache.zest.runtime.activation.ActivatorsModel;
+import org.apache.zest.runtime.composite.CompositeMethodModel;
+import org.apache.zest.runtime.injection.InjectedFieldModel;
+import org.apache.zest.runtime.model.Binder;
+import org.apache.zest.runtime.model.Resolution;
+import org.apache.zest.runtime.structure.ApplicationModel;
+import org.apache.zest.runtime.structure.LayerModel;
+import org.apache.zest.runtime.structure.ModuleModel;
+import org.apache.zest.runtime.structure.UsedLayersModel;
+
+/**
+ * Factory for Applications.
+ */
+public final class ApplicationModelFactoryImpl
+ implements ApplicationModelFactory
+{
+ @Override
+ public ApplicationDescriptor newApplicationModel( ApplicationAssembly assembly )
+ throws AssemblyException
+ {
+ AssemblyHelper helper = new AssemblyHelper();
+
+ ApplicationAssemblyImpl applicationAssembly = (ApplicationAssemblyImpl) assembly;
+ ActivatorsModel<Application> applicationActivators = new ActivatorsModel<>( applicationAssembly.activators() );
+ List<LayerModel> layerModels = new ArrayList<>();
+ final ApplicationModel applicationModel = new ApplicationModel( applicationAssembly.name(),
+ applicationAssembly.version(),
+ applicationAssembly.mode(),
+ applicationAssembly.metaInfo(),
+ applicationActivators,
+ layerModels );
+ Map<LayerAssembly, LayerModel> mapAssemblyModel = new HashMap<>();
+ Map<LayerAssembly, List<LayerModel>> mapUsedLayers = new HashMap<>();
+
+ // Build all layers
+ List<LayerAssemblyImpl> layerAssemblies = new ArrayList<>( applicationAssembly.layerAssemblies() );
+ for( LayerAssemblyImpl layerAssembly : layerAssemblies )
+ {
+ List<LayerModel> usedLayers = new ArrayList<>();
+ mapUsedLayers.put( layerAssembly, usedLayers );
+
+ UsedLayersModel usedLayersModel = new UsedLayersModel( usedLayers );
+ List<ModuleModel> moduleModels = new ArrayList<>();
+ String name = layerAssembly.name();
+ if( name == null )
+ {
+ throw new AssemblyException( "Layer must have name set" );
+ }
+ ActivatorsModel<Layer> layerActivators = new ActivatorsModel<>( layerAssembly.activators() );
+ LayerModel layerModel = new LayerModel( name, layerAssembly.metaInfo(), usedLayersModel, layerActivators, moduleModels );
+
+ for( ModuleAssemblyImpl moduleAssembly : layerAssembly.moduleAssemblies() )
+ {
+ moduleModels.add( moduleAssembly.assembleModule( helper ) );
+ }
+ mapAssemblyModel.put( layerAssembly, layerModel );
+ layerModels.add( layerModel );
+ }
+
+ // Populate used layer lists
+ for( LayerAssemblyImpl layerAssembly : layerAssemblies )
+ {
+ Set<LayerAssembly> usesLayers = layerAssembly.uses();
+ List<LayerModel> usedLayers = mapUsedLayers.get( layerAssembly );
+ for( LayerAssembly usesLayer : usesLayers )
+ {
+ LayerModel layerModel = mapAssemblyModel.get( usesLayer );
+ usedLayers.add( layerModel );
+ }
+ }
+
+ // Bind model
+ // This will resolve all dependencies
+ try
+ {
+// applicationModel.bind();
+ applicationModel.accept( new BindingVisitor( applicationModel ) );
+ }
+ catch( BindingException e )
+ {
+ throw new AssemblyException( "Unable to bind: " + applicationModel, e );
+ }
+
+ return applicationModel;
+ }
+
+ private static class BindingVisitor
+ implements HierarchicalVisitor<Object, Object, BindingException>
+ {
+ private LayerModel layer;
+ private ModuleModel module;
+ private ModelDescriptor objectDescriptor;
+ private CompositeMethodModel compositeMethodModel;
+
+ private Resolution resolution;
+ private final ApplicationModel applicationModel;
+
+ private BindingVisitor( ApplicationModel applicationModel )
+ {
+ this.applicationModel = applicationModel;
+ }
+
+ @Override
+ public boolean visitEnter( Object visited )
+ throws BindingException
+ {
+ if( visited instanceof Binder )
+ {
+ Binder binder = (Binder) visited;
+ binder.bind( resolution );
+
+ return false;
+ }
+ else if( visited instanceof CompositeMethodModel )
+ {
+ compositeMethodModel = (CompositeMethodModel) visited;
+ resolution = new Resolution( applicationModel, layer, module, objectDescriptor, compositeMethodModel, null );
+ }
+ else if( visited instanceof ModelDescriptor )
+ {
+ objectDescriptor = (ModelDescriptor) visited;
+ resolution = new Resolution( applicationModel, layer, module, objectDescriptor, null, null );
+ }
+ else if( visited instanceof InjectedFieldModel )
+ {
+ InjectedFieldModel fieldModel = (InjectedFieldModel) visited;
+ fieldModel.bind( new Resolution( applicationModel, layer, module,
+ objectDescriptor, compositeMethodModel, fieldModel.field() ) );
+ }
+ else if( visited instanceof ModuleModel )
+ {
+ module = (ModuleModel) visited;
+ }
+ else if( visited instanceof LayerModel )
+ {
+ layer = (LayerModel) visited;
+ }
+
+ return true;
+ }
+
+ @Override
+ public boolean visitLeave( Object visited )
+ throws BindingException
+ {
+ return true;
+ }
+
+ @Override
+ public boolean visit( Object visited )
+ throws BindingException
+ {
+ if( visited instanceof Binder )
+ {
+ ( (Binder) visited ).bind( resolution );
+ }
+ return true;
+ }
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/zest-java/blob/061ddaa0/core/runtime/src/main/java/org/apache/zest/runtime/bootstrap/AssemblyHelper.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/bootstrap/AssemblyHelper.java b/core/runtime/src/main/java/org/apache/zest/runtime/bootstrap/AssemblyHelper.java
new file mode 100644
index 0000000..374ac66
--- /dev/null
+++ b/core/runtime/src/main/java/org/apache/zest/runtime/bootstrap/AssemblyHelper.java
@@ -0,0 +1,198 @@
+/*
+ * Copyright (c) 2010, 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.apache.zest.runtime.bootstrap;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.lang.reflect.Type;
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.zest.api.common.AppliesTo;
+import org.apache.zest.api.common.AppliesToFilter;
+import org.apache.zest.api.common.ConstructionException;
+import org.apache.zest.api.constraint.Constraint;
+import org.apache.zest.runtime.composite.ConcernModel;
+import org.apache.zest.runtime.composite.ConstraintDeclaration;
+import org.apache.zest.runtime.composite.FragmentClassLoader;
+import org.apache.zest.runtime.composite.MixinModel;
+import org.apache.zest.runtime.composite.SideEffectModel;
+
+/**
+ * This helper is used when building the application model. It keeps track
+ * of already created classloaders and various models
+ */
+public class AssemblyHelper
+{
+ Map<Class, Class> instantiationClasses = new HashMap<>();
+ Map<Class, ConstraintDeclaration> constraintDeclarations = new HashMap<>();
+ Map<ClassLoader, FragmentClassLoader> modifierClassLoaders = new HashMap<>();
+ Map<Class<?>, AppliesToFilter> appliesToInstances = new HashMap<>();
+
+ public MixinModel getMixinModel( Class mixinClass )
+ {
+ return new MixinModel( mixinClass, instantiationClass( mixinClass ) );
+ }
+
+ public ConcernModel getConcernModel( Class concernClass )
+ {
+ return new ConcernModel( concernClass, instantiationClass( concernClass ) );
+ }
+
+ public SideEffectModel getSideEffectModel( Class sideEffectClass )
+ {
+ return new SideEffectModel( sideEffectClass, instantiationClass( sideEffectClass ) );
+ }
+
+ private Class instantiationClass( Class fragmentClass )
+ {
+ Class instantiationClass = fragmentClass;
+ if( !InvocationHandler.class.isAssignableFrom( fragmentClass ) )
+ {
+ instantiationClass = instantiationClasses.get( fragmentClass );
+
+ if( instantiationClass == null )
+ {
+ try
+ {
+ FragmentClassLoader fragmentLoader = getModifierClassLoader( fragmentClass.getClassLoader() );
+ instantiationClass = fragmentLoader.loadFragmentClass( fragmentClass );
+ instantiationClasses.put( fragmentClass, instantiationClass );
+ }
+ catch( ClassNotFoundException | VerifyError e )
+ {
+ throw new ConstructionException( "Could not generate mixin subclass " + fragmentClass.getName(), e );
+ }
+ }
+ }
+ return instantiationClass;
+ }
+
+ private FragmentClassLoader getModifierClassLoader( ClassLoader classLoader )
+ {
+ FragmentClassLoader cl = modifierClassLoaders.get( classLoader );
+ if( cl == null )
+ {
+ cl = new FragmentClassLoader( classLoader );
+ modifierClassLoaders.put( classLoader, cl );
+ }
+ return cl;
+ }
+
+ public boolean appliesTo( Class<?> fragmentClass, Method method, Iterable<Class<?>> types, Class<?> mixinClass )
+ {
+ AppliesToFilter appliesToFilter = appliesToInstances.get( fragmentClass );
+ if( appliesToFilter == null )
+ {
+ appliesToFilter = createAppliesToFilter( fragmentClass );
+ appliesToInstances.put( fragmentClass, appliesToFilter );
+ }
+ for( Class<?> compositeType : types )
+ {
+ if( appliesToFilter.appliesTo( method, mixinClass, compositeType, fragmentClass ) )
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public AppliesToFilter createAppliesToFilter( Class<?> fragmentClass )
+ {
+ AppliesToFilter result = null;
+ if( !InvocationHandler.class.isAssignableFrom( fragmentClass ) )
+ {
+ result = new TypedFragmentAppliesToFilter();
+ if( Modifier.isAbstract( fragmentClass.getModifiers() ) )
+ {
+ result = new AndAppliesToFilter( result, new ImplementsMethodAppliesToFilter() );
+ }
+ }
+ result = applyAppliesTo( result, fragmentClass );
+ if( result == null )
+ {
+ return AppliesToFilter.ALWAYS;
+ }
+ return result;
+ }
+
+ private AppliesToFilter applyAppliesTo( AppliesToFilter existing, Class<?> modifierClass )
+ {
+ AppliesTo appliesTo = modifierClass.getAnnotation( AppliesTo.class );
+ if( appliesTo != null )
+ {
+ // Use "or" for all filters specified in the annotation
+ AppliesToFilter appliesToAnnotation = null;
+ for( Class<?> appliesToClass : appliesTo.value() )
+ {
+ AppliesToFilter filter;
+ if( AppliesToFilter.class.isAssignableFrom( appliesToClass ) )
+ {
+ try
+ {
+ filter = (AppliesToFilter) appliesToClass.newInstance();
+ }
+ catch( Exception e )
+ {
+ throw new ConstructionException( e );
+ }
+ }
+ else if( Annotation.class.isAssignableFrom( appliesToClass ) )
+ {
+ filter = new AnnotationAppliesToFilter( appliesToClass );
+ }
+ else // Type check
+ {
+ filter = new TypeCheckAppliesToFilter( appliesToClass );
+ }
+
+ if( appliesToAnnotation == null )
+ {
+ appliesToAnnotation = filter;
+ }
+ else
+ {
+ appliesToAnnotation = new OrAppliesToFilter( appliesToAnnotation, filter );
+ }
+ }
+ // Add to the rest of the rules using "and"
+ if( existing == null )
+ {
+ return appliesToAnnotation;
+ }
+ else
+ {
+ return new AndAppliesToFilter( existing, appliesToAnnotation );
+ }
+ }
+ return existing;
+ }
+
+ public boolean appliesTo( Class<? extends Constraint<?, ?>> constraint,
+ Class<? extends Annotation> annotationType,
+ Type valueType
+ )
+ {
+ ConstraintDeclaration constraintDeclaration = constraintDeclarations.get( constraint );
+ if( constraintDeclaration == null )
+ {
+ constraintDeclaration = new ConstraintDeclaration( constraint );
+ constraintDeclarations.put( constraint, constraintDeclaration );
+ }
+
+ return constraintDeclaration.appliesTo( annotationType, valueType );
+ }
+}