Skip to content

Commit

Permalink
Merge branch 'master' into rest-demo
Browse files Browse the repository at this point in the history
  • Loading branch information
tdonohue committed Nov 22, 2019
2 parents 048897d + 3b578c6 commit 18e5695
Show file tree
Hide file tree
Showing 13 changed files with 253 additions and 76 deletions.
Expand Up @@ -13,6 +13,12 @@
import java.util.List;
import java.util.TreeMap;

import org.apache.commons.cli.ParseException;
import org.apache.log4j.Logger;
import org.dspace.scripts.DSpaceRunnable;
import org.dspace.scripts.factory.ScriptServiceFactory;
import org.dspace.scripts.handler.DSpaceRunnableHandler;
import org.dspace.scripts.handler.impl.CommandLineDSpaceRunnableHandler;
import org.dspace.servicemanager.DSpaceKernelImpl;
import org.dspace.servicemanager.DSpaceKernelInit;
import org.dspace.services.RequestService;
Expand All @@ -27,6 +33,9 @@
* @author Mark Diggory
*/
public class ScriptLauncher {

private static final Logger log = Logger.getLogger(ScriptLauncher.class);

/**
* The service manager kernel
*/
Expand Down Expand Up @@ -76,8 +85,9 @@ public static void main(String[] args)
}

// Look up command in the configuration, and execute.
int status;
status = runOneCommand(commandConfigs, args);

CommandLineDSpaceRunnableHandler commandLineDSpaceRunnableHandler = new CommandLineDSpaceRunnableHandler();
int status = handleScript(args, commandConfigs, commandLineDSpaceRunnableHandler, kernelImpl);

// Destroy the service kernel if it is still alive
if (kernelImpl != null) {
Expand All @@ -86,6 +96,50 @@ public static void main(String[] args)
}

System.exit(status);

}

/**
* This method will take the arguments from a commandline input and it'll find the script that the first argument
* refers to and it'll execute this script.
* It can return a 1 or a 0 depending on whether the script failed or passed respectively
* @param args The arguments for the script and the script as first one in the array
* @param commandConfigs The Document
* @param dSpaceRunnableHandler The DSpaceRunnableHandler for this execution
* @param kernelImpl The relevant DSpaceKernelImpl
* @return A 1 or 0 depending on whether the script failed or passed respectively
*/
public static int handleScript(String[] args, Document commandConfigs,
DSpaceRunnableHandler dSpaceRunnableHandler,
DSpaceKernelImpl kernelImpl) {
int status;
DSpaceRunnable script = ScriptServiceFactory.getInstance().getScriptService().getScriptForName(args[0]);
if (script != null) {
status = executeScript(args, dSpaceRunnableHandler, script);
} else {
status = runOneCommand(commandConfigs, args, kernelImpl);
}
return status;
}

/**
* This method will simply execute the script
* @param args The arguments of the script with the script name as first place in the array
* @param dSpaceRunnableHandler The relevant DSpaceRunnableHandler
* @param script The script to be executed
* @return A 1 or 0 depending on whether the script failed or passed respectively
*/
private static int executeScript(String[] args, DSpaceRunnableHandler dSpaceRunnableHandler,
DSpaceRunnable script) {
try {
script.initialize(args, dSpaceRunnableHandler);
script.run();
return 0;
} catch (ParseException e) {
script.printHelp();
e.printStackTrace();
return 1;
}
}

protected static int runOneCommand(Document commandConfigs, String[] args) {
Expand All @@ -98,7 +152,7 @@ protected static int runOneCommand(Document commandConfigs, String[] args) {
* @param commandConfigs Document
* @param args the command line arguments given
*/
public static int runOneCommand(Document commandConfigs, String[] args, DSpaceKernelImpl kernelImpl) {
protected static int runOneCommand(Document commandConfigs, String[] args, DSpaceKernelImpl kernelImpl) {
String request = args[0];
Element root = commandConfigs.getRootElement();
List<Element> commands = root.getChildren("command");
Expand Down

This file was deleted.

39 changes: 39 additions & 0 deletions dspace-api/src/main/java/org/dspace/scripts/ScriptServiceImpl.java
@@ -0,0 +1,39 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.scripts;

import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.dspace.core.Context;
import org.dspace.scripts.service.ScriptService;
import org.springframework.beans.factory.annotation.Autowired;

/**
* The implementation for the {@link ScriptService}
*/
public class ScriptServiceImpl implements ScriptService {

@Autowired
private List<DSpaceRunnable> dSpaceRunnables;

@Override
public DSpaceRunnable getScriptForName(String name) {
return dSpaceRunnables.stream()
.filter(dSpaceRunnable -> StringUtils.equalsIgnoreCase(dSpaceRunnable.getName(), name))
.findFirst()
.orElse(null);
}

@Override
public List<DSpaceRunnable> getDSpaceRunnables(Context context) {
return dSpaceRunnables.stream().filter(
dSpaceRunnable -> dSpaceRunnable.isAllowedToExecute(context)).collect(Collectors.toList());
}
}
@@ -0,0 +1,41 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.scripts.factory;

import org.dspace.scripts.service.ProcessService;
import org.dspace.scripts.service.ScriptService;
import org.dspace.services.factory.DSpaceServicesFactory;

/**
* Abstract factory to get services for the Script workload, use ScriptServiceFactory.getInstance() to retrieve an
* implementation
*
*/
public abstract class ScriptServiceFactory {

/**
* This method will return an instance of the ScriptService
* @return An instance of the ScriptService
*/
public abstract ScriptService getScriptService();

/**
* This method will return an instance of the ProcessService
* @return An instance of the ProcessService
*/
public abstract ProcessService getProcessService();

/**
* Use this method to retrieve an implementation of the ScriptServiceFactory to use to retrieve the different beans
* @return An implementation of the ScriptServiceFactory
*/
public static ScriptServiceFactory getInstance() {
return DSpaceServicesFactory.getInstance().getServiceManager()
.getServiceByName("scriptServiceFactory", ScriptServiceFactory.class);
}
}
Expand Up @@ -5,20 +5,29 @@
*
* http://www.dspace.org/license/
*/
package org.dspace.content.factory.impl;
package org.dspace.scripts.factory.impl;

import org.dspace.content.factory.ProcessServiceFactory;
import org.dspace.scripts.factory.ScriptServiceFactory;
import org.dspace.scripts.service.ProcessService;
import org.dspace.scripts.service.ScriptService;
import org.springframework.beans.factory.annotation.Autowired;

/**
* The implementation for the {@link ProcessServiceFactory}
* The implementation for the {@link ScriptServiceFactory}
*/
public class ProcessServiceFactoryImpl extends ProcessServiceFactory {
public class ScriptServiceFactoryImpl extends ScriptServiceFactory {

@Autowired(required = true)
private ScriptService scriptService;

@Autowired(required = true)
private ProcessService processService;

@Override
public ScriptService getScriptService() {
return scriptService;
}

@Override
public ProcessService getProcessService() {
return processService;
Expand Down
@@ -0,0 +1,33 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.scripts.service;

import java.util.List;

import org.dspace.core.Context;
import org.dspace.scripts.DSpaceRunnable;

/**
* This service will deal with logic to handle DSpaceRunnable objects
*/
public interface ScriptService {

/**
* This method will return the DSpaceRunnable that has the name that's equal to the name given in the parameters
* @param name The name that the script has to match
* @return The matching DSpaceRunnable script
*/
DSpaceRunnable getScriptForName(String name);

/**
* This method will return a list of DSpaceRunnable objects for which the given Context is authorized to use them
* @param context The relevant DSpace context
* @return The list of accessible DSpaceRunnable scripts for this context
*/
List<DSpaceRunnable> getDSpaceRunnables(Context context);
}
Expand Up @@ -35,6 +35,7 @@
import org.dspace.scripts.DSpaceCommandLineParameter;
import org.dspace.scripts.DSpaceRunnable;
import org.dspace.scripts.service.ProcessService;
import org.dspace.scripts.service.ScriptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand All @@ -49,9 +50,6 @@ public class ScriptRestRepository extends DSpaceRestRepository<ScriptRest, Strin

private static final Logger log = LogManager.getLogger();

@Autowired
private List<DSpaceRunnable> dspaceRunnables;

@Autowired
private ScriptConverter scriptConverter;

Expand All @@ -61,28 +59,31 @@ public class ScriptRestRepository extends DSpaceRestRepository<ScriptRest, Strin
@Autowired
private ProcessConverter processConverter;

@Autowired
private ScriptService scriptService;

@Autowired
private DSpaceRunnableParameterConverter dSpaceRunnableParameterConverter;


@Override
public ScriptRest findOne(Context context, String name) {
for (DSpaceRunnable dSpaceRunnable : dspaceRunnables) {
if (StringUtils.equalsIgnoreCase(dSpaceRunnable.getName(), name)) {
if (dSpaceRunnable.isAllowedToExecute(context)) {
return scriptConverter.fromModel(dSpaceRunnable);
} else {
throw new AccessDeniedException("The current user was not authorized to access this script");
}

DSpaceRunnable dSpaceRunnable = scriptService.getScriptForName(name);
if (dSpaceRunnable != null) {
if (dSpaceRunnable.isAllowedToExecute(context)) {
return scriptConverter.fromModel(dSpaceRunnable);
} else {
throw new AccessDeniedException("The current user was not authorized to access this script");
}
}
throw new DSpaceBadRequestException("The script with name: " + name + " could not be found");
}

@Override
public Page<ScriptRest> findAll(Context context, Pageable pageable) {
return utils.getPage(dspaceRunnables.stream().filter(
dSpaceRunnable -> dSpaceRunnable.isAllowedToExecute(context)).collect(Collectors.toList()), pageable)
.map(scriptConverter);
List<DSpaceRunnable> dSpaceRunnables = scriptService.getDSpaceRunnables(context);
return utils.getPage(dSpaceRunnables, pageable).map(scriptConverter);
}

@Override
Expand All @@ -108,7 +109,7 @@ public ProcessRest startProcess(String scriptName) throws SQLException, IOExcept
String properties = requestService.getCurrentRequest().getServletRequest().getParameter("properties");
List<DSpaceCommandLineParameter> dSpaceCommandLineParameters =
processPropertiesToDSpaceCommandLineParameters(properties);
DSpaceRunnable scriptToExecute = getdSpaceRunnableForName(scriptName);
DSpaceRunnable scriptToExecute = scriptService.getScriptForName(scriptName);
if (scriptToExecute == null) {
throw new DSpaceBadRequestException("The script for name: " + scriptName + " wasn't found");
}
Expand Down Expand Up @@ -145,17 +146,6 @@ private List<DSpaceCommandLineParameter> processPropertiesToDSpaceCommandLinePar
return dSpaceCommandLineParameters;
}

private DSpaceRunnable getdSpaceRunnableForName(String scriptName) {
DSpaceRunnable scriptToExecute = null;
for (DSpaceRunnable script : dspaceRunnables) {
if (StringUtils.equalsIgnoreCase(script.getName(), scriptName)) {
scriptToExecute = script;
break;
}
}
return scriptToExecute;
}

private List<String> constructArgs(List<DSpaceCommandLineParameter> dSpaceCommandLineParameters) {
List<String> args = new ArrayList<>();
for (DSpaceCommandLineParameter parameter : dSpaceCommandLineParameters) {
Expand Down
Expand Up @@ -17,12 +17,12 @@
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.logging.log4j.Logger;
import org.dspace.content.ProcessStatus;
import org.dspace.content.factory.ProcessServiceFactory;
import org.dspace.core.Context;
import org.dspace.eperson.EPerson;
import org.dspace.scripts.DSpaceCommandLineParameter;
import org.dspace.scripts.DSpaceRunnable;
import org.dspace.scripts.Process;
import org.dspace.scripts.factory.ScriptServiceFactory;
import org.dspace.scripts.handler.DSpaceRunnableHandler;
import org.dspace.scripts.service.ProcessService;

Expand All @@ -33,7 +33,7 @@ public class RestDSpaceRunnableHandler implements DSpaceRunnableHandler {
private static final Logger log = org.apache.logging.log4j.LogManager
.getLogger(RestDSpaceRunnableHandler.class);

private ProcessService processService = ProcessServiceFactory.getInstance().getProcessService();
private ProcessService processService = ScriptServiceFactory.getInstance().getProcessService();

private Integer processId;
private String scriptName;
Expand Down

0 comments on commit 18e5695

Please sign in to comment.