Search in sources :

Example 76 with RuntimeCamelException

use of org.apache.camel.RuntimeCamelException in project camel by apache.

the class KestrelComponent method getMemcachedClient.

public MemcachedClient getMemcachedClient(KestrelConfiguration config, String queue) {
    String key = config.getAddressesAsString() + "/" + queue;
    MemcachedClient memcachedClient = memcachedClientCache.get(key);
    if (memcachedClient != null) {
        return memcachedClient;
    }
    synchronized (memcachedClientCache) {
        if ((memcachedClient = memcachedClientCache.get(key)) == null) {
            LOG.info("Creating MemcachedClient for " + key);
            try {
                memcachedClient = new MemcachedClient(memcachedConnectionFactory, config.getInetSocketAddresses());
            } catch (Exception e) {
                throw new RuntimeCamelException("Failed to connect to " + key, e);
            }
            memcachedClientCache.put(key, memcachedClient);
        }
    }
    return memcachedClient;
}
Also used : MemcachedClient(net.spy.memcached.MemcachedClient) RuntimeCamelException(org.apache.camel.RuntimeCamelException) RuntimeCamelException(org.apache.camel.RuntimeCamelException)

Example 77 with RuntimeCamelException

use of org.apache.camel.RuntimeCamelException in project camel by apache.

the class FacebookProducer method findMethod.

private FacebookMethodsType findMethod(Exchange exchange, Map<String, Object> properties) {
    FacebookMethodsType method = null;
    final List<FacebookMethodsType> candidates = endpoint.getCandidates();
    if (processInBody(exchange, properties)) {
        // filter candidates based on endpoint and exchange properties
        final Set<String> argNames = properties.keySet();
        final List<FacebookMethodsType> filteredMethods = filterMethods(candidates, MatchType.SUPER_SET, argNames.toArray(new String[argNames.size()]));
        // get the method to call
        if (filteredMethods.isEmpty()) {
            final Set<String> missing = getMissingProperties(endpoint.getMethod(), endpoint.getNameStyle(), argNames);
            throw new RuntimeCamelException(String.format("Missing properties for %s, need one or more from %s", endpoint.getMethod(), missing));
        } else if (filteredMethods.size() == 1) {
            // found an exact match
            method = filteredMethods.get(0);
        } else {
            method = FacebookMethodsTypeHelper.getHighestPriorityMethod(filteredMethods);
            LOG.warn("Calling highest priority method {} from methods {}", method, filteredMethods);
        }
    }
    return method;
}
Also used : RuntimeCamelException(org.apache.camel.RuntimeCamelException) FacebookMethodsType(org.apache.camel.component.facebook.data.FacebookMethodsType)

Example 78 with RuntimeCamelException

use of org.apache.camel.RuntimeCamelException in project camel by apache.

the class FacebookMethodsTypeHelper method invokeMethod.

/**
     * Invokes given method with argument values from given properties.
     *
     * @param facebook Facebook4J target object for invoke
     * @param method method to invoke
     * @param properties Map of arguments
     * @return result of method invocation
     * @throws RuntimeCamelException on errors
     */
public static Object invokeMethod(Facebook facebook, FacebookMethodsType method, Map<String, Object> properties) throws RuntimeCamelException {
    LOG.debug("Invoking {} with arguments {}", method.getName(), properties);
    final List<String> argNames = method.getArgNames();
    final Object[] values = new Object[argNames.size()];
    final List<Class<?>> argTypes = method.getArgTypes();
    final Class<?>[] types = argTypes.toArray(new Class[argTypes.size()]);
    int index = 0;
    for (String name : argNames) {
        Object value = properties.get(name);
        // is the parameter an array type?
        if (value != null && types[index].isArray()) {
            Class<?> type = types[index];
            if (value instanceof Collection) {
                // convert collection to array
                Collection<?> collection = (Collection<?>) value;
                Object array = Array.newInstance(type.getComponentType(), collection.size());
                if (array instanceof Object[]) {
                    collection.toArray((Object[]) array);
                } else {
                    int i = 0;
                    for (Object el : collection) {
                        Array.set(array, i++, el);
                    }
                }
                value = array;
            } else if (value.getClass().isArray() && type.getComponentType().isAssignableFrom(value.getClass().getComponentType())) {
                // convert derived array to super array
                final int size = Array.getLength(value);
                Object array = Array.newInstance(type.getComponentType(), size);
                for (int i = 0; i < size; i++) {
                    Array.set(array, i, Array.get(value, i));
                }
                value = array;
            } else {
                throw new IllegalArgumentException(String.format("Cannot convert %s to %s", value.getClass(), type));
            }
        }
        values[index++] = value;
    }
    try {
        return method.getMethod().invoke(facebook, values);
    } catch (Throwable e) {
        // skip wrapper exception to simplify stack
        String msg;
        if (e.getCause() != null && e.getCause() instanceof FacebookException) {
            e = e.getCause();
            msg = ((FacebookException) e).getErrorMessage();
        } else {
            msg = e.getMessage();
        }
        throw new RuntimeCamelException(String.format("Error invoking %s with %s: %s", method.getName(), properties, msg), e);
    }
}
Also used : FacebookException(facebook4j.FacebookException) Collection(java.util.Collection) RuntimeCamelException(org.apache.camel.RuntimeCamelException)

Example 79 with RuntimeCamelException

use of org.apache.camel.RuntimeCamelException in project camel by apache.

the class InstanceMethodTypeConverter method convertTo.

@SuppressWarnings("unchecked")
public <T> T convertTo(Class<T> type, Exchange exchange, Object value) {
    Object instance = injector.newInstance();
    if (instance == null) {
        throw new RuntimeCamelException("Could not instantiate an instance of: " + type.getCanonicalName());
    }
    // inject parent type converter
    if (instance instanceof TypeConverterAware) {
        if (registry instanceof TypeConverter) {
            TypeConverter parentTypeConverter = (TypeConverter) registry;
            ((TypeConverterAware) instance).setTypeConverter(parentTypeConverter);
        }
    }
    return useExchange ? (T) ObjectHelper.invokeMethod(method, instance, value, exchange) : (T) ObjectHelper.invokeMethod(method, instance, value);
}
Also used : TypeConverter(org.apache.camel.TypeConverter) TypeConverterAware(org.apache.camel.spi.TypeConverterAware) RuntimeCamelException(org.apache.camel.RuntimeCamelException)

Example 80 with RuntimeCamelException

use of org.apache.camel.RuntimeCamelException in project camel by apache.

the class BeanExchangeAsReturnTypeNotAllowedTest method testExchangeAsReturnTypeNotAllowed.

public void testExchangeAsReturnTypeNotAllowed() throws Exception {
    MockEndpoint result = getMockEndpoint("mock:result");
    result.expectedMessageCount(0);
    try {
        template.sendBody("direct:in", "Hello World");
        fail("Should have thrown IllegalStateException");
    } catch (RuntimeCamelException e) {
        assertTrue(e.getCause() instanceof IllegalStateException);
    // expected
    }
    result.assertIsSatisfied();
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) RuntimeCamelException(org.apache.camel.RuntimeCamelException)

Aggregations

RuntimeCamelException (org.apache.camel.RuntimeCamelException)196 HashMap (java.util.HashMap)52 CamelContextAware (org.apache.camel.CamelContextAware)45 DataFormatFactory (org.apache.camel.spi.DataFormatFactory)45 ConditionalOnBean (org.springframework.boot.autoconfigure.condition.ConditionalOnBean)45 ConditionalOnClass (org.springframework.boot.autoconfigure.condition.ConditionalOnClass)45 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)45 Bean (org.springframework.context.annotation.Bean)45 IOException (java.io.IOException)36 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)32 Test (org.junit.Test)16 ArrayList (java.util.ArrayList)11 Exchange (org.apache.camel.Exchange)11 InputStream (java.io.InputStream)9 GeneralSecurityException (java.security.GeneralSecurityException)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8 TimeoutException (java.util.concurrent.TimeoutException)7 QName (javax.xml.namespace.QName)7 Message (org.apache.camel.Message)7 Method (java.lang.reflect.Method)6