1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.gwe.integration.slicer;
18
19 import java.io.BufferedReader;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.InputStreamReader;
23 import java.util.Map;
24
25 import org.gwe.utils.IOUtils;
26 import org.gwe.utils.StringUtils;
27
28
29
30
31
32
33 public class WrapperCLMProxyApp extends AbstractCLMProxyApp {
34
35 protected String proxiedModuleName;
36
37 public WrapperCLMProxyApp(String proxiedModuleName) {
38 this.proxiedModuleName = proxiedModuleName;
39 }
40
41 public String getProxiedModuleName() {
42 return proxiedModuleName;
43 }
44
45 public String getProxiedModuleInvocation() {
46 return getModuleInvocation(slicerHome);
47 }
48
49 public String getModuleInvocation(String slicerHomeDir) {
50 return slicerHomeDir + "/Slicer3 --launch " + IOUtils.concatenatePaths(getPluginsDir(slicerHomeDir), proxiedModuleName);
51 }
52
53 protected String generateCLMErrorXML(IOException e) {
54
55 return "";
56 }
57
58 public String generateProxyXML() {
59 return executeOSCommand(getProxiedModuleInvocation() + " --xml");
60 }
61
62 public String runProxyApp(String[] args) throws Exception {
63 return executeOSCommand(createOSCommand(args));
64 }
65
66 public String createOSCommand(String[] args) {
67 return getProxiedModuleInvocation() + " " + StringUtils.getArrayAsStr(args);
68 }
69
70 private String executeOSCommand(String cmd) {
71 try {
72 BufferedReader rdr = new BufferedReader(new InputStreamReader(executeOSCommandInternal(cmd)));
73 StringBuffer stdOut = new StringBuffer();
74 char[] cbuf = new char[1024];
75 for (int read = 0; read > -1; read = rdr.read(cbuf)) stdOut.append(new String(cbuf).substring(0, read));
76 return stdOut.toString();
77 } catch (IOException e) {
78 return generateCLMErrorXML(e);
79 }
80 }
81
82 private InputStream executeOSCommandInternal2(String cmd) {
83 try {
84 return Runtime.getRuntime().exec(cmd).getInputStream();
85 } catch (IOException e) {
86 return null;
87 }
88 }
89
90 private InputStream executeOSCommandInternal(String cmd) {
91 try {
92 ProcessBuilder processBuilder = new ProcessBuilder(cmd.split(" "));
93 Map<String, String> env = processBuilder.environment();
94 Process proc = processBuilder.start();
95 return proc.getInputStream();
96 } catch (IOException e) {
97 return null;
98 }
99 }
100
101 public static void main(String[] args) {
102 String cmd = "/Users/admin/Slicer3-3.0.2008-02-14-darwin-x86/Slicer3 --launch /Users/admin/Slicer3-3.0.2008-02-14-darwin-x86/lib/Slicer3/Plugins/dwiNoiseFilter /tmp/Slicer3admin/EAFB_vtkMRMLScalarVolumeNodeB.nrrd /tmp/Slicer3admin/EAFB_vtkMRMLScalarVolumeNodeC.nrrd --iter 1 --re 3,3,0 --rf 3,3,0 --mnvf 1 --mnve 1 --minnstd 0 --maxnstd 10000 --hrf 2 --uav";
103
104 new WrapperCLMProxyApp(cmd).executeOSCommand(cmd);
105 }
106 }
107