From 6c2fb01bcb4d02bc915ee04c0068e8c5c8a43d6b Mon Sep 17 00:00:00 2001 From: Matus Kasak Date: Fri, 22 Nov 2024 08:55:14 +0100 Subject: [PATCH] Added new process using python script --- .../dspace/administer/ProcessCleaner2.java | 140 ------------------ .../dspace/administer/ProcessCleanerCli2.java | 18 --- .../ProcessCleanerCliConfiguration2.java | 18 --- .../ProcessCleanerConfiguration2.java | 53 ------- .../main/java/org/dspace/testing/Testing.java | 70 +++++++++ .../testing/TestingScriptConfiguration.java | 36 +++++ .../src/main/resources/python-script.py | 1 + dspace/config/spring/api/scripts.xml | 10 +- dspace/config/spring/rest/scripts.xml | 10 +- 9 files changed, 117 insertions(+), 239 deletions(-) delete mode 100644 dspace-api/src/main/java/org/dspace/administer/ProcessCleaner2.java delete mode 100644 dspace-api/src/main/java/org/dspace/administer/ProcessCleanerCli2.java delete mode 100644 dspace-api/src/main/java/org/dspace/administer/ProcessCleanerCliConfiguration2.java delete mode 100644 dspace-api/src/main/java/org/dspace/administer/ProcessCleanerConfiguration2.java create mode 100644 dspace-api/src/main/java/org/dspace/testing/Testing.java create mode 100644 dspace-api/src/main/java/org/dspace/testing/TestingScriptConfiguration.java create mode 100644 dspace-api/src/main/resources/python-script.py diff --git a/dspace-api/src/main/java/org/dspace/administer/ProcessCleaner2.java b/dspace-api/src/main/java/org/dspace/administer/ProcessCleaner2.java deleted file mode 100644 index 0f48ea748dbf..000000000000 --- a/dspace-api/src/main/java/org/dspace/administer/ProcessCleaner2.java +++ /dev/null @@ -1,140 +0,0 @@ -/** - * 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.administer; - -import org.apache.commons.cli.ParseException; -import org.apache.commons.lang.time.DateUtils; -import org.dspace.authorize.AuthorizeException; -import org.dspace.content.ProcessStatus; -import org.dspace.core.Context; -import org.dspace.scripts.DSpaceRunnable; -import org.dspace.scripts.Process; -import org.dspace.scripts.factory.ScriptServiceFactory; -import org.dspace.scripts.service.ProcessService; -import org.dspace.services.ConfigurationService; -import org.dspace.services.factory.DSpaceServicesFactory; -import org.dspace.utils.DSpace; - -import java.io.IOException; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; - -/** - * Script to cleanup the old processes in the specified state. - * - * @author Luca Giamminonni (luca.giamminonni at 4science.it) - * - */ -public class ProcessCleaner2 extends DSpaceRunnable> { - - private ConfigurationService configurationService; - - private ProcessService processService; - - - private boolean cleanCompleted = false; - - private boolean cleanFailed = false; - - private boolean cleanRunning = false; - - private boolean help = false; - - private Integer days; - - - @Override - public void setup() throws ParseException { - - this.configurationService = DSpaceServicesFactory.getInstance().getConfigurationService(); - this.processService = ScriptServiceFactory.getInstance().getProcessService(); - - this.help = commandLine.hasOption('h'); - this.cleanFailed = commandLine.hasOption('f'); - this.cleanRunning = commandLine.hasOption('r'); - this.cleanCompleted = commandLine.hasOption('c') || (!cleanFailed && !cleanRunning); - - this.days = configurationService.getIntProperty("process-cleaner.days", 14); - - if (this.days <= 0) { - throw new IllegalStateException("The number of days must be a positive integer."); - } - - } - - @Override - public void internalRun() throws Exception { - - if (help) { - printHelp(); - return; - } - - Context context = new Context(); - - try { - context.turnOffAuthorisationSystem(); - performDeletion(context); - } finally { - context.restoreAuthSystemState(); - context.complete(); - } - - } - - /** - * Delete the processes based on the specified statuses and the configured days - * from their creation. - */ - private void performDeletion(Context context) throws SQLException, IOException, AuthorizeException { - - List statuses = getProcessToDeleteStatuses(); - Date creationDate = calculateCreationDate(); - - handler.logInfo("Searching for processes with status: " + statuses); - List processes = processService.findByStatusAndCreationTimeOlderThan(context, statuses, creationDate); - handler.logInfo("Found " + processes.size() + " processes to be deleted"); - for (Process process : processes) { - processService.delete(context, process); - } - - handler.logInfo("Process cleanup completed"); - - } - - /** - * Returns the list of Process statuses do be deleted. - */ - private List getProcessToDeleteStatuses() { - List statuses = new ArrayList(); - if (cleanCompleted) { - statuses.add(ProcessStatus.COMPLETED); - } - if (cleanFailed) { - statuses.add(ProcessStatus.FAILED); - } - if (cleanRunning) { - statuses.add(ProcessStatus.RUNNING); - } - return statuses; - } - - private Date calculateCreationDate() { - return DateUtils.addDays(new Date(), -days); - } - - @Override - @SuppressWarnings("unchecked") - public ProcessCleanerConfiguration2 getScriptConfiguration() { - return new DSpace().getServiceManager() - .getServiceByName("process-cleaner-2", ProcessCleanerConfiguration2.class); - } - -} diff --git a/dspace-api/src/main/java/org/dspace/administer/ProcessCleanerCli2.java b/dspace-api/src/main/java/org/dspace/administer/ProcessCleanerCli2.java deleted file mode 100644 index 9483585ce82e..000000000000 --- a/dspace-api/src/main/java/org/dspace/administer/ProcessCleanerCli2.java +++ /dev/null @@ -1,18 +0,0 @@ -/** - * 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.administer; - -/** - * The {@link ProcessCleaner} for CLI. - * - * @author Luca Giamminonni (luca.giamminonni at 4science.it) - * - */ -public class ProcessCleanerCli2 extends ProcessCleaner2 { - -} diff --git a/dspace-api/src/main/java/org/dspace/administer/ProcessCleanerCliConfiguration2.java b/dspace-api/src/main/java/org/dspace/administer/ProcessCleanerCliConfiguration2.java deleted file mode 100644 index fc90445c87fb..000000000000 --- a/dspace-api/src/main/java/org/dspace/administer/ProcessCleanerCliConfiguration2.java +++ /dev/null @@ -1,18 +0,0 @@ -/** - * 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.administer; - -/** - * The {@link ProcessCleanerConfiguration} for CLI. - * - * @author Luca Giamminonni (luca.giamminonni at 4science.it) - * - */ -public class ProcessCleanerCliConfiguration2 extends ProcessCleanerConfiguration2 { - -} \ No newline at end of file diff --git a/dspace-api/src/main/java/org/dspace/administer/ProcessCleanerConfiguration2.java b/dspace-api/src/main/java/org/dspace/administer/ProcessCleanerConfiguration2.java deleted file mode 100644 index 790751ae1852..000000000000 --- a/dspace-api/src/main/java/org/dspace/administer/ProcessCleanerConfiguration2.java +++ /dev/null @@ -1,53 +0,0 @@ -/** - * 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.administer; - -import org.apache.commons.cli.Options; -import org.dspace.scripts.configuration.ScriptConfiguration; - -/** - * The {@link ScriptConfiguration} for the {@link ProcessCleaner2} script. - */ -public class ProcessCleanerConfiguration2 extends ScriptConfiguration { - - private Class dspaceRunnableClass; - - @Override - public Options getOptions() { - if (options == null) { - - Options options = new Options(); - - options.addOption("h", "help", false, "help"); - - options.addOption("r", "running", false, "delete the process with RUNNING status"); - options.getOption("r").setType(boolean.class); - - options.addOption("f", "failed", false, "delete the process with FAILED status"); - options.getOption("f").setType(boolean.class); - - options.addOption("c", "completed", false, - "delete the process with COMPLETED status (default if no statuses are specified)"); - options.getOption("c").setType(boolean.class); - - super.options = options; - } - return options; - } - - @Override - public Class getDspaceRunnableClass() { - return dspaceRunnableClass; - } - - @Override - public void setDspaceRunnableClass(Class dspaceRunnableClass) { - this.dspaceRunnableClass = dspaceRunnableClass; - } - -} diff --git a/dspace-api/src/main/java/org/dspace/testing/Testing.java b/dspace-api/src/main/java/org/dspace/testing/Testing.java new file mode 100644 index 000000000000..4952aff04ace --- /dev/null +++ b/dspace-api/src/main/java/org/dspace/testing/Testing.java @@ -0,0 +1,70 @@ +/** + * 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.testing; + +import org.apache.commons.cli.ParseException; +import org.dspace.scripts.DSpaceRunnable; +import org.dspace.utils.DSpace; + +import java.io.*; +import java.nio.charset.StandardCharsets; + +public class Testing extends DSpaceRunnable { + + @Override + public TestingScriptConfiguration getScriptConfiguration() { + return new DSpace().getServiceManager() + .getServiceByName("testing-script", TestingScriptConfiguration.class); + } + + @Override + public void setup() throws ParseException { + + } + + @Override + public void internalRun() throws Exception { + System.out.println("Hello world from java"); + try { + // loading python scripts stored in resources + InputStream scriptInputStream = getClass().getClassLoader().getResourceAsStream("python-script.py"); + if (scriptInputStream == null) { + throw new FileNotFoundException("Python script not found in resources"); + } + + File tempFile = File.createTempFile("python-script", ".py"); + tempFile.deleteOnExit(); + // transfer the content to the temporary file + try (FileOutputStream out = new FileOutputStream(tempFile)) { + scriptInputStream.transferTo(out); + } + + // configure ProcessBuilder to run script using python commands + ProcessBuilder processBuilder = new ProcessBuilder("python", tempFile.getAbsolutePath()); + processBuilder.directory(tempFile.getParentFile()); + Process process = processBuilder.start(); + + InputStreamReader inputStreamReader = new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8); + BufferedReader reader = new BufferedReader(inputStreamReader); + + // reading the scripts output and then outputs it to the console + String line = reader.readLine(); + while (line != null) { + System.out.println(line); + line = reader.readLine(); + } + + inputStreamReader.close(); + reader.close(); + int exitCode = process.waitFor(); + System.out.println("Exited with code: " + exitCode); + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/dspace-api/src/main/java/org/dspace/testing/TestingScriptConfiguration.java b/dspace-api/src/main/java/org/dspace/testing/TestingScriptConfiguration.java new file mode 100644 index 000000000000..0bbc7c2a1352 --- /dev/null +++ b/dspace-api/src/main/java/org/dspace/testing/TestingScriptConfiguration.java @@ -0,0 +1,36 @@ +/** + * 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.testing; + +import org.apache.commons.cli.Options; +import org.dspace.scripts.configuration.ScriptConfiguration; + +public class TestingScriptConfiguration extends ScriptConfiguration { + + private Class dspaceRunnableclass; + + @Override + public Class getDspaceRunnableClass() { + return dspaceRunnableclass; + } + + @Override + public void setDspaceRunnableClass(Class dspaceRunnableClass) { + this.dspaceRunnableclass = dspaceRunnableClass; + } + + @Override + public Options getOptions() { + if (options == null) { + Options options = new Options(); + options.addOption("h", "help", false, "Display help message"); + super.options = options; + } + return options; + } +} diff --git a/dspace-api/src/main/resources/python-script.py b/dspace-api/src/main/resources/python-script.py new file mode 100644 index 000000000000..0ad14a6fc42b --- /dev/null +++ b/dspace-api/src/main/resources/python-script.py @@ -0,0 +1 @@ +print("Hello world from python") diff --git a/dspace/config/spring/api/scripts.xml b/dspace/config/spring/api/scripts.xml index c4594ed4a83a..fbe489e03e3d 100644 --- a/dspace/config/spring/api/scripts.xml +++ b/dspace/config/spring/api/scripts.xml @@ -56,11 +56,6 @@ - - - - - @@ -96,4 +91,9 @@ + + + + + diff --git a/dspace/config/spring/rest/scripts.xml b/dspace/config/spring/rest/scripts.xml index 078eddadd0f5..b835606d43c0 100644 --- a/dspace/config/spring/rest/scripts.xml +++ b/dspace/config/spring/rest/scripts.xml @@ -44,11 +44,6 @@ - - - - - @@ -74,4 +69,9 @@ + + + + +