Return-Path: X-Original-To: apmail-camel-commits-archive@www.apache.org Delivered-To: apmail-camel-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id E42459E2A for ; Thu, 27 Oct 2011 13:25:35 +0000 (UTC) Received: (qmail 53518 invoked by uid 500); 27 Oct 2011 13:25:35 -0000 Delivered-To: apmail-camel-commits-archive@camel.apache.org Received: (qmail 53489 invoked by uid 500); 27 Oct 2011 13:25:35 -0000 Mailing-List: contact commits-help@camel.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@camel.apache.org Delivered-To: mailing list commits@camel.apache.org Received: (qmail 53482 invoked by uid 99); 27 Oct 2011 13:25:35 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 27 Oct 2011 13:25:35 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 27 Oct 2011 13:25:32 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 7D58923889DE for ; Thu, 27 Oct 2011 13:25:11 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1189747 - in /camel/branches/camel-2.8.x: ./ camel-core/src/main/java/org/apache/camel/builder/xml/XPathBuilder.java camel-core/src/test/java/org/apache/camel/language/XPathFromFileExceptionTest.java Date: Thu, 27 Oct 2011 13:25:11 -0000 To: commits@camel.apache.org From: davsclaus@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20111027132511.7D58923889DE@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: davsclaus Date: Thu Oct 27 13:25:10 2011 New Revision: 1189747 URL: http://svn.apache.org/viewvc?rev=1189747&view=rev Log: CAMEL-4591: Fixed issue with locking File on Windows if XPath evaluation failed from a file source Added: camel/branches/camel-2.8.x/camel-core/src/test/java/org/apache/camel/language/XPathFromFileExceptionTest.java - copied unchanged from r1189737, camel/trunk/camel-core/src/test/java/org/apache/camel/language/XPathFromFileExceptionTest.java Modified: camel/branches/camel-2.8.x/ (props changed) camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/builder/xml/XPathBuilder.java Propchange: camel/branches/camel-2.8.x/ ------------------------------------------------------------------------------ --- svn:mergeinfo (original) +++ svn:mergeinfo Thu Oct 27 13:25:10 2011 @@ -1 +1 @@ -/camel/trunk:1186106,1186625,1186772,1187221,1187485,1187882,1187893,1188070-1188085,1188642,1188674,1188879,1188881,1189600,1189681,1189693 +/camel/trunk:1186106,1186625,1186772,1187221,1187485,1187882,1187893,1188070-1188085,1188642,1188674,1188879,1188881,1189600,1189681,1189693,1189737 Propchange: camel/branches/camel-2.8.x/ ------------------------------------------------------------------------------ Binary property 'svnmerge-integrated' - no diff available. Modified: camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/builder/xml/XPathBuilder.java URL: http://svn.apache.org/viewvc/camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/builder/xml/XPathBuilder.java?rev=1189747&r1=1189746&r2=1189747&view=diff ============================================================================== --- camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/builder/xml/XPathBuilder.java (original) +++ camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/builder/xml/XPathBuilder.java Thu Oct 27 13:25:10 2011 @@ -16,6 +16,7 @@ */ package org.apache.camel.builder.xml; +import java.io.File; import java.io.InputStream; import java.io.StringReader; import java.util.List; @@ -54,6 +55,7 @@ import org.apache.camel.impl.Synchroniza import org.apache.camel.spi.Language; import org.apache.camel.spi.NamespaceAware; import org.apache.camel.util.ExchangeHelper; +import org.apache.camel.util.IOHelper; import org.apache.camel.util.MessageHelper; import org.apache.camel.util.ObjectHelper; import org.slf4j.Logger; @@ -651,8 +653,18 @@ public class XPathBuilder implements Exp // set exchange and variable resolver as thread locals for concurrency this.exchange.set(exchange); + // the underlying input stream, which we need to close to avoid locking files or other resources + InputStream is = null; try { - Object document = getDocument(exchange); + Object document; + // only convert to input stream if really needed + if (isInputStreamNeeded(exchange)) { + is = exchange.getIn().getBody(InputStream.class); + document = getDocument(exchange, is); + } else { + Object body = exchange.getIn().getBody(); + document = getDocument(exchange, body); + } if (resultQName != null) { if (document instanceof InputSource) { InputSource inputSource = (InputSource) document; @@ -676,6 +688,9 @@ public class XPathBuilder implements Exp } } catch (XPathExpressionException e) { throw new InvalidXPathExpression(getText(), e); + } finally { + // IOHelper can handle if is is null + IOHelper.close(is); } if (LOG.isTraceEnabled()) { @@ -761,21 +776,47 @@ public class XPathBuilder implements Exp } /** + * Checks whether we need an {@link InputStream} to access the message body. + *

+ * Depending on the content in the message body, we may not need to convert + * to {@link InputStream}. + * + * @param exchange the current exchange + * @return true to convert to {@link InputStream} beforehand converting afterwards. + */ + protected boolean isInputStreamNeeded(Exchange exchange) { + Object body = exchange.getIn().getBody(); + if (body == null) { + return false; + } + + if (body instanceof WrappedFile) { + body = ((WrappedFile) body).getFile(); + } + if (body instanceof File) { + // input stream is needed for File to avoid locking the file in case of errors etc + return true; + } + + // input stream is not needed otherwise + return false; + } + + /** * Strategy method to extract the document from the exchange. */ @SuppressWarnings("unchecked") - protected Object getDocument(Exchange exchange) { + protected Object getDocument(Exchange exchange, Object body) { Object answer = null; - Message in = exchange.getIn(); Class type = getDocumentType(); if (type != null) { // try to get the body as the desired type - answer = in.getBody(type); + answer = exchange.getContext().getTypeConverter().convertTo(type, exchange, body); } // fallback to get the body as is if (answer == null) { - answer = in.getBody(); + answer = body; } // lets try coerce some common types into something JAXP can deal with