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.test;
21  
22  import org.waarp.common.command.exception.CommandAbstractException;
23  import org.waarp.common.logging.WaarpLogger;
24  import org.waarp.common.logging.WaarpLoggerFactory;
25  import org.waarp.openr66.context.filesystem.R66File;
26  import org.waarp.openr66.context.task.AbstractExecJavaTask;
27  import org.waarp.openr66.database.data.DbTaskRunner;
28  import org.waarp.openr66.protocol.exception.OpenR66ProtocolPacketException;
29  import org.waarp.openr66.protocol.localhandler.packet.BusinessRequestPacket;
30  import org.waarp.openr66.protocol.utils.ChannelUtils;
31  
32  /**
33   * Example of Java Task for ExecJava
34   * <p>
35   * 2nd argument is a numerical rank. When rank > 100 stops, else increment rank.
36   */
37  public class TestExecJavaTask extends AbstractExecJavaTask {
38  
39    /**
40     * Internal Logger
41     */
42    private static final WaarpLogger logger =
43        WaarpLoggerFactory.getLogger(TestExecJavaTask.class);
44  
45    @Override
46    public void run() {
47      if (callFromBusiness) {
48        // Business Request to validate?
49        if (isToValidate) {
50          final String[] args = BLANK.split(fullarg);
51          int rank = Integer.parseInt(args[1]);
52          rank++;
53          final BusinessRequestPacket packet = new BusinessRequestPacket(
54              getClass().getName() + " business " + rank + " final return", 0);
55          if (rank > 100) {
56            validate(packet);
57            logger.info("Will NOT close the channel: {}", rank);
58          } else {
59            logger.info("Continue: {}", rank);
60            if (session.getLocalChannelReference() != null) {
61              try {
62                ChannelUtils.writeAbstractLocalPacket(
63                    session.getLocalChannelReference(), packet, true);
64              } catch (final OpenR66ProtocolPacketException ignored) {
65                // nothing
66              }
67            }
68          }
69          status = 0;
70          return;
71        }
72        finalValidate("Validated");
73      } else {
74        // Rule EXECJAVA based
75        final R66File file = session.getFile();
76        final DbTaskRunner runner = session.getRunner();
77        if (file == null) {
78          logger.info("TestExecJavaTask No File");
79        } else {
80          try {
81            logger.info("TestExecJavaTask File: {}", file.getFile());
82          } catch (final CommandAbstractException ignored) {
83            // nothing
84          }
85        }
86        if (runner == null) {
87          logger.warn("TestExecJavaTask No Runner: " + fullarg);
88        } else {
89          logger.warn("TestExecJavaTask Runner: " + runner.toShortString());
90        }
91        status = 0;
92      }
93    }
94  }