Skip to content

Commit

Permalink
Run Kitodo Script commands via Active MQ
Browse files Browse the repository at this point in the history
  • Loading branch information
matthias-ronge committed Mar 25, 2024
1 parent 83b1b9f commit 7fd387a
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,8 @@ public enum ParameterCore implements ParameterInterface {

ACTIVE_MQ_FINALIZE_STEP_QUEUE(new Parameter<UndefinedParameter>("activeMQ.finalizeStep.queue")),

ACTIVE_MQ_KITODO_SCRIPT_QUEUE(new Parameter<UndefinedParameter>("activeMQ.kitodoScript.queue")),

ACTIVE_MQ_TASK_ACTION_QUEUE(new Parameter<UndefinedParameter>("activeMQ.taskAction.queue")),

ACTIVE_MQ_USER(new Parameter<UndefinedParameter>("activeMQ.user")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public class ActiveMQDirector implements Runnable, ServletContextListener {
private static Collection<? extends ActiveMQProcessor> services;

static {
services = Arrays.asList(new FinalizeStepProcessor(), new TaskActionProcessor());
services = Arrays.asList(new FinalizeStepProcessor(), new TaskActionProcessor(), new KitodoScriptProcessor());
}

private static Connection connection = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* (c) Kitodo. Key to digital objects e. V. <contact@kitodo.org>
*
* This file is part of the Kitodo project.
*
* It is licensed under GNU General Public License version 3 or later.
*
* For the full copyright and license information, please read the
* GPL3-License.txt file that was distributed with this source code.
*/

package org.kitodo.production.interfaces.activemq;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.jms.JMSException;

import org.kitodo.config.ConfigCore;
import org.kitodo.config.enums.ParameterCore;
import org.kitodo.data.database.beans.Process;
import org.kitodo.data.database.exceptions.DAOException;
import org.kitodo.data.exceptions.DataException;
import org.kitodo.exceptions.InvalidImagesException;
import org.kitodo.exceptions.MediaNotFoundException;
import org.kitodo.exceptions.ProcessorException;
import org.kitodo.production.services.ServiceManager;
import org.kitodo.production.services.command.KitodoScriptService;
import org.kitodo.production.services.data.ProcessService;

/**
* Executes instructions to start a Kitodo Script command from the Active MQ
* interface. The MapMessage must contain the command statement in the
* {@code script} argument. You pass a list of the process IDs as
* {@code processes}.
*/
public class KitodoScriptProcessor extends ActiveMQProcessor {

private final KitodoScriptService kitodoScriptService = ServiceManager.getKitodoScriptService();
private final ProcessService processService = ServiceManager.getProcessService();

public KitodoScriptProcessor() {
super(ConfigCore.getOptionalString(ParameterCore.ACTIVE_MQ_KITODO_SCRIPT_QUEUE).orElse(null));
}

@Override
protected void process(MapMessageObjectReader ticket) throws ProcessorException, JMSException {
try {
String script = ticket.getMandatoryString("script");
Collection<Integer> processIds = ticket.getCollectionOfInteger("processes");
List<Process> processes = new ArrayList<>(processIds.size());
for (Integer id : processIds) {
processes.add(processService.getById(id));
}
kitodoScriptService.execute(processes, script);
} catch (DAOException | DataException | IOException | InvalidImagesException | MediaNotFoundException e) {
throw new ProcessorException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@

package org.kitodo.production.interfaces.activemq;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

import javax.jms.JMSException;
import javax.jms.MapMessage;
Expand Down Expand Up @@ -110,6 +113,53 @@ public String getMandatoryString(String key) throws JMSException {
return mandatoryString;
}

/**
* Fetches a {@code Collection<Integer>} from a MapMessage. This is a loose
* implementation for an optional object with optional content. The
* collection content is filtered through {@code toString()} and split on
* non-digits, dealing generously with list variants and separators. If not
* found, returns an empty collection, never {@code null}.
*
* @param key
* the name of the set to return
* @return the set requested
* @throws JMSException
* can be thrown by MapMessage.getObject(String)
*/
public Collection<Integer> getCollectionOfInteger(String key) throws JMSException {
Collection<String> collectionOfString = getCollectionOfString(key);
List<Integer> collectionOfInteger = collectionOfString.stream()
.flatMap(string -> Arrays.stream(string.split("\\D+"))).map(Integer::valueOf)

Check notice

Code scanning / CodeQL

Missing catch of NumberFormatException Note

Potential uncaught 'java.lang.NumberFormatException'.
.collect(Collectors.toList());
return collectionOfInteger;
}

/**
* Fetches a {@code Collection<String>} from a MapMessage. This is a loose
* implementation for an optional object with optional content. The
* collection content is filtered through {@code toString()}, {@code null}
* objects will be skipped. If not found, returns an empty collection, never
* {@code null}.
*
* @param key
* the name of the set to return
* @return the set requested
* @throws JMSException
* can be thrown by MapMessage.getObject(String)
*/
public Collection<String> getCollectionOfString(String key) throws JMSException {

Object collectionObject = ticket.getObject(key);
if (Objects.isNull(collectionObject)) {
return Collections.emptyList();
}
if (!(collectionObject instanceof Collection<?>)) {
return Collections.singletonList(collectionObject.toString());
}
return ((Collection<?>) collectionObject).stream().filter(Objects::nonNull).map(Object::toString)
.collect(Collectors.toList());
}

/**
* Fetches a String from a MapMessage. This is an access forward to the
* native function of the MapMessage. You may consider to use
Expand Down

0 comments on commit 7fd387a

Please sign in to comment.