001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   https://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019
020package org.apache.commons.exec;
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(this);
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 Builder builder) {
076        super(builder);
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}