1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
50
51
52
53
54 public class ConfigExport implements Runnable {
55
56
57
58 static volatile GgInternalLogger logger;
59
60 protected final R66Future future;
61 protected final boolean host;
62 protected final boolean rule;
63 protected final NetworkTransaction networkTransaction;
64
65 public ConfigExport(R66Future future, boolean host, boolean rule,
66 NetworkTransaction networkTransaction) {
67 this.future = future;
68 this.host = host;
69 this.rule = rule;
70 this.networkTransaction = networkTransaction;
71 }
72
73
74
75
76 public void run() {
77 if (logger == null) {
78 logger = GgInternalLoggerFactory.getLogger(ConfigExport.class);
79 }
80 ValidPacket valid = new ValidPacket(Boolean.toString(host), Boolean.toString(rule),
81 LocalPacketFactory.CONFEXPORTPACKET);
82 DbHostAuth host = Configuration.configuration.HOST_SSLAUTH;
83 SocketAddress socketAddress = host.getSocketAddress();
84 boolean isSSL = host.isSsl();
85
86 LocalChannelReference localChannelReference = networkTransaction
87 .createConnectionWithRetry(socketAddress, isSSL, future);
88 socketAddress = null;
89 if (localChannelReference == null) {
90 host = null;
91 logger.error("Cannot Connect");
92 future.setResult(new R66Result(
93 new OpenR66ProtocolNoConnectionException("Cannot connect to server"),
94 null, true, ErrorCode.Internal, null));
95 future.setFailure(future.getResult().exception);
96 return;
97 }
98 localChannelReference.sessionNewState(R66FiniteDualStates.VALIDOTHER);
99 try {
100 ChannelUtils.writeAbstractLocalPacket(localChannelReference, valid, false);
101 } catch (OpenR66ProtocolPacketException e) {
102 logger.error("Bad Protocol", e);
103 Channels.close(localChannelReference.getLocalChannel());
104 localChannelReference = null;
105 host = null;
106 valid = null;
107 future.setResult(new R66Result(e, null, true,
108 ErrorCode.TransferError, null));
109 future.setFailure(e);
110 return;
111 }
112 host = null;
113 future.awaitUninterruptibly();
114 logger.info("Request done with "+(future.isSuccess()?"success":"error"));
115 Channels.close(localChannelReference.getLocalChannel());
116 localChannelReference = null;
117 }
118
119 protected static boolean shost = false;
120 protected static boolean srule = false;
121
122 protected static boolean getParams(String [] args) {
123 if (args.length < 2) {
124 logger.error("Need at least the configuration file as first argument then at least one from\n" +
125 " -hosts\n" +
126 " -rules");
127 return false;
128 }
129 if (! FileBasedConfiguration
130 .setClientConfigurationFromXml(Configuration.configuration, args[0])) {
131 logger.error("Need at least the configuration file as first argument then at least one from\n" +
132 " -hosts\n" +
133 " -rules");
134 return false;
135 }
136 for (int i = 1; i < args.length; i++) {
137 if (args[i].equalsIgnoreCase("-hosts")) {
138 shost = true;
139 } else if (args[i].equalsIgnoreCase("-rules")) {
140 srule = true;
141 }
142 }
143 if ((!shost) && (!srule)) {
144 logger.error("Need at least one of -hosts - rules");
145 return false;
146 }
147 return true;
148 }
149
150 public static void main(String[] args) {
151 InternalLoggerFactory.setDefaultFactory(new GgSlf4JLoggerFactory(null));
152 if (logger == null) {
153 logger = GgInternalLoggerFactory.getLogger(ConfigExport.class);
154 }
155 if (! getParams(args)) {
156 logger.error("Wrong initialization");
157 if (DbConstant.admin != null && DbConstant.admin.isConnected) {
158 DbConstant.admin.close();
159 }
160 System.exit(1);
161 }
162 long time1 = System.currentTimeMillis();
163 R66Future future = new R66Future(true);
164
165 Configuration.configuration.pipelineInit();
166 NetworkTransaction networkTransaction = new NetworkTransaction();
167 try {
168 ConfigExport transaction = new ConfigExport(future,
169 shost, srule,
170 networkTransaction);
171 transaction.run();
172 future.awaitUninterruptibly();
173 long time2 = System.currentTimeMillis();
174 long delay = time2 - time1;
175 R66Result result = future.getResult();
176 if (future.isSuccess()) {
177 if (result.code == ErrorCode.Warning) {
178 logger.warn("WARNED on files:\n " +
179 (result.other != null? ((ValidPacket)result.other).getSheader() :
180 "no file")
181 +"\n delay: "+delay);
182 } else {
183 logger.warn("SUCCESS on Final files:\n " +
184 (result.other != null? ((ValidPacket)result.other).getSheader() :
185 "no file")
186 +"\n delay: "+delay);
187 }
188 } else {
189 if (result.code == ErrorCode.Warning) {
190 logger.warn("Transfer is\n WARNED", future.getCause());
191 networkTransaction.closeAll();
192 System.exit(result.code.ordinal());
193 } else {
194 logger.error("Transfer in\n FAILURE", future.getCause());
195 networkTransaction.closeAll();
196 System.exit(result.code.ordinal());
197 }
198 }
199 } finally {
200 networkTransaction.closeAll();
201 }
202 }
203
204 }