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.client;
22  
23  import goldengate.common.logging.GgInternalLogger;
24  import goldengate.common.logging.GgInternalLoggerFactory;
25  import goldengate.common.logging.GgSlf4JLoggerFactory;
26  
27  import java.net.SocketAddress;
28  
29  import openr66.configuration.FileBasedConfiguration;
30  import openr66.context.ErrorCode;
31  import openr66.context.R66FiniteDualStates;
32  import openr66.context.authentication.R66Auth;
33  import openr66.database.DbConstant;
34  import openr66.database.data.DbHostAuth;
35  import openr66.protocol.configuration.Configuration;
36  import openr66.protocol.exception.OpenR66ProtocolNoConnectionException;
37  import openr66.protocol.exception.OpenR66ProtocolPacketException;
38  import openr66.protocol.localhandler.LocalChannelReference;
39  import openr66.protocol.localhandler.packet.BusinessRequestPacket;
40  import openr66.protocol.networkhandler.NetworkTransaction;
41  import openr66.protocol.utils.ChannelUtils;
42  import openr66.protocol.utils.R66Future;
43  
44  import org.jboss.netty.channel.Channels;
45  import org.jboss.netty.logging.InternalLoggerFactory;
46  
47  /**
48   * Abstract class for internal Business Request
49   * @author Frederic Bregier
50   *
51   */
52  public abstract class AbstractBusinessRequest implements Runnable {
53      /**
54       * Internal Logger
55       */
56      static protected volatile GgInternalLogger logger;
57      
58      public static final String BUSINESSREQUEST = "BusinessRequest";
59  
60      protected final R66Future future;
61  
62      protected final String remoteHost;
63  
64      protected final NetworkTransaction networkTransaction;
65  
66      private final BusinessRequestPacket businessPacket;
67  
68      private LocalChannelReference localChannelReference;
69  
70      public AbstractBusinessRequest(Class<?> clasz, 
71              R66Future future,
72              String remoteHost,
73              NetworkTransaction networkTransaction,
74              BusinessRequestPacket packet) {
75          if (logger == null) {
76              logger = GgInternalLoggerFactory.getLogger(clasz);
77          }
78          this.future = future;
79          this.remoteHost = remoteHost;
80          this.networkTransaction = networkTransaction;
81          this.businessPacket = packet;
82      }
83  
84      public void run() {
85          try {
86              initRequest();
87              sendRequest();
88          } catch (OpenR66ProtocolNoConnectionException e) {
89          }
90      }
91      
92      public void initRequest() throws OpenR66ProtocolNoConnectionException {
93          DbHostAuth host = R66Auth.getServerAuth(DbConstant.admin.session,
94                  remoteHost);
95          final SocketAddress socketServerAddress = host.getSocketAddress();
96          boolean isSSL = host.isSsl();
97          localChannelReference = networkTransaction
98              .createConnectionWithRetry(socketServerAddress, isSSL, future);
99          if (localChannelReference == null) {
100             future.setResult(null);
101             OpenR66ProtocolNoConnectionException e = 
102                 new OpenR66ProtocolNoConnectionException(
103                     "Cannot connect to server " + host.toString());
104             future.setFailure(e);
105             throw e;
106         }
107         localChannelReference.sessionNewState(R66FiniteDualStates.BUSINESSR);
108     }
109     
110     public void sendRequest() {
111         try {
112             ChannelUtils.writeAbstractLocalPacket(localChannelReference, businessPacket, false);
113         } catch (OpenR66ProtocolPacketException e) {
114             future.setResult(null);
115             future.setFailure(e);
116             Channels.close(localChannelReference.getLocalChannel());
117             return;
118         }
119         
120     }
121 
122     /**
123      * Dummy Main method
124      * @param args
125      */
126     public static void main(String[] args) {
127         InternalLoggerFactory.setDefaultFactory(new GgSlf4JLoggerFactory(
128                 null));
129         if (logger == null) {
130             logger = GgInternalLoggerFactory.getLogger(AbstractBusinessRequest.class);
131         }
132         if (! getParams(args)) {
133             logger.error("Wrong initialization");
134             if (DbConstant.admin != null && DbConstant.admin.isConnected) {
135                 DbConstant.admin.close();
136             }
137             ChannelUtils.stopLogger();
138             System.exit(2);
139         }
140 
141         Configuration.configuration.pipelineInit();
142         NetworkTransaction networkTransaction = new NetworkTransaction();
143         R66Future future = new R66Future(true);
144         
145         logger.info("Start Test of Transaction");
146         long time1 = System.currentTimeMillis();
147         
148         @SuppressWarnings("unused")
149         BusinessRequestPacket packet = 
150             new BusinessRequestPacket(classname+" "+classarg, 0);
151         // XXX FIXME this has to be adapted
152         /*
153         AbstractBusinessRequest transaction = new AbstractBusinessRequest(
154                 AbstractBusinessRequest.class,
155                 future, rhost, networkTransaction, packet);
156         
157         transaction.run();
158         
159         future.awaitUninterruptibly();
160         */
161         long time2 = System.currentTimeMillis();
162         logger.debug("Finish Business Request: "+future.isSuccess());
163         long delay = time2 - time1;
164         if (future.isSuccess()) {
165             logger.info("Business Request in status:\nSUCCESS"+
166                     "\n    <REMOTE>"+rhost+"</REMOTE>"+
167                     "\n    delay: "+delay);
168         } else {
169             logger.info("Business Request in status:\nFAILURE"+
170                     "\n    <REMOTE>"+rhost+"</REMOTE>"+
171                     "\n    <ERROR>"+future.getCause()+"</ERROR>"+
172                     "\n    delay: "+delay);
173                 networkTransaction.closeAll();
174                 System.exit(ErrorCode.Unknown.ordinal());
175         }
176         networkTransaction.closeAll();
177     }
178 
179 
180     static protected String rhost = null;
181     static protected String classname = null;
182     static protected String classarg = null;
183     static protected boolean nolog = false;
184     
185     /**
186      * Parse the parameter and set current values
187      * @param args
188      * @return True if all parameters were found and correct
189      */
190     protected static boolean getParams(String []args) {
191         if (args.length < 5) {
192             logger
193                     .error("Needs at least 3 or 4 arguments:\n" +
194                                 "  the XML client configuration file,\n" +
195                                 "  '-to' the remoteHost Id,\n" +
196                                 "  '-class' the Business full class name,\n" +
197                                 "  '-arg' the argument to pass (optional)\n"+
198                                 "Other options:\n" +
199                                 "  '-nolog' to not log locally this action\n");
200             return false;
201         }
202         if (! FileBasedConfiguration
203                 .setClientConfigurationFromXml(Configuration.configuration, args[0])) {
204             logger
205                     .error("Needs a correct configuration file as first argument");
206             return false;
207         }
208         // Now set default values from configuration
209         for (int i = 1; i < args.length; i++) {
210             if (args[i].equalsIgnoreCase("-to")) {
211                 i++;
212                 rhost = args[i];
213             } else if (args[i].equalsIgnoreCase("-class")) {
214                 i++;
215                 classname = args[i];
216             } else if (args[i].equalsIgnoreCase("-arg")) {
217                 i++;
218                 classarg = args[i];
219             } else if (args[i].equalsIgnoreCase("-nolog")) {
220                 nolog = true;
221                 i++;
222             }
223         }
224         if (rhost != null && classname != null) {
225             return true;
226         }
227         logger.error("All params are not set! Need at least (-to -class)");
228         return false;
229     }
230 
231 }