camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject svn commit: r560301 - in /activemq/camel/trunk/camel-core/src/main/java/org/apache/camel: NoSuchPropertyException.java builder/RouteBuilder.java component/file/FileProducer.java impl/converter/DefaultTypeConverter.java util/ExchangeHelper.java
Date Fri, 27 Jul 2007 16:24:20 GMT
Author: jstrachan
Date: Fri Jul 27 09:24:17 2007
New Revision: 560301

URL: http://svn.apache.org/viewvc?view=rev&rev=560301
Log:
made the file component a little more lenient in the payloads it can accept

Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/NoSuchPropertyException.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileProducer.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/DefaultTypeConverter.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExchangeHelper.java

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/NoSuchPropertyException.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/NoSuchPropertyException.java?view=diff&rev=560301&r1=560300&r2=560301
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/NoSuchPropertyException.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/NoSuchPropertyException.java
Fri Jul 27 09:24:17 2007
@@ -47,6 +47,10 @@
 
     protected static String reason(Exchange exchange, String propertyName) {
         Object value = exchange.getProperty(propertyName);
+        return valueDescription(value);
+    }
+
+    static String valueDescription(Object value) {
         if (value == null) {
             return "";
         }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java?view=diff&rev=560301&r1=560300&r2=560301
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java
Fri Jul 27 09:24:17 2007
@@ -18,6 +18,7 @@
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Collection;
 import java.util.concurrent.atomic.AtomicBoolean;
 
 import org.apache.camel.CamelContext;
@@ -25,6 +26,7 @@
 import org.apache.camel.Exchange;
 import org.apache.camel.Processor;
 import org.apache.camel.Route;
+import org.apache.camel.processor.DelegateProcessor;
 import org.apache.camel.impl.DefaultCamelContext;
 
 /**
@@ -37,6 +39,7 @@
     private List<FromBuilder> fromBuilders = new ArrayList<FromBuilder>();
     private AtomicBoolean initalized = new AtomicBoolean(false);
     private List<Route> routes = new ArrayList<Route>();
+    private List<DelegateProcessor> interceptors = new ArrayList<DelegateProcessor>();
 
     protected RouteBuilder() {
         this(null);
@@ -92,6 +95,13 @@
         return this;
     }
 
+    
+    @Fluent
+    public RouteBuilder intercept(@FluentArg("interceptor")DelegateProcessor interceptor)
{
+        interceptors.add(interceptor);
+        return this;
+    }
+    
     // Properties
     //-----------------------------------------------------------------------
     public CamelContext getContext() {
@@ -117,6 +127,10 @@
     public List<FromBuilder> getFromBuilders() throws Exception {
         checkInitialized();
         return fromBuilders;
+    }
+
+    public List<DelegateProcessor> getInterceptors() {
+        return interceptors;
     }
 
     // Implementation methods

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileProducer.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileProducer.java?view=diff&rev=560301&r1=560300&r2=560301
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileProducer.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileProducer.java
Fri Jul 27 09:24:17 2007
@@ -14,12 +14,15 @@
 package org.apache.camel.component.file;
 
 import org.apache.camel.Exchange;
+import org.apache.camel.InvalidPayloadException;
 import org.apache.camel.Producer;
 import org.apache.camel.impl.DefaultProducer;
+import org.apache.camel.util.ExchangeHelper;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
 import java.io.File;
+import java.io.InputStream;
 import java.io.RandomAccessFile;
 import java.nio.ByteBuffer;
 import java.nio.channels.FileChannel;
@@ -42,13 +45,17 @@
      * @param exchange
      * @see org.apache.camel.Processor#process(Exchange)
      */
-    public void process(Exchange exchange) {
+    public void process(Exchange exchange) throws Exception {
         process(endpoint.toExchangeType(exchange));
     }
 
-    public void process(FileExchange exchange) {
+    public void process(FileExchange exchange) throws Exception {
         String fileName = exchange.getIn().getMessageId();
         ByteBuffer payload = exchange.getIn().getBody(ByteBuffer.class);
+        if (payload == null) {
+            InputStream in = ExchangeHelper.getMandatoryInBody(exchange, InputStream.class);
+            payload = ExchangeHelper.convertToMandatoryType(exchange, ByteBuffer.class, in);
+        }
         payload.flip();
         File file = null;
         File endpointFile = endpoint.getFile();

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/DefaultTypeConverter.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/DefaultTypeConverter.java?view=diff&rev=560301&r1=560300&r2=560301
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/DefaultTypeConverter.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/DefaultTypeConverter.java
Fri Jul 27 09:24:17 2007
@@ -19,8 +19,8 @@
 
 import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.TypeConverter;
-import org.apache.camel.impl.ReflectionInjector;
 import org.apache.camel.spi.Injector;
+import org.apache.camel.spi.TypeConverterAware;
 import org.apache.camel.util.FactoryFinder;
 import org.apache.camel.util.NoFactoryAvailableException;
 import org.apache.camel.util.ObjectHelper;
@@ -47,10 +47,10 @@
 
     public DefaultTypeConverter(Injector injector) {
         typeConverterLoaders.add(new AnnotationTypeConverterLoader());
-        fallbackConverters.add(new PropertyEditorTypeConverter());
-        fallbackConverters.add(new ToStringTypeConverter());
-        fallbackConverters.add(new ArrayTypeConverter());
         this.injector = injector;
+        addFallbackConverter(new PropertyEditorTypeConverter());
+        addFallbackConverter(new ToStringTypeConverter());
+        addFallbackConverter(new ArrayTypeConverter());
     }
 
     public <T> T convertTo(Class<T> toType, Object value) {
@@ -88,6 +88,14 @@
         }
     }
 
+    public void addFallbackConverter(TypeConverter converter) {
+        fallbackConverters.add(converter);
+        if (converter instanceof TypeConverterAware) {
+            TypeConverterAware typeConverterAware = (TypeConverterAware) converter;
+            typeConverterAware.setTypeConverter(this);
+        }
+    }
+
     public TypeConverter getTypeConverter(Class toType, Class fromType) {
         TypeMapping key = new TypeMapping(toType, fromType);
         synchronized (typeMappings) {
@@ -209,7 +217,9 @@
     protected void loadFallbackTypeConverters() throws IOException, ClassNotFoundException
{
         FactoryFinder finder = new FactoryFinder();
         List<TypeConverter> converters = finder.newInstances("FallbackTypeConverter",
getInjector(), TypeConverter.class);
-        fallbackConverters.addAll(converters);
+        for (TypeConverter converter : converters) {
+          addFallbackConverter(converter);
+        }
     }
 
     /**

Modified: 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=diff&rev=560301&r1=560300&r2=560301
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExchangeHelper.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExchangeHelper.java
Fri Jul 27 09:24:17 2007
@@ -19,11 +19,10 @@
 
 import org.apache.camel.Endpoint;
 import org.apache.camel.Exchange;
+import org.apache.camel.InvalidPayloadException;
+import org.apache.camel.InvalidTypeException;
 import org.apache.camel.NoSuchEndpointException;
 import org.apache.camel.NoSuchPropertyException;
-import org.apache.camel.component.file.FileExchange;
-
-import java.nio.channels.Channel;
 
 /**
  * Some helper methods for working with {@link Exchange} objects
@@ -64,5 +63,34 @@
             throw new NoSuchPropertyException(exchange, propertyName, type);
         }
         return answer;
+    }
+
+    /**
+     * Returns the mandatory inbound message body of the correct type or throws an exception
if it is not present
+     */
+    public static <T> T getMandatoryInBody(Exchange exchange, Class<T> type)
throws InvalidPayloadException {
+        T answer = exchange.getIn().getBody(type);
+        if (answer == null) {
+            throw new InvalidPayloadException(exchange, type);
+        }
+        return answer;
+    }
+    
+    /**
+     * Converts the value to the given expected type or throws an exception
+     */
+    public static <T> T convertToMandatoryType(Exchange exchange, Class<T> type,
Object value) throws InvalidTypeException {
+        T answer = convertToType(exchange, type, value);
+        if (answer == null) {
+            throw new InvalidTypeException(exchange, value, type);
+        }
+        return answer;
+    }
+
+    /**
+     * Converts the value to the given expected type returning null if it could not be converted
+     */
+    public static <T> T convertToType(Exchange exchange, Class<T> type, Object
value) {
+        return exchange.getContext().getTypeConverter().convertTo(type, value);
     }
 }



Mime
View raw message