1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
40
41 public abstract class AbstractExecJavaTask implements R66Runnable {
42
43
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
64
65
66
67
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
84 }
85 return true;
86 } else {
87 finalInformation = packet.toString();
88 }
89 }
90 return false;
91 }
92
93
94
95
96
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
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
138 }
139 localChannelReference.invalidateRequest(result);
140 localChannelReference.close();
141 }
142 }
143
144 @Override
145 public void run() {
146 if (callFromBusiness && isToValidate) {
147
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 }