77 *******************************************************************************/
88package org .eclipse .rdf4j .query .impl ;
99
10- import java .io .IOException ;
1110import java .io .InputStream ;
1211import java .io .InputStreamReader ;
13- import java .lang .reflect .UndeclaredThrowableException ;
1412import java .nio .charset .Charset ;
1513import java .util .Collections ;
1614import java .util .Map ;
17- import java .util .NoSuchElementException ;
1815import java .util .concurrent .ConcurrentHashMap ;
1916import java .util .concurrent .CountDownLatch ;
2017
@@ -46,6 +43,8 @@ public class BackgroundGraphResult extends IterationWrapper<Statement, QueryEval
4643
4744 private final CountDownLatch namespacesReady = new CountDownLatch (1 );
4845
46+ private final CountDownLatch finishedParsing = new CountDownLatch (1 );
47+
4948 private final Map <String , String > namespaces = new ConcurrentHashMap <String , String >();
5049
5150 private final QueueCursor <Statement > queue ;
@@ -66,118 +65,47 @@ public BackgroundGraphResult(QueueCursor<Statement> queue, RDFParser parser, Inp
6665 }
6766
6867 @ Override
69- public boolean hasNext ()
70- throws QueryEvaluationException
71- {
72- if (isClosed ()) {
73- return false ;
74- }
75- if (Thread .currentThread ().isInterrupted ()) {
76- close ();
77- return false ;
78- }
79-
80- boolean result = queue .hasNext ();
81- if (!result ) {
82- close ();
83- }
84- return result ;
85- }
86-
87- @ Override
88- public Statement next ()
68+ protected void handleClose ()
8969 throws QueryEvaluationException
9070 {
91- if (isClosed ()) {
92- throw new NoSuchElementException ("The iteration has been closed." );
93- }
94- if (Thread .currentThread ().isInterrupted ()) {
95- close ();
96- throw new NoSuchElementException ("The iteration has been closed." );
97- }
98-
9971 try {
100- return queue .next ();
101- }
102- catch (NoSuchElementException e ) {
103- close ();
104- throw e ;
72+ super .handleClose ();
10573 }
106- }
107-
108- @ Override
109- public void remove ()
110- throws QueryEvaluationException
111- {
112- if (isClosed ()) {
113- throw new IllegalStateException ("The iteration has been closed." );
114- }
115- if (Thread .currentThread ().isInterrupted ()) {
116- close ();
117- throw new IllegalStateException ("The iteration has been closed." );
74+ finally {
75+ queue .done ();
11876 }
119-
12077 try {
121- queue . remove ();
78+ finishedParsing . await ();
12279 }
123- catch (IllegalStateException e ) {
124- close ();
125- throw e ;
80+ catch (InterruptedException e ) {
81+ Thread .currentThread ().interrupt ();
82+ } finally {
83+ queue .checkException ();
12684 }
12785 }
12886
12987 @ Override
130- protected void handleClose ()
131- throws QueryEvaluationException
132- {
88+ public void run () {
13389 try {
13490 try {
135- super .handleClose ();
136- }
137- finally {
138- try {
139- // After checking that we ourselves cannot possibly be generating an NPE ourselves,
140- // attempt to close the input stream we were given
141- InputStream toClose = in ;
142- if (toClose != null ) {
143- toClose .close ();
144- }
91+ parser .setRDFHandler (this );
92+ if (charset == null ) {
93+ parser .parse (in , baseURI );
14594 }
146- catch ( NullPointerException e ) {
147- // Swallow NullPointerException that Apache HTTPClient is hiding behind a NotThreadSafe annotation
95+ else {
96+ parser . parse ( new InputStreamReader ( in , charset ), baseURI );
14897 }
98+ } finally {
99+ in .close ();
149100 }
150101 }
151- catch (IOException e ) {
152- throw new QueryEvaluationException (e );
153- }
154- finally {
155- queue .close ();
156- }
157- }
158-
159- @ Override
160- public void run () {
161- try {
162- parser .setRDFHandler (this );
163- if (charset == null ) {
164- parser .parse (in , baseURI );
165- }
166- else {
167- parser .parse (new InputStreamReader (in , charset ), baseURI );
168- }
169- }
170- catch (RDFHandlerException e ) {
171- // parsing was cancelled or interrupted
172- close ();
173- }
174102 catch (Exception e ) {
175103 queue .toss (e );
176- close ();
177104 }
178105 finally {
179106 queue .done ();
180107 namespacesReady .countDown ();
108+ finishedParsing .countDown ();
181109 }
182110 }
183111
@@ -197,8 +125,9 @@ public Map<String, String> getNamespaces() {
197125 }
198126 catch (InterruptedException e ) {
199127 Thread .currentThread ().interrupt ();
200- close ();
201- throw new UndeclaredThrowableException (e );
128+ return Collections .emptyMap ();
129+ } finally {
130+ queue .checkException ();
202131 }
203132 }
204133
@@ -226,11 +155,8 @@ public void handleStatement(Statement st)
226155 }
227156 catch (InterruptedException e ) {
228157 Thread .currentThread ().interrupt ();
229- close ();
230- throw new RDFHandlerException (e );
231- }
232- if (isClosed ()) {
233- throw new RDFHandlerException ("Result closed" );
158+ queue .toss (e );
159+ queue .done ();
234160 }
235161 }
236162
0 commit comments