use of org.mule.runtime.api.component.location.Location 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.api.component.location.Location in project mule by mulesoft.
the class LookupFunction method call.
@Override
public Object call(Object[] parameters, BindingContext context) {
String flowName = (String) parameters[0];
Object payload = parameters[1];
Location componentLocation = Location.builder().globalName(flowName).build();
Component component = componentLocator.find(componentLocation).orElseThrow(() -> new IllegalArgumentException(format("There is no component named '%s'.", flowName)));
if (component instanceof Flow) {
try {
Message incomingMessage = lookupValue(context, MESSAGE, Message.builder().nullValue().build());
Map<String, ?> incomingVariables = lookupValue(context, VARS, EMPTY_MAP);
Error incomingError = lookupValue(context, ERROR, null);
Message message = Message.builder(incomingMessage).value(payload).mediaType(APPLICATION_JAVA).build();
CoreEvent event = CoreEvent.builder(PrivilegedEvent.getCurrentEvent().getContext()).variables(incomingVariables).error(incomingError).message(message).build();
return ((ExecutableComponent) component).execute(event).get().getMessage().getPayload();
} catch (ExecutionException e) {
ComponentExecutionException componentExecutionException = (ComponentExecutionException) e.getCause();
Error error = componentExecutionException.getEvent().getError().get();
throw new MuleRuntimeException(createStaticMessage(format("Flow '%s' has failed with error '%s' (%s)", flowName, error.getErrorType(), error.getDescription())), error.getCause());
} catch (InterruptedException e) {
throw new MuleRuntimeException(e);
}
} else {
throw new IllegalArgumentException(format("Component '%s' is not a flow.", flowName));
}
}
Aggregations