1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.mycore.services.packaging;
20
21 import java.util.Map;
22 import java.util.concurrent.ExecutionException;
23 import java.util.stream.Collectors;
24
25 import org.apache.logging.log4j.LogManager;
26 import org.apache.logging.log4j.Logger;
27 import org.mycore.common.config.MCRConfiguration2;
28 import org.mycore.services.queuedjob.MCRJob;
29 import org.mycore.services.queuedjob.MCRJobAction;
30
31
32
33
34
35
36 public class MCRPackerJobAction extends MCRJobAction {
37
38 private static final Logger LOGGER = LogManager.getLogger();
39
40 private MCRPacker packerInstance;
41
42 public MCRPackerJobAction() {
43 }
44
45 public MCRPackerJobAction(MCRJob job) {
46 super(job);
47 }
48
49 @Override
50 public final boolean isActivated() {
51 return true;
52 }
53
54 @Override
55 public String name() {
56 return "MCRPackerJobAction-" + getPackerId();
57 }
58
59 private String getPackerId() {
60 return getParameters().get("packer");
61 }
62
63 public MCRPacker getPackerInstance() {
64 return this.packerInstance;
65 }
66
67 @Override
68 public final void execute() throws ExecutionException {
69 String packerId = getPackerId();
70 Map<String, String> packerConfiguration = getConfiguration(packerId);
71 packerInstance = MCRConfiguration2.getOrThrow(MCRPacker.PACKER_CONFIGURATION_PREFIX + packerId + ".Class",
72 MCRConfiguration2::instantiateClass);
73
74 Map<String, String> parameters = getParameters();
75
76 packerInstance.setParameter(parameters);
77 packerInstance.setConfiguration(packerConfiguration);
78 LOGGER.info(() -> {
79 StringBuilder messageBuilder = new StringBuilder();
80 messageBuilder.append(getPackerId()).append(" starts packing with parameters: ");
81 parameters.forEach((key, value) -> messageBuilder.append(key).append("=").append(value).append(";"));
82 return messageBuilder.toString();
83 });
84 packerInstance.pack();
85 }
86
87 protected final Map<String, String> getParameters() {
88 return this.job.getParameters();
89 }
90
91 public static Map<String, String> getConfiguration(String packerId) {
92 String packerConfigPrefix = MCRPacker.PACKER_CONFIGURATION_PREFIX + packerId + ".";
93 return MCRConfiguration2.getPropertiesMap()
94 .entrySet()
95 .stream()
96 .filter(p -> p.getKey().startsWith(packerConfigPrefix))
97 .collect(
98 Collectors.toMap(e -> e.getKey().substring(packerConfigPrefix.length()), Map.Entry::getValue));
99 }
100
101 @Override
102 public void rollback() {
103 if (packerInstance != null) {
104 packerInstance.rollback();
105 }
106 }
107
108 }