use of org.mule.runtime.config.internal.model.ApplicationModel in project mule by mulesoft.
the class LazyMuleArtifactContext method createComponents.
private List<String> createComponents(Optional<Predicate> predicateOptional, Optional<Location> locationOptional, Optional<ComponentModelInitializerAdapter> parentComponentModelInitializerAdapter) {
checkState(predicateOptional.isPresent() != locationOptional.isPresent(), "predicate or location has to be passed");
List<String> alreadyCreatedApplicationComponents = new ArrayList<>();
alreadyCreatedApplicationComponents.addAll(trackingPostProcessor.getBeansTracked());
reverse(alreadyCreatedApplicationComponents);
trackingPostProcessor.startTracking();
Reference<List<String>> createdComponents = new Reference<>();
withContextClassLoader(muleContext.getExecutionClassLoader(), () -> {
applicationModel.executeOnEveryMuleComponentTree(componentModel -> componentModel.setEnabled(false));
ConfigurationDependencyResolver dependencyResolver = new ConfigurationDependencyResolver(this.applicationModel, componentBuildingDefinitionRegistry);
MinimalApplicationModelGenerator minimalApplicationModelGenerator = new MinimalApplicationModelGenerator(dependencyResolver);
Reference<ApplicationModel> minimalApplicationModel = new Reference<>();
predicateOptional.ifPresent(predicate -> minimalApplicationModel.set(minimalApplicationModelGenerator.getMinimalModel(predicate)));
locationOptional.ifPresent(location -> minimalApplicationModel.set(minimalApplicationModelGenerator.getMinimalModel(location)));
// First unregister any already initialized/started component
unregisterBeans(alreadyCreatedApplicationComponents);
objectProviders.clear();
if (parentComponentModelInitializerAdapter.isPresent()) {
List<String> missingComponentNames = dependencyResolver.getMissingDependencies().stream().filter(dependencyNode -> dependencyNode.isTopLevel()).map(dependencyNode -> dependencyNode.getComponentName()).collect(toList());
parentComponentModelInitializerAdapter.get().initializeComponents(componentModel -> {
if (componentModel.getNameAttribute() != null) {
return missingComponentNames.contains(componentModel.getNameAttribute());
}
return false;
});
} else {
dependencyResolver.getMissingDependencies().stream().forEach(globalElementName -> {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(String.format("Ignoring dependency %s because it does not exists", globalElementName));
}
});
}
List<String> applicationComponents = createApplicationComponents((DefaultListableBeanFactory) this.getBeanFactory(), minimalApplicationModel.get(), false);
createdComponents.set(applicationComponents);
super.prepareObjectProviders();
// This is required to force the execution of postProcessAfterInitialization() for each created component
applicationComponents.forEach(component -> getRegistry().lookupByName(component).get());
});
trackingPostProcessor.stopTracking();
List<String> createdComponentNames = createdComponents.get();
trackingPostProcessor.intersection(createdComponentNames);
return createdComponentNames;
}
use of org.mule.runtime.config.internal.model.ApplicationModel in project mule by mulesoft.
the class MuleArtifactContext method createApplicationComponents.
/**
* Creates te definition for all the objects to be created form the enabled components in the {@code applicationModel}.
*
* @param beanFactory the bean factory in which definition must be created.
* @param applicationModel the artifact application model.
* @param mustBeRoot if the component must be root to be created.
* @return an order list of the created bean names. The order must be respected for the creation of the objects.
*/
protected List<String> createApplicationComponents(DefaultListableBeanFactory beanFactory, ApplicationModel applicationModel, boolean mustBeRoot) {
// This should only be done once at the initial application model creation, called from Spring
List<Pair<ComponentModel, Optional<String>>> objectProvidersByName = lookObjectProvidersComponentModels(applicationModel);
List<String> createdComponentModels = new ArrayList<>();
applicationModel.executeOnEveryMuleComponentTree(cm -> {
SpringComponentModel componentModel = (SpringComponentModel) cm;
if (!mustBeRoot || componentModel.isRoot()) {
if (componentModel.getIdentifier().equals(MULE_IDENTIFIER) || componentModel.getIdentifier().equals(MULE_DOMAIN_IDENTIFIER) || componentModel.getIdentifier().equals(MULE_EE_DOMAIN_IDENTIFIER)) {
return;
}
SpringComponentModel parentComponentModel = componentModel.getParent() != null ? (SpringComponentModel) componentModel.getParent() : (SpringComponentModel) applicationModel.getRootComponentModel();
if (componentModel.isEnabled()) {
if (componentModel.getNameAttribute() != null && componentModel.isRoot()) {
createdComponentModels.add(componentModel.getNameAttribute());
}
beanDefinitionFactory.resolveComponentRecursively(parentComponentModel, componentModel, beanFactory, (resolvedComponentModel, registry) -> {
SpringComponentModel resolvedSpringComponentModel = (SpringComponentModel) resolvedComponentModel;
if (resolvedComponentModel.isRoot()) {
String nameAttribute = resolvedComponentModel.getNameAttribute();
if (resolvedComponentModel.getIdentifier().equals(CONFIGURATION_IDENTIFIER)) {
nameAttribute = OBJECT_MULE_CONFIGURATION;
} else if (nameAttribute == null) {
// This may be a configuration that does not requires a name.
nameAttribute = uniqueValue(resolvedSpringComponentModel.getBeanDefinition().getBeanClassName());
}
registry.registerBeanDefinition(nameAttribute, resolvedSpringComponentModel.getBeanDefinition());
postProcessBeanDefinition(componentModel, registry, nameAttribute);
}
}, null, componentLocator);
} else {
beanDefinitionFactory.resolveComponentRecursively(parentComponentModel, componentModel, beanFactory, null, null, componentLocator);
}
componentLocator.addComponentLocation(cm.getComponentLocation());
}
});
this.objectProviders.addAll(objectProvidersByName.stream().map(pair -> (ConfigurableObjectProvider) pair.getFirst().getObjectInstance()).collect(toList()));
registerObjectFromObjectProviders(beanFactory);
Set<String> alwaysEnabledComponents = dependencyResolver.resolveAlwaysEnabledComponents().stream().map(dependencyNode -> dependencyNode.getComponentName()).collect(toSet());
Set<String> objectProviderNames = objectProvidersByName.stream().map(Pair::getSecond).filter(Optional::isPresent).map(Optional::get).collect(toSet());
// Put object providers first, then always enabled components, then the rest
createdComponentModels.sort(Comparator.comparing(beanName -> {
if (objectProviderNames.contains(beanName)) {
return 1;
} else if (alwaysEnabledComponents.contains(beanName)) {
return 2;
} else {
return 3;
}
}));
return createdComponentModels;
}
use of org.mule.runtime.config.internal.model.ApplicationModel in project mule by mulesoft.
the class MuleArtifactContext method createApplicationModel.
private void createApplicationModel() {
try {
ArtifactConfig artifactConfig = resolveArtifactConfig();
Set<ExtensionModel> extensions = muleContext.getExtensionManager() != null ? muleContext.getExtensionManager().getExtensions() : emptySet();
ResourceProvider externalResourceProvider = new ClassLoaderResourceProvider(muleContext.getExecutionClassLoader());
applicationModel = new ApplicationModel(artifactConfig, artifactDeclaration, extensions, artifactProperties, parentConfigurationProperties, of(componentBuildingDefinitionRegistry), true, externalResourceProvider);
} catch (MuleRuntimeException e) {
throw e;
} catch (Exception e) {
throw new MuleRuntimeException(e);
}
}
Aggregations