activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject svn commit: r520517 - in /activemq/camel/trunk/camel-core/src: main/java/org/apache/camel/ main/java/org/apache/camel/builder/ main/java/org/apache/camel/impl/ main/java/org/apache/camel/util/ test/java/org/apache/camel/
Date Tue, 20 Mar 2007 18:47:35 GMT
Author: jstrachan
Date: Tue Mar 20 11:47:34 2007
New Revision: 520517

URL: http://svn.apache.org/viewvc?view=rev&rev=520517
Log:
added more builder methods together with a better Splitter example

Added:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/TypeConverter.java   (with
props)
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultTypeConverter.java
  (with props)
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExchangeHelper.java
  (with props)
Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/BuilderSupport.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/RouteBuilderTest.java

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java?view=diff&rev=520517&r1=520516&r2=520517
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java Tue Mar
20 11:47:34 2007
@@ -24,6 +24,7 @@
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.impl.DefaultEndpointResolver;
 import org.apache.camel.impl.DefaultExchangeConverter;
+import org.apache.camel.impl.DefaultTypeConverter;
 
 /**
  * Represents the context used to configure routes and the policies to use.
@@ -37,6 +38,7 @@
     private ExchangeConverter exchangeConverter;
     private Map<String, Component> components = new HashMap<String, Component>();
 	private Map<Endpoint<E>, Processor<E>> routes;
+    private TypeConverter typeConverter;
     
     /**
      * Activates all the starting endpoints in that were added as routes.
@@ -58,6 +60,35 @@
         }
     }
 
+
+    public Component getOrCreateComponent(String componentName, Callable<Component<E>>
factory) {
+        synchronized (components) {
+            Component component = components.get(componentName);
+            if (component == null) {
+                try {
+                    component = factory.call();
+                    if (component == null) {
+                        throw new IllegalArgumentException("Factory failed to create the
" + componentName + " component, it returned null.");
+                    }
+                    components.put(componentName, component);
+                    component.setContainer(this);
+                }
+                catch (Exception e) {
+                    throw new IllegalArgumentException("Factory failed to create the " +
componentName + " component", e);
+                }
+            }
+            return component;
+        }
+    }
+
+    public Component getComponent(String componentName) {
+        synchronized (components) {
+            Component component = components.get(componentName);
+            return component;
+        }
+    }
+
+
     // Builder APIs
     //-----------------------------------------------------------------------
     public void routes(RouteBuilder<E> builder) {
@@ -123,43 +154,41 @@
         this.exchangeConverter = exchangeConverter;
     }
 
+    public TypeConverter getTypeConverter() {
+        if (typeConverter == null) {
+            typeConverter = createTypeConverter();
+        }
+        return typeConverter;
+    }
+
+    public void setTypeConverter(TypeConverter typeConverter) {
+        this.typeConverter = typeConverter;
+    }
+
     // Implementation methods
     //-----------------------------------------------------------------------
+
+    /**
+     * Lazily create a default implementation
+     */
     protected EndpointResolver<E> createEndpointResolver() {
         return new DefaultEndpointResolver<E>();
     }
 
+
     /**
-     * Lazily create a default exchange converter implementation
+     * Lazily create a default implementation
      */
     protected ExchangeConverter createExchangeConverter() {
         return new DefaultExchangeConverter();
     }
 
-    public Component getOrCreateComponent(String componentName, Callable<Component<E>>
factory) {
-        synchronized (components) {
-            Component component = components.get(componentName);
-            if (component == null) {
-                try {
-                    component = factory.call();
-                    if (component == null) {
-                        throw new IllegalArgumentException("Factory failed to create the
" + componentName + " component, it returned null.");
-                    }
-                    components.put(componentName, component);
-                    component.setContainer(this);
-                }
-                catch (Exception e) {
-                    throw new IllegalArgumentException("Factory failed to create the " +
componentName + " component", e);
-                }
-            }
-            return component;
-        }
-    }
 
-    public Component getComponent(String componentName) {
-        synchronized (components) {
-            Component component = components.get(componentName);
-            return component;
-        }
+    /**
+     * Lazily create a default implementation
+     */
+    private TypeConverter createTypeConverter() {
+        return new DefaultTypeConverter();
     }
+
 }

Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/TypeConverter.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/TypeConverter.java?view=auto&rev=520517
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/TypeConverter.java (added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/TypeConverter.java Tue
Mar 20 11:47:34 2007
@@ -0,0 +1,33 @@
+/**
+ *
+ * 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;
+
+/**
+ * A pluggable strategy to be able to convert objects to different types such as from DOM
to JAXB2 or String to DOM etc
+ *
+ * @version $Revision$
+ */
+public interface TypeConverter {
+    /**
+     * Converts the value to the specified type
+     * @param type the requested type
+     * @param value the value to be converted
+     * @return the converted value or null if it can not be converted
+     */
+    <T> T convertTo(Class<T> type, Object value);
+}

Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/TypeConverter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/TypeConverter.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/TypeConverter.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/BuilderSupport.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/BuilderSupport.java?view=diff&rev=520517&r1=520516&r2=520517
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/BuilderSupport.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/BuilderSupport.java
Tue Mar 20 11:47:34 2007
@@ -32,4 +32,39 @@
     public ValueBuilder<E> header(String name) {
         Expression<E> expression = ExpressionBuilder.headerExpression(name);
         return new ValueBuilder<E>(expression);
-    }}
+    }
+
+    /**
+     * Returns a predicate and value builder for the inbound body on an exchange
+     */
+    public ValueBuilder<E> body() {
+        Expression<E> expression = ExpressionBuilder.bodyExpression();
+        return new ValueBuilder<E>(expression);
+    }
+
+    /**
+     * Returns a predicate and value builder for the inbound message body as a specific type
+     */
+    public ValueBuilder<E> bodyAs(Class type) {
+        Expression<E> expression = ExpressionBuilder.bodyExpression(type);
+        return new ValueBuilder<E>(expression);
+    }
+
+    /**
+     * Returns a predicate and value builder for the outbound body on an exchange
+     */
+    public ValueBuilder<E> outBody() {
+        Expression<E> expression = ExpressionBuilder.bodyExpression();
+        return new ValueBuilder<E>(expression);
+    }
+
+    /**
+     * Returns a predicate and value builder for the outbound message body as a specific
type
+     */
+    public ValueBuilder<E> outBody(Class type) {
+        Expression<E> expression = ExpressionBuilder.bodyExpression(type);
+        return new ValueBuilder<E>(expression);
+    }
+
+
+}

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java?view=diff&rev=520517&r1=520516&r2=520517
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
Tue Mar 20 11:47:34 2007
@@ -19,11 +19,14 @@
 import org.apache.camel.Exchange;
 import org.apache.camel.Expression;
 
+import java.util.StringTokenizer;
+import java.util.List;
+import java.util.ArrayList;
+
 /**
  * @version $Revision: $
  */
 public class ExpressionBuilder {
-
     /**
      * Returns an expression for the header value with the given name
      *
@@ -47,6 +50,7 @@
             }
         };
     }
+
     /**
      * Returns an expression for the contant value
      *
@@ -62,6 +66,78 @@
             @Override
             public String toString() {
                 return "" + value;
+            }
+        };
+    }
+
+    /**
+     * Returns the expression for the exchanges inbound message body
+     */
+    public static <E extends Exchange> Expression<E> bodyExpression() {
+        return new Expression<E>() {
+            public Object evaluate(E exchange) {
+                return exchange.getIn().getBody();
+            }
+
+            @Override
+            public String toString() {
+                return "Body";
+            }
+        };
+    }
+
+
+    /**
+     * Returns the expression for the exchanges inbound message body converted to the given
type
+     */
+    public static <E extends Exchange> Expression<E> bodyExpression(final Class
type) {
+        return new Expression<E>() {
+            public Object evaluate(E exchange) {
+                return exchange.getIn().getBody(type);
+            }
+
+            @Override
+            public String toString() {
+                return "BodyAs[" + type.getName() + "]";
+            }
+        };
+    }
+
+    /**
+     * Returns the expression for the out messages body
+     */
+    public static <E extends Exchange> Expression<E> outBodyExpression() {
+        return new Expression<E>() {
+            public Object evaluate(E exchange) {
+                return exchange.getOut().getBody();
+            }
+
+            @Override
+            public String toString() {
+                return "OutBody";
+            }
+        };
+    }
+
+    /**
+     * Returns a tokenize expression which will tokenize the string with the given token
+     */
+    public static <E extends Exchange> Expression<E> tokenizeExpression(final
Expression<E> expression, final String token) {
+        return new Expression<E>() {
+            public Object evaluate(E exchange) {
+                Object value = expression.evaluate(exchange);
+                String text = exchange.getContext().getTypeConverter().convertTo(String.class,
value);
+                StringTokenizer iter = new StringTokenizer(text, token);
+                List<String> answer = new ArrayList<String>();
+                while (iter.hasMoreTokens()) {
+                    answer.add(iter.nextToken());
+                }
+                return answer;
+            }
+
+            @Override
+            public String toString() {
+                return "tokenize(" + expression + ", " + token + ")";
             }
         };
     }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java?view=diff&rev=520517&r1=520516&r2=520517
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java
Tue Mar 20 11:47:34 2007
@@ -132,4 +132,16 @@
             }
         };
     }
+
+    public static <E extends Exchange> Predicate<E> isInstanceOf(final Expression<E>
expression, final Class type) {
+        notNull(expression, "expression");
+        notNull(type, "type");
+
+        return new Predicate<E>() {
+            public boolean evaluate(E exchange) {
+                Object value = expression.evaluate(exchange);
+                return type.isInstance(value);
+            }
+        };
+    }
 }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java?view=diff&rev=520517&r1=520516&r2=520517
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java
Tue Mar 20 11:47:34 2007
@@ -62,7 +62,20 @@
         return PredicateBuilder.isGreaterThanOrEqualTo(expression, right);
     }
 
+    public Predicate<E> isInstanceOf(Class type) {
+        return PredicateBuilder.isInstanceOf(expression, type);
+    }
+
     public Expression<E> getExpression() {
         return expression;
+    }
+
+    public ValueBuilder<E> tokenize() {
+        return tokenize("\n");
+    }
+
+    public ValueBuilder<E> tokenize(String token) {
+        Expression<E> newExp = ExpressionBuilder.tokenizeExpression(expression, token);
+        return new ValueBuilder<E>(newExp);
     }
 }

Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultTypeConverter.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultTypeConverter.java?view=auto&rev=520517
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultTypeConverter.java
(added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultTypeConverter.java
Tue Mar 20 11:47:34 2007
@@ -0,0 +1,37 @@
+/**
+ *
+ * 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.impl;
+
+import org.apache.camel.TypeConverter;
+
+/**
+ * @version $Revision$
+ */
+public class DefaultTypeConverter implements TypeConverter {
+    public <T> T convertTo(Class<T> type, Object value) {
+        if (type.isInstance(value)) {
+            return type.cast(value);
+        }
+        if (value != null) {
+            if (type.equals(String.class)) {
+                return (T) value.toString();
+            }
+        }
+        return null;
+    }
+}

Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultTypeConverter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultTypeConverter.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultTypeConverter.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExchangeHelper.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExchangeHelper.java?view=auto&rev=520517
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExchangeHelper.java
(added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExchangeHelper.java
Tue Mar 20 11:47:34 2007
@@ -0,0 +1,29 @@
+/**
+ *
+ * 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.util;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Expression;
+
+/**
+ * Some helper methods for working with {@link Exchange} objects
+ *
+ * @version $Revision$
+ */
+public class ExchangeHelper {
+}

Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExchangeHelper.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExchangeHelper.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExchangeHelper.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/RouteBuilderTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/RouteBuilderTest.java?view=diff&rev=520517&r1=520516&r2=520517
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/RouteBuilderTest.java (original)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/RouteBuilderTest.java Tue
Mar 20 11:47:34 2007
@@ -377,7 +377,7 @@
         // START SNIPPET: e9
         RouteBuilder<Exchange> builder = new RouteBuilder<Exchange>() {
             public void configure() {
-                from("queue:a").splitter(header("cheese")).to("queue:b");
+                from("queue:a").splitter(bodyAs(String.class).tokenize("\n")).to("queue:b");
             }
         };
         // END SNIPPET: e9



Mime
View raw message