Search in sources :

Example 11 with MetaInfo

use of org.qi4j.api.common.MetaInfo in project qi4j-sdk by Qi4j.

the class ModuleAssemblyImpl method assembleModule.

ModuleModel assembleModule(AssemblyHelper helper) throws AssemblyException {
    List<TransientModel> transientModels = new ArrayList<>();
    List<ObjectModel> objectModels = new ArrayList<>();
    List<ValueModel> valueModels = new ArrayList<>();
    List<ServiceModel> serviceModels = new ArrayList<>();
    List<ImportedServiceModel> importedServiceModels = new ArrayList<>();
    if (name == null) {
        throw new AssemblyException("Module must have name set");
    }
    for (TransientAssemblyImpl compositeDeclaration : transientAssemblies.values()) {
        transientModels.add(compositeDeclaration.newTransientModel(metaInfoDeclaration, helper));
    }
    for (ValueAssemblyImpl valueDeclaration : valueAssemblies.values()) {
        valueModels.add(valueDeclaration.newValueModel(metaInfoDeclaration, helper));
    }
    List<EntityModel> entityModels = new ArrayList<>();
    for (EntityAssemblyImpl entityDeclaration : entityAssemblies.values()) {
        entityModels.add(entityDeclaration.newEntityModel(metaInfoDeclaration, metaInfoDeclaration, metaInfoDeclaration, metaInfoDeclaration, helper));
    }
    for (ObjectAssemblyImpl objectDeclaration : objectAssemblies.values()) {
        objectDeclaration.addObjectModel(objectModels);
    }
    for (ServiceAssemblyImpl serviceDeclaration : serviceAssemblies) {
        if (serviceDeclaration.identity == null) {
            serviceDeclaration.identity = generateId(serviceDeclaration.types());
        }
        serviceModels.add(serviceDeclaration.newServiceModel(metaInfoDeclaration, helper));
    }
    for (ImportedServiceAssemblyImpl importedServiceDeclaration : importedServiceAssemblies.values()) {
        importedServiceDeclaration.addImportedServiceModel(importedServiceModels);
    }
    ModuleModel moduleModel = new ModuleModel(name, metaInfo, new ActivatorsModel<>(activators), new TransientsModel(transientModels), new EntitiesModel(entityModels), new ObjectsModel(objectModels), new ValuesModel(valueModels), new ServicesModel(serviceModels), new ImportedServicesModel(importedServiceModels));
    // Check for duplicate service identities
    Set<String> identities = new HashSet<>();
    for (ServiceModel serviceModel : serviceModels) {
        String identity = serviceModel.identity();
        if (identities.contains(identity)) {
            throw new DuplicateServiceIdentityException("Duplicated service identity: " + identity + " in module " + moduleModel.name());
        }
        identities.add(identity);
    }
    for (ImportedServiceModel serviceModel : importedServiceModels) {
        String identity = serviceModel.identity();
        if (identities.contains(identity)) {
            throw new DuplicateServiceIdentityException("Duplicated service identity: " + identity + " in module " + moduleModel.name());
        }
        identities.add(identity);
    }
    for (ImportedServiceModel importedServiceModel : importedServiceModels) {
        boolean found = false;
        for (ObjectModel objectModel : objectModels) {
            if (first(objectModel.types()).equals(importedServiceModel.serviceImporter())) {
                found = true;
                break;
            }
        }
        if (!found) {
            @SuppressWarnings("raw") Class<? extends ServiceImporter> serviceFactoryType = importedServiceModel.serviceImporter();
            ObjectModel objectModel = new ObjectModel(serviceFactoryType, Visibility.module, new MetaInfo());
            objectModels.add(objectModel);
        }
    }
    return moduleModel;
}
Also used : ObjectModel(org.qi4j.runtime.object.ObjectModel) ValueModel(org.qi4j.runtime.value.ValueModel) ImportedServiceModel(org.qi4j.runtime.service.ImportedServiceModel) ArrayList(java.util.ArrayList) ImportedServicesModel(org.qi4j.runtime.service.ImportedServicesModel) MetaInfo(org.qi4j.api.common.MetaInfo) ObjectsModel(org.qi4j.runtime.object.ObjectsModel) ModuleModel(org.qi4j.runtime.structure.ModuleModel) EntitiesModel(org.qi4j.runtime.entity.EntitiesModel) AssemblyException(org.qi4j.bootstrap.AssemblyException) ServiceModel(org.qi4j.runtime.service.ServiceModel) ImportedServiceModel(org.qi4j.runtime.service.ImportedServiceModel) HashSet(java.util.HashSet) ImportedServicesModel(org.qi4j.runtime.service.ImportedServicesModel) ServicesModel(org.qi4j.runtime.service.ServicesModel) EntityModel(org.qi4j.runtime.entity.EntityModel) DuplicateServiceIdentityException(org.qi4j.api.service.DuplicateServiceIdentityException) ValuesModel(org.qi4j.runtime.value.ValuesModel) TransientModel(org.qi4j.runtime.composite.TransientModel) TransientsModel(org.qi4j.runtime.composite.TransientsModel)

Example 12 with MetaInfo

use of org.qi4j.api.common.MetaInfo in project qi4j-sdk by Qi4j.

the class ApplicationModel method newInstance.

@Override
public ApplicationInstance newInstance(Qi4j runtime, Object... importedServiceInstances) throws InvalidApplicationException {
    MetaInfo instanceMetaInfo = new MetaInfo(metaInfo);
    for (Object importedServiceInstance : importedServiceInstances) {
        instanceMetaInfo.set(importedServiceInstance);
    }
    ApplicationInstance applicationInstance = new ApplicationInstance(this, (Qi4jRuntime) runtime, instanceMetaInfo);
    // Create layer instances
    Map<LayerModel, LayerInstance> layerInstanceMap = new HashMap<>();
    Map<LayerModel, List<LayerInstance>> usedLayers = new HashMap<>();
    for (LayerModel layer : layers) {
        List<LayerInstance> usedLayerInstances = new ArrayList<>();
        usedLayers.put(layer, usedLayerInstances);
        UsedLayersInstance usedLayersInstance = layer.usedLayers().newInstance(usedLayerInstances);
        LayerInstance layerInstance = layer.newInstance(applicationInstance, usedLayersInstance);
        applicationInstance.addLayer(layerInstance);
        layerInstanceMap.put(layer, layerInstance);
    }
    // Resolve used layer instances
    for (LayerModel layer : layers) {
        List<LayerInstance> usedLayerInstances = usedLayers.get(layer);
        for (LayerModel usedLayer : layer.usedLayers().layers()) {
            LayerInstance layerInstance = layerInstanceMap.get(usedLayer);
            if (layerInstance == null) {
                throw new InvalidApplicationException("Could not find used layer:" + usedLayer.name());
            }
            usedLayerInstances.add(layerInstance);
        }
    }
    return applicationInstance;
}
Also used : HashMap(java.util.HashMap) MetaInfo(org.qi4j.api.common.MetaInfo) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) InvalidApplicationException(org.qi4j.api.common.InvalidApplicationException)

Example 13 with MetaInfo

use of org.qi4j.api.common.MetaInfo in project qi4j-sdk by Qi4j.

the class ValueAssemblyImpl method newAssociationModel.

public AssociationModel newAssociationModel(AccessibleObject accessor, Iterable<Class<? extends Constraint<?, ?>>> constraintClasses) {
    Iterable<Annotation> annotations = Annotations.findAccessorAndTypeAnnotationsIn(accessor);
    boolean optional = first(filter(isType(Optional.class), annotations)) != null;
    // Constraints for Association references
    ValueConstraintsModel valueConstraintsModel = constraintsFor(annotations, GenericAssociationInfo.associationTypeOf(accessor), ((Member) accessor).getName(), optional, constraintClasses, accessor);
    ValueConstraintsInstance valueConstraintsInstance = null;
    if (valueConstraintsModel.isConstrained()) {
        valueConstraintsInstance = valueConstraintsModel.newInstance();
    }
    // Constraints for the Association itself
    valueConstraintsModel = constraintsFor(annotations, Association.class, ((Member) accessor).getName(), optional, constraintClasses, accessor);
    ValueConstraintsInstance associationValueConstraintsInstance = null;
    if (valueConstraintsModel.isConstrained()) {
        associationValueConstraintsInstance = valueConstraintsModel.newInstance();
    }
    MetaInfo metaInfo = stateDeclarations.metaInfoFor(accessor);
    AssociationModel associationModel = new AssociationModel(accessor, valueConstraintsInstance, associationValueConstraintsInstance, metaInfo);
    return associationModel;
}
Also used : Association(org.qi4j.api.association.Association) ManyAssociation(org.qi4j.api.association.ManyAssociation) NamedAssociation(org.qi4j.api.association.NamedAssociation) Optional(org.qi4j.api.common.Optional) MetaInfo(org.qi4j.api.common.MetaInfo) ValueConstraintsModel(org.qi4j.runtime.composite.ValueConstraintsModel) ValueConstraintsInstance(org.qi4j.runtime.composite.ValueConstraintsInstance) Member(java.lang.reflect.Member) Annotation(java.lang.annotation.Annotation) NamedAssociationModel(org.qi4j.runtime.association.NamedAssociationModel) AssociationModel(org.qi4j.runtime.association.AssociationModel) ManyAssociationModel(org.qi4j.runtime.association.ManyAssociationModel)

Example 14 with MetaInfo

use of org.qi4j.api.common.MetaInfo in project qi4j-sdk by Qi4j.

the class ValueAssemblyImpl method newNamedAssociationModel.

public NamedAssociationModel newNamedAssociationModel(AccessibleObject accessor, Iterable<Class<? extends Constraint<?, ?>>> constraintClasses) {
    Iterable<Annotation> annotations = Annotations.findAccessorAndTypeAnnotationsIn(accessor);
    boolean optional = first(filter(isType(Optional.class), annotations)) != null;
    // Constraints for entities in NamedAssociation
    ValueConstraintsModel valueConstraintsModel = constraintsFor(annotations, GenericAssociationInfo.associationTypeOf(accessor), ((Member) accessor).getName(), optional, constraintClasses, accessor);
    ValueConstraintsInstance valueConstraintsInstance = null;
    if (valueConstraintsModel.isConstrained()) {
        valueConstraintsInstance = valueConstraintsModel.newInstance();
    }
    // Constraints for the NamedAssociation itself
    valueConstraintsModel = constraintsFor(annotations, NamedAssociation.class, ((Member) accessor).getName(), optional, constraintClasses, accessor);
    ValueConstraintsInstance namedValueConstraintsInstance = null;
    if (valueConstraintsModel.isConstrained()) {
        namedValueConstraintsInstance = valueConstraintsModel.newInstance();
    }
    MetaInfo metaInfo = stateDeclarations.metaInfoFor(accessor);
    NamedAssociationModel associationModel = new NamedAssociationModel(accessor, valueConstraintsInstance, namedValueConstraintsInstance, metaInfo);
    return associationModel;
}
Also used : Optional(org.qi4j.api.common.Optional) NamedAssociation(org.qi4j.api.association.NamedAssociation) MetaInfo(org.qi4j.api.common.MetaInfo) ValueConstraintsModel(org.qi4j.runtime.composite.ValueConstraintsModel) NamedAssociationModel(org.qi4j.runtime.association.NamedAssociationModel) ValueConstraintsInstance(org.qi4j.runtime.composite.ValueConstraintsInstance) Member(java.lang.reflect.Member) Annotation(java.lang.annotation.Annotation)

Example 15 with MetaInfo

use of org.qi4j.api.common.MetaInfo in project qi4j-sdk by Qi4j.

the class ValueAssemblyImpl method newPropertyModel.

@Override
protected PropertyModel newPropertyModel(AccessibleObject accessor, Iterable<Class<? extends Constraint<?, ?>>> constraintClasses) {
    Iterable<Annotation> annotations = Annotations.findAccessorAndTypeAnnotationsIn(accessor);
    boolean optional = first(filter(isType(Optional.class), annotations)) != null;
    ValueConstraintsModel valueConstraintsModel = constraintsFor(annotations, GenericPropertyInfo.propertyTypeOf(accessor), ((Member) accessor).getName(), optional, constraintClasses, accessor);
    ValueConstraintsInstance valueConstraintsInstance = null;
    if (valueConstraintsModel.isConstrained()) {
        valueConstraintsInstance = valueConstraintsModel.newInstance();
    }
    MetaInfo metaInfo = stateDeclarations.metaInfoFor(accessor);
    boolean useDefaults = metaInfo.get(UseDefaults.class) != null || stateDeclarations.useDefaults(accessor);
    Object initialValue = stateDeclarations.initialValueOf(accessor);
    return new PropertyModel(accessor, true, useDefaults, valueConstraintsInstance, metaInfo, initialValue);
}
Also used : Optional(org.qi4j.api.common.Optional) MetaInfo(org.qi4j.api.common.MetaInfo) PropertyModel(org.qi4j.runtime.property.PropertyModel) ValueConstraintsModel(org.qi4j.runtime.composite.ValueConstraintsModel) AccessibleObject(java.lang.reflect.AccessibleObject) ValueConstraintsInstance(org.qi4j.runtime.composite.ValueConstraintsInstance) Annotation(java.lang.annotation.Annotation)

Aggregations

MetaInfo (org.qi4j.api.common.MetaInfo)15 Annotation (java.lang.annotation.Annotation)9 Optional (org.qi4j.api.common.Optional)9 ValueConstraintsInstance (org.qi4j.runtime.composite.ValueConstraintsInstance)9 ValueConstraintsModel (org.qi4j.runtime.composite.ValueConstraintsModel)9 Member (java.lang.reflect.Member)7 ManyAssociation (org.qi4j.api.association.ManyAssociation)4 NamedAssociation (org.qi4j.api.association.NamedAssociation)4 ManyAssociationModel (org.qi4j.runtime.association.ManyAssociationModel)4 NamedAssociationModel (org.qi4j.runtime.association.NamedAssociationModel)4 AccessibleObject (java.lang.reflect.AccessibleObject)3 InvalidApplicationException (org.qi4j.api.common.InvalidApplicationException)3 PropertyModel (org.qi4j.runtime.property.PropertyModel)3 Type (java.lang.reflect.Type)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Association (org.qi4j.api.association.Association)2 AssociationModel (org.qi4j.runtime.association.AssociationModel)2 CompositeMethodsModel (org.qi4j.runtime.composite.CompositeMethodsModel)2 PropertiesModel (org.qi4j.runtime.property.PropertiesModel)2