camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject svn commit: r558732 - in /activemq/camel/trunk/camel-core/src: main/java/org/apache/camel/ main/java/org/apache/camel/component/file/strategy/ main/java/org/apache/camel/util/ test/java/org/apache/camel/util/
Date Mon, 23 Jul 2007 14:05:00 GMT
Author: jstrachan
Date: Mon Jul 23 07:04:59 2007
New Revision: 558732

URL: http://svn.apache.org/viewvc?view=rev&rev=558732
Log:
applied patch from Willem Jiang to support separate lock files when working with the file
component for CAMEL-62. Many thanks! FWIW I made a minor change to the patch to use a helper
method to check for mandatory properties on the exchange & throw a nice exception if they
were missing

Added:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/NoSuchPropertyException.java
      - copied, changed from r558674, activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/NoSuchLanguageException.java
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/util/ExchangeHelperTest.java
  (with props)
Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileStategySupport.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExchangeHelper.java

Copied: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/NoSuchPropertyException.java
(from r558674, activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/NoSuchLanguageException.java)
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/NoSuchPropertyException.java?view=diff&rev=558732&p1=activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/NoSuchLanguageException.java&r1=558674&p2=activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/NoSuchPropertyException.java&r2=558732
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/NoSuchLanguageException.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/NoSuchPropertyException.java
Mon Jul 23 07:04:59 2007
@@ -17,23 +17,39 @@
  */
 package org.apache.camel;
 
+import org.apache.camel.util.ExchangeHelper;
+
 /**
- * A runtime exception thrown if an attempt is made to resolve an unknown language definition.
+ * An exception caused when a mandatory property is not available on a message {@link Exchange}
  *
- * @see org.apache.camel.CamelContext#resolveLanguage(String)
+ * @see ExchangeHelper#getMandatoryProperty(Exchange, String, Class)
  *
  * @version $Revision$
  */
-public class NoSuchLanguageException extends RuntimeCamelException {
+public class NoSuchPropertyException extends CamelException {
 	private static final long serialVersionUID = -8721487431101572630L;
-	private final String language;
+	private final String propertyName;
+    private final Class<?> type;
+
+    public NoSuchPropertyException(Exchange exchange, String propertyName, Class<?>
type) {
+        super("No '"+ propertyName + "' property available of type: " + type.getName() +
reason(exchange, propertyName) + " on the exchange: " +  exchange);
+        this.propertyName = propertyName;
+        this.type = type;
+    }
+
+    public String getPropertyName() {
+        return propertyName;
+    }
 
-    public NoSuchLanguageException(String language) {
-        super("No language could be found for: " + language);
-        this.language = language;
+    public Class<?> getType() {
+        return type;
     }
 
-    public String getLanguage() {
-        return language;
+    protected static String reason(Exchange exchange, String propertyName) {
+        Object value = exchange.getProperty(propertyName);
+        if (value == null) {
+            return "";
+        }
+        return " but has value: " + value + " of type: " + value.getClass().getName();
     }
 }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileStategySupport.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileStategySupport.java?view=diff&rev=558732&r1=558731&r2=558732
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileStategySupport.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileStategySupport.java
Mon Jul 23 07:04:59 2007
@@ -19,21 +19,21 @@
 
 import org.apache.camel.component.file.FileEndpoint;
 import org.apache.camel.component.file.FileExchange;
+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.RandomAccessFile;
+import java.nio.channels.Channel;
 import java.nio.channels.FileChannel;
 import java.nio.channels.FileLock;
-import java.nio.channels.Channel;
 
 /**
  * @version $Revision: 1.1 $
  */
 public abstract class FileStategySupport implements FileStrategy {
     private static final transient Log log = LogFactory.getLog(FileStategySupport.class);
-
     private boolean lockFile;
 
     protected FileStategySupport() {
@@ -45,16 +45,18 @@
     }
 
     public boolean begin(FileEndpoint endpoint, FileExchange exchange, File file) throws
Exception {
-        if (isLockFile() ) {
+        if (isLockFile()) {
+            String lockFileName = file.getAbsoluteFile() + ".lock";
             if (log.isDebugEnabled()) {
-                log.debug("Locking file: " + file);
+                log.debug("Locking the file: " + file + " using the lock file name: " + lockFileName);
             }
 
-            FileChannel channel = new RandomAccessFile(file, "rw").getChannel();
+            FileChannel channel = new RandomAccessFile(lockFileName, "rw").getChannel();
             FileLock lock = channel.lock();
             if (lock != null) {
                 exchange.setProperty("org.apache.camel.fileChannel", channel);
                 exchange.setProperty("org.apache.camel.file.lock", lock);
+                exchange.setProperty("org.apache.camel.file.lock.name", lockFileName);
                 return true;
             }
             return false;
@@ -64,17 +66,14 @@
 
     public void commit(FileEndpoint endpoint, FileExchange exchange, File file) throws Exception
{
         if (isLockFile()) {
-            Channel channel = exchange.getProperty("org.apache.camel.fileChannel", Channel.class);
-            if (channel != null) {
-                if (log.isDebugEnabled()) {
-                    log.debug("Unlocking file: " + file);
-                }
-
-                channel.close();
-            }
-            else {
-                throw new Exception("No Channel available to close on exchange: " + exchange);
+            Channel channel = ExchangeHelper.getMandatoryProperty(exchange, "org.apache.camel.fileChannel",
Channel.class);
+            String lockfile = ExchangeHelper.getMandatoryProperty(exchange, "org.apache.camel.file.lock.name",
String.class);
+            if (log.isDebugEnabled()) {
+                log.debug("Unlocking file: " + file);
             }
+            channel.close();
+            File lock = new File(lockfile);
+            lock.delete();
         }
     }
 

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=558732&r1=558731&r2=558732
==============================================================================
--- 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
Mon Jul 23 07:04:59 2007
@@ -20,6 +20,10 @@
 import org.apache.camel.Endpoint;
 import org.apache.camel.Exchange;
 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
@@ -52,5 +56,13 @@
             }
         }
         return endpoint;
+    }
+
+    public static <T> T getMandatoryProperty(Exchange exchange, String propertyName,
Class<T> type) throws NoSuchPropertyException {
+        T  answer = exchange.getProperty(propertyName, type);
+        if (answer == null) {
+            throw new NoSuchPropertyException(exchange, propertyName, type);
+        }
+        return answer;
     }
 }

Added: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/util/ExchangeHelperTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/util/ExchangeHelperTest.java?view=auto&rev=558732
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/util/ExchangeHelperTest.java
(added)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/util/ExchangeHelperTest.java
Mon Jul 23 07:04:59 2007
@@ -0,0 +1,68 @@
+/**
+ *
+ * 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 junit.framework.TestCase;
+import org.apache.camel.Exchange;
+import org.apache.camel.NoSuchPropertyException;
+import org.apache.camel.impl.DefaultExchange;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import java.util.List;
+
+/**
+ * @version $Revision: 1.1 $
+ */
+public class ExchangeHelperTest extends TestCase {
+    private static final transient Log log = LogFactory.getLog(ExchangeHelperTest.class);
+
+    protected Exchange exchange;
+
+    public void testValidProperty() throws Exception {
+            String value = ExchangeHelper.getMandatoryProperty(exchange, "foo", String.class);
+        assertEquals("foo property", "123", value);
+    }
+
+    public void testMissingProperty() throws Exception {
+        try {
+            String value = ExchangeHelper.getMandatoryProperty(exchange, "bar", String.class);
+            fail("Should have failed but got: " + value);
+        }
+        catch (NoSuchPropertyException e) {
+            log.debug("Caught expected: " + e, e);
+        }
+    }
+
+    public void testPropertyOfIncompatibleType() throws Exception {
+        try {
+            List value = ExchangeHelper.getMandatoryProperty(exchange, "foo", List.class);
+            fail("Should have failed but got: " + value);
+        }
+        catch (NoSuchPropertyException e) {
+            log.debug("Caught expected: " + e, e);
+        }
+    }
+
+    @Override
+    protected void setUp() throws Exception {
+        exchange = new DefaultExchange(new DefaultCamelContext());
+        exchange.setProperty("foo", 123);        
+    }
+}

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



Mime
View raw message