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 *     http://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.io.File;
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         * Creates a new configured DaemonExecutor.
038         *
039         * @return a new configured DaemonExecutor.
040         */
041        @Override
042        public DefaultExecutor get() {
043            return new DaemonExecutor(getThreadFactory(), getExecuteStreamHandler(), getWorkingDirectory());
044        }
045
046    }
047
048    /**
049     * Creates a new builder.
050     *
051     * @return a new builder.
052     * @since 1.4.0
053     */
054    public static Builder builder() {
055        return new Builder();
056    }
057
058    /**
059     * Constructs a new instance.
060     *
061     * @deprecated Use {@link Builder#get()}.
062     */
063    @Deprecated
064    public DaemonExecutor() {
065        // super
066    }
067
068    private DaemonExecutor(final ThreadFactory threadFactory, final ExecuteStreamHandler executeStreamHandler, final File workingDirectory) {
069        super(threadFactory, executeStreamHandler, workingDirectory);
070    }
071
072    /**
073     * Factory method to create a thread waiting for the result of an asynchronous execution.
074     *
075     * @param runnable the runnable passed to the thread.
076     * @param name     the name of the thread.
077     * @return the thread.
078     */
079    @Override
080    protected Thread createThread(final Runnable runnable, final String name) {
081        final Thread thread = super.createThread(runnable, name);
082        thread.setDaemon(true);
083        return thread;
084    }
085}