1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
package org.apache.commons.pipeline.stage; |
19 | |
|
20 | |
import java.io.IOException; |
21 | |
import java.io.InputStream; |
22 | |
import java.net.MalformedURLException; |
23 | |
import java.net.URL; |
24 | |
import java.util.ArrayList; |
25 | |
import java.util.List; |
26 | |
import java.util.Queue; |
27 | |
import org.apache.commons.logging.Log; |
28 | |
import org.apache.commons.logging.LogFactory; |
29 | |
import org.apache.commons.pipeline.stage.BaseStage; |
30 | |
import org.apache.commons.pipeline.StageException; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
public class URLToInputStreamStage extends BaseStage { |
37 | 2 | private final Log log = LogFactory.getLog(URLToInputStreamStage.class); |
38 | 2 | private List<InputStream> inputStreams = new ArrayList<InputStream>(); |
39 | |
|
40 | |
|
41 | 4 | public URLToInputStreamStage() { } |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
public void process(Object obj) throws org.apache.commons.pipeline.StageException { |
50 | 2 | URL url = null; |
51 | 2 | if (obj instanceof URL){ |
52 | 2 | url = (URL) obj; |
53 | 0 | } else if (obj instanceof String) { |
54 | 0 | String urlString = (String) obj; |
55 | |
try { |
56 | 0 | url = new URL(urlString); |
57 | 0 | } catch (MalformedURLException e){ |
58 | 0 | throw new StageException(this, "Error converting url String:" + urlString, e); |
59 | 0 | } |
60 | |
} |
61 | |
|
62 | |
try { |
63 | 2 | InputStream inputStream = url.openStream(); |
64 | 2 | this.inputStreams.add(inputStream); |
65 | 2 | log.info("enqueing input stream"); |
66 | 2 | this.emit(inputStream); |
67 | 0 | } catch (IOException e){ |
68 | 0 | throw new StageException(this, "Error with stream from url:" + url, e); |
69 | 2 | } |
70 | 2 | } |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
public void release() { |
76 | 1 | log.info("running post process number of streams:" + inputStreams.size()); |
77 | 2 | while(inputStreams.size() > 0){ |
78 | 1 | InputStream is = (InputStream) inputStreams.remove(0); |
79 | |
try { |
80 | 1 | is.close(); |
81 | 1 | log.info("closed stream"); |
82 | 0 | } catch (IOException e){ |
83 | 0 | log.warn("Error closing stream",e); |
84 | 1 | } |
85 | 1 | } |
86 | 1 | } |
87 | |
} |