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.server;
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.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.LocalPacketFactory;
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.channel.Channels;
46  import org.jboss.netty.logging.InternalLoggerFactory;
47  
48  /**
49   * Config Import from a local client without database connection
50   *
51   * @author Frederic Bregier
52   *
53   */
54  public class ConfigImport implements Runnable {
55      /**
56       * Internal Logger
57       */
58      static volatile GgInternalLogger logger;
59  
60      protected final R66Future future;
61      protected final String host;
62      protected final boolean hostPurge;
63      protected final String rule;
64      protected final boolean rulePurge;
65      protected final NetworkTransaction networkTransaction;
66  
67      public ConfigImport(R66Future future, boolean hostPurge, boolean rulePurge,
68              String host, String rule,
69              NetworkTransaction networkTransaction) {
70          this.future = future;
71          this.host = host;
72          this.rule = rule;
73          this.hostPurge = hostPurge;
74          this.rulePurge = rulePurge;
75          this.networkTransaction = networkTransaction;
76      }
77      /**
78       * Prior to call this method, the pipeline and NetworkTransaction must have been initialized.
79       * It is the responsibility of the caller to finish all network resources.
80       */
81      public void run() {
82          if (logger == null) {
83              logger = GgInternalLoggerFactory.getLogger(ConfigImport.class);
84          }
85          ValidPacket valid = new ValidPacket((hostPurge? "1 ":"0 ")+host, 
86                  (rulePurge? "1 ":"0 ")+rule, 
87                  LocalPacketFactory.CONFIMPORTPACKET);
88          DbHostAuth host = Configuration.configuration.HOST_SSLAUTH;
89          SocketAddress socketAddress = host.getSocketAddress();
90          boolean isSSL = host.isSsl();
91  
92          LocalChannelReference localChannelReference = networkTransaction
93              .createConnectionWithRetry(socketAddress, isSSL, future);
94          socketAddress = null;
95          if (localChannelReference == null) {
96              host = null;
97              logger.error("Cannot Connect");
98              future.setResult(new R66Result(
99                      new OpenR66ProtocolNoConnectionException("Cannot connect to server"),
100                     null, true, ErrorCode.Internal, null));
101             future.setFailure(future.getResult().exception);
102             return;
103         }
104         localChannelReference.sessionNewState(R66FiniteDualStates.VALIDOTHER);
105         try {
106             ChannelUtils.writeAbstractLocalPacket(localChannelReference, valid, false);
107         } catch (OpenR66ProtocolPacketException e) {
108             logger.error("Bad Protocol", e);
109             Channels.close(localChannelReference.getLocalChannel());
110             localChannelReference = null;
111             host = null;
112             valid = null;
113             future.setResult(new R66Result(e, null, true,
114                     ErrorCode.TransferError, null));
115             future.setFailure(e);
116             return;
117         }
118         host = null;
119         future.awaitUninterruptibly();
120         logger.debug("Request done with "+(future.isSuccess()?"success":"error"));
121         Channels.close(localChannelReference.getLocalChannel());
122         localChannelReference = null;
123     }
124 
125     protected static String shost = null;
126     protected static String srule = null;
127     protected static boolean shostpurge = false;
128     protected static boolean srulepurge = false;
129 
130     protected static boolean getParams(String [] args) {
131         if (args.length < 3) {
132             logger.error("Need at least the configuration file as first argument then at least one from\n" +
133             		"    -hosts file\n" +
134             		"    -rules file\n" +
135             		"    -purgehosts\n"+
136             		"    -purgerules");
137             return false;
138         }
139         if (! FileBasedConfiguration
140                 .setClientConfigurationFromXml(Configuration.configuration, args[0])) {
141             logger.error("Need at least the configuration file as first argument then at least one from\n" +
142                     "    -hosts file\n" +
143                     "    -rules file\n" +
144                     "    -purgehosts\n"+
145                     "    -purgerules");
146             return false;
147         }
148         for (int i = 1; i < args.length; i++) {
149             if (args[i].equalsIgnoreCase("-hosts")) {
150                 i++;
151                 if (args.length <= i) {
152                     return false;
153                 }
154                 shost = args[i];
155             } else if (args[i].equalsIgnoreCase("-rules")) {
156                 i++;
157                 if (args.length <= i) {
158                     return false;
159                 }
160                 srule = args[i];
161             } else if (args[i].equalsIgnoreCase("-purgehosts")) {
162                 shostpurge = true;
163             } else if (args[i].equalsIgnoreCase("-purgerules")) {
164                 srulepurge = true;
165             }
166         }
167         if ((shost == null) && (srule == null)) {
168             logger.error("Need at least one of -hosts - rules");
169             return false;
170         }
171         return true;
172     }
173 
174     public static void main(String[] args) {
175         InternalLoggerFactory.setDefaultFactory(new GgSlf4JLoggerFactory(null));
176         if (logger == null) {
177             logger = GgInternalLoggerFactory.getLogger(ConfigImport.class);
178         }
179         if (! getParams(args)) {
180             logger.error("Wrong initialization");
181             if (DbConstant.admin != null && DbConstant.admin.isConnected) {
182                 DbConstant.admin.close();
183             }
184             System.exit(1);
185         }
186         long time1 = System.currentTimeMillis();
187         R66Future future = new R66Future(true);
188 
189         Configuration.configuration.pipelineInit();
190         NetworkTransaction networkTransaction = new NetworkTransaction();
191         try {
192             ConfigImport transaction = new ConfigImport(future,
193                     shostpurge, srulepurge, shost, srule,
194                     networkTransaction);
195             transaction.run();
196             future.awaitUninterruptibly();
197             long time2 = System.currentTimeMillis();
198             long delay = time2 - time1;
199             R66Result result = future.getResult();
200             if (future.isSuccess()) {
201                 if (result.code == ErrorCode.Warning) {
202                     logger.warn("WARNED on import:\n    " +
203                             (result.other != null? ((ValidPacket)result.other).getSheader() :
204                                 "no import")
205                             +"\n    delay: "+delay);
206                 } else {
207                     logger.warn("SUCCESS on import:\n    " +
208                             (result.other != null? ((ValidPacket)result.other).getSheader() :
209                             "no import")
210                             +"\n    delay: "+delay);
211                 }
212             } else {
213                 if (result.code == ErrorCode.Warning) {
214                     logger.warn("Transfer is\n    WARNED", future.getCause());
215                     networkTransaction.closeAll();
216                     System.exit(result.code.ordinal());
217                 } else {
218                     logger.error("Transfer in\n    FAILURE", future.getCause());
219                     networkTransaction.closeAll();
220                     System.exit(result.code.ordinal());
221                 }
222             }
223         } finally {
224             networkTransaction.closeAll();
225         }
226     }
227 
228 }