View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
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   * Used to run a {@link MCRPacker} inside a {@link org.mycore.services.queuedjob.MCRJobQueue}
33   *
34   * @author Sebastian Hofmann (mcrshofm)
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 }