use of org.mule.runtime.api.connection.ConnectionProvider in project mule by mulesoft.
the class LifecycleAwareConfigurationInstance method testConnectivity.
private void testConnectivity() throws MuleException {
ConnectionProvider provider = connectionProvider.get();
if (provider instanceof NoConnectivityTest) {
return;
}
Scheduler retryScheduler = schedulerService.ioScheduler();
RetryPolicyTemplate retryTemplate = connectionManager.getRetryTemplateFor(provider);
ReconnectionConfig reconnectionConfig = connectionManager.getReconnectionConfigFor(provider);
RetryCallback retryCallback = new RetryCallback() {
@Override
public void doWork(RetryContext context) throws Exception {
Lock lock = testConnectivityLock;
if (lock != null) {
final boolean lockAcquired = lock.tryLock();
if (lockAcquired) {
LOGGER.info("Doing testConnectivity() for config " + getName());
try {
ConnectionValidationResult result = connectionManager.testConnectivity(LifecycleAwareConfigurationInstance.this);
if (result.isValid()) {
context.setOk();
} else {
if ((reconnectionConfig.isFailsDeployment())) {
context.setFailed(result.getException());
throw new ConnectionException(format("Connectivity test failed for config '%s'", getName()), result.getException());
} else {
if (LOGGER.isInfoEnabled()) {
LOGGER.info(format("Connectivity test failed for config '%s'. Application deployment will continue. Error was: ", getName(), result.getMessage()), result.getException());
}
}
}
} finally {
lock.unlock();
}
} else {
LOGGER.warn("There is a testConnectivity() already running for config " + getName());
}
}
}
@Override
public String getWorkDescription() {
return format("Testing connectivity for config '%s'", getName());
}
@Override
public Object getWorkOwner() {
return value;
}
};
try {
retryTemplate.execute(retryCallback, retryScheduler);
} catch (Exception e) {
throw new DefaultMuleException(createStaticMessage(format("Could not perform connectivity testing for config '%s'", getName())), e);
} finally {
if (retryScheduler != null) {
retryScheduler.stop();
}
}
}
use of org.mule.runtime.api.connection.ConnectionProvider in project mule by mulesoft.
the class SourceAdapter method setConnection.
private void setConnection() throws MuleException {
if (!connectionSetter.isPresent()) {
return;
}
FieldSetter<Object, ConnectionProvider> setter = connectionSetter.get();
ConfigurationInstance config = configurationInstance.orElseThrow(() -> new DefaultMuleException(createStaticMessage("Message Source on root component '%s' requires a connection but it doesn't point to any configuration. Please review your " + "application", component.getLocation().getRootContainerName())));
if (!config.getConnectionProvider().isPresent()) {
throw new DefaultMuleException(createStaticMessage(format("Message Source on root component '%s' requires a connection, but points to config '%s' which doesn't specify any. " + "Please review your application", component.getLocation().getRootContainerName(), config.getName())));
}
ConnectionProvider<Object> connectionProvider = new SourceConnectionProvider(connectionManager, config);
setter.set(sourceInvokationTarget.get(), connectionProvider);
}
use of org.mule.runtime.api.connection.ConnectionProvider in project mule by mulesoft.
the class CachedConnectionHandlerTestCase method getConnectionConcurrentlyAndConnectOnlyOnce.
@Test
public void getConnectionConcurrentlyAndConnectOnlyOnce() throws Exception {
Banana mockConnection = mock(Banana.class);
connectionProvider = mock(ConnectionProvider.class);
before();
Latch latch = new Latch();
when(connectionProvider.connect()).thenAnswer(invocation -> {
new Thread(() -> {
try {
latch.release();
getConnection();
} catch (Exception e) {
throw new RuntimeException(e);
}
}).start();
return mockConnection;
});
Banana connection = managedConnection.getConnection();
assertThat(latch.await(5, TimeUnit.SECONDS), is(true));
assertThat(connection, is(sameInstance(mockConnection)));
verify(connectionProvider).connect();
}
use of org.mule.runtime.api.connection.ConnectionProvider in project mule by mulesoft.
the class ConfigurationProviderToolingAdapter method withConnectionProviderInfo.
private <T> T withConnectionProviderInfo(WithConnectionProviderCallable<T> withConnectionProviderCallable) throws ValueResolvingException {
ConnectionProvider<?> connectionProvider = configuration.getConnectionProvider().orElseThrow(() -> new ValueResolvingException("Unable to obtain the Connection Provider Instance", UNKNOWN));
ConnectionProvider unwrap = unwrapProviderWrapper(connectionProvider);
ConnectionProviderModel connectionProviderModel = getConnectionProviderModel(unwrap.getClass(), getAllConnectionProviders(getExtensionModel(), getConfigurationModel())).orElseThrow(() -> new ValueResolvingException("Internal error. Unable to obtain the Connection Provider Model", UNKNOWN));
return withConnectionProviderCallable.call(unwrap, connectionProviderModel);
}
use of org.mule.runtime.api.connection.ConnectionProvider 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);
}
}
Aggregations