1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.exec;
18
19 import java.nio.file.Path;
20 import java.util.concurrent.ThreadFactory;
21
22 /**
23 * Runs daemon processes asynchronously. Callers are expected to register a {@link ProcessDestroyer} before executing any processes.
24 *
25 * @since 1.3
26 */
27 public class DaemonExecutor extends DefaultExecutor {
28
29 /**
30 * Constructs a new builder.
31 *
32 * @since 1.4.0
33 */
34 public static class Builder extends DefaultExecutor.Builder<Builder> {
35
36 /**
37 * Constructs a new instance.
38 */
39 public Builder() {
40 // empty
41 }
42
43 /**
44 * Creates a new configured DaemonExecutor.
45 *
46 * @return a new configured DaemonExecutor.
47 */
48 @Override
49 public DefaultExecutor get() {
50 return new DaemonExecutor(getThreadFactory(), getExecuteStreamHandler(), getWorkingDirectoryPath());
51 }
52
53 }
54
55 /**
56 * Creates a new builder.
57 *
58 * @return a new builder.
59 * @since 1.4.0
60 */
61 public static Builder builder() {
62 return new Builder();
63 }
64
65 /**
66 * Constructs a new instance.
67 *
68 * @deprecated Use {@link Builder#get()}.
69 */
70 @Deprecated
71 public DaemonExecutor() {
72 // super
73 }
74
75 private DaemonExecutor(final ThreadFactory threadFactory, final ExecuteStreamHandler executeStreamHandler, final Path workingDirectory) {
76 super(threadFactory, executeStreamHandler, workingDirectory);
77 }
78
79 /**
80 * Factory method to create a thread waiting for the result of an asynchronous execution.
81 *
82 * @param runnable the runnable passed to the thread.
83 * @param name the name of the thread.
84 * @return the thread.
85 */
86 @Override
87 protected Thread createThread(final Runnable runnable, final String name) {
88 final Thread thread = super.createThread(runnable, name);
89 thread.setDaemon(true);
90 return thread;
91 }
92 }