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.dspace.core.Context;
|
||||
import org.dspace.scripts.DSpaceRunnable;
|
||||
import org.dspace.scripts.DSpaceRunnable.StepResult;
|
||||
import org.dspace.scripts.configuration.ScriptConfiguration;
|
||||
import org.dspace.scripts.factory.ScriptServiceFactory;
|
||||
import org.dspace.scripts.handler.DSpaceRunnableHandler;
|
||||
@@ -145,9 +146,16 @@ public class ScriptLauncher {
|
||||
private static int executeScript(String[] args, DSpaceRunnableHandler dSpaceRunnableHandler,
|
||||
DSpaceRunnable script) {
|
||||
try {
|
||||
script.initialize(args, dSpaceRunnableHandler, null);
|
||||
script.run();
|
||||
return 0;
|
||||
StepResult result = script.initialize(args, dSpaceRunnableHandler, null);
|
||||
|
||||
if (StepResult.Continue.equals(result)) {
|
||||
// only run the script, if the normal initialize is successful
|
||||
script.run();
|
||||
} else {
|
||||
// otherwise - for example the script is started with the help argument
|
||||
}
|
||||
|
||||
return 0;
|
||||
} catch (ParseException e) {
|
||||
script.printHelp();
|
||||
e.printStackTrace();
|
||||
|
@@ -35,6 +35,11 @@ public abstract class DSpaceRunnable<T extends ScriptConfiguration> implements R
|
||||
* The CommandLine object for the script that'll hold the information
|
||||
*/
|
||||
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
|
||||
@@ -64,27 +69,64 @@ public abstract class DSpaceRunnable<T extends ScriptConfiguration> implements R
|
||||
* @param args The arguments given to the script
|
||||
* @param dSpaceRunnableHandler The DSpaceRunnableHandler object that defines from where the script was ran
|
||||
* @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
|
||||
*/
|
||||
public void initialize(String[] args, DSpaceRunnableHandler dSpaceRunnableHandler,
|
||||
public StepResult initialize(String[] args, DSpaceRunnableHandler dSpaceRunnableHandler,
|
||||
EPerson currentUser) throws ParseException {
|
||||
if (currentUser != null) {
|
||||
this.setEpersonIdentifier(currentUser.getID());
|
||||
}
|
||||
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
|
||||
* 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
|
||||
* @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);
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method has to be included in every script and handles the setup of the script by parsing the CommandLine
|
||||
@@ -158,4 +200,9 @@ public abstract class DSpaceRunnable<T extends ScriptConfiguration> implements R
|
||||
public void setEpersonIdentifier(UUID epersonIdentifier) {
|
||||
this.epersonIdentifier = epersonIdentifier;
|
||||
}
|
||||
|
||||
public enum StepResult {
|
||||
Continue,
|
||||
Exit;
|
||||
}
|
||||
}
|
||||
|
@@ -10,6 +10,7 @@ package org.dspace.scripts.configuration;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.cli.Option;
|
||||
import org.apache.commons.cli.Options;
|
||||
import org.dspace.authorize.service.AuthorizeService;
|
||||
import org.dspace.core.Context;
|
||||
@@ -104,6 +105,20 @@ public abstract class ScriptConfiguration<T extends DSpaceRunnable> implements B
|
||||
* @return the options value of this ScriptConfiguration
|
||||
*/
|
||||
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
|
||||
public void setBeanName(String beanName) {
|
||||
|
@@ -99,8 +99,9 @@ public class MetadataExportIT
|
||||
script = scriptService.createDSpaceRunnableForScriptConfiguration(scriptConfiguration);
|
||||
}
|
||||
if (script != null) {
|
||||
script.initialize(args, testDSpaceRunnableHandler, null);
|
||||
script.run();
|
||||
if (DSpaceRunnable.StepResult.Continue.equals(script.initialize(args, testDSpaceRunnableHandler, null))) {
|
||||
script.run();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -206,8 +207,9 @@ public class MetadataExportIT
|
||||
script = scriptService.createDSpaceRunnableForScriptConfiguration(scriptConfiguration);
|
||||
}
|
||||
if (script != null) {
|
||||
script.initialize(args, testDSpaceRunnableHandler, null);
|
||||
script.run();
|
||||
if (DSpaceRunnable.StepResult.Continue.equals(script.initialize(args, testDSpaceRunnableHandler, null))) {
|
||||
script.run();
|
||||
}
|
||||
}
|
||||
|
||||
Exception exceptionDuringTestRun = testDSpaceRunnableHandler.getException();
|
||||
@@ -235,8 +237,9 @@ public class MetadataExportIT
|
||||
script = scriptService.createDSpaceRunnableForScriptConfiguration(scriptConfiguration);
|
||||
}
|
||||
if (script != null) {
|
||||
script.initialize(args, testDSpaceRunnableHandler, null);
|
||||
script.run();
|
||||
if (DSpaceRunnable.StepResult.Continue.equals(script.initialize(args, testDSpaceRunnableHandler, null))) {
|
||||
script.run();
|
||||
}
|
||||
}
|
||||
|
||||
Exception exceptionDuringTestRun = testDSpaceRunnableHandler.getException();
|
||||
|
@@ -144,8 +144,9 @@ public class MetadataImportIT extends AbstractIntegrationTestWithDatabase {
|
||||
script = scriptService.createDSpaceRunnableForScriptConfiguration(scriptConfiguration);
|
||||
}
|
||||
if (script != null) {
|
||||
script.initialize(args, testDSpaceRunnableHandler, null);
|
||||
script.run();
|
||||
if (DSpaceRunnable.StepResult.Continue.equals(script.initialize(args, testDSpaceRunnableHandler, null))) {
|
||||
script.run();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -702,8 +702,9 @@ public class CSVMetadataImportReferenceIT extends AbstractIntegrationTestWithDat
|
||||
script = scriptService.createDSpaceRunnableForScriptConfiguration(scriptConfiguration);
|
||||
}
|
||||
if (script != null) {
|
||||
script.initialize(args, testDSpaceRunnableHandler, null);
|
||||
script.run();
|
||||
if (DSpaceRunnable.StepResult.Continue.equals(script.initialize(args, testDSpaceRunnableHandler, null))) {
|
||||
script.run();
|
||||
}
|
||||
}
|
||||
if (testDSpaceRunnableHandler.getException() != null) {
|
||||
throw testDSpaceRunnableHandler.getException();
|
||||
|
@@ -43,8 +43,9 @@ public class CurationIT extends AbstractIntegrationTestWithDatabase {
|
||||
script = scriptService.createDSpaceRunnableForScriptConfiguration(scriptConfiguration);
|
||||
}
|
||||
if (script != null) {
|
||||
script.initialize(args, testDSpaceRunnableHandler, null);
|
||||
script.run();
|
||||
if (DSpaceRunnable.StepResult.Continue.equals(script.initialize(args, testDSpaceRunnableHandler, null))) {
|
||||
script.run();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,8 +70,9 @@ public class CurationIT extends AbstractIntegrationTestWithDatabase {
|
||||
script = scriptService.createDSpaceRunnableForScriptConfiguration(scriptConfiguration);
|
||||
}
|
||||
if (script != null) {
|
||||
script.initialize(args, testDSpaceRunnableHandler, null);
|
||||
script.run();
|
||||
if (DSpaceRunnable.StepResult.Continue.equals(script.initialize(args, testDSpaceRunnableHandler, null))) {
|
||||
script.run();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user