Search in sources :

Example 6 with Method

use of java.lang.reflect.Method in project camel by apache.

the class CxfRsProducer method invokeAsyncProxyClient.

protected void invokeAsyncProxyClient(Exchange exchange, final AsyncCallback callback) throws Exception {
    Message inMessage = exchange.getIn();
    Object[] varValues = inMessage.getHeader(CxfConstants.CAMEL_CXF_RS_VAR_VALUES, Object[].class);
    String methodName = inMessage.getHeader(CxfConstants.OPERATION_NAME, String.class);
    Client target;
    JAXRSClientFactoryBean cfb = clientFactoryBeanCache.get(CxfEndpointUtils.getEffectiveAddress(exchange, ((CxfRsEndpoint) getEndpoint()).getAddress()));
    Bus bus = ((CxfRsEndpoint) getEndpoint()).getBus();
    // We need to apply the bus setting from the CxfRsEndpoint which is not use the default bus
    if (bus != null) {
        cfb.setBus(bus);
    }
    if (varValues == null) {
        target = cfb.create();
    } else {
        target = cfb.createWithValues(varValues);
    }
    setupClientHeaders(target, exchange);
    // find out the method which we want to invoke
    JAXRSServiceFactoryBean sfb = cfb.getServiceFactory();
    sfb.getResourceClasses();
    // check the null body first
    Object[] parameters = null;
    if (inMessage.getBody() != null) {
        parameters = inMessage.getBody(Object[].class);
    }
    // get the method
    Method method = findRightMethod(sfb.getResourceClasses(), methodName, getParameterTypes(parameters));
    CxfRsEndpoint cxfRsEndpoint = (CxfRsEndpoint) getEndpoint();
    final CxfProxyInvocationCallback invocationCallback = new CxfProxyInvocationCallback(target, exchange, cxfRsEndpoint, callback);
    WebClient.getConfig(target).getRequestContext().put(InvocationCallback.class.getName(), invocationCallback);
    // handle cookies
    CookieHandler cookieHandler = ((CxfRsEndpoint) getEndpoint()).getCookieHandler();
    loadCookies(exchange, target, cookieHandler);
    method.invoke(target, parameters);
}
Also used : Bus(org.apache.cxf.Bus) Message(org.apache.camel.Message) JAXRSClientFactoryBean(org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean) Method(java.lang.reflect.Method) JAXRSServiceFactoryBean(org.apache.cxf.jaxrs.JAXRSServiceFactoryBean) InvocationCallback(javax.ws.rs.client.InvocationCallback) WebClient(org.apache.cxf.jaxrs.client.WebClient) Client(org.apache.cxf.jaxrs.client.Client) CookieHandler(org.apache.camel.http.common.cookie.CookieHandler)

Example 7 with Method

use of java.lang.reflect.Method in project camel by apache.

the class MapperWithTwoMethods method selectMapperMultipleMethods.

@Test
public void selectMapperMultipleMethods() throws Exception {
    Method selectedMethod = customMapper.selectMethod(MapperWithTwoMethods.class, B.class);
    Assert.assertNotNull(selectedMethod);
    Assert.assertEquals(MapperWithTwoMethods.class.getMethod("convertToA", B.class), selectedMethod);
}
Also used : Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 8 with Method

use of java.lang.reflect.Method in project camel by apache.

the class FacebookMethodsTypeTest method areAllMethodsMapped.

@Test
public void areAllMethodsMapped() throws Exception {
    final Class<?>[] interfaces = Facebook.class.getInterfaces();
    for (Class<?> clazz : interfaces) {
        if (clazz.getName().endsWith("Methods")) {
            // check all methods of this *Methods interface
            for (Method method : clazz.getDeclaredMethods()) {
                final FacebookMethodsType methodsType = FacebookMethodsType.findMethod(method.getName(), method.getParameterTypes());
                assertNotNull("Expected method mapping not found:" + method.getName(), methodsType);
                assertEquals("Methods are not equal", method, methodsType.getMethod());
            }
        }
    }
}
Also used : Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 9 with Method

use of java.lang.reflect.Method in project camel by apache.

the class GenericFileEndpoint method createGenericFileStrategy.

/**
     * A strategy method to lazily create the file strategy
     */
@SuppressWarnings("unchecked")
protected GenericFileProcessStrategy<T> createGenericFileStrategy() {
    Class<?> factory = null;
    try {
        FactoryFinder finder = getCamelContext().getFactoryFinder("META-INF/services/org/apache/camel/component/");
        log.trace("Using FactoryFinder: {}", finder);
        factory = finder.findClass(getScheme(), "strategy.factory.", CamelContext.class);
    } catch (ClassNotFoundException e) {
        log.trace("'strategy.factory.class' not found", e);
    } catch (IOException e) {
        log.trace("No strategy factory defined in 'META-INF/services/org/apache/camel/component/'", e);
    }
    if (factory == null) {
        // use default
        try {
            log.trace("Using ClassResolver to resolve class: {}", DEFAULT_STRATEGYFACTORY_CLASS);
            factory = this.getCamelContext().getClassResolver().resolveClass(DEFAULT_STRATEGYFACTORY_CLASS);
        } catch (Exception e) {
            log.trace("Cannot load class: {}", DEFAULT_STRATEGYFACTORY_CLASS, e);
        }
        // fallback and us this class loader
        try {
            if (log.isTraceEnabled()) {
                log.trace("Using classloader: {} to resolve class: {}", this.getClass().getClassLoader(), DEFAULT_STRATEGYFACTORY_CLASS);
            }
            factory = this.getCamelContext().getClassResolver().resolveClass(DEFAULT_STRATEGYFACTORY_CLASS, this.getClass().getClassLoader());
        } catch (Exception e) {
            if (log.isTraceEnabled()) {
                log.trace("Cannot load class: {} using classloader: " + this.getClass().getClassLoader(), DEFAULT_STRATEGYFACTORY_CLASS, e);
            }
        }
        if (factory == null) {
            throw new TypeNotPresentException(DEFAULT_STRATEGYFACTORY_CLASS + " class not found", null);
        }
    }
    try {
        Method factoryMethod = factory.getMethod("createGenericFileProcessStrategy", CamelContext.class, Map.class);
        Map<String, Object> params = getParamsAsMap();
        log.debug("Parameters for Generic file process strategy {}", params);
        return (GenericFileProcessStrategy<T>) ObjectHelper.invokeMethod(factoryMethod, null, getCamelContext(), params);
    } catch (NoSuchMethodException e) {
        throw new TypeNotPresentException(factory.getSimpleName() + ".createGenericFileProcessStrategy method not found", e);
    }
}
Also used : CamelContext(org.apache.camel.CamelContext) IOException(java.io.IOException) Method(java.lang.reflect.Method) ExpressionIllegalSyntaxException(org.apache.camel.ExpressionIllegalSyntaxException) IOException(java.io.IOException) FactoryFinder(org.apache.camel.spi.FactoryFinder)

Example 10 with Method

use of java.lang.reflect.Method in project camel by apache.

the class HeaderSelectorProducer method doStart.

@Override
protected void doStart() throws Exception {
    for (final Method method : target.getClass().getDeclaredMethods()) {
        InvokeOnHeaders annotation = method.getAnnotation(InvokeOnHeaders.class);
        if (annotation != null) {
            for (InvokeOnHeader processor : annotation.value()) {
                bind(processor, method);
            }
        } else {
            bind(method.getAnnotation(InvokeOnHeader.class), method);
        }
    }
    handlers = Collections.unmodifiableMap(handlers);
    super.doStart();
}
Also used : InvokeOnHeaders(org.apache.camel.InvokeOnHeaders) InvokeOnHeader(org.apache.camel.InvokeOnHeader) Method(java.lang.reflect.Method)

Aggregations

Method (java.lang.reflect.Method)7234 Test (org.junit.Test)1402 InvocationTargetException (java.lang.reflect.InvocationTargetException)935 ArrayList (java.util.ArrayList)557 Field (java.lang.reflect.Field)498 IOException (java.io.IOException)463 HashMap (java.util.HashMap)279 Map (java.util.Map)232 PropertyDescriptor (java.beans.PropertyDescriptor)230 List (java.util.List)221 Annotation (java.lang.annotation.Annotation)174 Type (java.lang.reflect.Type)174 IndexedPropertyDescriptor (java.beans.IndexedPropertyDescriptor)168 BeanInfo (java.beans.BeanInfo)155 HashSet (java.util.HashSet)147 File (java.io.File)138 SimpleBeanInfo (java.beans.SimpleBeanInfo)128 FakeFox01BeanInfo (org.apache.harmony.beans.tests.support.mock.FakeFox01BeanInfo)128 URL (java.net.URL)114 ParameterizedType (java.lang.reflect.ParameterizedType)113