001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     https://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.exec;
018
019import java.nio.file.Path;
020import java.util.concurrent.ThreadFactory;
021
022/**
023 * Runs daemon processes asynchronously. Callers are expected to register a {@link ProcessDestroyer} before executing any processes.
024 *
025 * @since 1.3
026 */
027public class DaemonExecutor extends DefaultExecutor {
028
029    /**
030     * Constructs a new builder.
031     *
032     * @since 1.4.0
033     */
034    public static class Builder extends DefaultExecutor.Builder<Builder> {
035
036        /**
037         * Constructs a new instance.
038         */
039        public Builder() {
040            // empty
041        }
042
043        /**
044         * Creates a new configured DaemonExecutor.
045         *
046         * @return a new configured DaemonExecutor.
047         */
048        @Override
049        public DefaultExecutor get() {
050            return new DaemonExecutor(getThreadFactory(), getExecuteStreamHandler(), getWorkingDirectoryPath());
051        }
052
053    }
054
055    /**
056     * Creates a new builder.
057     *
058     * @return a new builder.
059     * @since 1.4.0
060     */
061    public static Builder builder() {
062        return new Builder();
063    }
064
065    /**
066     * Constructs a new instance.
067     *
068     * @deprecated Use {@link Builder#get()}.
069     */
070    @Deprecated
071    public DaemonExecutor() {
072        // super
073    }
074
075    private DaemonExecutor(final ThreadFactory threadFactory, final ExecuteStreamHandler executeStreamHandler, final Path workingDirectory) {
076        super(threadFactory, executeStreamHandler, workingDirectory);
077    }
078
079    /**
080     * Factory method to create a thread waiting for the result of an asynchronous execution.
081     *
082     * @param runnable the runnable passed to the thread.
083     * @param name     the name of the thread.
084     * @return the thread.
085     */
086    @Override
087    protected Thread createThread(final Runnable runnable, final String name) {
088        final Thread thread = super.createThread(runnable, name);
089        thread.setDaemon(true);
090        return thread;
091    }
092}