Search in sources :

Example 21 with MuleContextWithRegistries

use of org.mule.runtime.core.internal.context.MuleContextWithRegistries in project mule by mulesoft.

the class DefaultTransformationService method getPayload.

/**
 * Attempts to obtain the payload of this message with the desired Class type. This will try and resolve a transformer that can
 * do this transformation. If a transformer cannot be found an exception is thrown. Any transformers added to the registry will
 * be checked for compatibility.
 *
 * @param resultType the desired return type
 * @param encoding the encoding to use if required
 * @return The converted payload of this message. Note that this method will not alter the payload of this message <b>unless</b>
 *         the payload is an {@link InputStream} in which case the stream will be read and the payload will become the fully
 *         read stream.
 * @throws MessageTransformerException if a transformer cannot be found or there is an error during transformation of the payload.
 */
@SuppressWarnings("unchecked")
protected <T> T getPayload(Message message, DataType resultType, Charset encoding) throws MessageTransformerException {
    // Handle null by ignoring the request
    if (resultType == null) {
        throw new IllegalArgumentException(objectIsNull("resultType").getMessage());
    }
    DataType dataType = DataType.builder(resultType).type(message.getPayload().getDataType().getType()).build();
    // If no conversion is necessary, just return the payload as-is
    if (resultType.isCompatibleWith(dataType)) {
        return (T) message.getPayload().getValue();
    }
    // The transformer to execute on this message
    Transformer transformer = null;
    try {
        transformer = ((MuleContextWithRegistries) muleContext).getRegistry().lookupTransformer(dataType, resultType);
        if (transformer == null) {
            throw new MessageTransformerException(noTransformerFoundForMessage(dataType, resultType), null, message);
        }
        // Pass in the message itself
        Object result = transformer.transform(message, encoding);
        // Unless we disallow Object.class as a valid return type we need to do this extra check
        checkResultDataType(message, resultType, result);
        return (T) result;
    } catch (MessageTransformerException e) {
        throw e;
    } catch (TransformerException e) {
        throw new MessageTransformerException(transformer, e, message);
    }
}
Also used : Transformer(org.mule.runtime.core.api.transformer.Transformer) MuleContextWithRegistries(org.mule.runtime.core.internal.context.MuleContextWithRegistries) DataType(org.mule.runtime.api.metadata.DataType) MessageTransformerException(org.mule.runtime.core.api.transformer.MessageTransformerException) MessageTransformerException(org.mule.runtime.core.api.transformer.MessageTransformerException) TransformerException(org.mule.runtime.core.api.transformer.TransformerException)

Example 22 with MuleContextWithRegistries

use of org.mule.runtime.core.internal.context.MuleContextWithRegistries in project mule by mulesoft.

the class AbstractAsyncRequestReplyRequester method initialise.

@Override
public void initialise() throws InitialisationException {
    name = format(NAME_TEMPLATE, storePrefix, muleContext.getConfiguration().getId(), getLocation().getRootContainerName());
    MuleRegistry registry = ((MuleContextWithRegistries) muleContext).getRegistry();
    store = ((ObjectStoreManager) registry.get(OBJECT_STORE_MANAGER)).createObjectStore(name, ObjectStoreSettings.builder().persistent(false).maxEntries(MAX_PROCESSED_GROUPS).entryTtl(UNCLAIMED_TIME_TO_LIVE).expirationInterval(UNCLAIMED_INTERVAL).build());
    try {
        notificationFirer = registry.lookupObject(NotificationDispatcher.class);
    } catch (RegistrationException e) {
        throw new InitialisationException(e, this);
    }
}
Also used : RegistrationException(org.mule.runtime.core.privileged.registry.RegistrationException) MuleContextWithRegistries(org.mule.runtime.core.internal.context.MuleContextWithRegistries) NotificationDispatcher(org.mule.runtime.api.notification.NotificationDispatcher) MuleRegistry(org.mule.runtime.core.internal.registry.MuleRegistry) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException)

Example 23 with MuleContextWithRegistries

use of org.mule.runtime.core.internal.context.MuleContextWithRegistries in project mule by mulesoft.

the class DefaultMuleContextFactoryTestCase method testCreateMuleContextConfigurationBuilderProperties.

@Test
public void testCreateMuleContextConfigurationBuilderProperties() throws InitialisationException, ConfigurationException {
    Properties properties = new Properties();
    properties.put("testKey3", "testValue3");
    properties.put("testKey4", "testValue4");
    context = muleContextFactory.createMuleContext(asList(testServicesConfigurationBuilder, testConfigBuilder), (Map) properties);
    assertMuleContextConfiguration(context);
    assertConfigurationBuilder1Objects(context);
    assertEquals("testValue3", ((MuleContextWithRegistries) context).getRegistry().lookupObject("testKey3"));
    assertEquals("testValue4", ((MuleContextWithRegistries) context).getRegistry().lookupObject("testKey4"));
    assertNoDefaults(context);
}
Also used : MuleContextWithRegistries(org.mule.runtime.core.internal.context.MuleContextWithRegistries) Properties(java.util.Properties) HashMap(java.util.HashMap) Map(java.util.Map) Collections.singletonMap(java.util.Collections.singletonMap) Test(org.junit.Test)

Example 24 with MuleContextWithRegistries

use of org.mule.runtime.core.internal.context.MuleContextWithRegistries in project mule by mulesoft.

the class TransformerCachingTestCase method testCacheUpdate.

@Test
public void testCacheUpdate() throws Exception {
    DataType sourceType = DataType.fromType(FilterInputStream.class);
    MuleRegistry registry = ((MuleContextWithRegistries) muleContext).getRegistry();
    Transformer trans = registry.lookupTransformer(sourceType, BYTE_ARRAY);
    assertNotNull(trans);
    assertTrue(trans instanceof InputStreamToByteArray);
    Transformer trans2 = new FilterInputStreamToByteArray();
    registry.registerTransformer(trans2);
    trans = registry.lookupTransformer(sourceType, BYTE_ARRAY);
    assertNotNull(trans);
    assertTrue(trans instanceof FilterInputStreamToByteArray);
    trans = registry.lookupTransformer(INPUT_STREAM, BYTE_ARRAY);
    assertNotNull(trans);
    assertTrue(trans instanceof InputStreamToByteArray);
    registry.unregisterTransformer(trans2.getName());
    trans = registry.lookupTransformer(sourceType, BYTE_ARRAY);
    assertNotNull(trans);
    assertTrue(trans instanceof InputStreamToByteArray);
}
Also used : Transformer(org.mule.runtime.core.api.transformer.Transformer) DiscoverableTransformer(org.mule.runtime.core.api.transformer.DiscoverableTransformer) AbstractTransformer(org.mule.runtime.core.api.transformer.AbstractTransformer) InputStreamToByteArray(org.mule.runtime.core.internal.transformer.simple.InputStreamToByteArray) MuleContextWithRegistries(org.mule.runtime.core.internal.context.MuleContextWithRegistries) DataType(org.mule.runtime.api.metadata.DataType) Test(org.junit.Test)

Example 25 with MuleContextWithRegistries

use of org.mule.runtime.core.internal.context.MuleContextWithRegistries in project mule by mulesoft.

the class RegistryBrokerTestCase method testCrossRegistryLifecycleOrder.

@Test
public void testCrossRegistryLifecycleOrder() throws MuleException {
    TransientRegistry reg1 = new TransientRegistry(getUUID(), muleContext, new MuleLifecycleInterceptor());
    reg1.initialise();
    TransientRegistry reg2 = new TransientRegistry(getUUID(), muleContext, new MuleLifecycleInterceptor());
    reg2.initialise();
    reg1.registerObject("flow", new LifecycleTrackerFlow("flow", muleContext));
    reg2.registerObject("flow2", new LifecycleTrackerFlow("flow2", muleContext));
    ((MuleContextWithRegistries) muleContext).addRegistry(reg1);
    ((MuleContextWithRegistries) muleContext).addRegistry(reg2);
    muleContext.start();
    // Both connectors are started before either flow
    assertEquals("flow2-start flow-start ", tracker.toString());
    tracker = new String();
    muleContext.stop();
    // Both services are stopped before either connector
    assertEquals("flow2-stop flow-stop ", tracker);
}
Also used : MuleContextWithRegistries(org.mule.runtime.core.internal.context.MuleContextWithRegistries) MuleLifecycleInterceptor(org.mule.runtime.core.internal.lifecycle.MuleLifecycleInterceptor) Test(org.junit.Test)

Aggregations

MuleContextWithRegistries (org.mule.runtime.core.internal.context.MuleContextWithRegistries)25 Test (org.junit.Test)12 MuleRegistry (org.mule.runtime.core.internal.registry.MuleRegistry)7 Before (org.junit.Before)6 NotificationDispatcher (org.mule.runtime.api.notification.NotificationDispatcher)6 RegistrationException (org.mule.runtime.core.privileged.registry.RegistrationException)6 Collections.singletonMap (java.util.Collections.singletonMap)5 HashMap (java.util.HashMap)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 Map (java.util.Map)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 Assert.assertEquals (org.junit.Assert.assertEquals)4 DataType (org.mule.runtime.api.metadata.DataType)4 Transformer (org.mule.runtime.core.api.transformer.Transformer)4 FileReader (java.io.FileReader)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 BigDecimal (java.math.BigDecimal)3 BigInteger (java.math.BigInteger)3 URI (java.net.URI)3