View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.core.logic.job;
20  
21  import java.util.Map;
22  import java.util.Set;
23  import java.util.concurrent.ConcurrentHashMap;
24  import javax.validation.ConstraintViolation;
25  import javax.validation.Validator;
26  import org.apache.syncope.common.lib.command.CommandArgs;
27  import org.apache.syncope.core.logic.api.Command;
28  import org.apache.syncope.core.persistence.api.dao.ImplementationDAO;
29  import org.apache.syncope.core.persistence.api.entity.Implementation;
30  import org.apache.syncope.core.persistence.api.entity.task.MacroTask;
31  import org.apache.syncope.core.persistence.api.entity.task.TaskExec;
32  import org.apache.syncope.core.provisioning.api.serialization.POJOHelper;
33  import org.apache.syncope.core.provisioning.java.job.AbstractSchedTaskJobDelegate;
34  import org.apache.syncope.core.spring.implementation.ImplementationManager;
35  import org.quartz.JobExecutionContext;
36  import org.quartz.JobExecutionException;
37  import org.springframework.beans.factory.annotation.Autowired;
38  
39  public class MacroRunJobDelegate extends AbstractSchedTaskJobDelegate<MacroTask> {
40  
41      @Autowired
42      protected ImplementationDAO implementationDAO;
43  
44      @Autowired
45      protected Validator validator;
46  
47      protected final Map<String, Command<?>> perContextCommands = new ConcurrentHashMap<>();
48  
49      @SuppressWarnings("unchecked")
50      @Override
51      protected String doExecute(final boolean dryRun, final String executor, final JobExecutionContext context)
52              throws JobExecutionException {
53  
54          StringBuilder output = new StringBuilder();
55          for (int i = 0; i < task.getCommands().size(); i++) {
56              Implementation command = task.getCommands().get(i);
57  
58              Command<CommandArgs> runnable;
59              try {
60                  runnable = (Command<CommandArgs>) ImplementationManager.build(
61                          command,
62                          () -> perContextCommands.get(command.getKey()),
63                          instance -> perContextCommands.put(command.getKey(), instance));
64              } catch (Exception e) {
65                  throw new JobExecutionException("Could not build " + command.getKey(), e);
66              }
67  
68              String args = POJOHelper.serialize(task.getCommandArgs().get(i));
69  
70              output.append("Command[").append(i).append("]: ").
71                      append(command.getKey()).append(" ").append(args).append("\n");
72              if (dryRun) {
73                  output.append(command).append(' ').append(args);
74              } else {
75                  try {
76                      if (task.getCommandArgs().get(i) != null) {
77                          Set<ConstraintViolation<Object>> violations = validator.validate(task.getCommandArgs().get(i));
78                          if (!violations.isEmpty()) {
79                              LOG.error("Errors while validating {}: {}", task.getCommandArgs().get(i), violations);
80                              throw new IllegalArgumentException(task.getCommandArgs().get(i).getClass().getName());
81                          }
82                      }
83  
84                      output.append(runnable.run(task.getCommandArgs().get(i)));
85                  } catch (Exception e) {
86                      if (task.isContinueOnError()) {
87                          output.append("Continuing on error: <").append(e.getMessage()).append('>');
88                          LOG.error("While running {} with args {}, continuing on error", command.getKey(), args, e);
89                      } else {
90                          throw new RuntimeException("While running " + command.getKey(), e);
91                      }
92                  }
93              }
94              output.append("\n\n");
95          }
96  
97          output.append("COMPLETED");
98          return output.toString();
99      }
100 
101     @Override
102     protected boolean hasToBeRegistered(final TaskExec<?> execution) {
103         return task.isSaveExecs();
104     }
105 }