use of org.mule.runtime.api.exception.MuleRuntimeException in project mule by mulesoft.
the class LifecycleUtils method initialiseIfNeeded.
/**
* The same as {@link #initialiseIfNeeded(Object)}, only that before checking for {@code object} being {@link Initialisable}, it
* uses the given {@code muleContext} to perform further initialization.
* <p>
* It checks if the {@code object} implements {@link MuleContextAware}, in which case it will invoke
* {@link MuleContextAware#setMuleContext(MuleContext)} with the given {@code muleContext}.
* <p>
* Also depending on the value of the {@code inject} argument, it will perform dependency injection on the {@code object}
*
* @param object the object you're trying to initialise
* @param inject whether it should perform dependency injection on the {@code object} before actually initialising it
* @param muleContext a {@link MuleContext}
* @throws InitialisationException
* @throws IllegalArgumentException if {@code MuleContext} is {@code null}
*/
public static void initialiseIfNeeded(Object object, boolean inject, MuleContext muleContext) throws InitialisationException {
checkArgument(muleContext != null, "muleContext cannot be null");
object = unwrap(object);
if (object == null) {
return;
}
if (object instanceof MuleContextAware) {
((MuleContextAware) object).setMuleContext(muleContext);
}
if (inject) {
try {
muleContext.getInjector().inject(object);
} catch (MuleException e) {
I18nMessage message = createStaticMessage(format("Found exception trying to inject object of type '%s' on initialising phase", object.getClass().getName()));
if (object instanceof Initialisable) {
throw new InitialisationException(message, e, (Initialisable) object);
}
throw new MuleRuntimeException(message, e);
}
}
initialiseIfNeeded(object);
}
use of org.mule.runtime.api.exception.MuleRuntimeException in project mule by mulesoft.
the class DefaultMuleContext method initialise.
@Override
public void initialise() throws InitialisationException {
synchronized (lifecycleStateLock) {
lifecycleManager.checkPhase(Initialisable.PHASE_NAME);
if (getNotificationManager() == null) {
throw new MuleRuntimeException(objectIsNull(OBJECT_NOTIFICATION_MANAGER));
}
try {
JdkVersionUtils.validateJdk();
} catch (RuntimeException e) {
throw new InitialisationException(invalidJdk(JAVA_VERSION, getSupportedJdks()), this);
}
try {
id = getConfiguration().getDomainId() + "." + getClusterId() + "." + getConfiguration().getId();
// Initialize the helper, this only initialises the helper class and does not call the registry lifecycle manager
// The registry lifecycle is called below using 'getLifecycleManager().fireLifecycle(Initialisable.PHASE_NAME);'
getRegistry().initialise();
fireNotification(new MuleContextNotification(this, CONTEXT_INITIALISING));
getLifecycleManager().fireLifecycle(Initialisable.PHASE_NAME);
fireNotification(new MuleContextNotification(this, CONTEXT_INITIALISED));
listeners.forEach(l -> l.onInitialization(this, getApiRegistry()));
initialiseIfNeeded(getExceptionListener(), true, this);
getNotificationManager().initialise();
// refresh object serializer reference in case a default one was redefined in the config.
objectSerializer = registryBroker.get(DEFAULT_OBJECT_SERIALIZER_NAME);
} catch (InitialisationException e) {
dispose();
throw e;
} catch (Exception e) {
dispose();
throw new InitialisationException(e, this);
}
}
}
use of org.mule.runtime.api.exception.MuleRuntimeException in project mule by mulesoft.
the class PolicyNextActionMessageProcessor method apply.
@Override
public Publisher<CoreEvent> apply(Publisher<CoreEvent> publisher) {
return from(publisher).doOnNext(coreEvent -> logExecuteNextEvent("Before execute-next", coreEvent.getContext(), coreEvent.getMessage(), this.muleContext.getConfiguration().getId())).flatMap(event -> {
Processor nextOperation = policyStateHandler.retrieveNextOperation(event.getContext().getCorrelationId());
if (nextOperation == null) {
return error(new MuleRuntimeException(createStaticMessage("There's no next operation configured for event context id " + event.getContext().getCorrelationId())));
}
popBeforeNextFlowFlowStackElement().accept(event);
notificationHelper.notification(BEFORE_NEXT).accept(event);
return from(processWithChildContext(event, nextOperation, ofNullable(getLocation()))).doOnSuccessOrError(notificationHelper.successOrErrorNotification(AFTER_NEXT).andThen((ev, t) -> pushAfterNextFlowStackElement().accept(event))).onErrorResume(MessagingException.class, t -> {
PolicyStateId policyStateId = new PolicyStateId(event.getContext().getCorrelationId(), muleContext.getConfiguration().getId());
policyStateHandler.getLatestState(policyStateId).ifPresent(latestStateEvent -> t.setProcessedEvent(policyEventConverter.createEvent((PrivilegedEvent) t.getEvent(), (PrivilegedEvent) latestStateEvent)));
// Given we've used child context to ensure AFTER_NEXT notifications are fired at exactly the right time we need
// to propagate the error to parent context manually.
((BaseEventContext) event.getContext()).error(resolveMessagingException(t.getFailingComponent(), muleContext).apply(t));
return empty();
}).doOnNext(coreEvent -> logExecuteNextEvent("After execute-next", coreEvent.getContext(), coreEvent.getMessage(), this.muleContext.getConfiguration().getId()));
});
}
use of org.mule.runtime.api.exception.MuleRuntimeException in project mule by mulesoft.
the class PoolingByteBufferManager method deallocate.
/**
* {@inheritDoc}
*/
@Override
public void deallocate(ByteBuffer byteBuffer) {
int capacity = byteBuffer.capacity();
BufferPool pool = pools.getIfPresent(capacity);
if (pool != null) {
try {
pool.returnBuffer(byteBuffer);
} catch (Exception e) {
throw new MuleRuntimeException(createStaticMessage("Could not deallocate buffer of capacity " + capacity), e);
}
}
}
use of org.mule.runtime.api.exception.MuleRuntimeException in project mule by mulesoft.
the class CursorManager method manage.
/**
* Becomes aware of the given {@code provider} and returns a replacement provider which is managed by the runtime, allowing for
* automatic resource handling
*
* @param provider the provider to be tracked
* @param creatorEvent the event that created the provider
* @return a {@link CursorContext}
*/
public CursorProvider manage(CursorProvider provider, CoreEvent creatorEvent) {
final BaseEventContext ownerContext = ((BaseEventContext) creatorEvent.getContext()).getRootContext();
registerEventContext(ownerContext);
registry.getUnchecked(ownerContext.getId()).addProvider(provider);
final CursorContext context = new CursorContext(provider, ownerContext);
if (provider instanceof CursorStreamProvider) {
return new ManagedCursorStreamProvider(context, this);
} else if (provider instanceof CursorIteratorProvider) {
return new ManagedCursorIteratorProvider(context, this);
}
throw new MuleRuntimeException(createStaticMessage("Unknown cursor provider type: " + context.getClass().getName()));
}
Aggregations