You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2011/10/15 12:13:27 UTC
svn commit: r1183614 - in /camel/trunk/camel-core/src:
main/java/org/apache/camel/component/bean/
test/java/org/apache/camel/component/bean/
test/java/org/apache/camel/component/bean/issues/
Author: davsclaus
Date: Sat Oct 15 10:13:26 2011
New Revision: 1183614
URL: http://svn.apache.org/viewvc?rev=1183614&view=rev
Log:
CAMEL-4541: Bean component supports binding to private class beans by fallback to use interfaces. Thanks to Mathieu for the patch.
Added:
camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanPrivateClassWithInterfaceMethodTest.java
camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/issues/PrivateClasses.java
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java
Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java?rev=1183614&r1=1183613&r2=1183614&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java Sat Oct 15 10:13:26 2011
@@ -213,7 +213,18 @@ public class BeanInfo {
LOG.trace("Introspecting class: {}", clazz);
- Method[] methods = clazz.getDeclaredMethods();
+ // if the class is not public then fallback and use interface methods if possible
+ // this allow Camel to invoke private beans which implements interfaces
+ List<Method> methods = Arrays.asList(clazz.getDeclaredMethods());
+ if (!Modifier.isPublic(clazz.getModifiers())) {
+ LOG.trace("Preferring interface methods as class: {} is not public accessible", clazz);
+ List<Method> interfaceMethods = getInterfaceMethods(clazz);
+
+ // still keep non-accessible class methods to provide more specific Exception if method is non-accessible
+ interfaceMethods.addAll(methods);
+ methods = interfaceMethods;
+ }
+
for (Method method : methods) {
boolean valid = isValidMethod(clazz, method);
LOG.trace("Method: {} is valid: {}", method, valid);
@@ -300,9 +311,8 @@ public class BeanInfo {
return answer;
}
- @SuppressWarnings("unchecked")
- protected MethodInfo createMethodInfo(Class clazz, Method method) {
- Class[] parameterTypes = method.getParameterTypes();
+ protected MethodInfo createMethodInfo(Class<?> clazz, Method method) {
+ Class<?>[] parameterTypes = method.getParameterTypes();
List<Annotation>[] parametersAnnotations = collectParameterAnnotations(clazz, method);
List<ParameterInfo> parameters = new ArrayList<ParameterInfo>();
@@ -317,7 +327,7 @@ public class BeanInfo {
}
for (int i = 0; i < size; i++) {
- Class parameterType = parameterTypes[i];
+ Class<?> parameterType = parameterTypes[i];
Annotation[] parameterAnnotations = parametersAnnotations[i].toArray(new Annotation[parametersAnnotations[i].size()]);
Expression expression = createParameterUnmarshalExpression(clazz, method, parameterType, parameterAnnotations);
hasCustomAnnotation |= expression != null;
@@ -457,7 +467,7 @@ public class BeanInfo {
Message in = exchange.getIn();
Object body = in.getBody();
if (body != null) {
- Class bodyType = body.getClass();
+ Class<?> bodyType = body.getClass();
if (LOG.isTraceEnabled()) {
LOG.trace("Matching for method with a single parameter that matches type: {}", bodyType.getCanonicalName());
}
@@ -678,6 +688,17 @@ public class BeanInfo {
return null;
}
+
+ private static List<Method> getInterfaceMethods(Class<?> clazz) {
+ final List<Method> answer = new ArrayList<Method>();
+ for (Class<?> interfaceClazz : clazz.getInterfaces()) {
+ for (Method interfaceMethod : interfaceClazz.getDeclaredMethods()) {
+ answer.add(interfaceMethod);
+ }
+ }
+
+ return answer;
+ }
private static void removeAllSetterOrGetterMethods(List<MethodInfo> methods) {
Iterator<MethodInfo> it = methods.iterator();
@@ -728,7 +749,7 @@ public class BeanInfo {
String types = ObjectHelper.between(methodName, "(", ")");
if (types != null) {
// we must qualify based on types to match method
- Iterator it = ObjectHelper.createIterator(types);
+ Iterator<?> it = ObjectHelper.createIterator(types);
for (int i = 0; i < method.getParameterTypes().length; i++) {
if (it.hasNext()) {
Class<?> parameterType = method.getParameterTypes()[i];
@@ -825,10 +846,9 @@ public class BeanInfo {
*
* @return the methods.
*/
- @SuppressWarnings("unchecked")
public List<MethodInfo> getMethods() {
if (operations.isEmpty()) {
- return Collections.EMPTY_LIST;
+ return Collections.emptyList();
}
List<MethodInfo> methods = new ArrayList<MethodInfo>();
Added: camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanPrivateClassWithInterfaceMethodTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanPrivateClassWithInterfaceMethodTest.java?rev=1183614&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanPrivateClassWithInterfaceMethodTest.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanPrivateClassWithInterfaceMethodTest.java Sat Oct 15 10:13:26 2011
@@ -0,0 +1,77 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.bean;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.bean.issues.PrivateClasses.HelloCamel;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.Test;
+
+import static org.apache.camel.component.bean.issues.PrivateClasses.EXPECTED_OUTPUT;
+import static org.apache.camel.component.bean.issues.PrivateClasses.METHOD_NAME;
+import static org.apache.camel.component.bean.issues.PrivateClasses.newPackagePrivateHelloCamel;
+import static org.apache.camel.component.bean.issues.PrivateClasses.newPrivateHelloCamel;
+
+/**
+ * Tests Bean binding for private & package-private classes where the target method is accessible through an interface.
+ */
+public final class BeanPrivateClassWithInterfaceMethodTest extends ContextTestSupport {
+
+ private static final String INPUT_BODY = "Whatever";
+ private final HelloCamel packagePrivateImpl = newPackagePrivateHelloCamel();
+ private final HelloCamel privateImpl = newPrivateHelloCamel();
+
+ @Test
+ public void testPackagePrivateClassBinding() throws InterruptedException {
+ MockEndpoint mockResult = getMockEndpoint("mock:packagePrivateClassResult");
+ mockResult.setExpectedMessageCount(1);
+ mockResult.message(0).body().equals(EXPECTED_OUTPUT);
+
+ template.sendBody("direct:testPackagePrivateClass", INPUT_BODY);
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Test
+ public void testPrivateClassBinding() throws InterruptedException {
+ MockEndpoint mockResult = getMockEndpoint("mock:privateClassResult");
+ mockResult.setExpectedMessageCount(1);
+ mockResult.message(0).body().equals(EXPECTED_OUTPUT);
+
+ template.sendBody("direct:testPrivateClass", INPUT_BODY);
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("direct:testPackagePrivateClass")
+ .bean(packagePrivateImpl, METHOD_NAME)
+ .to("mock:packagePrivateClassResult");
+
+ from("direct:testPrivateClass")
+ .bean(privateImpl, METHOD_NAME)
+ .to("mock:privateClassResult");
+ }
+ };
+ }
+
+}
Added: camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/issues/PrivateClasses.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/issues/PrivateClasses.java?rev=1183614&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/issues/PrivateClasses.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/issues/PrivateClasses.java Sat Oct 15 10:13:26 2011
@@ -0,0 +1,66 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.bean.issues;
+
+/**
+ * Holds Private classes to be bean-binded through their interface
+ */
+public final class PrivateClasses {
+ public static final String EXPECTED_OUTPUT = "Hello Camel";
+ public static final String METHOD_NAME = "sayHello";
+
+ private PrivateClasses() {
+ // Utility class; can't be instantiated
+ }
+
+ /**
+ * Public interface through which we can bean-bind a private impl
+ */
+ public static interface HelloCamel {
+ String sayHello(String input);
+ }
+
+ static final class PackagePrivateHelloCamel implements HelloCamel {
+ @Override
+ public String sayHello(String input) {
+ return EXPECTED_OUTPUT;
+ }
+ }
+
+ private static final class PrivateHelloCamel implements HelloCamel {
+ @Override
+ public String sayHello(String input) {
+ return EXPECTED_OUTPUT;
+ }
+ }
+
+ /**
+ * @return package-private implementation that can only be bean-binded
+ * through its interface
+ */
+ public static HelloCamel newPackagePrivateHelloCamel() {
+ return new PackagePrivateHelloCamel();
+ }
+
+ /**
+ * @return private implementation that can only be bean-binded through its
+ * interface
+ */
+ public static HelloCamel newPrivateHelloCamel() {
+ return new PrivateHelloCamel();
+ }
+}