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.R66Result;
33  import openr66.context.authentication.R66Auth;
34  import openr66.database.DbConstant;
35  import openr66.database.data.DbHostAuth;
36  import openr66.protocol.configuration.Configuration;
37  import openr66.protocol.exception.OpenR66ProtocolPacketException;
38  import openr66.protocol.localhandler.LocalChannelReference;
39  import openr66.protocol.localhandler.packet.InformationPacket;
40  import openr66.protocol.localhandler.packet.ValidPacket;
41  import openr66.protocol.networkhandler.NetworkTransaction;
42  import openr66.protocol.utils.ChannelUtils;
43  import openr66.protocol.utils.R66Future;
44  
45  import org.jboss.netty.logging.InternalLoggerFactory;
46  
47  /**
48   * Class to request information on remote files
49   *
50   * @author Frederic Bregier
51   *
52   */
53  public class RequestInformation implements Runnable {
54      /**
55       * Internal Logger
56       */
57      static volatile GgInternalLogger logger;
58  
59      protected final NetworkTransaction networkTransaction;
60      final R66Future future;
61      String requested = null;
62      String filename = null;
63      String rulename = null;
64      byte code;
65  
66      static String srequested = null;
67      static String sfilename = null;
68      static String srulename = null;
69      static byte scode = 0;
70  
71      /**
72       * Parse the parameter and set current values
73       * @param args
74       * @return True if all parameters were found and correct
75       */
76      protected static boolean getParams(String []args) {
77          if (args.length < 5) {
78              logger
79                      .error("Needs at least 3 arguments:\n" +
80                              "  the XML client configuration file,\n" +
81                              "  '-to' the remoteHost Id,\n" +
82                              "  '-rule' the rule\n"+
83                              "Other options:\n" +
84                              "  '-file' the optional file for which to get info,\n" +
85                              "  '-exist' to test the existence\n" +
86                              "  '-detail' to get the detail on file\n" +
87                              "  '-list' to get the list of files\n" +
88                              "  '-mlsx' to get the list and details of files");
89              return false;
90          }
91          if (! FileBasedConfiguration
92                  .setClientConfigurationFromXml(Configuration.configuration, args[0])) {
93              logger
94                      .error("Needs a correct configuration file as first argument");
95              return false;
96          }
97          for (int i = 1; i < args.length; i++) {
98              if (args[i].equalsIgnoreCase("-to")) {
99                  i++;
100                 srequested = args[i];
101             } else if (args[i].equalsIgnoreCase("-file")) {
102                 i++;
103                 sfilename = args[i];
104             } else if (args[i].equalsIgnoreCase("-rule")) {
105                 i++;
106                 srulename = args[i];
107             } else if (args[i].equalsIgnoreCase("-exist")) {
108                 scode = (byte) InformationPacket.ASKENUM.ASKEXIST.ordinal();
109             } else if (args[i].equalsIgnoreCase("-detail")) {
110                 scode = (byte) InformationPacket.ASKENUM.ASKMLSDETAIL.ordinal();
111             } else if (args[i].equalsIgnoreCase("-list")) {
112                 scode = (byte) InformationPacket.ASKENUM.ASKLIST.ordinal();
113             } else if (args[i].equalsIgnoreCase("-mlsx")) {
114                 scode = (byte) InformationPacket.ASKENUM.ASKMLSLIST.ordinal();
115             }
116         }
117         if (srulename == null || srequested == null) {
118             logger.error("Rulename and Requested HostId must be set");
119             return false;
120         }
121 
122         return true;
123     }
124 
125 
126     /**
127      * @param future
128      * @param requested
129      * @param rulename
130      * @param filename
131      * @param request
132      * @param networkTransaction
133      */
134     public RequestInformation(R66Future future, String requested, String rulename,
135             String filename, byte request,
136             NetworkTransaction networkTransaction) {
137         this.future = future;
138         this.rulename = rulename;
139         this.requested = requested;
140         this.filename = filename;
141         this.code = request;
142         this.networkTransaction = networkTransaction;
143     }
144 
145 
146     public void run() {
147         if (logger == null) {
148             logger = GgInternalLoggerFactory.getLogger(RequestInformation.class);
149         }
150         InformationPacket request = new InformationPacket(rulename, code,
151                 filename);
152 
153         // Connection
154         DbHostAuth host = R66Auth.getServerAuth(DbConstant.admin.session,
155                 requested);
156         if (host == null) {
157             logger.error("Requested host cannot be found: "+requested);
158             R66Result result = new R66Result(null, true, ErrorCode.ConnectionImpossible, null);
159             this.future.setResult(result);
160             this.future.cancel();
161             return;
162         }
163         if (host.isClient()) {
164             logger.error("Requested host is a client and cannot be requested: "+requested);
165             R66Result result = new R66Result(null, true, ErrorCode.ConnectionImpossible, null);
166             this.future.setResult(result);
167             this.future.cancel();
168             return;
169         }
170         SocketAddress socketAddress = host.getSocketAddress();
171         boolean isSSL = host.isSsl();
172 
173         LocalChannelReference localChannelReference = networkTransaction
174             .createConnectionWithRetry(socketAddress, isSSL, future);
175         socketAddress = null;
176         if (localChannelReference == null) {
177             logger.error("Cannot connect to server: "+requested);
178             R66Result result = new R66Result(null, true, ErrorCode.ConnectionImpossible, null);
179             this.future.setResult(result);
180             this.future.cancel();
181             return;
182         }
183         localChannelReference.sessionNewState(R66FiniteDualStates.INFORMATION);
184         try {
185             ChannelUtils.writeAbstractLocalPacket(localChannelReference, request, false);
186         } catch (OpenR66ProtocolPacketException e) {
187             logger.error("Cannot write request");
188             R66Result result = new R66Result(null, true, ErrorCode.TransferError, null);
189             this.future.setResult(result);
190             this.future.cancel();
191             return;
192         }
193         localChannelReference.getFutureRequest().awaitUninterruptibly();
194     }
195 
196     /**
197      * @param args
198      */
199     public static void main(String[] args) {
200         InternalLoggerFactory.setDefaultFactory(new GgSlf4JLoggerFactory(null));
201         if (logger == null) {
202             logger = GgInternalLoggerFactory.getLogger(RequestInformation.class);
203         }
204         if (! getParams(args)) {
205             logger.error("Wrong initialization");
206             if (DbConstant.admin != null && DbConstant.admin.isConnected) {
207                 DbConstant.admin.close();
208             }
209             ChannelUtils.stopLogger();
210             System.exit(1);
211         }
212         NetworkTransaction networkTransaction = null;
213         int value = 3;
214         try {
215             Configuration.configuration.pipelineInit();
216             networkTransaction = new NetworkTransaction();
217             R66Future result = new R66Future(true);
218             RequestInformation requestInformation =
219                 new RequestInformation(result, srequested, srulename,
220                         sfilename, scode,
221                         networkTransaction);
222             requestInformation.run();
223             result.awaitUninterruptibly();
224             if (result.isSuccess()) {
225                 value = 0;
226                 R66Result r66result = result.getResult();
227                 ValidPacket info = (ValidPacket) r66result.other;
228                 logger.warn("SUCCESS\n    "+info.getSmiddle()+"\n    "+info.getSheader());
229             } else {
230                 value = 2;
231                 logger.error("FAILURE\n    " +
232                         result.getResult().toString());
233             }
234 
235         } finally {
236             if (networkTransaction != null) {
237                 networkTransaction.closeAll();
238             }
239             if (DbConstant.admin != null) {
240                 DbConstant.admin.close();
241             }
242             System.exit(value);
243         }
244     }
245 
246 }