001/*
002 * JGrapes Event driven Framework
003 * Copyright (C) 2023 Michael N. Lipp
004 * 
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
017 */
018
019package org.jgrapes.io.events;
020
021import java.io.File;
022import java.util.HashMap;
023import java.util.List;
024import java.util.Map;
025import org.jgrapes.core.Event;
026import org.jgrapes.io.process.ProcessManager;
027
028/**
029 * Starts a new process managed by the {@link ProcessManager}
030 * component that handles the event.
031 */
032public class StartProcess extends Event<Void> {
033
034    private final String[] command;
035    private File directory;
036    private Map<String, String> environment;
037
038    /**
039     * Signals that a new process should be started.
040     *
041     * @param command the command
042     */
043    @SuppressWarnings("PMD.ArrayIsStoredDirectly")
044    public StartProcess(String... command) {
045        this.command = command;
046    }
047
048    /**
049     * Signals that a new process should be started.
050     *
051     * @param command the command
052     */
053    public StartProcess(List<String> command) {
054        this(command.toArray(new String[0]));
055    }
056
057    /**
058     * Returns the command.
059     * 
060     * @return the command
061     */
062    @SuppressWarnings("PMD.MethodReturnsInternalArray")
063    public String[] command() {
064        return command;
065    }
066
067    /**
068     * Sets the working directory for the process.
069     *
070     * @param directory the directory
071     * @return the event for method chaining
072     */
073    public StartProcess directory(File directory) {
074        this.directory = directory;
075        return this;
076    }
077
078    /**
079     * Returns the directory.
080     * 
081     * @return the directory
082     */
083    public File directory() {
084        return directory;
085    }
086
087    /**
088     * Sets the environment for the process to be created.
089     * The values given will be merged with the defaults
090     * used by the {@link ProcessBuilder}. Overrides any
091     * values from a previous invocation or from invoking
092     * {@link #environment(String, String)}.
093     *
094     * @param environment the environment
095     * @return the event for method chaining
096     */
097    public StartProcess environment(Map<String, String> environment) {
098        this.environment = environment;
099        return this;
100    }
101
102    /**
103     * Returns the environment.
104     * 
105     * @return the environment
106     */
107    public Map<String, String> environment() {
108        return environment;
109    }
110
111    /**
112     * Sets a single value in the environment of the process
113     * to be created, see {@link StartProcess#environment(Map)}.
114     *
115     * @param key the key
116     * @param value the value
117     * @return the event for method chaining
118     */
119    public StartProcess environment(String key, String value) {
120        if (environment == null) {
121            environment = new HashMap<>();
122        }
123        environment.put(key, value);
124        return this;
125    }
126}