View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5    *  tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    *
8    *  All Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
19   */
20  package org.waarp.openr66.context.task;
21  
22  import org.waarp.common.logging.WaarpLogger;
23  import org.waarp.common.logging.WaarpLoggerFactory;
24  import org.waarp.openr66.context.task.exception.OpenR66RunnerErrorException;
25  import org.waarp.openr66.protocol.localhandler.packet.BusinessRequestPacket;
26  import org.waarp.openr66.protocol.utils.R66Future;
27  
28  /**
29   * Business Execution of a Java Task
30   * <p>
31   * Fullarg = First argument is the Java class name, Last argument is the delay.
32   */
33  public class ExecBusinessTask extends AbstractExecJavaTask {
34  
35    /**
36     * Internal Logger
37     */
38    private static final WaarpLogger logger =
39        WaarpLoggerFactory.getLogger(ExecBusinessTask.class);
40  
41    @Override
42    public final void run() {
43      if (callFromBusiness) {
44        // Business Request to validate?
45        String validate = "Validated";
46        if (isToValidate) {
47          logger.debug("DEBUG: {}", fullarg);
48          final String[] args = BLANK.split(fullarg);
49          final String operation = args[0];
50          int newdelay;
51          String argRule;
52          try {
53            newdelay = Integer.parseInt(args[args.length - 1]);
54            argRule = fullarg.substring(fullarg.indexOf(' ') + 1,
55                                        fullarg.lastIndexOf(' '));
56          } catch (final NumberFormatException e) {
57            newdelay = 0;
58            argRule = fullarg.substring(fullarg.indexOf(' ') + 1);
59          }
60          try {
61            final AbstractTask task =
62                TaskType.getTaskFromIdForBusiness(operation, argRule, newdelay,
63                                                  session);
64            if (task != null) {
65              task.run();
66              task.getFutureCompletion().awaitOrInterruptible();
67              final R66Future future = task.getFutureCompletion();
68              if (!future.isDone() || future.isFailed()) {
69                invalid();
70                return;
71              }
72              if (future.getResult() != null &&
73                  future.getResult().getOther() != null) {
74                validate = future.getResult().getOther().toString();
75              }
76            } else {
77              logger.error("ExecBusiness in error, Task invalid: " + operation);
78              invalid();
79              return;
80            }
81          } catch (final OpenR66RunnerErrorException e1) {
82            logger.error("ExecBusiness in error: " + e1);
83            invalid();
84            return;
85          }
86          final BusinessRequestPacket packet =
87              new BusinessRequestPacket(getClass().getName() + " execution ok",
88                                        0);
89          validate(packet);
90          return;
91        }
92        finalValidate(validate);
93      } else {
94        // Rule EXECJAVA based should be used instead
95        status = 2;
96        fullarg = "EXECJAVA should be used instead";
97      }
98    }
99  }