Search in sources :

Example 1 with Location

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;
}
Also used : Optional.empty(java.util.Optional.empty) Optional.of(java.util.Optional.of) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) ConfigurationProperties(org.mule.runtime.api.component.ConfigurationProperties) Location(org.mule.runtime.api.component.location.Location) NON_LAZY_VALUE_PROVIDER_SERVICE(org.mule.runtime.config.internal.LazyValueProviderService.NON_LAZY_VALUE_PROVIDER_SERVICE) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) ArrayList(java.util.ArrayList) ValueProviderService(org.mule.runtime.api.value.ValueProviderService) MuleContext(org.mule.runtime.core.api.MuleContext) NON_LAZY_CONNECTIVITY_TESTING_SERVICE(org.mule.runtime.config.internal.LazyConnectivityTestingService.NON_LAZY_CONNECTIVITY_TESTING_SERVICE) MuleException(org.mule.runtime.api.exception.MuleException) ArtifactDeclaration(org.mule.runtime.app.declaration.api.ArtifactDeclaration) Map(java.util.Map) Location.builderFromStringRepresentation(org.mule.runtime.api.component.location.Location.builderFromStringRepresentation) Preconditions.checkState(org.mule.runtime.api.util.Preconditions.checkState) LegacyRegistryUtils.unregisterObject(org.mule.runtime.core.privileged.registry.LegacyRegistryUtils.unregisterObject) ConfigurationDependencyResolver(org.mule.runtime.config.internal.dsl.model.ConfigurationDependencyResolver) ClassUtils.withContextClassLoader(org.mule.runtime.core.api.util.ClassUtils.withContextClassLoader) MinimalApplicationModelGenerator(org.mule.runtime.config.internal.dsl.model.MinimalApplicationModelGenerator) MetadataService(org.mule.runtime.api.metadata.MetadataService) VALUE_PROVIDER_SERVICE_KEY(org.mule.runtime.api.value.ValueProviderService.VALUE_PROVIDER_SERVICE_KEY) Collections.reverse(java.util.Collections.reverse) Startable(org.mule.runtime.api.lifecycle.Startable) Logger(org.slf4j.Logger) DefaultConnectivityTestingService(org.mule.runtime.core.internal.connectivity.DefaultConnectivityTestingService) Predicate(java.util.function.Predicate) ConfigResource(org.mule.runtime.core.api.config.ConfigResource) ApplicationModel(org.mule.runtime.config.internal.model.ApplicationModel) BeansException(org.springframework.beans.BeansException) MuleDeploymentProperties(org.mule.runtime.core.api.config.MuleDeploymentProperties) Initialisable(org.mule.runtime.api.lifecycle.Initialisable) String.format(java.lang.String.format) ConnectivityTestingService(org.mule.runtime.api.connectivity.ConnectivityTestingService) MuleValueProviderService(org.mule.runtime.core.internal.value.MuleValueProviderService) Collectors.toList(java.util.stream.Collectors.toList) ArtifactType(org.mule.runtime.core.api.config.bootstrap.ArtifactType) List(java.util.List) METADATA_SERVICE_KEY(org.mule.runtime.api.metadata.MetadataService.METADATA_SERVICE_KEY) ComponentModel(org.mule.runtime.config.internal.model.ComponentModel) Reference(org.mule.runtime.api.util.Reference) MuleMetadataService(org.mule.runtime.core.internal.metadata.MuleMetadataService) NON_LAZY_METADATA_SERVICE(org.mule.runtime.config.internal.LazyMetadataService.NON_LAZY_METADATA_SERVICE) Optional(java.util.Optional) CONNECTIVITY_TESTING_SERVICE_KEY(org.mule.runtime.api.connectivity.ConnectivityTestingService.CONNECTIVITY_TESTING_SERVICE_KEY) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Reference(org.mule.runtime.api.util.Reference) ArrayList(java.util.ArrayList) ApplicationModel(org.mule.runtime.config.internal.model.ApplicationModel) ArrayList(java.util.ArrayList) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) ConfigurationDependencyResolver(org.mule.runtime.config.internal.dsl.model.ConfigurationDependencyResolver) MinimalApplicationModelGenerator(org.mule.runtime.config.internal.dsl.model.MinimalApplicationModelGenerator)

Example 2 with Location

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));
    }
}
Also used : Message(org.mule.runtime.api.message.Message) I18nMessageFactory.createStaticMessage(org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage) Error(org.mule.runtime.api.message.Error) Flow(org.mule.runtime.core.api.construct.Flow) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) ComponentExecutionException(org.mule.runtime.api.component.execution.ComponentExecutionException) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) Component(org.mule.runtime.api.component.Component) ExecutableComponent(org.mule.runtime.api.component.execution.ExecutableComponent) ExecutionException(java.util.concurrent.ExecutionException) ComponentExecutionException(org.mule.runtime.api.component.execution.ComponentExecutionException) Location(org.mule.runtime.api.component.location.Location)

Aggregations

Location (org.mule.runtime.api.component.location.Location)2 String.format (java.lang.String.format)1 ArrayList (java.util.ArrayList)1 Collections.reverse (java.util.Collections.reverse)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Optional.empty (java.util.Optional.empty)1 Optional.of (java.util.Optional.of)1 ExecutionException (java.util.concurrent.ExecutionException)1 Predicate (java.util.function.Predicate)1 Collectors.toList (java.util.stream.Collectors.toList)1 Component (org.mule.runtime.api.component.Component)1 ConfigurationProperties (org.mule.runtime.api.component.ConfigurationProperties)1 ComponentExecutionException (org.mule.runtime.api.component.execution.ComponentExecutionException)1 ExecutableComponent (org.mule.runtime.api.component.execution.ExecutableComponent)1 Location.builderFromStringRepresentation (org.mule.runtime.api.component.location.Location.builderFromStringRepresentation)1 ConnectivityTestingService (org.mule.runtime.api.connectivity.ConnectivityTestingService)1 CONNECTIVITY_TESTING_SERVICE_KEY (org.mule.runtime.api.connectivity.ConnectivityTestingService.CONNECTIVITY_TESTING_SERVICE_KEY)1