mirror of
https://github.com/DSpace/DSpace.git
synced 2025-10-07 18:14:26 +00:00
Merge branch '9005_cli_throw_excpetion_on_help' into '9005_cli_throw_excpetion_on_help_short'
9005 cli throw excpetion on help See merge request ulb/mirror-dspace!92
This commit is contained in:
@@ -21,6 +21,7 @@ import org.apache.logging.log4j.LogManager;
|
|||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
import org.dspace.core.Context;
|
import org.dspace.core.Context;
|
||||||
import org.dspace.scripts.DSpaceRunnable;
|
import org.dspace.scripts.DSpaceRunnable;
|
||||||
|
import org.dspace.scripts.DSpaceRunnable.StepResult;
|
||||||
import org.dspace.scripts.configuration.ScriptConfiguration;
|
import org.dspace.scripts.configuration.ScriptConfiguration;
|
||||||
import org.dspace.scripts.factory.ScriptServiceFactory;
|
import org.dspace.scripts.factory.ScriptServiceFactory;
|
||||||
import org.dspace.scripts.handler.DSpaceRunnableHandler;
|
import org.dspace.scripts.handler.DSpaceRunnableHandler;
|
||||||
@@ -145,8 +146,15 @@ public class ScriptLauncher {
|
|||||||
private static int executeScript(String[] args, DSpaceRunnableHandler dSpaceRunnableHandler,
|
private static int executeScript(String[] args, DSpaceRunnableHandler dSpaceRunnableHandler,
|
||||||
DSpaceRunnable script) {
|
DSpaceRunnable script) {
|
||||||
try {
|
try {
|
||||||
script.initialize(args, dSpaceRunnableHandler, null);
|
StepResult result = script.initialize(args, dSpaceRunnableHandler, null);
|
||||||
|
|
||||||
|
if (StepResult.Continue.equals(result)) {
|
||||||
|
// only run the script, if the normal initialize is successful
|
||||||
script.run();
|
script.run();
|
||||||
|
} else {
|
||||||
|
// otherwise - for example the script is started with the help argument
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
} catch (ParseException e) {
|
} catch (ParseException e) {
|
||||||
script.printHelp();
|
script.printHelp();
|
||||||
|
@@ -36,6 +36,11 @@ public abstract class DSpaceRunnable<T extends ScriptConfiguration> implements R
|
|||||||
*/
|
*/
|
||||||
protected CommandLine commandLine;
|
protected CommandLine commandLine;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The minimal CommandLine object for the script that'll hold help information
|
||||||
|
*/
|
||||||
|
protected CommandLine helpCommandLine;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This EPerson identifier variable is the UUID of the EPerson that's running the script
|
* This EPerson identifier variable is the UUID of the EPerson that's running the script
|
||||||
*/
|
*/
|
||||||
@@ -64,26 +69,63 @@ public abstract class DSpaceRunnable<T extends ScriptConfiguration> implements R
|
|||||||
* @param args The arguments given to the script
|
* @param args The arguments given to the script
|
||||||
* @param dSpaceRunnableHandler The DSpaceRunnableHandler object that defines from where the script was ran
|
* @param dSpaceRunnableHandler The DSpaceRunnableHandler object that defines from where the script was ran
|
||||||
* @param currentUser
|
* @param currentUser
|
||||||
|
* @return the result of this step; StepResult.Continue: continue the normal process, initialize is successful;
|
||||||
|
* otherwise exit the process (the help or version is shown)
|
||||||
* @throws ParseException If something goes wrong
|
* @throws ParseException If something goes wrong
|
||||||
*/
|
*/
|
||||||
public void initialize(String[] args, DSpaceRunnableHandler dSpaceRunnableHandler,
|
public StepResult initialize(String[] args, DSpaceRunnableHandler dSpaceRunnableHandler,
|
||||||
EPerson currentUser) throws ParseException {
|
EPerson currentUser) throws ParseException {
|
||||||
if (currentUser != null) {
|
if (currentUser != null) {
|
||||||
this.setEpersonIdentifier(currentUser.getID());
|
this.setEpersonIdentifier(currentUser.getID());
|
||||||
}
|
}
|
||||||
this.setHandler(dSpaceRunnableHandler);
|
this.setHandler(dSpaceRunnableHandler);
|
||||||
this.parse(args);
|
|
||||||
|
// parse the command line in a first step for the help options
|
||||||
|
// --> no other option is required
|
||||||
|
StepResult result = this.parseForHelp(args);
|
||||||
|
switch (result) {
|
||||||
|
case Exit:
|
||||||
|
// arguments of the command line matches the help options, handle this
|
||||||
|
handleHelpCommandLine();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Continue:
|
||||||
|
// arguments of the command line matches NOT the help options, parse the args for the normal options
|
||||||
|
result = this.parse(args);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/** This method handle the help command line. In this easy implementation only the help is printed.
|
||||||
|
* For more complexity override this method.
|
||||||
|
*/
|
||||||
|
private void handleHelpCommandLine() {
|
||||||
|
printHelp();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method will take the primitive array of String objects that represent the parameters given to the String
|
* This method will take the primitive array of String objects that represent the parameters given to the String
|
||||||
* and it'll parse these into a CommandLine object that can be used by the script to retrieve the data
|
* and it'll parse these into a CommandLine object that can be used by the script to retrieve the data
|
||||||
* @param args The primitive array of Strings representing the parameters
|
* @param args The primitive array of Strings representing the parameters
|
||||||
* @throws ParseException If something goes wrong
|
* @throws ParseException If something goes wrong
|
||||||
*/
|
*/
|
||||||
private void parse(String[] args) throws ParseException {
|
private StepResult parse(String[] args) throws ParseException {
|
||||||
commandLine = new DefaultParser().parse(getScriptConfiguration().getOptions(), args);
|
commandLine = new DefaultParser().parse(getScriptConfiguration().getOptions(), args);
|
||||||
setup();
|
setup();
|
||||||
|
return StepResult.Continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
private StepResult parseForHelp(String[] args) throws ParseException {
|
||||||
|
helpCommandLine = new DefaultParser().parse(getScriptConfiguration().getHelpOptions(), args);
|
||||||
|
if (helpCommandLine.getOptions() != null && helpCommandLine.getOptions().length > 0) {
|
||||||
|
return StepResult.Exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
return StepResult.Continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -158,4 +200,9 @@ public abstract class DSpaceRunnable<T extends ScriptConfiguration> implements R
|
|||||||
public void setEpersonIdentifier(UUID epersonIdentifier) {
|
public void setEpersonIdentifier(UUID epersonIdentifier) {
|
||||||
this.epersonIdentifier = epersonIdentifier;
|
this.epersonIdentifier = epersonIdentifier;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public enum StepResult {
|
||||||
|
Continue,
|
||||||
|
Exit;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -10,6 +10,7 @@ package org.dspace.scripts.configuration;
|
|||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.apache.commons.cli.Option;
|
||||||
import org.apache.commons.cli.Options;
|
import org.apache.commons.cli.Options;
|
||||||
import org.dspace.authorize.service.AuthorizeService;
|
import org.dspace.authorize.service.AuthorizeService;
|
||||||
import org.dspace.core.Context;
|
import org.dspace.core.Context;
|
||||||
@@ -105,6 +106,20 @@ public abstract class ScriptConfiguration<T extends DSpaceRunnable> implements B
|
|||||||
*/
|
*/
|
||||||
public abstract Options getOptions();
|
public abstract Options getOptions();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The getter for the options of the Script (help informations)
|
||||||
|
* @return the options value of this ScriptConfiguration for help
|
||||||
|
*/
|
||||||
|
public Options getHelpOptions() {
|
||||||
|
Options options = new Options();
|
||||||
|
|
||||||
|
options.addOption(Option.builder("h").longOpt("help")
|
||||||
|
.desc("help")
|
||||||
|
.hasArg(false).required(false).build());
|
||||||
|
|
||||||
|
return options;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setBeanName(String beanName) {
|
public void setBeanName(String beanName) {
|
||||||
this.name = beanName;
|
this.name = beanName;
|
||||||
|
@@ -99,10 +99,11 @@ public class MetadataExportIT
|
|||||||
script = scriptService.createDSpaceRunnableForScriptConfiguration(scriptConfiguration);
|
script = scriptService.createDSpaceRunnableForScriptConfiguration(scriptConfiguration);
|
||||||
}
|
}
|
||||||
if (script != null) {
|
if (script != null) {
|
||||||
script.initialize(args, testDSpaceRunnableHandler, null);
|
if (DSpaceRunnable.StepResult.Continue.equals(script.initialize(args, testDSpaceRunnableHandler, null))) {
|
||||||
script.run();
|
script.run();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void metadataExportToCsvTestUUID() throws Exception {
|
public void metadataExportToCsvTestUUID() throws Exception {
|
||||||
@@ -206,9 +207,10 @@ public class MetadataExportIT
|
|||||||
script = scriptService.createDSpaceRunnableForScriptConfiguration(scriptConfiguration);
|
script = scriptService.createDSpaceRunnableForScriptConfiguration(scriptConfiguration);
|
||||||
}
|
}
|
||||||
if (script != null) {
|
if (script != null) {
|
||||||
script.initialize(args, testDSpaceRunnableHandler, null);
|
if (DSpaceRunnable.StepResult.Continue.equals(script.initialize(args, testDSpaceRunnableHandler, null))) {
|
||||||
script.run();
|
script.run();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Exception exceptionDuringTestRun = testDSpaceRunnableHandler.getException();
|
Exception exceptionDuringTestRun = testDSpaceRunnableHandler.getException();
|
||||||
assertTrue("Random UUID caused IllegalArgumentException",
|
assertTrue("Random UUID caused IllegalArgumentException",
|
||||||
@@ -235,9 +237,10 @@ public class MetadataExportIT
|
|||||||
script = scriptService.createDSpaceRunnableForScriptConfiguration(scriptConfiguration);
|
script = scriptService.createDSpaceRunnableForScriptConfiguration(scriptConfiguration);
|
||||||
}
|
}
|
||||||
if (script != null) {
|
if (script != null) {
|
||||||
script.initialize(args, testDSpaceRunnableHandler, null);
|
if (DSpaceRunnable.StepResult.Continue.equals(script.initialize(args, testDSpaceRunnableHandler, null))) {
|
||||||
script.run();
|
script.run();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Exception exceptionDuringTestRun = testDSpaceRunnableHandler.getException();
|
Exception exceptionDuringTestRun = testDSpaceRunnableHandler.getException();
|
||||||
assertTrue("UUID of non-supported dsoType IllegalArgumentException",
|
assertTrue("UUID of non-supported dsoType IllegalArgumentException",
|
||||||
|
@@ -144,10 +144,11 @@ public class MetadataImportIT extends AbstractIntegrationTestWithDatabase {
|
|||||||
script = scriptService.createDSpaceRunnableForScriptConfiguration(scriptConfiguration);
|
script = scriptService.createDSpaceRunnableForScriptConfiguration(scriptConfiguration);
|
||||||
}
|
}
|
||||||
if (script != null) {
|
if (script != null) {
|
||||||
script.initialize(args, testDSpaceRunnableHandler, null);
|
if (DSpaceRunnable.StepResult.Continue.equals(script.initialize(args, testDSpaceRunnableHandler, null))) {
|
||||||
script.run();
|
script.run();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void relationshipMetadataImportTest() throws Exception {
|
public void relationshipMetadataImportTest() throws Exception {
|
||||||
|
@@ -702,9 +702,10 @@ public class CSVMetadataImportReferenceIT extends AbstractIntegrationTestWithDat
|
|||||||
script = scriptService.createDSpaceRunnableForScriptConfiguration(scriptConfiguration);
|
script = scriptService.createDSpaceRunnableForScriptConfiguration(scriptConfiguration);
|
||||||
}
|
}
|
||||||
if (script != null) {
|
if (script != null) {
|
||||||
script.initialize(args, testDSpaceRunnableHandler, null);
|
if (DSpaceRunnable.StepResult.Continue.equals(script.initialize(args, testDSpaceRunnableHandler, null))) {
|
||||||
script.run();
|
script.run();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (testDSpaceRunnableHandler.getException() != null) {
|
if (testDSpaceRunnableHandler.getException() != null) {
|
||||||
throw testDSpaceRunnableHandler.getException();
|
throw testDSpaceRunnableHandler.getException();
|
||||||
}
|
}
|
||||||
|
@@ -43,10 +43,11 @@ public class CurationIT extends AbstractIntegrationTestWithDatabase {
|
|||||||
script = scriptService.createDSpaceRunnableForScriptConfiguration(scriptConfiguration);
|
script = scriptService.createDSpaceRunnableForScriptConfiguration(scriptConfiguration);
|
||||||
}
|
}
|
||||||
if (script != null) {
|
if (script != null) {
|
||||||
script.initialize(args, testDSpaceRunnableHandler, null);
|
if (DSpaceRunnable.StepResult.Continue.equals(script.initialize(args, testDSpaceRunnableHandler, null))) {
|
||||||
script.run();
|
script.run();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void curationWithEPersonParameterTest() throws Exception {
|
public void curationWithEPersonParameterTest() throws Exception {
|
||||||
@@ -69,8 +70,9 @@ public class CurationIT extends AbstractIntegrationTestWithDatabase {
|
|||||||
script = scriptService.createDSpaceRunnableForScriptConfiguration(scriptConfiguration);
|
script = scriptService.createDSpaceRunnableForScriptConfiguration(scriptConfiguration);
|
||||||
}
|
}
|
||||||
if (script != null) {
|
if (script != null) {
|
||||||
script.initialize(args, testDSpaceRunnableHandler, null);
|
if (DSpaceRunnable.StepResult.Continue.equals(script.initialize(args, testDSpaceRunnableHandler, null))) {
|
||||||
script.run();
|
script.run();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user