1
|
package eu.dnetlib.data.collector.plugins.filesystem;
|
2
|
|
3
|
import java.io.File;
|
4
|
import java.io.FileInputStream;
|
5
|
import java.io.IOException;
|
6
|
import java.net.MalformedURLException;
|
7
|
import java.net.URL;
|
8
|
import java.util.Iterator;
|
9
|
|
10
|
import org.apache.commons.io.FileUtils;
|
11
|
import org.apache.commons.io.IOUtils;
|
12
|
import org.apache.commons.logging.Log;
|
13
|
import org.apache.commons.logging.LogFactory;
|
14
|
|
15
|
import com.google.common.base.Function;
|
16
|
import com.google.common.base.Splitter;
|
17
|
import com.google.common.collect.Iterables;
|
18
|
import com.google.common.collect.Iterators;
|
19
|
|
20
|
import eu.dnetlib.data.collector.rmi.CollectorServiceException;
|
21
|
import eu.dnetlib.data.collector.rmi.InterfaceDescriptor;
|
22
|
|
23
|
/**
|
24
|
* The Class FilesystemIterable.
|
25
|
*
|
26
|
* @author Sandro, Michele
|
27
|
*/
|
28
|
public class FilesystemIterable implements Iterable<String> {
|
29
|
|
30
|
/** The Constant log. */
|
31
|
private static final Log log = LogFactory.getLog(FilesystemIterable.class);
|
32
|
|
33
|
/** The base dir. */
|
34
|
private File baseDir;
|
35
|
|
36
|
/** The extensions. */
|
37
|
private String[] extensions;
|
38
|
|
39
|
/**
|
40
|
* Instantiates a new filesystem iterable.
|
41
|
*
|
42
|
* @param descriptor
|
43
|
* the descriptor
|
44
|
* @throws CollectorServiceException
|
45
|
* the collector service exception
|
46
|
*/
|
47
|
public FilesystemIterable(final InterfaceDescriptor descriptor) throws CollectorServiceException {
|
48
|
try {
|
49
|
final String baseUrl = descriptor.getBaseUrl();
|
50
|
URL basePath = new URL(baseUrl);
|
51
|
this.baseDir = new File(basePath.getPath());
|
52
|
if (!baseDir.exists()) throw new CollectorServiceException(String.format("The base ULR %s, does not exist", basePath.getPath()));
|
53
|
this.extensions = Iterables
|
54
|
.toArray(Splitter.on(",").omitEmptyStrings().trimResults().split(descriptor.getParams().get("extensions")), String.class);
|
55
|
} catch (MalformedURLException e) {
|
56
|
throw new CollectorServiceException("Filesystem collector failed! ", e);
|
57
|
}
|
58
|
}
|
59
|
|
60
|
/**
|
61
|
* {@inheritDoc}
|
62
|
*
|
63
|
* @see java.lang.Iterable#iterator()
|
64
|
*/
|
65
|
@Override
|
66
|
public Iterator<String> iterator() {
|
67
|
return Iterators.transform(FileUtils.iterateFiles(baseDir, extensions, true), new Function<File, String>() {
|
68
|
|
69
|
@Override
|
70
|
public String apply(final File input) {
|
71
|
FileInputStream fileInputStream = null;
|
72
|
try {
|
73
|
fileInputStream = new FileInputStream(input);
|
74
|
String s = IOUtils.toString(fileInputStream);
|
75
|
return s.startsWith("\uFEFF") ? s.substring(1) : s;
|
76
|
} catch (Exception e) {
|
77
|
log.error("Unable to read " + input.getPath());
|
78
|
return null;
|
79
|
} finally {
|
80
|
if (fileInputStream != null) {
|
81
|
try {
|
82
|
fileInputStream.close();
|
83
|
} catch (IOException e) {
|
84
|
log.error("Unable to close inputstream for " + input.getPath());
|
85
|
}
|
86
|
}
|
87
|
}
|
88
|
}
|
89
|
});
|
90
|
}
|
91
|
|
92
|
}
|