Compare commits

...

9 Commits

12 changed files with 520 additions and 131 deletions

View File

@@ -70,7 +70,7 @@ of Emmanuel Bigeon. -->
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>gclc-socket</artifactId>
<version>1.1.8-SNAPSHOT</version>
<version>1.1.9-SNAPSHOT</version>
<packaging>jar</packaging>
<url>http://www.bigeon.fr/emmanuel</url>
<properties>

View File

@@ -35,7 +35,7 @@
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>gclc</artifactId>
<version>1.3.7</version>
<version>1.5.0</version>
<packaging>jar</packaging>
<url>http://www.bigeon.fr/emmanuel</url>
<properties>
@@ -83,6 +83,6 @@
<scm>
<developerConnection>scm:git:gogs@git.code.bigeon.net:emmanuel/gclc.git</developerConnection>
<tag>gclc-1.3.7</tag>
<tag>gclc-1.5.0</tag>
</scm>
</project>

View File

@@ -39,6 +39,7 @@
package fr.bigeon.gclc.command;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
@@ -103,37 +104,6 @@ public abstract class ParametrizedCommand extends Command {
this(null, name, strict);
}
/** <p>
* Add a parameter to the defined parameters
*
* @param param the parameter identification
* @param stringParameter if the parameter is a parameter with an argument
* @param needed if the parameter is required
* @throws InvalidParameterException if the parameter was invalid
* @deprecated since gclc-1.3.3, use the
* {@link #addStringParameter(String, boolean)} and
* {@link #addBooleanParameter(String)} */
@Deprecated
protected void addParameter(String param, boolean stringParameter,
boolean needed) throws InvalidParameterException {
if (params.containsKey(param)) {
checkParam(param, stringParameter, needed);
return;
}
if (stringParameter) {
stringParams.put(param, Boolean.valueOf(needed));
params.put(param, Boolean.valueOf(needed));
} else {
if (needed) {
// ERROR the boolean parameters cannot be needed
throw new InvalidParameterException(
"Boolean parameter are present by their very nature. They should not be defined as needed"); //$NON-NLS-1$
}
boolParams.add(param);
params.put(param, Boolean.valueOf(false));
}
}
/** Add a boolean parameter to defined parmaters.
*
* @param flag the boolean flag
@@ -164,31 +134,6 @@ public abstract class ParametrizedCommand extends Command {
params.put(flag, Boolean.valueOf(needed));
}
/** @param param the parameter
* @param stringParameter the string parameter type
* @param needed if the parameter is needed
* @throws InvalidParameterException if the new definition is invalid
* @deprecated since 1.3.3 */
@Deprecated
private void checkParam(String param, boolean stringParameter,
boolean needed) throws InvalidParameterException {
if (stringParameter) {
if (stringParams.containsKey(param)) {
Boolean need = Boolean.valueOf(
needed || stringParams.get(param).booleanValue());
stringParams.put(param, need);
params.put(param, need);
return;
}
throw new InvalidParameterException(
"Parameter is already defined as boolean"); //$NON-NLS-1$
}
if (stringParams.containsKey(param) || needed) {
throw new InvalidParameterException(
"Parameter is already defined as string"); //$NON-NLS-1$
}
}
/** @param param the string parameter
* @param needed if the parameter is needed
* @throws InvalidParameterException if the new definition is invalid */
@@ -247,10 +192,13 @@ public abstract class ParametrizedCommand extends Command {
for (final String string : toProvide) {
String value;
try {
value = manager.prompt("value of " + string + "? ");
value = manager
.prompt(MessageFormat.format("value of {0}? ", string)); //$NON-NLS-1$
while (value.isEmpty()) {
value = manager.prompt(
"value of " + string + "? (cannot be empty) ");
MessageFormat.format(
"value of {0}? (cannot be empty) ", //$NON-NLS-1$
string));
}
} catch (IOException e) {
throw new CommandRunException(

View File

@@ -210,6 +210,9 @@ public class ReadingRunnable implements Runnable {
}
}
/** @param timeout the read time out
* @return The next message that was in the input
* @throws IOException if the input was closed */
public String getNextMessage(long timeout) throws IOException {
synchronized (lock) {
if (!running) {

View File

@@ -0,0 +1,47 @@
/*
* Copyright Bigeon Emmanuel (2014)
*
* emmanuel@bigeon.fr
*
* This software is a computer program whose purpose is to
* provide a generic framework for console applications.
*
* 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.proc;
/** A listener for interruption
*
* @author Emmanuel Bigeon */
public interface InterruptionListener {
/** Notification of an interuption of a listened object */
void interrupted();
}

View File

@@ -0,0 +1,75 @@
/*
* Copyright Bigeon Emmanuel (2014)
*
* emmanuel@bigeon.fr
*
* This software is a computer program whose purpose is to
* provide a generic framework for console applications.
*
* 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.proc;
import fr.bigeon.gclc.command.Command;
import fr.bigeon.gclc.exception.CommandRunException;
/** A command that will flag a task to stop
*
* @author Emmanuel Bigeon */
public class ProcessKill extends Command {
/** The taskpool */
private final TaskPool pool;
/** @param name the command name
* @param pool the pool */
public ProcessKill(String name, TaskPool pool) {
super(name);
this.pool = pool;
}
/* (non-Javadoc)
* @see fr.bigeon.gclc.command.ICommand#execute(java.lang.String[])
*/
@Override
public void execute(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)";
}
}

View File

@@ -0,0 +1,101 @@
/*
* Copyright Bigeon Emmanuel (2014)
*
* emmanuel@bigeon.fr
*
* This software is a computer program whose purpose is to
* provide a generic framework for console applications.
*
* 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.proc;
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.ConsoleManager;
/** A command to list current processes
*
* @author Emmanuel Bigeon */
public class ProcessList extends Command {
/** The process pool */
private final TaskPool pool;
/** the interaction object */
private final ConsoleManager manager;
/** @param name the command name
* @param pool the pool
* @param manager the console manager */
public ProcessList(String name, TaskPool pool,
ConsoleManager manager) {
super(name);
this.pool = pool;
this.manager = manager;
}
/* (non-Javadoc)
* @see fr.bigeon.gclc.command.ICommand#execute(java.lang.String[])
*/
@Override
public void execute(String... args) throws CommandRunException {
ArrayList<String> pids = new ArrayList<>(pool.getPIDs());
Collections.sort(pids);
for (String string : pids) {
try {
manager.println(
MessageFormat.format("{0}\t{1}", string, //$NON-NLS-1$
pool.get(string).getName()));
} catch (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";
}
}

View File

@@ -0,0 +1,77 @@
/*
* Copyright Bigeon Emmanuel (2014)
*
* emmanuel@bigeon.fr
*
* This software is a computer program whose purpose is to
* provide a generic framework for console applications.
*
* 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.proc;
/** 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 {
/** @return the task name */
public String getName();
/** @return if the command is supposed to be running */
boolean isRunning();
/** 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);
/** Add a listener for this command end of execution
*
* @param listener the listener */
void addInterruptionListener(InterruptionListener listener);
/** Remove a listener of this command end of execution
*
* @param listener the listener */
void rmInterruptionListener(InterruptionListener listener);
}

View File

@@ -0,0 +1,104 @@
/*
* Copyright Bigeon Emmanuel (2014)
*
* emmanuel@bigeon.fr
*
* This software is a computer program whose purpose is to
* provide a generic framework for console applications.
*
* 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.proc;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
/** A process pool
*
* @author Emmanuel Bigeon */
public 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();
/** Add a process in the pool
*
* @param cmd the process */
public void add(final Task cmd) {
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 the process id */
private String getPID() {
synchronized (lock) {
String pid;
do {
pid = Integer.toString(count++);
} while (running.containsKey(pid));
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(String pid) {
synchronized (lock) {
return running.get(pid);
}
}
/** @return the pids */
public Collection<String> getPIDs() {
return new HashSet<>(running.keySet());
}
}

View File

@@ -0,0 +1,72 @@
/*
* Copyright Bigeon Emmanuel (2014)
*
* emmanuel@bigeon.fr
*
* This software is a computer program whose purpose is to
* provide a generic framework for console applications.
*
* 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.proc;
import fr.bigeon.gclc.command.Command;
import fr.bigeon.gclc.exception.CommandRunException;
/** 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(String name, TaskPool pool) {
super(name);
this.pool = pool;
}
/* (non-Javadoc)
* @see fr.bigeon.gclc.command.ICommand#execute(java.lang.String[])
*/
@Override
public final void execute(String... args) throws CommandRunException {
Task task = createTask(args);
Thread th = new Thread(task);
th.start();
pool.add(task);
}
/** @param args the arguments
* @return the process to start and add to the pool */
protected abstract Task createTask(String... args);
}

View File

@@ -196,32 +196,25 @@ public class ParametrizedCommandTest {
};
// XXX Boolean flag should not be specified mandatory! They are by
// nature qualified
try {
cmd.addParameter("boolFlag", false, true);
fail("Boolean parameters should never be needed specified");
} catch (InvalidParameterException e) {
// OK
assertNotNull(e);
}
String str = "str";
try {
assertTrue(cmd.getBooleanParameters().isEmpty());
assertTrue(cmd.getStringParameters().isEmpty());
cmd.addParameter("boolFlag", false, false);
cmd.addBooleanParameter("boolFlag");
assertEquals(1, cmd.getBooleanParameters().size());
assertTrue(cmd.getStringParameters().isEmpty());
cmd.addParameter(str, true, false);
cmd.addStringParameter(str, false);
assertEquals(1, cmd.getBooleanParameters().size());
assertEquals(1, cmd.getStringParameters().size());
assertFalse(cmd.isNeeded(str));
cmd.addParameter("boolFlag", false, false);
cmd.addBooleanParameter("boolFlag");
assertEquals(1, cmd.getBooleanParameters().size());
assertEquals(1, cmd.getStringParameters().size());
cmd.addParameter(str, true, true);
cmd.addStringParameter(str, true);
assertEquals(1, cmd.getBooleanParameters().size());
assertEquals(1, cmd.getStringParameters().size());
assertTrue(cmd.isNeeded(str));
cmd.addParameter(str, true, false);
cmd.addStringParameter(str, false);
assertEquals(1, cmd.getBooleanParameters().size());
assertEquals(1, cmd.getStringParameters().size());
assertTrue(cmd.isNeeded(str));
@@ -229,34 +222,6 @@ public class ParametrizedCommandTest {
fail("Unexpected error in addition of legitimate parameter");
assertNotNull(e);
}
try {
cmd.addParameter(str, false, false);
fail("parameter type conversion shall fail");
} catch (InvalidParameterException e) {
// OK
assertNotNull(e);
}
try {
cmd.addParameter("boolFlag", true, false);
fail("parameter type conversion shall fail");
} catch (InvalidParameterException e) {
// OK
assertNotNull(e);
}
try {
cmd.addParameter("boolFlag", false, true);
fail("parameter type conversion shall fail");
} catch (InvalidParameterException e) {
// OK
assertNotNull(e);
}
try {
cmd.addParameter("boolFlag", true, true);
fail("parameter type conversion shall fail");
} catch (InvalidParameterException e) {
// OK
assertNotNull(e);
}
}
/** Test method for
@@ -306,10 +271,10 @@ public class ParametrizedCommandTest {
{
try {
addParameter(str1, true, false);
addParameter(str2, true, false);
addParameter(bool1, false, false);
addParameter(bool2, false, false);
addStringParameter(str1, false);
addStringParameter(str2, false);
addBooleanParameter(bool1);
addBooleanParameter(bool2);
} catch (InvalidParameterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
@@ -371,10 +336,10 @@ public class ParametrizedCommandTest {
{
try {
addParameter(str1, true, false);
addParameter(str2, true, false);
addParameter(bool1, false, false);
addParameter(bool2, false, false);
addStringParameter(str1, false);
addStringParameter(str2, false);
addBooleanParameter(bool1);
addBooleanParameter(bool2);
} catch (InvalidParameterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
@@ -443,10 +408,10 @@ public class ParametrizedCommandTest {
{
try {
addParameter(str1, true, true);
addParameter(str2, true, false);
addParameter(bool1, false, false);
addParameter(bool2, false, false);
addStringParameter(str1, true);
addStringParameter(str2, false);
addBooleanParameter(bool1);
addBooleanParameter(bool2);
} catch (InvalidParameterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
@@ -484,10 +449,10 @@ public class ParametrizedCommandTest {
{
try {
addParameter(str1, true, true);
addParameter(str2, true, false);
addParameter(bool1, false, false);
addParameter(bool2, false, false);
addStringParameter(str1, true);
addStringParameter(str2, false);
addBooleanParameter(bool1);
addBooleanParameter(bool2);
} catch (InvalidParameterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
@@ -541,10 +506,10 @@ public class ParametrizedCommandTest {
cmd = new ParametrizedCommand(test, "name", false) {
{
try {
addParameter(str1, true, true);
addParameter(str2, true, false);
addParameter(bool1, false, false);
addParameter(bool2, false, false);
addStringParameter(str1, true);
addStringParameter(str2, false);
addBooleanParameter(bool1);
addBooleanParameter(bool2);
} catch (InvalidParameterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
@@ -631,10 +596,10 @@ public class ParametrizedCommandTest {
cmd = new ParametrizedCommand(test, "name") {
{
try {
addParameter(str1, true, true);
addParameter(str2, true, false);
addParameter(bool1, false, false);
addParameter(bool2, false, false);
addStringParameter(str1, true);
addStringParameter(str2, false);
addBooleanParameter(bool1);
addBooleanParameter(bool2);
} catch (InvalidParameterException e) {
// TODO Auto-generated catch block
e.printStackTrace();

View File

@@ -53,13 +53,10 @@ import fr.bigeon.gclc.exception.CommandRunException;
import fr.bigeon.gclc.exception.CommandRunExceptionType;
import fr.bigeon.gclc.manager.PipedConsoleManager;
/**
* <p>
* TODO
/** <p>
* Test class for {@link ScriptExecution}
*
* @author Emmanuel Bigeon
*
*/
* @author Emmanuel Bigeon */
@SuppressWarnings("static-method")
public class ScriptExecutionTest {