1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.exec.launcher;
19
20 import java.io.File;
21 import java.io.IOException;
22 import java.io.PrintWriter;
23 import java.nio.charset.Charset;
24 import java.nio.file.Files;
25 import java.nio.file.Path;
26 import java.util.Map;
27 import java.util.Map.Entry;
28 import java.util.Set;
29
30 import org.apache.commons.exec.CommandLine;
31 import org.apache.commons.exec.util.StringUtils;
32
33
34
35
36
37 public class VmsCommandLauncher extends Java13CommandLauncher {
38
39
40
41
42 private File createCommandFile(final CommandLine cmd, final Map<String, String> env) throws IOException {
43 final Path path = Files.createTempFile("EXEC", ".TMP");
44 final File script = path.toFile();
45 script.deleteOnExit();
46 try (PrintWriter writer = new PrintWriter(Files.newBufferedWriter(path, Charset.defaultCharset()))) {
47
48 if (env != null) {
49 final Set<Entry<String, String>> entries = env.entrySet();
50 for (final Entry<String, String> entry : entries) {
51 writer.print("$ ");
52 writer.print(entry.getKey());
53 writer.print(" == ");
54 writer.println('\"');
55 String value = entry.getValue();
56
57 if (value.indexOf('\"') > 0) {
58 final StringBuilder sb = new StringBuilder();
59 for (int i = 0; i < value.length(); i++) {
60 final char c = value.charAt(i);
61 if (c == '\"') {
62 sb.append('\"');
63 }
64 sb.append(c);
65 }
66 value = sb.toString();
67 }
68 writer.print(value);
69 writer.println('\"');
70 }
71 }
72
73 final String command = cmd.getExecutable();
74 if (cmd.isFile()) {
75 writer.print("$ @");
76
77 final String[] parts = StringUtils.split(command, "/");
78 writer.print(parts[0]);
79 writer.print(":[");
80 writer.print(parts[1]);
81 final int lastPart = parts.length - 1;
82 for (int i = 2; i < lastPart; i++) {
83 writer.print(".");
84 writer.print(parts[i]);
85 }
86 writer.print("]");
87 writer.print(parts[lastPart]);
88 } else {
89 writer.print("$ ");
90 writer.print(command);
91 }
92 final String[] args = cmd.getArguments();
93 for (final String arg : args) {
94 writer.println(" -");
95 writer.print(arg);
96 }
97 writer.println();
98 }
99 return script;
100 }
101
102
103
104
105 @Override
106 public Process exec(final CommandLine cmd, final Map<String, String> env) throws IOException {
107 return super.exec(new CommandLine(createCommandFile(cmd, env).getPath()), env);
108 }
109
110
111
112
113
114 @Override
115 public Process exec(final CommandLine cmd, final Map<String, String> env, final File workingDir) throws IOException {
116 return super.exec(new CommandLine(createCommandFile(cmd, env).getPath()), env, workingDir);
117 }
118
119
120 @Override
121 public boolean isFailure(final int exitValue) {
122
123 return exitValue % 2 == 0;
124 }
125 }