use of org.mule.runtime.api.util.Reference in project mule by mulesoft.
the class MessagingExceptionResolver method findRoot.
private Optional<Pair<Throwable, ErrorType>> findRoot(Component obj, MessagingException me, ErrorTypeLocator locator) {
List<Pair<Throwable, ErrorType>> errors = collectErrors(obj, me, locator);
if (errors.isEmpty()) {
return collectCritical(obj, me, locator).stream().findFirst();
}
// We look if there is a more specific error in the chain that matches with the root error (is child or has the same error)
SingleErrorTypeMatcher matcher = new SingleErrorTypeMatcher(errors.get(0).getSecond());
Reference<Pair<Throwable, ErrorType>> result = new Reference<>();
errors.forEach(p -> {
if (matcher.match(p.getSecond())) {
result.set(p);
}
});
return Optional.ofNullable(result.get());
}
use of org.mule.runtime.api.util.Reference in project mule by mulesoft.
the class DefaultPolicyStateHandlerTestCase method destroyStateWhenEventIsCompleted.
@Test
public void destroyStateWhenEventIsCompleted() {
PolicyStateId policyStateExecutionId = new PolicyStateId(TEST_EXECUTION_ID, TEST_POLICY_ID);
Reference<BiConsumer> subscriberReference = new Reference<>();
BaseEventContext rootEventContext = eventTestExecutionId.getContext().getRootContext();
Mockito.doAnswer(invocationOnMock -> {
subscriberReference.set((BiConsumer) invocationOnMock.getArguments()[0]);
return null;
}).when(rootEventContext).onTerminated(any(BiConsumer.class));
defaultPolicyStateHandler.updateState(policyStateExecutionId, eventTestExecutionId);
subscriberReference.get().accept(null, null);
assertThat(defaultPolicyStateHandler.getLatestState(policyStateExecutionId).isPresent(), is(false));
assertThat(defaultPolicyStateHandler.policyStateIdsByExecutionIdentifier.containsKey(policyStateExecutionId.getExecutionIdentifier()), is(false));
assertThat(defaultPolicyStateHandler.nextOperationMap.containsKey(policyStateExecutionId.getExecutionIdentifier()), is(false));
assertThat(defaultPolicyStateHandler.stateMap.containsKey(policyStateExecutionId), is(false));
}
use of org.mule.runtime.api.util.Reference in project mule by mulesoft.
the class StreamingUtils method withCursoredEvent.
/**
* Executes the given function {@code f} considering that the given {@code event} might have a {@link CursorProvider} as
* payload. In that case, this method obtains a cursor from the provider and executes the function.
* <p>
* Closing the opened cursor, handling exceptions and return values are all taken care of by this utility method.
*
* @param event an {@link CoreEvent}
* @param f the function to execute
* @return the output {@link CoreEvent}
* @throws MuleException
*/
public static CoreEvent withCursoredEvent(CoreEvent event, CheckedFunction<CoreEvent, CoreEvent> f) throws MuleException {
if (event.getMessage().getPayload() == null) {
return event;
}
Reference<Throwable> exception = new Reference<>();
CheckedFunction<CoreEvent, CoreEvent> function = new CheckedFunction<CoreEvent, CoreEvent>() {
@Override
public CoreEvent applyChecked(CoreEvent event) throws Throwable {
return f.apply(event);
}
@Override
public CoreEvent handleException(Throwable throwable) {
exception.set(unwrap(throwable));
return null;
}
};
Object payload = event.getMessage().getPayload().getValue();
CursorProvider cursorProvider = null;
Cursor cursor = null;
try {
if (payload instanceof CursorProvider) {
cursorProvider = (CursorProvider) payload;
cursor = cursorProvider.openCursor();
event = replacePayload(event, cursor);
}
CoreEvent value = function.apply(event);
if (value == null) {
handlePossibleException(exception);
} else if (value.getMessage().getPayload().getValue() == cursor) {
value = replacePayload(value, cursorProvider);
}
return value;
} finally {
if (cursor != null) {
closeQuietly(cursor);
}
}
}
use of org.mule.runtime.api.util.Reference in project mule by mulesoft.
the class PollingSourceDeclarationEnricher method enrich.
@Override
public void enrich(ExtensionLoadingContext extensionLoadingContext) {
ClassTypeLoader loader = ExtensionsTypeLoaderFactory.getDefault().createTypeLoader();
ExtensionDeclarer extensionDeclarer = extensionLoadingContext.getExtensionDeclarer();
Reference<Boolean> thereArePollingSources = new Reference<>(false);
new IdempotentDeclarationWalker() {
@Override
protected void onSource(SourceDeclaration source) {
extractType(source).ifPresent(type -> {
if (type.isAssignableTo(PollingSource.class)) {
source.setRunsOnPrimaryNodeOnly(true);
ParameterDeclaration parameter = new ParameterDeclaration(SCHEDULING_STRATEGY_PARAMETER_NAME);
parameter.setDescription(SCHEDULING_STRATEGY_PARAMETER_DESCRIPTION);
parameter.setRequired(true);
parameter.setType(loader.load(Scheduler.class), false);
parameter.setExpressionSupport(NOT_SUPPORTED);
parameter.addModelProperty(new InfrastructureParameterModelProperty(10));
parameter.addModelProperty(new QNameModelProperty(new QName(CORE_NAMESPACE, SCHEDULING_STRATEGY_ELEMENT_IDENTIFIER, CORE_PREFIX)));
parameter.setDslConfiguration(ParameterDslConfiguration.builder().allowsInlineDefinition(true).allowsReferences(false).allowTopLevelDefinition(false).build());
thereArePollingSources.set(true);
source.getParameterGroup(DEFAULT_GROUP_NAME).addParameter(parameter);
}
});
}
}.walk(extensionDeclarer.getDeclaration());
if (thereArePollingSources.get() && !isSchedulerAlreadyImported(extensionDeclarer.getDeclaration())) {
ClassTypeLoader typeLoader = ExtensionsTypeLoaderFactory.getDefault().createTypeLoader();
extensionDeclarer.withImportedType(new ImportedTypeModel((ObjectType) typeLoader.load(Scheduler.class)));
}
}
use of org.mule.runtime.api.util.Reference in project mule by mulesoft.
the class RedeliveryPolicyDeclarationEnricher method enrich.
@Override
public void enrich(ExtensionLoadingContext extensionLoadingContext) {
final Reference<Boolean> hasObjectStoreParams = new Reference<>(false);
ExtensionDeclaration extension = extensionLoadingContext.getExtensionDeclarer().getDeclaration();
new IdempotentDeclarationWalker() {
@Override
protected void onSource(SourceDeclaration declaration) {
addRedeliveryPolicy(declaration);
hasObjectStoreParams.set(true);
}
}.walk(extension);
if (hasObjectStoreParams.get() && !isObjectStoreAlreadyImported(extension)) {
ClassTypeLoader typeLoader = ExtensionsTypeLoaderFactory.getDefault().createTypeLoader();
extension.getImportedTypes().add(new ImportedTypeModel((ObjectType) typeLoader.load(ObjectStore.class)));
}
}
Aggregations