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;
}
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;
}
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);
}
}
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);
}
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();
}
Aggregations