diff --git a/gclc-process/.gitignore b/gclc-process/.gitignore new file mode 100644 index 0000000..d98981c --- /dev/null +++ b/gclc-process/.gitignore @@ -0,0 +1,4 @@ +/target/ +/.classpath +/.project +/.settings/ diff --git a/gclc-process/pom.xml b/gclc-process/pom.xml new file mode 100644 index 0000000..94a5122 --- /dev/null +++ b/gclc-process/pom.xml @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 4.0.0 + + fr.bigeon.gclc + process + 0.0.1-SNAPSHOT + jar + + process + http://maven.apache.org + + + UTF-8 + + + + fr.bigeon + ebigeon-config + 1.8.0 + + + + fr.bigeon + gclc + 2.0.0-SNAPSHOT + + + diff --git a/gclc-process/src/main/java/fr/bigeon/gclc/process/CommandFork.java b/gclc-process/src/main/java/fr/bigeon/gclc/process/CommandFork.java new file mode 100644 index 0000000..6c051ad --- /dev/null +++ b/gclc-process/src/main/java/fr/bigeon/gclc/process/CommandFork.java @@ -0,0 +1,148 @@ +/* + * process, Distribution repositories and basic setup for Emmanuel Bigeon projects + * Copyright (C) 2014-2017 E. Bigeon + * mailto:emmanuel@bigeon.fr + * + * This software is governed by the CeCILL license under French law and + * abiding by the rules of distribution of free software. You can use, + * modify and/or redistribute the software under the terms of the CeCILL + * license as circulated by CEA, CNRS and INRIA at the following URL + * "http://www.cecill.info". + * + * As a counterpart to the access to the source code and rights to copy, + * modify and redistribute granted by the license, users are provided only + * with a limited warranty and the software's author, the holder of the + * economic rights, and the successive licensors have only limited + * liability. + * + * In this respect, the user's attention is drawn to the risks associated + * with loading, using, modifying and/or developing or reproducing the + * software by the user in light of its specific status of free software, + * that may mean that it is complicated to manipulate, and that also + * therefore means that it is reserved for developers and experienced + * professionals having in-depth computer knowledge. Users are therefore + * encouraged to load and test the software's suitability as regards their + * requirements in conditions enabling the security of their systems and/or + * data to be ensured and, more generally, to use and operate it in the + * same conditions as regards security. + * + * The fact that you are presently reading this means that you have had + * knowledge of the CeCILL license and that you accept its terms. + */ +/** + * gclc-process:fr.bigeon.gclc.process.CommandFork.java + * Created on: Nov 13, 2017 + */ +package fr.bigeon.gclc.process; + +import java.io.IOException; +import java.util.Arrays; +import java.util.List; + +import fr.bigeon.gclc.command.CommandParameters; +import fr.bigeon.gclc.command.ICommand; +import fr.bigeon.gclc.command.ICommandProvider; +import fr.bigeon.gclc.command.ParametrizedCommand; +import fr.bigeon.gclc.exception.CommandRunException; +import fr.bigeon.gclc.exception.CommandRunExceptionType; +import fr.bigeon.gclc.exception.InvalidParameterException; +import fr.bigeon.gclc.manager.ConsoleInput; +import fr.bigeon.gclc.manager.ConsoleOutput; + +/** A command that is launched inside an internal terminal. + *

+ * Several things are to be considered before adding this command to an + * application: + *

+ * + * @author Emmanuel Bigeon */ +public class CommandFork extends ParametrizedCommand { + + private final TaskPool pool = new TaskPool(); + private ICommandProvider provider; + + /** Add the fork command. + * + * @param name the command name */ + public CommandFork(final String name) { + super(name); + addParameters(); + } + + /** + * + */ + private void addParameters() { + try { + addStringParameter("join", false); + addBooleanParameter("list"); + } catch (final InvalidParameterException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + /* (non-Javadoc) + * @see fr.bigeon.gclc.command.ParametrizedCommand#doExecute(fr.bigeon.gclc. + * manager.ConsoleOutput, fr.bigeon.gclc.manager.ConsoleInput, + * fr.bigeon.gclc.command.CommandParameters) */ + @Override + protected void doExecute(final ConsoleOutput out, final ConsoleInput in, + final CommandParameters parameters) throws CommandRunException { + if (parameters.getBool("list")) { + for (final String id : pool.getPIDs()) { + try { + out.println(id + "\t" + pool.get(id).getName()); + } catch (final IOException e) { + throw new CommandRunException( + CommandRunExceptionType.INTERACTION, + "Failed to write to console", e, this); + } + } + } + + final String string = parameters.get("join"); + if (string != null) { + // Join the command. + final ForkTask cmd = (ForkTask) pool.get(string); + if (cmd == null) { + throw new CommandRunException("No such fork process", this); + } + cmd.join(out, in); + return; + } + + final List strings = parameters.getAdditionals(); + final ICommand cmd = provider.get(strings.get(0)); + final String[] args = Arrays.copyOfRange(strings.toArray(new String[0]), + 1, strings.size()); + final ForkTask task = new ForkTask(cmd, args); + final Thread th = new Thread(task); + pool.add(task); + th.start(); + } + + /* (non-Javadoc) + * @see fr.bigeon.gclc.command.ICommand#tip() */ + @Override + public String tip() { + // TODO Auto-generated method stub + // return null; + throw new RuntimeException("Not implemented yet"); + } + + /* (non-Javadoc) + * @see fr.bigeon.gclc.command.Command#usageDetail() */ + @Override + protected String usageDetail() { + // TODO Auto-generated method stub + // return null; + throw new RuntimeException("Not implemented yet"); + } + +} diff --git a/gclc-process/src/main/java/fr/bigeon/gclc/process/ForkTask.java b/gclc-process/src/main/java/fr/bigeon/gclc/process/ForkTask.java new file mode 100644 index 0000000..f19c504 --- /dev/null +++ b/gclc-process/src/main/java/fr/bigeon/gclc/process/ForkTask.java @@ -0,0 +1,127 @@ +/* + * process, Distribution repositories and basic setup for Emmanuel Bigeon projects + * Copyright (C) 2014-2017 E. Bigeon + * mailto:emmanuel@bigeon.fr + * + * This software is governed by the CeCILL license under French law and + * abiding by the rules of distribution of free software. You can use, + * modify and/or redistribute the software under the terms of the CeCILL + * license as circulated by CEA, CNRS and INRIA at the following URL + * "http://www.cecill.info". + * + * As a counterpart to the access to the source code and rights to copy, + * modify and redistribute granted by the license, users are provided only + * with a limited warranty and the software's author, the holder of the + * economic rights, and the successive licensors have only limited + * liability. + * + * In this respect, the user's attention is drawn to the risks associated + * with loading, using, modifying and/or developing or reproducing the + * software by the user in light of its specific status of free software, + * that may mean that it is complicated to manipulate, and that also + * therefore means that it is reserved for developers and experienced + * professionals having in-depth computer knowledge. Users are therefore + * encouraged to load and test the software's suitability as regards their + * requirements in conditions enabling the security of their systems and/or + * data to be ensured and, more generally, to use and operate it in the + * same conditions as regards security. + * + * The fact that you are presently reading this means that you have had + * knowledge of the CeCILL license and that you accept its terms. + */ +/** + * gclc-process:fr.bigeon.gclc.process.ForkTask.java + * Created on: Nov 13, 2017 + */ +package fr.bigeon.gclc.process; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +import fr.bigeon.gclc.command.ICommand; +import fr.bigeon.gclc.manager.ConsoleInput; +import fr.bigeon.gclc.manager.ConsoleOutput; + +/** + *

+ * TODO + * + * @author Emmanuel Bigeon + * + */ +public class ForkTask implements Task { + + private final ICommand command; + private final String[] args; + private final Set listeners = new HashSet<>(); + private boolean running = false; + + /** @param cmd the command + * @param args the arguements */ + public ForkTask(final ICommand cmd, final String[] args) { + command = cmd; + this.args = Arrays.copyOf(args, args.length); + } + + /* (non-Javadoc) + * @see fr.bigeon.gclc.process.Task#addInterruptionListener(fr.bigeon.gclc. + * process.InterruptionListener) */ + @Override + public void addInterruptionListener(final InterruptionListener listener) { + listeners.add(listener); + } + + /* (non-Javadoc) + * @see fr.bigeon.gclc.process.Task#getName() */ + @Override + public String getName() { + return command.getCommandName(); + } + + /* (non-Javadoc) + * @see fr.bigeon.gclc.process.Task#isRunning() */ + @Override + public boolean isRunning() { + return running; + } + + /** @param out the console output + * @param in the console input */ + public void join(final ConsoleOutput out, final ConsoleInput in) { + // TODO Auto-generated method stub + // + throw new RuntimeException("Not implemented yet"); + } + + /* (non-Javadoc) + * @see + * fr.bigeon.gclc.process.Task#rmInterruptionListener(fr.bigeon.gclc.process + * .InterruptionListener) */ + @Override + public void rmInterruptionListener(final InterruptionListener listener) { + listeners.remove(listener); + } + + /* (non-Javadoc) + * @see java.lang.Runnable#run() */ + @Override + public void run() { + running = true; + try { + // TODO Auto-generated method stub + // + throw new RuntimeException("Not implemented yet"); + } finally { + running = false; + } + } + + /* (non-Javadoc) + * @see fr.bigeon.gclc.process.Task#setRunning(boolean) */ + @Override + public void setRunning(final boolean running) { + this.running = running; + } + +} diff --git a/gclc-process/src/main/java/fr/bigeon/gclc/process/InterruptionListener.java b/gclc-process/src/main/java/fr/bigeon/gclc/process/InterruptionListener.java new file mode 100644 index 0000000..0034f0c --- /dev/null +++ b/gclc-process/src/main/java/fr/bigeon/gclc/process/InterruptionListener.java @@ -0,0 +1,44 @@ +/* + * process, Distribution repositories and basic setup for Emmanuel Bigeon projects + * Copyright (C) 2014-2017 E. Bigeon + * mailto:emmanuel@bigeon.fr + * + * This software is governed by the CeCILL license under French law and + * abiding by the rules of distribution of free software. You can use, + * modify and/or redistribute the software under the terms of the CeCILL + * license as circulated by CEA, CNRS and INRIA at the following URL + * "http://www.cecill.info". + * + * As a counterpart to the access to the source code and rights to copy, + * modify and redistribute granted by the license, users are provided only + * with a limited warranty and the software's author, the holder of the + * economic rights, and the successive licensors have only limited + * liability. + * + * In this respect, the user's attention is drawn to the risks associated + * with loading, using, modifying and/or developing or reproducing the + * software by the user in light of its specific status of free software, + * that may mean that it is complicated to manipulate, and that also + * therefore means that it is reserved for developers and experienced + * professionals having in-depth computer knowledge. Users are therefore + * encouraged to load and test the software's suitability as regards their + * requirements in conditions enabling the security of their systems and/or + * data to be ensured and, more generally, to use and operate it in the + * same conditions as regards security. + * + * The fact that you are presently reading this means that you have had + * knowledge of the CeCILL license and that you accept its terms. + */ +/** + * gclc:fr.bigeon.gclc.proc.InterruptionListener.java + * Created on: May 10, 2017 + */ +package fr.bigeon.gclc.process; + +/** A listener for interruption + * + * @author Emmanuel Bigeon */ +public interface InterruptionListener { + /** Notification of an interuption of a listened object */ + void interrupted(); +} diff --git a/gclc-process/src/main/java/fr/bigeon/gclc/process/ProcessKill.java b/gclc-process/src/main/java/fr/bigeon/gclc/process/ProcessKill.java new file mode 100644 index 0000000..9364994 --- /dev/null +++ b/gclc-process/src/main/java/fr/bigeon/gclc/process/ProcessKill.java @@ -0,0 +1,82 @@ +/* + * process, Distribution repositories and basic setup for Emmanuel Bigeon projects + * Copyright (C) 2014-2017 E. Bigeon + * mailto:emmanuel@bigeon.fr + * + * This software is governed by the CeCILL license under French law and + * abiding by the rules of distribution of free software. You can use, + * modify and/or redistribute the software under the terms of the CeCILL + * license as circulated by CEA, CNRS and INRIA at the following URL + * "http://www.cecill.info". + * + * As a counterpart to the access to the source code and rights to copy, + * modify and redistribute granted by the license, users are provided only + * with a limited warranty and the software's author, the holder of the + * economic rights, and the successive licensors have only limited + * liability. + * + * In this respect, the user's attention is drawn to the risks associated + * with loading, using, modifying and/or developing or reproducing the + * software by the user in light of its specific status of free software, + * that may mean that it is complicated to manipulate, and that also + * therefore means that it is reserved for developers and experienced + * professionals having in-depth computer knowledge. Users are therefore + * encouraged to load and test the software's suitability as regards their + * requirements in conditions enabling the security of their systems and/or + * data to be ensured and, more generally, to use and operate it in the + * same conditions as regards security. + * + * The fact that you are presently reading this means that you have had + * knowledge of the CeCILL license and that you accept its terms. + */ +/** + * gclc:fr.bigeon.gclc.proc.ProcessList.java + * Created on: May 10, 2017 + */ +package fr.bigeon.gclc.process; + +import fr.bigeon.gclc.command.Command; +import fr.bigeon.gclc.exception.CommandRunException; +import fr.bigeon.gclc.manager.ConsoleInput; +import fr.bigeon.gclc.manager.ConsoleOutput; + +/** A command that will flag a task to stop + * + * @author Emmanuel Bigeon */ +public final class ProcessKill extends Command { + /** The taskpool */ + private final TaskPool pool; + + /** @param name the command name + * @param pool the pool */ + public ProcessKill(final String name, final TaskPool pool) { + super(name); + this.pool = pool; + } + + /* (non-Javadoc) + * @see fr.bigeon.gclc.command.ICommand#execute(fr.bigeon.gclc.manager. + * ConsoleOutput, fr.bigeon.gclc.manager.ConsoleInput, + * java.lang.String[]) */ + @Override + public void execute(final ConsoleOutput out, final ConsoleInput in, + final String... args) throws CommandRunException { + pool.get(args[0]).setRunning(false); + } + + /* (non-Javadoc) + * @see fr.bigeon.gclc.command.ICommand#tip() */ + @SuppressWarnings("nls") + @Override + public String tip() { + return "Request a process to stop (softly)"; + } + + /* (non-Javadoc) + * @see fr.bigeon.gclc.command.Command#usageDetail() */ + @Override + protected String usageDetail() { + return null; + } + +} diff --git a/gclc-process/src/main/java/fr/bigeon/gclc/process/ProcessList.java b/gclc-process/src/main/java/fr/bigeon/gclc/process/ProcessList.java new file mode 100644 index 0000000..61b5508 --- /dev/null +++ b/gclc-process/src/main/java/fr/bigeon/gclc/process/ProcessList.java @@ -0,0 +1,100 @@ +/* + * process, Distribution repositories and basic setup for Emmanuel Bigeon projects + * Copyright (C) 2014-2017 E. Bigeon + * mailto:emmanuel@bigeon.fr + * + * This software is governed by the CeCILL license under French law and + * abiding by the rules of distribution of free software. You can use, + * modify and/or redistribute the software under the terms of the CeCILL + * license as circulated by CEA, CNRS and INRIA at the following URL + * "http://www.cecill.info". + * + * As a counterpart to the access to the source code and rights to copy, + * modify and redistribute granted by the license, users are provided only + * with a limited warranty and the software's author, the holder of the + * economic rights, and the successive licensors have only limited + * liability. + * + * In this respect, the user's attention is drawn to the risks associated + * with loading, using, modifying and/or developing or reproducing the + * software by the user in light of its specific status of free software, + * that may mean that it is complicated to manipulate, and that also + * therefore means that it is reserved for developers and experienced + * professionals having in-depth computer knowledge. Users are therefore + * encouraged to load and test the software's suitability as regards their + * requirements in conditions enabling the security of their systems and/or + * data to be ensured and, more generally, to use and operate it in the + * same conditions as regards security. + * + * The fact that you are presently reading this means that you have had + * knowledge of the CeCILL license and that you accept its terms. + */ +/** + * gclc:fr.bigeon.gclc.proc.ProcessList.java + * Created on: May 10, 2017 + */ +package fr.bigeon.gclc.process; + +import java.io.IOException; +import java.text.MessageFormat; +import java.util.ArrayList; +import java.util.Collections; + +import fr.bigeon.gclc.command.Command; +import fr.bigeon.gclc.exception.CommandRunException; +import fr.bigeon.gclc.exception.CommandRunExceptionType; +import fr.bigeon.gclc.manager.ConsoleInput; +import fr.bigeon.gclc.manager.ConsoleOutput; + +/** A command to list current processes + * + * @author Emmanuel Bigeon */ +public final class ProcessList extends Command { + /** The process pool */ + private final TaskPool pool; + + /** @param name the command name + * @param pool the pool */ + public ProcessList(final String name, final TaskPool pool) { + super(name); + this.pool = pool; + } + + /* (non-Javadoc) + * @see fr.bigeon.gclc.command.ICommand#execute(fr.bigeon.gclc.manager. + * ConsoleOutput, fr.bigeon.gclc.manager.ConsoleInput, + * java.lang.String[]) */ + @Override + public void execute(final ConsoleOutput out, final ConsoleInput in, + final String... args) throws CommandRunException { + final ArrayList pids = new ArrayList<>(pool.getPIDs()); + Collections.sort(pids); + for (final String string : pids) { + try { + out.println(MessageFormat.format("{0}\t{1}", string, //$NON-NLS-1$ + pool.get(string).getName())); + } catch (final IOException e) { + throw new CommandRunException( + CommandRunExceptionType.INTERACTION, + "Unable to communicate with user", e, this); //$NON-NLS-1$ + } + } + + } + + /* (non-Javadoc) + * @see fr.bigeon.gclc.command.ICommand#tip() */ + @SuppressWarnings("nls") + @Override + public String tip() { + return "List all processes"; + } + + /* (non-Javadoc) + * @see fr.bigeon.gclc.command.Command#usageDetail() */ + @Override + protected String usageDetail() { + return null; + } + +} diff --git a/gclc-process/src/main/java/fr/bigeon/gclc/process/Task.java b/gclc-process/src/main/java/fr/bigeon/gclc/process/Task.java new file mode 100644 index 0000000..c7aa3c9 --- /dev/null +++ b/gclc-process/src/main/java/fr/bigeon/gclc/process/Task.java @@ -0,0 +1,78 @@ +/* + * process, Distribution repositories and basic setup for Emmanuel Bigeon projects + * Copyright (C) 2014-2017 E. Bigeon + * mailto:emmanuel@bigeon.fr + * + * This software is governed by the CeCILL license under French law and + * abiding by the rules of distribution of free software. You can use, + * modify and/or redistribute the software under the terms of the CeCILL + * license as circulated by CEA, CNRS and INRIA at the following URL + * "http://www.cecill.info". + * + * As a counterpart to the access to the source code and rights to copy, + * modify and redistribute granted by the license, users are provided only + * with a limited warranty and the software's author, the holder of the + * economic rights, and the successive licensors have only limited + * liability. + * + * In this respect, the user's attention is drawn to the risks associated + * with loading, using, modifying and/or developing or reproducing the + * software by the user in light of its specific status of free software, + * that may mean that it is complicated to manipulate, and that also + * therefore means that it is reserved for developers and experienced + * professionals having in-depth computer knowledge. Users are therefore + * encouraged to load and test the software's suitability as regards their + * requirements in conditions enabling the security of their systems and/or + * data to be ensured and, more generally, to use and operate it in the + * same conditions as regards security. + * + * The fact that you are presently reading this means that you have had + * knowledge of the CeCILL license and that you accept its terms. + */ +/** + * gclc:fr.bigeon.gclc.proc.ThreadCommand.java + * Created on: May 10, 2017 + */ +package fr.bigeon.gclc.process; + +/** Tasks are named runnable that can be interrupted. + *

+ * Good practice for those objects include an absence of interaction with the + * user (otherwise the user may not know from which running command comes + * information and requests) and unicity of the object to have a coherent + * control through the {@link #setRunning(boolean)} method. + *

+ * Typical cases where such command can be useful is for an application that + * does long computations. + * + * @author Emmanuel Bigeon */ +public interface Task extends Runnable { + /** Add a listener for this command end of execution + * + * @param listener the listener */ + void addInterruptionListener(InterruptionListener listener); + + /** Get the task name. + * + * @return the task name */ + String getName(); + + /** Test the running status of the task. + * + * @return if the command is supposed to be running */ + boolean isRunning(); + + /** Remove a listener of this command end of execution + * + * @param listener the listener */ + void rmInterruptionListener(InterruptionListener listener); + + /** Set the running state. + *

+ * This method should be only called by external objects with the false + * argument. Calling this method with true has unspecified behavior and + * could do nothing as well as restart the command for example. + * + * @param running the running state */ + void setRunning(boolean running); +} diff --git a/gclc-process/src/main/java/fr/bigeon/gclc/process/TaskPool.java b/gclc-process/src/main/java/fr/bigeon/gclc/process/TaskPool.java new file mode 100644 index 0000000..c4fb770 --- /dev/null +++ b/gclc-process/src/main/java/fr/bigeon/gclc/process/TaskPool.java @@ -0,0 +1,115 @@ +/* + * process, Distribution repositories and basic setup for Emmanuel Bigeon projects + * Copyright (C) 2014-2017 E. Bigeon + * mailto:emmanuel@bigeon.fr + * + * This software is governed by the CeCILL license under French law and + * abiding by the rules of distribution of free software. You can use, + * modify and/or redistribute the software under the terms of the CeCILL + * license as circulated by CEA, CNRS and INRIA at the following URL + * "http://www.cecill.info". + * + * As a counterpart to the access to the source code and rights to copy, + * modify and redistribute granted by the license, users are provided only + * with a limited warranty and the software's author, the holder of the + * economic rights, and the successive licensors have only limited + * liability. + * + * In this respect, the user's attention is drawn to the risks associated + * with loading, using, modifying and/or developing or reproducing the + * software by the user in light of its specific status of free software, + * that may mean that it is complicated to manipulate, and that also + * therefore means that it is reserved for developers and experienced + * professionals having in-depth computer knowledge. Users are therefore + * encouraged to load and test the software's suitability as regards their + * requirements in conditions enabling the security of their systems and/or + * data to be ensured and, more generally, to use and operate it in the + * same conditions as regards security. + * + * The fact that you are presently reading this means that you have had + * knowledge of the CeCILL license and that you accept its terms. + */ +/** + * gclc:fr.bigeon.gclc.proc.TaskPool.java + * Created on: May 10, 2017 + */ +package fr.bigeon.gclc.process; + +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; + +/** A process pool. + * + * @author Emmanuel Bigeon */ +public final class TaskPool { + /** The running processes. */ + private final Map running = new HashMap<>(); + /** The count for process id. */ + private int count = 0; + /** The lock for pid attribution synchronization. */ + private final Object lock = new Object(); + + /** Default constructor. */ + public TaskPool() { + // + } + + /** Add a process in the pool. + * + * @param cmd the process + * @return the pid */ + public String add(final Task cmd) { + if (cmd == null) { + throw new IllegalArgumentException("Task cannot be null"); //$NON-NLS-1$ + } + final String pid = getPID(); + synchronized (lock) { + running.put(pid, cmd); + } + cmd.addInterruptionListener(new InterruptionListener() { + + @SuppressWarnings("synthetic-access") + @Override + public void interrupted() { + synchronized (lock) { + running.remove(pid); + count = Math.min(count, Integer.parseInt(pid)); + } + cmd.rmInterruptionListener(this); + } + }); + return pid; + } + + /** Get a process by it associated identifier. + * + * @param pid the task id + * @return the task, if any, associated to this id */ + public Task get(final String pid) { + synchronized (lock) { + return running.get(pid); + } + } + + /** Get the next process id. + * + * @return the process id */ + private String getPID() { + synchronized (lock) { + String pid; + do { + pid = Integer.toString(count++); + } while (running.containsKey(pid)); + return pid; + } + } + + /** Get the running processes' identifiers. + * + * @return the pids */ + public Collection getPIDs() { + return new HashSet<>(running.keySet()); + } +} diff --git a/gclc-process/src/main/java/fr/bigeon/gclc/process/TaskSpawner.java b/gclc-process/src/main/java/fr/bigeon/gclc/process/TaskSpawner.java new file mode 100644 index 0000000..b287f72 --- /dev/null +++ b/gclc-process/src/main/java/fr/bigeon/gclc/process/TaskSpawner.java @@ -0,0 +1,76 @@ +/* + * process, Distribution repositories and basic setup for Emmanuel Bigeon projects + * Copyright (C) 2014-2017 E. Bigeon + * mailto:emmanuel@bigeon.fr + * + * This software is governed by the CeCILL license under French law and + * abiding by the rules of distribution of free software. You can use, + * modify and/or redistribute the software under the terms of the CeCILL + * license as circulated by CEA, CNRS and INRIA at the following URL + * "http://www.cecill.info". + * + * As a counterpart to the access to the source code and rights to copy, + * modify and redistribute granted by the license, users are provided only + * with a limited warranty and the software's author, the holder of the + * economic rights, and the successive licensors have only limited + * liability. + * + * In this respect, the user's attention is drawn to the risks associated + * with loading, using, modifying and/or developing or reproducing the + * software by the user in light of its specific status of free software, + * that may mean that it is complicated to manipulate, and that also + * therefore means that it is reserved for developers and experienced + * professionals having in-depth computer knowledge. Users are therefore + * encouraged to load and test the software's suitability as regards their + * requirements in conditions enabling the security of their systems and/or + * data to be ensured and, more generally, to use and operate it in the + * same conditions as regards security. + * + * The fact that you are presently reading this means that you have had + * knowledge of the CeCILL license and that you accept its terms. + */ +/** + * gclc:fr.bigeon.gclc.proc.ProcessList.java + * Created on: May 10, 2017 + */ +package fr.bigeon.gclc.process; + +import fr.bigeon.gclc.command.Command; +import fr.bigeon.gclc.exception.CommandRunException; +import fr.bigeon.gclc.manager.ConsoleInput; +import fr.bigeon.gclc.manager.ConsoleOutput; + +/** An abstract command to generate a task and return the control to the user + * + * @author Emmanuel Bigeon */ +public abstract class TaskSpawner extends Command { + /** The process pool */ + private final TaskPool pool; + + /** @param name the command name + * @param pool the pool */ + public TaskSpawner(final String name, final TaskPool pool) { + super(name); + this.pool = pool; + } + + /** @param in the input + * @param out the output + * @param args the arguments + * @return the process to start and add to the pool */ + protected abstract Task createTask(ConsoleOutput out, ConsoleInput in, + String... args); + + /* (non-Javadoc) + * @see fr.bigeon.gclc.command.ICommand#execute(fr.bigeon.gclc.manager. + * ConsoleOutput, fr.bigeon.gclc.manager.ConsoleInput, + * java.lang.String[]) */ + @Override + public void execute(final ConsoleOutput out, final ConsoleInput in, + final String... args) throws CommandRunException { + final Task task = createTask(out, in, args); + final Thread th = new Thread(task); + pool.add(task); + th.start(); + } +}