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.
+ * <p/>
+ * Depending on the content in the message body, we may not need to convert
+ * to {@link InputStream}.
+ *
+ * @param exchange the current exchange
+ * @return <tt>true</tt> 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
|