1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.common.util.service;
17
18 import static org.kuali.common.util.log.Loggers.newLogger;
19
20 import java.util.List;
21
22 import org.codehaus.plexus.util.cli.CommandLineException;
23 import org.codehaus.plexus.util.cli.CommandLineUtils;
24 import org.codehaus.plexus.util.cli.Commandline;
25 import org.codehaus.plexus.util.cli.StreamConsumer;
26 import org.kuali.common.util.CollectionUtils;
27 import org.kuali.common.util.log.LoggerLevel;
28 import org.kuali.common.util.stream.LoggingStreamConsumer;
29 import org.slf4j.Logger;
30
31 public class DefaultExecService implements ExecService {
32
33 private static final Logger logger = newLogger();
34
35 @Override
36 public int execute(ExecContext context) {
37 Commandline cl = getCommandLine(context);
38 return execute(context, cl);
39 }
40
41 @Override
42 public int execute(String executable, List<String> args) {
43 DefaultExecContext context = new DefaultExecContext();
44 context.setExecutable(executable);
45 context.setArgs(args);
46 return execute(context);
47 }
48
49 protected int executeAndValidate(String executable, List<String> args) {
50 int exitValue = execute(executable, args);
51 validateExitValue(exitValue);
52 return exitValue;
53 }
54
55 protected int executeAndValidate(ExecContext context) {
56 int exitValue = execute(context);
57 validateExitValue(exitValue);
58 return exitValue;
59 }
60
61 protected int execute(ExecContext context, Commandline cl) {
62 try {
63 logger.debug("[{}]", cl);
64 StreamConsumer stdout = getConsumer(context.getStandardOutConsumer(), logger, LoggerLevel.INFO);
65 StreamConsumer stderr = getConsumer(context.getStandardErrConsumer(), logger, LoggerLevel.WARN);
66 return CommandLineUtils.executeCommandLine(cl, context.getInput(), stdout, stderr, context.getTimeoutInSeconds());
67 } catch (CommandLineException e) {
68 throw new IllegalStateException(e);
69 }
70 }
71
72 protected StreamConsumer getConsumer(StreamConsumer provided, Logger logger, LoggerLevel level) {
73 if (provided != null) {
74 return provided;
75 } else {
76 return new LoggingStreamConsumer(logger, level);
77 }
78 }
79
80
81
82
83 @Deprecated
84 protected StreamConsumer getStreamConsumer(StreamConsumer provided, Logger logger, org.kuali.common.util.LoggerLevel level) {
85 if (provided != null) {
86 return provided;
87 } else {
88 return new org.kuali.common.util.LoggingStreamConsumer(logger, level);
89 }
90 }
91
92 protected void validateExitValue(int exitValue) {
93 if (exitValue != 0) {
94 throw new IllegalStateException("Non-zero exit value - " + exitValue);
95 }
96 }
97
98 protected Commandline getCommandLine(ExecContext context) {
99 Commandline cl = new Commandline();
100 cl.setExecutable(context.getExecutable());
101 if (context.isAddSystemEnvironment()) {
102 try {
103 cl.addSystemEnvironment();
104 } catch (Exception e) {
105 throw new IllegalStateException(e);
106 }
107 }
108 if (context.getArgs() != null) {
109 cl.addArguments(CollectionUtils.toStringArray(context.getArgs()));
110 }
111 if (context.getWorkingDirectory() != null) {
112 cl.setWorkingDirectory(context.getWorkingDirectory());
113 }
114 return cl;
115 }
116
117 }