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.json.JsonHandler;
23  import org.waarp.common.logging.WaarpLogger;
24  import org.waarp.common.logging.WaarpLoggerFactory;
25  import org.waarp.common.utility.WaarpStringUtils;
26  import org.waarp.openr66.context.ErrorCode;
27  import org.waarp.openr66.context.R66FiniteDualStates;
28  import org.waarp.openr66.context.R66Result;
29  import org.waarp.openr66.context.R66Session;
30  import org.waarp.openr66.protocol.exception.OpenR66ProtocolPacketException;
31  import org.waarp.openr66.protocol.localhandler.LocalChannelReference;
32  import org.waarp.openr66.protocol.localhandler.packet.BusinessRequestPacket;
33  import org.waarp.openr66.protocol.localhandler.packet.ErrorPacket;
34  import org.waarp.openr66.protocol.utils.ChannelUtils;
35  
36  import java.util.regex.Pattern;
37  
38  /**
39   * Dummy Runnable Task that only logs
40   */
41  public abstract class AbstractExecJavaTask implements R66Runnable {
42    /**
43     * Internal Logger
44     */
45    private static final WaarpLogger logger =
46        WaarpLoggerFactory.getLogger(AbstractExecJavaTask.class);
47    protected static final Pattern BLANK = WaarpStringUtils.BLANK;
48  
49    protected int delay;
50    protected int status = -1;
51    protected R66Session session;
52    protected boolean waitForValidation;
53    protected boolean useLocalExec;
54  
55    protected String classname;
56    protected String fullarg;
57    protected boolean isToValidate;
58    protected boolean callFromBusiness;
59  
60    protected String finalInformation;
61  
62    /**
63     * Server side method to validate the request
64     *
65     * @param packet
66     *
67     * @return True if feedback is done
68     */
69    public final boolean validate(final BusinessRequestPacket packet) {
70      status = 0;
71      packet.validate();
72      if (callFromBusiness) {
73        final LocalChannelReference localChannelReference =
74            session.getLocalChannelReference();
75        if (localChannelReference != null) {
76          final R66Result result =
77              new R66Result(session, true, ErrorCode.CompleteOk, null);
78          localChannelReference.validateRequest(result);
79          try {
80            ChannelUtils.writeAbstractLocalPacket(localChannelReference, packet,
81                                                  true);
82          } catch (final OpenR66ProtocolPacketException ignored) {
83            // nothing
84          }
85          return true;
86        } else {
87          finalInformation = packet.toString();
88        }
89      }
90      return false;
91    }
92  
93    /**
94     * To be called by the requester when finished
95     *
96     * @param object special object to get back
97     */
98    public final void finalValidate(final Object object) {
99      status = 0;
100     if (callFromBusiness) {
101       final LocalChannelReference localChannelReference =
102           session.getLocalChannelReference();
103       if (localChannelReference != null) {
104         final R66Result result =
105             new R66Result(session, true, ErrorCode.CompleteOk, null);
106         result.setOther(object);
107         localChannelReference.validateRequest(result);
108         localChannelReference.close();
109       } else {
110         finalInformation = JsonHandler.writeAsString(object);
111       }
112     }
113   }
114 
115   /**
116    * To be used if abnormal usage is made of one Java Method
117    */
118   public final void invalid() {
119     status = 2;
120     if (!callFromBusiness) {
121       return;
122     }
123     final R66Result result =
124         new R66Result(null, session, true, ErrorCode.Unimplemented,
125                       session.getRunner());
126     final LocalChannelReference localChannelReference =
127         session.getLocalChannelReference();
128     if (localChannelReference != null) {
129       localChannelReference.sessionNewState(R66FiniteDualStates.ERROR);
130       final ErrorPacket error = new ErrorPacket("Command Incompatible",
131                                                 ErrorCode.ExternalOp.getCode(),
132                                                 ErrorPacket.FORWARDCLOSECODE);
133       try {
134         ChannelUtils.writeAbstractLocalPacket(localChannelReference, error,
135                                               false);
136       } catch (final OpenR66ProtocolPacketException ignored) {
137         // nothing
138       }
139       localChannelReference.invalidateRequest(result);
140       localChannelReference.close();
141     }
142   }
143 
144   @Override
145   public void run() {
146     if (callFromBusiness && isToValidate) {
147       // Business Request to validate?
148       final BusinessRequestPacket packet =
149           new BusinessRequestPacket(classname + ' ' + fullarg, 0);
150       validate(packet);
151     }
152     final String builder =
153         getClass().getSimpleName() + ':' + "args(" + fullarg + ')';
154     logger.warn(builder);
155     status = 0;
156   }
157 
158   @Override
159   public final void setArgs(final R66Session session,
160                             final boolean waitForValidation,
161                             final boolean useLocalExec, final int delay,
162                             final String classname, final String arg,
163                             final boolean callFromBusiness,
164                             final boolean isToValidate) {
165     this.session = session;
166     this.waitForValidation = waitForValidation;
167     this.useLocalExec = useLocalExec;
168     this.delay = delay;
169     this.classname = classname;
170     this.callFromBusiness = callFromBusiness;
171     fullarg = arg;
172     this.isToValidate = isToValidate;
173   }
174 
175   @Override
176   public final int getFinalStatus() {
177     return status;
178   }
179 
180   @Override
181   public final String toString() {
182     if (status == -1 || finalInformation == null) {
183       return getClass().getSimpleName() + ": [" + fullarg + ']';
184     } else {
185       return finalInformation;
186     }
187   }
188 
189 }