use of org.mule.runtime.api.exception.MuleRuntimeException in project mule by mulesoft.
the class ConfigurationProviderObjectFactory method createInnerInstance.
private ConfigurationProvider createInnerInstance() throws ConfigurationException {
if (expirationPolicy == null) {
expirationPolicy = muleContext.getConfiguration().getDynamicConfigExpiration().getExpirationPolicy();
}
ResolverSet resolverSet = getParametersResolver().getParametersAsHashedResolverSet(configurationModel, muleContext);
final ConnectionProviderValueResolver connectionProviderResolver = getConnectionProviderResolver();
return withContextClassLoader(getExtensionClassLoader(), () -> {
connectionProviderResolver.getResolverSet().ifPresent((CheckedConsumer) resolver -> initialiseIfNeeded(resolver, true, muleContext));
ConfigurationProvider configurationProvider;
try {
if (resolverSet.isDynamic() || connectionProviderResolver.isDynamic()) {
configurationProvider = configurationProviderFactory.createDynamicConfigurationProvider(name, extensionModel, configurationModel, resolverSet, connectionProviderResolver, expirationPolicy, reflectionCache, muleContext);
} else {
configurationProvider = configurationProviderFactory.createStaticConfigurationProvider(name, extensionModel, configurationModel, resolverSet, connectionProviderResolver, reflectionCache, muleContext);
}
} catch (Exception e) {
throw new MuleRuntimeException(createStaticMessage(format("Could not create an implicit configuration '%s' for the extension '%s'", configurationModel.getName(), extensionModel.getName())), e);
}
return configurationProvider;
});
}
use of org.mule.runtime.api.exception.MuleRuntimeException in project mule by mulesoft.
the class DefaultConnectionProviderObjectBuilder method applyExtensionClassLoaderProxy.
/**
* Wraps the {@link ConnectionProvider} inside of a dynamic proxy which changes the current {@link ClassLoader} to the the
* extension's {@link ClassLoader} when executing any method of this wrapped {@link ConnectionProvider}
* <p>
* This ensures that every time that the {@link ConnectionProvider} is used, it will work in the correct classloader.
* <p>
* Although if the {@link ConnectionProvider} is created with the correct classloader and then used with an incorrect one this
* may work, due that static class references were loaded correctly, logic loading class in a dynamic way will fail.
*
* @param provider The {@link ConnectionProvider} to wrap
* @return The wrapped {@link ConnectionProvider}
*/
private ConnectionProvider<C> applyExtensionClassLoaderProxy(ConnectionProvider provider) {
Enhancer enhancer = new Enhancer();
ClassLoader classLoader = getClassLoader(extensionModel);
Class[] proxyInterfaces = getProxyInterfaces(provider);
enhancer.setInterfaces(proxyInterfaces);
MethodInterceptor returnProviderInterceptor = (obj, method, args, proxy) -> provider;
MethodInterceptor invokerInterceptor = (obj, method, args, proxy) -> {
Reference<Object> resultReference = new Reference<>();
Reference<Throwable> errorReference = new Reference<>();
withContextClassLoader(classLoader, () -> {
try {
resultReference.set(method.invoke(provider, args));
} catch (InvocationTargetException e) {
errorReference.set(e.getTargetException());
} catch (Throwable t) {
errorReference.set(t);
}
});
if (errorReference.get() != null) {
throw errorReference.get();
} else {
return resultReference.get();
}
};
CallbackHelper callbackHelper = new CallbackHelper(Object.class, proxyInterfaces) {
@Override
protected Object getCallback(Method method) {
if (method.getDeclaringClass().equals(HasDelegate.class) && method.getName().equals("getDelegate")) {
return returnProviderInterceptor;
} else {
return invokerInterceptor;
}
}
};
enhancer.setCallbackTypes(callbackHelper.getCallbackTypes());
enhancer.setCallbackFilter(callbackHelper);
if (Enhancer.class.getClassLoader() != classLoader) {
enhancer.setClassLoader(new CompositeClassLoader(DefaultConnectionProviderObjectBuilder.class.getClassLoader(), classLoader));
enhancer.setUseCache(false);
}
Class<ConnectionProvider<C>> proxyClass = enhancer.createClass();
registerStaticCallbacks(proxyClass, callbackHelper.getCallbacks());
try {
return proxyClass.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new MuleRuntimeException(e);
}
}
use of org.mule.runtime.api.exception.MuleRuntimeException in project mule by mulesoft.
the class ComponentMessageProcessor method createComponentExecutor.
private ComponentExecutor<T> createComponentExecutor() {
Map<String, Object> params = new HashMap<>();
LazyValue<Optional<ConfigurationInstance>> staticConfiguration = new LazyValue<>(this::getStaticConfiguration);
LazyValue<ValueResolvingContext> resolvingContext = new LazyValue<>(() -> {
CoreEvent initialiserEvent = null;
try {
initialiserEvent = getInitialiserEvent();
return from(initialiserEvent, staticConfiguration.get());
} finally {
if (initialiserEvent != null) {
((BaseEventContext) initialiserEvent.getContext()).success();
}
}
});
componentModel.getParameterGroupModels().stream().forEach(group -> {
if (group.getName().equals(DEFAULT_GROUP_NAME)) {
group.getParameterModels().stream().filter(p -> p.getModelProperty(FieldOperationParameterModelProperty.class).isPresent()).forEach(p -> {
ValueResolver<?> resolver = resolverSet.getResolvers().get(p.getName());
if (resolver != null) {
try {
params.put(getMemberName(p), resolveValue(resolver, resolvingContext.get()));
} catch (MuleException e) {
throw new MuleRuntimeException(e);
}
}
});
} else {
ParameterGroupDescriptor groupDescriptor = group.getModelProperty(ParameterGroupModelProperty.class).map(g -> g.getDescriptor()).orElse(null);
if (groupDescriptor == null) {
return;
}
List<ParameterModel> fieldParameters = getGroupsOfFieldParameters(group);
if (fieldParameters.isEmpty()) {
return;
}
ObjectBuilder groupBuilder = createFieldParameterGroupBuilder(groupDescriptor, fieldParameters);
try {
params.put(((Field) groupDescriptor.getContainer()).getName(), groupBuilder.build(resolvingContext.get()));
} catch (MuleException e) {
throw new MuleRuntimeException(e);
}
}
});
return getOperationExecutorFactory(componentModel).createExecutor(componentModel, params);
}
use of org.mule.runtime.api.exception.MuleRuntimeException in project mule by mulesoft.
the class ComponentMessageProcessorBuilder method build.
public P build() {
return withContextClassLoader(getClassLoader(extensionModel), () -> {
try {
final ExtensionManager extensionManager = muleContext.getExtensionManager();
final ResolverSet operationArguments = getArgumentsResolverSet();
P processor = createMessageProcessor(extensionManager, operationArguments);
// TODO: MULE-5002 this should not be necessary but lifecycle issues when injecting message processors automatically
muleContext.getInjector().inject(processor);
return processor;
} catch (Exception e) {
throw new MuleRuntimeException(e);
}
});
}
use of org.mule.runtime.api.exception.MuleRuntimeException in project mule by mulesoft.
the class DefaultExtensionsOAuthManager method createDancer.
private AuthorizationCodeOAuthDancer createDancer(OAuthConfig config) throws MuleException {
OAuthAuthorizationCodeDancerBuilder dancerBuilder = oauthService.get().authorizationCodeGrantTypeDancerBuilder(lockId -> lockFactory.createLock(lockId), new LazyObjectStoreToMapAdapter(getObjectStoreSupplier(config)), expressionEvaluator);
final AuthCodeConfig authCodeConfig = config.getAuthCodeConfig();
final AuthorizationCodeGrantType grantType = config.getGrantType();
final OAuthCallbackConfig callbackConfig = config.getCallbackConfig();
dancerBuilder.encoding(getDefaultEncoding(muleContext)).clientCredentials(authCodeConfig.getConsumerKey(), authCodeConfig.getConsumerSecret()).tokenUrl(authCodeConfig.getAccessTokenUrl()).responseExpiresInExpr(grantType.getExpirationRegex()).responseRefreshTokenExpr(grantType.getRefreshTokenExpr()).responseAccessTokenExpr(grantType.getAccessTokenExpr()).resourceOwnerIdTransformer(ownerId -> ownerId + "-" + config.getOwnerConfigName());
String scopes = authCodeConfig.getScope().orElseGet(() -> grantType.getDefaultScope().orElse(null));
if (scopes != null) {
dancerBuilder.scopes(scopes);
}
HttpServer httpServer;
try {
httpServer = httpService.get().getServerFactory().lookup(callbackConfig.getListenerConfig());
} catch (ServerNotFoundException e) {
throw new MuleRuntimeException(createStaticMessage(format("Connector '%s' defines '%s' as the http:listener-config to use for provisioning callbacks, but no such definition " + "exists in the application configuration", config.getOwnerConfigName(), callbackConfig.getListenerConfig())), e);
}
dancerBuilder.localCallback(httpServer, callbackConfig.getCallbackPath()).externalCallbackUrl(getExternalCallback(httpServer, callbackConfig)).authorizationUrl(authCodeConfig.getAuthorizationUrl()).localAuthorizationUrlPath(callbackConfig.getLocalAuthorizePath()).localAuthorizationUrlResourceOwnerId("#[attributes.queryParams.resourceOwnerId]").state("#[attributes.queryParams.state]").customParameters(config.getCustomParameters()).customParametersExtractorsExprs(getParameterExtractors(config));
Pair<Optional<Flow>, Optional<Flow>> listenerFlows = getListenerFlows(config);
listenerFlows.getFirst().ifPresent(flow -> dancerBuilder.beforeDanceCallback(beforeCallback(config, flow)));
listenerFlows.getSecond().ifPresent(flow -> dancerBuilder.afterDanceCallback(afterCallback(config, flow)));
AuthorizationCodeOAuthDancer dancer = dancerBuilder.build();
if (started) {
start(dancer);
}
return dancer;
}
Aggregations