View Javadoc
1   /**
2      This file is part of GoldenGate Project (named also GoldenGate or GG).
3   
4      Copyright 2009, Frederic Bregier, 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 GoldenGate Project is free software: you can redistribute it and/or 
9      modify it under the terms of the GNU General Public License as published 
10     by the Free Software Foundation, either version 3 of the License, or
11     (at your option) any later version.
12  
13     GoldenGate is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17  
18     You should have received a copy of the GNU General Public License
19     along with GoldenGate .  If not, see <http://www.gnu.org/licenses/>.
20   */
21  package openr66.context.task;
22  
23  import goldengate.common.logging.GgInternalLogger;
24  import goldengate.common.logging.GgInternalLoggerFactory;
25  import openr66.client.AbstractBusinessRequest;
26  import openr66.context.ErrorCode;
27  import openr66.context.R66FiniteDualStates;
28  import openr66.context.R66Result;
29  import openr66.context.R66Session;
30  import openr66.context.task.R66Runnable;
31  import openr66.protocol.exception.OpenR66ProtocolPacketException;
32  import openr66.protocol.localhandler.LocalChannelReference;
33  import openr66.protocol.localhandler.packet.BusinessRequestPacket;
34  import openr66.protocol.localhandler.packet.ErrorPacket;
35  import openr66.protocol.utils.ChannelUtils;
36  
37  /**
38   * Dummy Runnable Task that only logs
39   * 
40   * @author Frederic Bregier
41   *
42   */
43  public abstract class AbstractExecJavaTask implements R66Runnable {
44      /**
45       * Internal Logger
46       */
47      private static final GgInternalLogger logger = GgInternalLoggerFactory
48              .getLogger(AbstractExecJavaTask.class);
49      
50      protected int delay;
51      protected String[] args = null;
52      protected int status = -1;
53      protected R66Session session;
54      protected boolean waitForValidation;
55      protected boolean useLocalExec;
56  
57      protected String classname;
58      protected String fullarg;
59      protected boolean isToValidate;
60      protected boolean callFromBusiness;
61      
62      /**
63       * Server side methode to validate the request
64       * @param packet
65       */
66      public void validate(BusinessRequestPacket packet) {
67          this.status = 0;
68          packet.validate();
69          if (callFromBusiness) {
70              R66Result result = new R66Result(session, true,
71                  ErrorCode.CompleteOk, null);
72              session.getLocalChannelReference().validateRequest(result);
73              try {
74                  ChannelUtils.writeAbstractLocalPacket(session.getLocalChannelReference(),
75                          packet, true);
76              } catch (OpenR66ProtocolPacketException e) {
77              }
78          }
79      }
80      
81      /**
82       * To be called by the requester when finished
83       * @param object special object to get back
84       */
85      public void finalValidate(Object object) {
86          this.status = 0;
87          if (callFromBusiness) {
88              R66Result result = new R66Result(session, true,
89                      ErrorCode.CompleteOk, null);
90              result.other = object;
91              session.getLocalChannelReference().validateRequest(result);
92              ChannelUtils.close(session.getLocalChannelReference().getLocalChannel());
93          }
94      }
95  
96      /**
97       * To be used if abnormal usage is made of one Java Method
98       */
99      public void invalid() {
100         this.status = 2;
101         if (!callFromBusiness) {
102             return;
103         }
104         R66Result result = new R66Result(null, session, true,
105                 ErrorCode.Unimplemented, session.getRunner());
106         LocalChannelReference localChannelReference = session.getLocalChannelReference();
107         if (localChannelReference != null) {
108             localChannelReference.sessionNewState(R66FiniteDualStates.ERROR);
109             ErrorPacket error = new ErrorPacket("Command Incompatible",
110                 ErrorCode.ExternalOp.getCode(), ErrorPacket.FORWARDCLOSECODE);
111             try {
112                 ChannelUtils.writeAbstractLocalPacket(localChannelReference, error, true);
113             } catch (OpenR66ProtocolPacketException e1) {
114             }
115             localChannelReference.invalidateRequest(result);
116             ChannelUtils.close(localChannelReference.getLocalChannel());
117         }
118     }
119 
120     @Override
121     public void run() {
122         if (callFromBusiness) {
123             // Business Request to validate?
124             if (isToValidate) {
125                 BusinessRequestPacket packet = 
126                     new BusinessRequestPacket(this.classname+" "+this.fullarg, 0);
127                 validate(packet);
128             }
129         }
130         StringBuilder builder = new StringBuilder(this.getClass().getSimpleName()+":");
131         for (int i = 0; i < args.length; i++) {
132             builder.append(' ');
133             builder.append(args[i]);
134         }
135         logger.warn(builder.toString());
136         this.status = 0;
137     }
138 
139     @Override
140     public void setArgs(R66Session session, boolean waitForValidation, 
141             boolean useLocalExec, int delay, String []args) {
142         this.session = session;
143         this.waitForValidation = waitForValidation;
144         this.useLocalExec = useLocalExec;
145         this.delay = delay;
146         this.args = args;
147         this.classname = args[0];
148         if (args.length > 2) {
149             callFromBusiness = this.args[this.args.length-2].
150                 equals(AbstractBusinessRequest.BUSINESSREQUEST);
151         }
152         if (callFromBusiness) {
153             isToValidate = Boolean.parseBoolean(this.args[this.args.length-1]);
154             StringBuilder builder = new StringBuilder(args[1]);
155             for (int i = 2; i < args.length-2; i++) {
156                 builder.append(' ');
157                 builder.append(args[i]);
158             }
159             fullarg = builder.toString();
160         } else {
161             StringBuilder builder = new StringBuilder(args[1]);
162             for (int i = 2; i < args.length; i++) {
163                 builder.append(' ');
164                 builder.append(args[i]);
165             }
166             fullarg = builder.toString();
167         }
168     }
169 
170     @Override
171     public int getFinalStatus() {
172         return status;
173     }
174 
175     @Override
176     public String toString() {
177         StringBuilder builder = new StringBuilder(this.getClass().getSimpleName()+": [");
178         builder.append(args[0]);
179         builder.append(']');
180         for (int i = 1; i < args.length ; i++) {
181             builder.append(' ');
182             builder.append(args[i]);
183         }
184         return builder.toString();
185     }
186 
187 }