Search in sources :

Example 21 with Component

use of org.mule.runtime.api.component.Component in project mule by mulesoft.

the class ComponentInvocationHandlerTestCase method implementsAnnotated.

@Test
public void implementsAnnotated() throws Exception {
    Component annotated = addAnnotationsToClass(ImplementsAnnotated.class).newInstance();
    assertThat(annotated.getAnnotations(), is(nullValue()));
}
Also used : Component(org.mule.runtime.api.component.Component) AbstractComponent(org.mule.runtime.api.component.AbstractComponent) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 22 with Component

use of org.mule.runtime.api.component.Component in project mule by mulesoft.

the class ComponentInvocationHandlerTestCase method differentClassLoader.

@Test
public void differentClassLoader() throws Exception {
    ClassLoader childCl = createDelegatorClassLoader();
    Class<Component> annotatedClass = addAnnotationsToClass(childCl.loadClass(Delegator.class.getName()));
    assertThat(annotatedClass.getClassLoader(), instanceOf(CompositeClassLoader.class));
}
Also used : CompositeClassLoader(org.mule.runtime.core.internal.util.CompositeClassLoader) Component(org.mule.runtime.api.component.Component) AbstractComponent(org.mule.runtime.api.component.AbstractComponent) CompositeClassLoader(org.mule.runtime.core.internal.util.CompositeClassLoader) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 23 with Component

use of org.mule.runtime.api.component.Component 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)

Example 24 with Component

use of org.mule.runtime.api.component.Component in project mule by mulesoft.

the class DefaultPolicyManager method createSourcePolicyInstance.

@Override
public SourcePolicy createSourcePolicyInstance(Component source, CoreEvent sourceEvent, Processor flowExecutionProcessor, MessageSourceResponseParametersProcessor messageSourceResponseParametersProcessor) {
    PolicyPointcutParameters sourcePointcutParameters = policyPointcutParametersManager.createSourcePointcutParameters(source, sourceEvent);
    List<Policy> parameterizedPolicies = policyProvider.findSourceParameterizedPolicies(sourcePointcutParameters);
    if (parameterizedPolicies.isEmpty()) {
        return event -> from(process(event, flowExecutionProcessor)).defaultIfEmpty(CoreEvent.builder(sourceEvent).message(of(null)).build()).<Either<SourcePolicyFailureResult, SourcePolicySuccessResult>>map(flowExecutionResult -> right(new SourcePolicySuccessResult(flowExecutionResult, () -> messageSourceResponseParametersProcessor.getSuccessfulExecutionResponseParametersFunction().apply(flowExecutionResult), messageSourceResponseParametersProcessor))).onErrorResume(Exception.class, e -> {
            MessagingException messagingException = e instanceof MessagingException ? (MessagingException) e : new MessagingException(event, e, (Component) flowExecutionProcessor);
            return just(Either.left(new SourcePolicyFailureResult(messagingException, () -> messageSourceResponseParametersProcessor.getFailedExecutionResponseParametersFunction().apply(messagingException.getEvent()))));
        });
    }
    return new CompositeSourcePolicy(parameterizedPolicies, lookupSourceParametersTransformer(source.getLocation().getComponentIdentifier().getIdentifier()), sourcePolicyProcessorFactory, flowExecutionProcessor, messageSourceResponseParametersProcessor);
}
Also used : Policy(org.mule.runtime.core.api.policy.Policy) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) Processor(org.mule.runtime.core.api.processor.Processor) StreamingManager(org.mule.runtime.core.api.streaming.StreamingManager) Inject(javax.inject.Inject) MuleContextWithRegistries(org.mule.runtime.core.internal.context.MuleContextWithRegistries) MuleContext(org.mule.runtime.core.api.MuleContext) MessageProcessors.process(org.mule.runtime.core.privileged.processor.MessageProcessors.process) Component(org.mule.runtime.api.component.Component) Map(java.util.Map) Mono.from(reactor.core.publisher.Mono.from) Mono.just(reactor.core.publisher.Mono.just) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) SourcePolicyPointcutParametersFactory(org.mule.runtime.policy.api.SourcePolicyPointcutParametersFactory) PolicyStateHandler(org.mule.runtime.core.api.policy.PolicyStateHandler) SourcePolicyParametersTransformer(org.mule.runtime.core.api.policy.SourcePolicyParametersTransformer) Collections.emptyList(java.util.Collections.emptyList) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Message.of(org.mule.runtime.api.message.Message.of) Initialisable(org.mule.runtime.api.lifecycle.Initialisable) OperationPolicyParametersTransformer(org.mule.runtime.core.api.policy.OperationPolicyParametersTransformer) Either.right(org.mule.runtime.core.api.functional.Either.right) Either(org.mule.runtime.core.api.functional.Either) PolicyProvider(org.mule.runtime.core.api.policy.PolicyProvider) List(java.util.List) Policy(org.mule.runtime.core.api.policy.Policy) PolicyPointcutParameters(org.mule.runtime.policy.api.PolicyPointcutParameters) Optional(java.util.Optional) ComponentIdentifier(org.mule.runtime.api.component.ComponentIdentifier) MuleRegistry(org.mule.runtime.core.internal.registry.MuleRegistry) OperationPolicyPointcutParametersFactory(org.mule.runtime.policy.api.OperationPolicyPointcutParametersFactory) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) Component(org.mule.runtime.api.component.Component) PolicyPointcutParameters(org.mule.runtime.policy.api.PolicyPointcutParameters)

Example 25 with Component

use of org.mule.runtime.api.component.Component in project mule by mulesoft.

the class PolicyPointcutParametersManager method createSourcePointcutParameters.

/**
 * Creates {@link PolicyPointcutParameters} for a specific source. The created parameters is also stored so it can be used in
 * case a matching policy also defines an operation part.
 *
 * @param source the source component to which policies will be applied
 * @param event the event which will execute the source policies
 * @return the created {@link PolicyPointcutParameters}
 */
public PolicyPointcutParameters createSourcePointcutParameters(Component source, CoreEvent event) {
    ComponentIdentifier sourceIdentifier = source.getLocation().getComponentIdentifier().getIdentifier();
    PolicyPointcutParameters sourcePointcutParameters = createPointcutParameters(source, SourcePolicyPointcutParametersFactory.class, sourcePointcutFactories, factory -> factory.supportsSourceIdentifier(sourceIdentifier), factory -> factory.createPolicyPointcutParameters(source, event.getMessage().getAttributes())).orElse(new PolicyPointcutParameters(source));
    String correlationId = event.getContext().getCorrelationId();
    sourceParametersMap.put(correlationId, sourcePointcutParameters);
    ((BaseEventContext) event.getContext()).getRootContext().onTerminated((e, t) -> sourceParametersMap.remove(correlationId));
    return sourcePointcutParameters;
}
Also used : Optional.empty(java.util.Optional.empty) SourcePolicyPointcutParametersFactory(org.mule.runtime.policy.api.SourcePolicyPointcutParametersFactory) I18nMessageFactory.createStaticMessage(org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage) Predicate(java.util.function.Predicate) Optional.of(java.util.Optional.of) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Collection(java.util.Collection) BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) Function(java.util.function.Function) String.format(java.lang.String.format) Component(org.mule.runtime.api.component.Component) Map(java.util.Map) OperationPolicyPointcutParametersParameters(org.mule.runtime.policy.api.OperationPolicyPointcutParametersParameters) PolicyPointcutParameters(org.mule.runtime.policy.api.PolicyPointcutParameters) Optional(java.util.Optional) ComponentIdentifier(org.mule.runtime.api.component.ComponentIdentifier) OperationPolicyPointcutParametersFactory(org.mule.runtime.policy.api.OperationPolicyPointcutParametersFactory) SourcePolicyPointcutParametersFactory(org.mule.runtime.policy.api.SourcePolicyPointcutParametersFactory) ComponentIdentifier(org.mule.runtime.api.component.ComponentIdentifier) PolicyPointcutParameters(org.mule.runtime.policy.api.PolicyPointcutParameters)

Aggregations

Component (org.mule.runtime.api.component.Component)39 Test (org.junit.Test)15 Map (java.util.Map)12 AbstractComponent (org.mule.runtime.api.component.AbstractComponent)12 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)12 SmallTest (org.mule.tck.size.SmallTest)11 Optional (java.util.Optional)9 MuleRuntimeException (org.mule.runtime.api.exception.MuleRuntimeException)9 MessagingException (org.mule.runtime.core.internal.exception.MessagingException)9 List (java.util.List)8 ComponentIdentifier (org.mule.runtime.api.component.ComponentIdentifier)8 I18nMessageFactory.createStaticMessage (org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage)7 HashMap (java.util.HashMap)6 String.format (java.lang.String.format)5 Optional.empty (java.util.Optional.empty)5 Optional.of (java.util.Optional.of)5 Set (java.util.Set)5 Inject (javax.inject.Inject)5 Arrays.asList (java.util.Arrays.asList)4 Collection (java.util.Collection)4