Share a project for process management in console
Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
This commit is contained in:
parent
9b071a378f
commit
fce2b01914
4
gclc-process/.gitignore
vendored
Normal file
4
gclc-process/.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
/target/
|
||||
/.classpath
|
||||
/.project
|
||||
/.settings/
|
60
gclc-process/pom.xml
Normal file
60
gclc-process/pom.xml
Normal file
@ -0,0 +1,60 @@
|
||||
|
||||
<!-- 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. -->
|
||||
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>fr.bigeon.gclc</groupId>
|
||||
<artifactId>process</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>process</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<parent>
|
||||
<groupId>fr.bigeon</groupId>
|
||||
<artifactId>ebigeon-config</artifactId>
|
||||
<version>1.8.0</version>
|
||||
</parent>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>fr.bigeon</groupId>
|
||||
<artifactId>gclc</artifactId>
|
||||
<version>2.0.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
@ -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.
|
||||
* <p>
|
||||
* Several things are to be considered before adding this command to an
|
||||
* application:
|
||||
* <ul>
|
||||
* <li>The commands will be able to run in parallel.
|
||||
* <li>The managing of the commands will be handled through piped systems and
|
||||
* buffered in so switching to a forled command will actually print again all
|
||||
* the command history to the output console.
|
||||
* </ul>
|
||||
*
|
||||
* @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<String> 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");
|
||||
}
|
||||
|
||||
}
|
127
gclc-process/src/main/java/fr/bigeon/gclc/process/ForkTask.java
Normal file
127
gclc-process/src/main/java/fr/bigeon/gclc/process/ForkTask.java
Normal file
@ -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;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* TODO
|
||||
*
|
||||
* @author Emmanuel Bigeon
|
||||
*
|
||||
*/
|
||||
public class ForkTask implements Task {
|
||||
|
||||
private final ICommand command;
|
||||
private final String[] args;
|
||||
private final Set<InterruptionListener> 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;
|
||||
}
|
||||
|
||||
}
|
@ -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();
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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<String> 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;
|
||||
}
|
||||
|
||||
}
|
78
gclc-process/src/main/java/fr/bigeon/gclc/process/Task.java
Normal file
78
gclc-process/src/main/java/fr/bigeon/gclc/process/Task.java
Normal file
@ -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.
|
||||
* <p>
|
||||
* 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.
|
||||
* <p>
|
||||
* 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.
|
||||
* <p>
|
||||
* 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);
|
||||
}
|
115
gclc-process/src/main/java/fr/bigeon/gclc/process/TaskPool.java
Normal file
115
gclc-process/src/main/java/fr/bigeon/gclc/process/TaskPool.java
Normal file
@ -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<String, Task> 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<String> getPIDs() {
|
||||
return new HashSet<>(running.keySet());
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user