Search in sources :

Example 11 with ConnectionException

use of org.mule.runtime.api.connection.ConnectionException in project mule by mulesoft.

the class TransactionalSource method onStart.

@Override
public void onStart(SourceCallback<TestTransactionalConnection, Object> sourceCallback) throws MuleException {
    connectExecutor = newFixedThreadPool(1);
    connectExecutor.execute(() -> {
        SourceCallbackContext ctx = sourceCallback.createContext();
        try {
            TestTransactionalConnection connection = connectionProvider.connect();
            boolean isXa = false;
            if (connection instanceof XATransactionalConnection) {
                isXa = true;
                xaResource = (DummyXaResource) ((XATransactionalConnection) connection).getXAResource();
            }
            ctx.addVariable(IS_XA, isXa);
            ctx.bindConnection(connection);
            sourceCallback.handle(Result.<TestTransactionalConnection, Object>builder().output(connection).build(), ctx);
        } catch (ConnectionException e) {
            sourceCallback.onConnectionException(e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    });
}
Also used : SourceCallbackContext(org.mule.runtime.extension.api.runtime.source.SourceCallbackContext) TestTransactionalConnection(org.mule.test.transactional.connection.TestTransactionalConnection) XATransactionalConnection(org.mule.runtime.extension.api.connectivity.XATransactionalConnection) ConnectionException(org.mule.runtime.api.connection.ConnectionException) ConnectionException(org.mule.runtime.api.connection.ConnectionException) TransactionException(org.mule.runtime.api.tx.TransactionException) MuleException(org.mule.runtime.api.exception.MuleException)

Example 12 with ConnectionException

use of org.mule.runtime.api.connection.ConnectionException in project mule by mulesoft.

the class PoolingConnectionManagementStrategyTestCase method exhaustion.

@Test
public void exhaustion() throws Exception {
    poolingProfile = new PoolingProfile(1, 1, DEFAULT_MAX_POOL_WAIT, WHEN_EXHAUSTED_FAIL, INITIALISE_NONE);
    initStrategy();
    ConnectionHandler<Object> connectionHandler = strategy.getConnectionHandler();
    try {
        strategy.getConnectionHandler();
        fail("Was expecting the pool to be exhausted");
    } catch (ConnectionException e) {
    // wiiiii
    }
    connectionHandler.release();
    assertThat(strategy.getConnectionHandler(), is(notNullValue()));
}
Also used : Matchers.anyObject(org.mockito.Matchers.anyObject) PoolingProfile(org.mule.runtime.api.config.PoolingProfile) ConnectionException(org.mule.runtime.api.connection.ConnectionException) Test(org.junit.Test)

Example 13 with ConnectionException

use of org.mule.runtime.api.connection.ConnectionException in project mule by mulesoft.

the class ConnectionArgumentResolver method resolve.

/**
 * Returns the connection previously set on the {@code executionContext} under the key
 * {@link ExtensionProperties#CONNECTION_PARAM}
 *
 * @param executionContext an {@link ExecutionContext}
 * @return the connection
 * @throws IllegalArgumentException if the connection was not set
 * @throws ClassCastException if {@code executionContext} is not an {@link ExecutionContextAdapter}
 */
@Override
public LazyValue<Object> resolve(ExecutionContext executionContext) {
    return new LazyValue<>(() -> {
        ConnectionHandler connectionHandler = ((ExecutionContextAdapter<ComponentModel>) executionContext).getVariable(CONNECTION_PARAM);
        checkArgument(connectionHandler != null, "No connection was provided for the component [" + executionContext.getComponentModel().getName() + "]");
        try {
            return connectionHandler.getConnection();
        } catch (ConnectionException e) {
            throw new MuleRuntimeException(I18nMessageFactory.createStaticMessage(String.format("Error was found trying to obtain a connection to execute %s '%s' of extension '%s'", getComponentModelTypeName(executionContext.getComponentModel()), executionContext.getComponentModel().getName(), executionContext.getExtensionModel().getName())), e);
        }
    });
}
Also used : LazyValue(org.mule.runtime.api.util.LazyValue) ConnectionHandler(org.mule.runtime.api.connection.ConnectionHandler) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) ExecutionContextAdapter(org.mule.runtime.module.extension.api.runtime.privileged.ExecutionContextAdapter) ConnectionException(org.mule.runtime.api.connection.ConnectionException)

Example 14 with ConnectionException

use of org.mule.runtime.api.connection.ConnectionException in project mule by mulesoft.

the class ReconnectionWithStreamingTestCase method createMockCursor.

private CursorStream createMockCursor() throws IOException {
    CursorStream cursorStream = mock(CursorStream.class);
    when(cursorStream.getPosition()).thenReturn(ORIGINAL_POSITION);
    when(cursorStream.read(any(byte[].class), anyInt(), anyInt())).thenThrow(new RuntimeException(new ConnectionException("kaboom"))).thenAnswer(i -> {
        byte[] buffer = (byte[]) i.getArguments()[0];
        buffer[0] = 'h';
        buffer[1] = 'n';
        return 2;
    }).thenReturn(-1);
    return cursorStream;
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) AbstractExtensionFunctionalTestCase(org.mule.test.module.extension.AbstractExtensionFunctionalTestCase) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) CursorStreamProvider(org.mule.runtime.api.streaming.bytes.CursorStreamProvider) Mockito.times(org.mockito.Mockito.times) IOException(java.io.IOException) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) Assert.assertThat(org.junit.Assert.assertThat) Matchers.any(org.mockito.Matchers.any) List(java.util.List) ConnectionException(org.mule.runtime.api.connection.ConnectionException) CursorStream(org.mule.runtime.api.streaming.bytes.CursorStream) Matchers.hasSize(org.hamcrest.Matchers.hasSize) Matchers.anyInt(org.mockito.Matchers.anyInt) Mockito.mock(org.mockito.Mockito.mock) ConnectionException(org.mule.runtime.api.connection.ConnectionException) CursorStream(org.mule.runtime.api.streaming.bytes.CursorStream)

Example 15 with ConnectionException

use of org.mule.runtime.api.connection.ConnectionException in project mule by mulesoft.

the class DsqlQueryMetadataResolver method getOutputType.

/**
 * Automatically resolves the output metadata for the {@link DsqlQuery}.
 * <p>
 * The base entity is resolved using the component {@link QueryEntityResolver} and assuming the key of the entity is the DSQL
 * {@link DsqlQuery#getType() type}.
 *
 * @param context {@link MetadataContext} of the MetaData resolution
 * @param query the {@link DsqlQuery} to resolve the output metadata from.
 */
@Override
public MetadataType getOutputType(MetadataContext context, Object query) throws MetadataResolvingException, ConnectionException {
    if (query instanceof DsqlQuery) {
        DsqlQuery dsqlQuery = (DsqlQuery) query;
        MetadataType entityMetadata = entityResolver.getEntityMetadata(context, dsqlQuery.getType().getName());
        BaseTypeBuilder builder = context.getTypeBuilder();
        final List<Field> fields = dsqlQuery.getFields();
        if (fields.size() == 1 && fields.get(0).getName().equals("*")) {
            return entityMetadata;
        }
        entityMetadata.accept(new MetadataTypeVisitor() {

            @Override
            public void visitObject(ObjectType objectType) {
                ObjectTypeBuilder objectTypeBuilder = builder.objectType();
                objectType.getFields().stream().filter(p -> fields.stream().anyMatch(f -> f.getName().equalsIgnoreCase(p.getKey().getName().getLocalPart()))).forEach(p -> {
                    ObjectFieldTypeBuilder field = objectTypeBuilder.addField();
                    field.key(p.getKey().getName());
                    field.value(p.getValue());
                });
            }
        });
        return builder.build();
    } else {
        return nativeOutputResolver.getOutputType(context, query);
    }
}
Also used : ObjectFieldTypeBuilder(org.mule.metadata.api.builder.ObjectFieldTypeBuilder) ObjectType(org.mule.metadata.api.model.ObjectType) OutputTypeResolver(org.mule.runtime.api.metadata.resolving.OutputTypeResolver) Field(org.mule.runtime.extension.api.dsql.Field) BaseTypeBuilder(org.mule.metadata.api.builder.BaseTypeBuilder) MetadataTypeVisitor(org.mule.metadata.api.visitor.MetadataTypeVisitor) List(java.util.List) ConnectionException(org.mule.runtime.api.connection.ConnectionException) MetadataContext(org.mule.runtime.api.metadata.MetadataContext) ObjectTypeBuilder(org.mule.metadata.api.builder.ObjectTypeBuilder) QueryEntityResolver(org.mule.runtime.api.metadata.resolving.QueryEntityResolver) MetadataType(org.mule.metadata.api.model.MetadataType) MetadataResolvingException(org.mule.runtime.api.metadata.MetadataResolvingException) DsqlQuery(org.mule.runtime.extension.api.dsql.DsqlQuery) BaseTypeBuilder(org.mule.metadata.api.builder.BaseTypeBuilder) Field(org.mule.runtime.extension.api.dsql.Field) ObjectType(org.mule.metadata.api.model.ObjectType) DsqlQuery(org.mule.runtime.extension.api.dsql.DsqlQuery) ObjectFieldTypeBuilder(org.mule.metadata.api.builder.ObjectFieldTypeBuilder) MetadataType(org.mule.metadata.api.model.MetadataType) MetadataTypeVisitor(org.mule.metadata.api.visitor.MetadataTypeVisitor) ObjectTypeBuilder(org.mule.metadata.api.builder.ObjectTypeBuilder)

Aggregations

ConnectionException (org.mule.runtime.api.connection.ConnectionException)24 Test (org.junit.Test)18 SmallTest (org.mule.tck.size.SmallTest)10 IOException (java.io.IOException)6 MuleException (org.mule.runtime.api.exception.MuleException)6 ExceptionUtils.extractConnectionException (org.mule.runtime.core.api.util.ExceptionUtils.extractConnectionException)4 List (java.util.List)2 ExpectedException (org.junit.rules.ExpectedException)2 MuleFatalException (org.mule.runtime.api.exception.MuleFatalException)2 Error (org.mule.runtime.api.message.Error)2 HeisenbergException (org.mule.test.heisenberg.extension.exception.HeisenbergException)2 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)1 ExecutionException (java.util.concurrent.ExecutionException)1 Lock (java.util.concurrent.locks.Lock)1 ThrowableAssert.catchThrowable (org.assertj.core.api.ThrowableAssert.catchThrowable)1 CoreMatchers.instanceOf (org.hamcrest.CoreMatchers.instanceOf)1 CoreMatchers.is (org.hamcrest.CoreMatchers.is)1 Matchers.hasSize (org.hamcrest.Matchers.hasSize)1 Assert.assertThat (org.junit.Assert.assertThat)1 Matchers.any (org.mockito.Matchers.any)1