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 2013/02/02 11:22:03 UTC
svn commit: r1441724 - in /camel/trunk/camel-core/src:
main/java/org/apache/camel/builder/
main/java/org/apache/camel/language/simple/ast/
main/java/org/apache/camel/util/ test/java/org/apache/camel/language/simple/
test/java/org/apache/camel/processor/
Author: davsclaus
Date: Sat Feb 2 10:22:03 2013
New Revision: 1441724
URL: http://svn.apache.org/viewvc?rev=1441724&view=rev
Log:
CAMEL-6016: Added type function to simple language.
Added:
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/CBRSimpleTypeTest.java (with props)
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/Customer.java (with props)
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java
camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
camel/trunk/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java?rev=1441724&r1=1441723&r2=1441724&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java Sat Feb 2 10:22:03 2013
@@ -690,6 +690,41 @@ public final class ExpressionBuilder {
}
/**
+ * Returns an expression for a type value
+ *
+ * @param name the type name
+ * @return an expression object which will return the type value
+ */
+ public static Expression typeExpression(final String name) {
+ return new ExpressionAdapter() {
+ public Object evaluate(Exchange exchange) {
+ // it may refer to a class type
+ Class<?> type = exchange.getContext().getClassResolver().resolveClass(name);
+ if (type != null) {
+ return type;
+ }
+
+ int pos = name.lastIndexOf(".");
+ if (pos > 0) {
+ String before = name.substring(0, pos);
+ String after = name.substring(pos + 1);
+ type = exchange.getContext().getClassResolver().resolveClass(before);
+ if (type != null) {
+ return ObjectHelper.lookupConstantFieldValue(type, after);
+ }
+ }
+
+ throw ObjectHelper.wrapCamelExecutionException(exchange, new ClassNotFoundException("Cannot find type " + name));
+ }
+
+ @Override
+ public String toString() {
+ return "type:" + name;
+ }
+ };
+ }
+
+ /**
* Returns the expression for the exchanges inbound message body
*/
public static Expression bodyExpression() {
Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java?rev=1441724&r1=1441723&r2=1441724&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java Sat Feb 2 10:22:03 2013
@@ -175,6 +175,12 @@ public class SimpleFunctionExpression ex
return ExpressionBuilder.refExpression(remainder);
}
+ // const: prefix
+ remainder = ifStartsWithReturnRemainder("type:", function);
+ if (remainder != null) {
+ return ExpressionBuilder.typeExpression(remainder);
+ }
+
if (strict) {
throw new SimpleParserException("Unknown function: " + function, token.getIndex());
} else {
Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java?rev=1441724&r1=1441723&r2=1441724&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java Sat Feb 2 10:22:03 2013
@@ -1464,10 +1464,16 @@ public final class ObjectHelper {
return null;
}
+ // remove leading dots
+ if (name.startsWith(",")) {
+ name = name.substring(1);
+ }
+
for (Field field : clazz.getFields()) {
if (field.getName().equals(name)) {
try {
- return (String) field.get(null);
+ Object v = field.get(null);
+ return v.toString();
} catch (IllegalAccessException e) {
// ignore
return null;
Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java?rev=1441724&r1=1441723&r2=1441724&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java Sat Feb 2 10:22:03 2013
@@ -28,6 +28,7 @@ import java.util.Map;
import org.apache.camel.CamelAuthorizationException;
import org.apache.camel.CamelExecutionException;
import org.apache.camel.Exchange;
+import org.apache.camel.ExchangePattern;
import org.apache.camel.Expression;
import org.apache.camel.ExpressionIllegalSyntaxException;
import org.apache.camel.InvalidPayloadException;
@@ -1186,6 +1187,15 @@ public class SimpleTest extends Language
assertExpression("${camelContext.version}", context.getVersion());
}
+ public void testTypeConstant() throws Exception {
+ assertExpression("${type:org.apache.camel.Exchange.FILE_NAME}", Exchange.FILE_NAME);
+ assertExpression("${type:org.apache.camel.ExchangePattern.InOut}", ExchangePattern.InOut);
+
+ // non existing fields
+ assertExpression("${type:org.apache.camel.ExchangePattern.}", null);
+ assertExpression("${type:org.apache.camel.ExchangePattern.UNKNOWN}", null);
+ }
+
protected String getLanguageName() {
return "simple";
}
Added: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/CBRSimpleTypeTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/CBRSimpleTypeTest.java?rev=1441724&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/CBRSimpleTypeTest.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/CBRSimpleTypeTest.java Sat Feb 2 10:22:03 2013
@@ -0,0 +1,57 @@
+/**
+ * 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.processor;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+
+/**
+ *
+ */
+public class CBRSimpleTypeTest extends ContextTestSupport {
+
+ public void testCBR() throws Exception {
+ getMockEndpoint("mock:gold").expectedBodiesReceived("James");
+ getMockEndpoint("mock:silver").expectedBodiesReceived("Claus");
+ getMockEndpoint("mock:other").expectedBodiesReceived("Willem");
+
+ template.sendBodyAndHeader("direct:start", "Claus", "customer", Customer.SILVER);
+ template.sendBodyAndHeader("direct:start", "Willem", "customer", Customer.BRONZE);
+ template.sendBodyAndHeader("direct:start", "James", "customer", Customer.GOLD);
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ // START SNIPPET: e1
+ from("direct:start")
+ .choice()
+ .when().simple("${header.customer} == ${type:org.apache.camel.processor.Customer.GOLD}")
+ .to("mock:gold")
+ .when().simple("${header.customer} == ${type:org.apache.camel.processor.Customer.SILVER}")
+ .to("mock:silver")
+ .otherwise()
+ .to("mock:other");
+ // END SNIPPET: e1
+ }
+ };
+ }
+}
Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/CBRSimpleTypeTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/CBRSimpleTypeTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/Customer.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/Customer.java?rev=1441724&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/Customer.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/Customer.java Sat Feb 2 10:22:03 2013
@@ -0,0 +1,27 @@
+/**
+ * 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.processor;
+
+/**
+ *
+ */
+// START SNIPPET: e1
+public enum Customer {
+
+ GOLD, SILVER, BRONZE
+}
+// END SNIPPET: e1
Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/Customer.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/Customer.java
------------------------------------------------------------------------------
svn:keywords = Rev Date