Package: ProcessExecutor
ProcessExecutor
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
ProcessExecutor() |
|
|
|
|
|
||||||||||||||||||||
ProcessExecutor(String, List, Callback) |
|
|
|
|
|
||||||||||||||||||||
execute() |
|
|
|
|
|
||||||||||||||||||||
forExecutable(String) |
|
|
|
|
|
||||||||||||||||||||
lambda$execute$0(Process) |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
||||||||||||||||||||
withArguments(List) |
|
|
|
|
|
||||||||||||||||||||
withArguments2(String[]) |
|
|
|
|
|
||||||||||||||||||||
withPostMortemTask(Callback) |
|
|
|
|
|
Coverage
1: /*
2: * #%L
3: * *********************************************************************************************************************
4: *
5: * NorthernWind - lightweight CMS
6: * http://northernwind.tidalwave.it - git clone git@bitbucket.org:tidalwave/northernwind-rca-src.git
7: * %%
8: * Copyright (C) 2013 - 2021 Tidalwave s.a.s. (http://tidalwave.it)
9: * %%
10: * *********************************************************************************************************************
11: *
12: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
13: * the License. You may obtain a copy of the License at
14: *
15: * http://www.apache.org/licenses/LICENSE-2.0
16: *
17: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
18: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
19: * specific language governing permissions and limitations under the License.
20: *
21: * *********************************************************************************************************************
22: *
23: *
24: * *********************************************************************************************************************
25: * #L%
26: */
27: package it.tidalwave.northernwind.rca.ui.contenteditor.impl;
28:
29: import javax.annotation.Nonnull;
30: import java.util.ArrayList;
31: import java.util.Arrays;
32: import java.util.List;
33: import java.util.concurrent.Executors;
34: import java.io.IOException;
35: import java.io.File;
36: import it.tidalwave.util.Callback;
37: import lombok.AllArgsConstructor;
38: import lombok.With;
39: import lombok.extern.slf4j.Slf4j;
40:
41: /***********************************************************************************************************************
42: *
43: * @author Fabrizio Giudici
44: *
45: **********************************************************************************************************************/
46:•@AllArgsConstructor @Slf4j
47: public class ProcessExecutor
48: {
49: @Nonnull
50: private final String executable;
51:
52:• @With
53: private final List<String> arguments;
54:
55:• @With
56: private final Callback postMortemTask;
57:
58: public ProcessExecutor()
59: {
60: this("", new ArrayList<>(), Callback.EMPTY);
61: }
62:
63: @Nonnull
64: public ProcessExecutor withArguments2 (@Nonnull final String ... arguments)
65: {
66: return withArguments(Arrays.asList(arguments));
67: }
68:
69: @Nonnull
70: public static ProcessExecutor forExecutable (@Nonnull final String executable)
71: {
72: return new ProcessExecutor(executable, new ArrayList<>(), Callback.EMPTY);
73: }
74:
75: public void execute()
76: {
77: final List<String> args = new ArrayList<>();
78: args.add("open");
79: args.add("-W");
80: args.add("-a");
81:
82: final String[] paths =
83: {
84: "/Applications/", System.getProperty("user.home") + "/Applications/"
85: };
86:
87:• for (final String path : paths)
88: {
89: final File file = new File(new File(path), executable);
90:
91:• if (file.exists())
92: {
93: args.add(file.getAbsolutePath());
94: break;
95: }
96: }
97:
98: args.addAll(arguments);
99:
100: try
101: {
102: log.info("Executing {}", args);
103: final Process process = Runtime.getRuntime().exec(args.toArray(new String[0]));
104: // FIXME; inject the executor
105: Executors.newSingleThreadExecutor().submit(() ->
106: {
107: try
108: {
109: log.debug(">>>> waiting for the process to terminate...");
110: process.waitFor();
111: log.debug(">>>> process terminated");
112: postMortemTask.call();
113: }
114: catch (Throwable e)
115: {
116: log.error("", e);
117: }
118: });
119: }
120: catch (IOException e)
121: {
122: log.error("", e); // shouldn't happen
123: }
124: }
125: }