From 62a6c73fd396287bbf64600fa12cc30bc37f161d Mon Sep 17 00:00:00 2001
From: gmantele <gmantele@ari.uni-heidelberg.de>
Date: Mon, 13 Apr 2015 14:27:19 +0200
Subject: [PATCH] [TAP] Fix unterminated thread after a failed UPLOAD. This bug
 happened when an uploaded VOTable reading was interrupted by an exception
 (like a ParseException)....the streaming thread was not stopped and was still
 waiting for a notification in order to read the next row.

---
 src/tap/data/VOTableIterator.java | 30 ++++++++++++++++++++++++++----
 src/tap/upload/Uploader.java      | 10 +++++++---
 2 files changed, 33 insertions(+), 7 deletions(-)

diff --git a/src/tap/data/VOTableIterator.java b/src/tap/data/VOTableIterator.java
index f659dcc..1f332df 100644
--- a/src/tap/data/VOTableIterator.java
+++ b/src/tap/data/VOTableIterator.java
@@ -1,5 +1,24 @@
 package tap.data;
 
+/*
+ * This file is part of TAPLibrary.
+ * 
+ * TAPLibrary is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ * 
+ * TAPLibrary is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with TAPLibrary.  If not, see <http://www.gnu.org/licenses/>.
+ * 
+ * Copyright 2015 - Astronomisches Rechen Institut (ARI)
+ */
+
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.NoSuchElementException;
@@ -23,11 +42,14 @@ import adql.db.DBType;
  * <p>{@link #getColType()} will return TAP type based on the type declared in the VOTable metadata part.</p>
  * 
  * @author Gr&eacute;gory Mantelet (ARI)
- * @version 2.0 (02/2015)
+ * @version 2.0 (04/2015)
  * @since 2.0
  */
 public class VOTableIterator implements TableIterator {
 
+	/** Message of the IOException sent when the streaming is aborted. */
+	protected static final String STREAM_ABORTED_MESSAGE = "Streaming aborted!";
+
 	/**
 	 * <p>This class lets consume the metadata and rows of a VOTable document.</p>
 	 * 
@@ -42,7 +64,7 @@ public class VOTableIterator implements TableIterator {
 	 * </p> 
 	 * 
 	 * @author Gr&eacute;gory Mantelet (ARI)
-	 * @version 2.0 (01/2015)
+	 * @version 2.0 (04/2015)
 	 * @since 2.0
 	 */
 	protected static class StreamVOTableSink implements TableSink {
@@ -102,7 +124,7 @@ public class VOTableIterator implements TableIterator {
 				 * (because endRows() is always called after acceptRow()...so, it means the iteration has been aborted before the end)
 				 * and so the stream reading should be interrupted: */
 				if (endReached)
-					throw new IOException("Streaming aborted!");
+					throw new IOException(STREAM_ABORTED_MESSAGE);
 
 				// Otherwise, keep the given row:
 				pendingRow = row;
@@ -336,7 +358,7 @@ public class VOTableIterator implements TableIterator {
 					try{
 						tb.streamStarTable(input, sink, null);
 					}catch(IOException e){
-						if (e.getMessage() != null && !e.getMessage().equals("Reading interrupted!"))
+						if (e.getMessage() != null && !e.getMessage().equals(STREAM_ABORTED_MESSAGE))
 							e.printStackTrace();
 					}
 				}
diff --git a/src/tap/upload/Uploader.java b/src/tap/upload/Uploader.java
index b2f7c69..27f7d81 100644
--- a/src/tap/upload/Uploader.java
+++ b/src/tap/upload/Uploader.java
@@ -16,7 +16,7 @@ package tap.upload;
  * You should have received a copy of the GNU Lesser General Public License
  * along with TAPLibrary.  If not, see <http://www.gnu.org/licenses/>.
  * 
- * Copyright 2012-2014 - UDS/Centre de Données astronomiques de Strasbourg (CDS),
+ * Copyright 2012-2015 - UDS/Centre de Données astronomiques de Strasbourg (CDS),
  *                       Astronomisches Rechen Institut (ARI)
  */
 
@@ -51,7 +51,7 @@ import com.oreilly.servlet.multipart.ExceededSizeException;
  * </p>
  * 
  * @author Gr&eacute;gory Mantelet (CDS;ARI)
- * @version 2.0 (01/2015)
+ * @version 2.0 (04/2015)
  * 
  * @see LimitedTableIterator
  * @see VOTableIterator
@@ -148,6 +148,7 @@ public class Uploader {
 	 * @see DBConnection#addUploadedTable(TAPTable, tap.data.TableIterator)
 	 */
 	public TAPSchema upload(final DALIUpload[] uploads) throws TAPException{
+		TableIterator dataIt = null;
 		InputStream votable = null;
 		String tableName = null;
 		try{
@@ -159,7 +160,7 @@ public class Uploader {
 				votable = upl.open();
 
 				// Start reading the VOTable (with the identified limit, if any):
-				TableIterator dataIt = new LimitedTableIterator(VOTableIterator.class, votable, limitUnit, limit);
+				dataIt = new LimitedTableIterator(VOTableIterator.class, votable, limitUnit, limit);
 
 				// Define the table to upload:
 				TAPColumn[] columns = dataIt.getMetadata();
@@ -175,6 +176,7 @@ public class Uploader {
 				dbConn.addUploadedTable(table, dataIt);
 
 				// Close the VOTable stream:
+				dataIt.close();
 				votable.close();
 				votable = null;
 			}
@@ -189,6 +191,8 @@ public class Uploader {
 			throw new TAPException("URI error while trying to open the VOTable of \"" + tableName + "\"!", e);
 		}finally{
 			try{
+				if (dataIt != null)
+					dataIt.close();
 				if (votable != null)
 					votable.close();
 			}catch(IOException ioe){
-- 
GitLab