1
|
|
|
package br.ufrj.ppgi.greco.kettle.silk; |
2
|
|
|
|
3
|
|
|
import java.io.BufferedReader; |
4
|
|
|
import java.io.File; |
5
|
|
|
import java.io.FileReader; |
6
|
|
|
import java.io.IOException; |
7
|
|
|
import java.net.URISyntaxException; |
8
|
|
|
import java.nio.file.Paths; |
9
|
|
|
|
10
|
|
|
import javax.xml.bind.JAXBContext; |
11
|
|
|
import javax.xml.bind.Marshaller; |
12
|
|
|
|
13
|
|
|
import org.pentaho.di.core.exception.KettleException; |
14
|
|
|
import br.ufrj.ppgi.greco.kettle.LinkDiscoveryToolStepMeta; |
15
|
|
|
|
16
|
|
|
public class SilkRunner { |
17
|
|
|
|
18
|
|
|
private LinkDiscoveryToolStepMeta input; |
19
|
|
|
private File configFile; |
20
|
|
|
private File log; |
21
|
|
|
|
22
|
|
|
public SilkRunner(LinkDiscoveryToolStepMeta input) { |
23
|
|
|
this.input = input; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Saves the SLS-XML file using the params on the view. |
28
|
|
|
* |
29
|
|
|
* @throws KettleException |
30
|
|
|
*/ |
31
|
|
|
public void saveXML() throws KettleException { |
32
|
|
|
Silk silk = new Silk(input); |
33
|
|
|
try { |
34
|
|
|
this.configFile = File.createTempFile("sls", ".xml"); |
35
|
|
|
JAXBContext context = JAXBContext.newInstance(Silk.class); |
36
|
|
|
Marshaller m = context.createMarshaller(); |
37
|
|
|
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); |
38
|
|
|
m.marshal(silk, this.configFile); |
39
|
|
|
} catch (Exception e) { |
40
|
|
|
throw new KettleException("Saving config file error: " + e.getMessage()); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Runs a SLS-XML file using Silk Single Machine. |
46
|
|
|
* |
47
|
|
|
* @param slsFilePath |
48
|
|
|
* SLS file path |
49
|
|
|
* @throws IOException |
50
|
|
|
* if SLS file not found |
51
|
|
|
* @throws KettleException |
52
|
|
|
* if Silk.jar not found |
53
|
|
|
* @throws InterruptedException |
54
|
|
|
* silk.jar was interrupted |
55
|
|
|
*/ |
56
|
|
|
public void run(String slsFilePath) throws IOException, KettleException, URISyntaxException, InterruptedException { |
57
|
|
|
String silkSingleMachine = ""; |
58
|
|
|
silkSingleMachine = Paths |
59
|
|
|
.get(new File(SilkRunner.class.getProtectionDomain().getCodeSource().getLocation().toURI()) |
60
|
|
|
.getParentFile().getPath(), "silk.jar") |
61
|
|
|
.toString(); |
62
|
|
|
if (new File(silkSingleMachine).exists()) { |
63
|
|
|
ProcessBuilder pb = new ProcessBuilder("java", "-DconfigFile=" + slsFilePath, "-jar", silkSingleMachine); |
64
|
|
|
if (!input.isSparqlOutput()){ |
65
|
|
|
File result = new File(input.getFilePath()); |
66
|
|
|
pb.redirectOutput(result); |
67
|
|
|
} |
68
|
|
|
log = File.createTempFile("status", ".log"); |
69
|
|
|
pb.redirectError(log); |
70
|
|
|
Process p = pb.start(); |
71
|
|
|
p.waitFor(); |
72
|
|
|
p.destroy(); |
73
|
|
|
this.configFile.deleteOnExit(); |
74
|
|
|
this.log.deleteOnExit(); |
75
|
|
|
} else { |
76
|
|
|
throw new KettleException(silkSingleMachine |
77
|
|
|
+ " Silk single machine was not found! Make sure you have silk.jar as a dependency."); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public String getConfigFilename() { |
82
|
|
|
return configFile.getAbsolutePath(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
private String getContents(File file) throws IOException { |
86
|
|
|
BufferedReader buffer = new BufferedReader(new FileReader(file)); |
87
|
|
|
try { |
88
|
|
|
StringBuilder builder = new StringBuilder(); |
89
|
|
|
String line = buffer.readLine(); |
90
|
|
|
while (line != null) { |
91
|
|
|
builder.append(line).append("\n"); |
92
|
|
|
line = buffer.readLine(); |
93
|
|
|
} |
94
|
|
|
return builder.toString(); |
95
|
|
|
} finally { |
96
|
|
|
buffer.close(); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public String getLogs() throws IOException { |
101
|
|
|
return getContents(log); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
} |
105
|
|
|
|