use of org.qi4j.runtime.structure.ModuleModel in project qi4j-sdk by Qi4j.
the class ApplicationModelFactoryImpl method newApplicationModel.
@Override
public ApplicationDescriptor newApplicationModel(ApplicationAssembly assembly) throws AssemblyException {
AssemblyHelper helper = new AssemblyHelper();
ApplicationAssemblyImpl applicationAssembly = (ApplicationAssemblyImpl) assembly;
ActivatorsModel<Application> applicationActivators = new ActivatorsModel<Application>(applicationAssembly.activators());
List<LayerModel> layerModels = new ArrayList<LayerModel>();
final ApplicationModel applicationModel = new ApplicationModel(applicationAssembly.name(), applicationAssembly.version(), applicationAssembly.mode(), applicationAssembly.metaInfo(), applicationActivators, layerModels);
Map<LayerAssembly, LayerModel> mapAssemblyModel = new HashMap<LayerAssembly, LayerModel>();
Map<LayerAssembly, List<LayerModel>> mapUsedLayers = new HashMap<LayerAssembly, List<LayerModel>>();
// Build all layers
List<LayerAssemblyImpl> layerAssemblies = new ArrayList<LayerAssemblyImpl>(applicationAssembly.layerAssemblies());
for (LayerAssemblyImpl layerAssembly : layerAssemblies) {
List<LayerModel> usedLayers = new ArrayList<LayerModel>();
mapUsedLayers.put(layerAssembly, usedLayers);
UsedLayersModel usedLayersModel = new UsedLayersModel(usedLayers);
List<ModuleModel> moduleModels = new ArrayList<ModuleModel>();
String name = layerAssembly.name();
if (name == null) {
throw new AssemblyException("Layer must have name set");
}
ActivatorsModel<Layer> layerActivators = new ActivatorsModel<Layer>(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);
}
}
// 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;
}
use of org.qi4j.runtime.structure.ModuleModel in project qi4j-sdk by Qi4j.
the class ModuleAssemblyImpl method assembleModule.
ModuleModel assembleModule(AssemblyHelper helper) throws AssemblyException {
List<TransientModel> transientModels = new ArrayList<TransientModel>();
List<ObjectModel> objectModels = new ArrayList<ObjectModel>();
List<ValueModel> valueModels = new ArrayList<ValueModel>();
List<ServiceModel> serviceModels = new ArrayList<ServiceModel>();
List<ImportedServiceModel> importedServiceModels = new ArrayList<ImportedServiceModel>();
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<EntityModel>();
for (EntityAssemblyImpl entityDeclaration : entityAssemblies.values()) {
entityModels.add(entityDeclaration.newEntityModel(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<Module>(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<String>();
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) {
Class<? extends ServiceImporter> serviceFactoryType = importedServiceModel.serviceImporter();
ObjectModel objectModel = new ObjectModel(serviceFactoryType, Visibility.module, new MetaInfo());
objectModels.add(objectModel);
}
}
return moduleModel;
}
Aggregations