View Javadoc
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    *     http://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.io.File;
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           * Creates a new configured DaemonExecutor.
38           *
39           * @return a new configured DaemonExecutor.
40           */
41          @Override
42          public DefaultExecutor get() {
43              return new DaemonExecutor(getThreadFactory(), getExecuteStreamHandler(), getWorkingDirectory());
44          }
45  
46      }
47  
48      /**
49       * Creates a new builder.
50       *
51       * @return a new builder.
52       * @since 1.4.0
53       */
54      public static Builder builder() {
55          return new Builder();
56      }
57  
58      /**
59       * Constructs a new instance.
60       *
61       * @deprecated Use {@link Builder#get()}.
62       */
63      @Deprecated
64      public DaemonExecutor() {
65          // super
66      }
67  
68      private DaemonExecutor(final ThreadFactory threadFactory, final ExecuteStreamHandler executeStreamHandler, final File workingDirectory) {
69          super(threadFactory, executeStreamHandler, workingDirectory);
70      }
71  
72      /**
73       * Factory method to create a thread waiting for the result of an asynchronous execution.
74       *
75       * @param runnable the runnable passed to the thread.
76       * @param name     the name of the thread.
77       * @return the thread.
78       */
79      @Override
80      protected Thread createThread(final Runnable runnable, final String name) {
81          final Thread thread = super.createThread(runnable, name);
82          thread.setDaemon(true);
83          return thread;
84      }
85  }