1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.waarp.openr66.server;
21
22 import org.waarp.common.database.exception.WaarpDatabaseException;
23 import org.waarp.common.database.exception.WaarpDatabaseNoConnectionException;
24 import org.waarp.common.logging.WaarpLogger;
25 import org.waarp.common.logging.WaarpLoggerFactory;
26 import org.waarp.common.logging.WaarpSlf4JLoggerFactory;
27 import org.waarp.common.utility.WaarpStringUtils;
28 import org.waarp.openr66.client.AbstractTransfer;
29 import org.waarp.openr66.configuration.FileBasedConfiguration;
30 import org.waarp.openr66.context.ErrorCode;
31 import org.waarp.openr66.context.R66FiniteDualStates;
32 import org.waarp.openr66.context.R66Result;
33 import org.waarp.openr66.database.data.DbHostAuth;
34 import org.waarp.openr66.database.data.DbTaskRunner;
35 import org.waarp.openr66.protocol.configuration.Configuration;
36 import org.waarp.openr66.protocol.localhandler.LocalChannelReference;
37 import org.waarp.openr66.protocol.localhandler.packet.LocalPacketFactory;
38 import org.waarp.openr66.protocol.localhandler.packet.ValidPacket;
39 import org.waarp.openr66.protocol.networkhandler.NetworkTransaction;
40 import org.waarp.openr66.protocol.utils.R66Future;
41
42 import java.sql.Timestamp;
43
44 import static org.waarp.common.database.DbConstant.*;
45
46
47
48
49 public class LogExport implements Runnable {
50
51
52
53 static volatile WaarpLogger logger;
54
55 protected static final String INFO_ARGS =
56 "Need at least the configuration file as first argument then optionally\n" +
57 " -purge\n" + " -clean\n" +
58 " -start timestamp in format yyyyMMddHHmmssSSS possibly truncated and where one of ':-. ' can be separators\n" +
59 " -stop timestamp in same format than start\n" +
60 "If not start and no stop are given, stop is Today Midnight (00:00:00)\n" +
61 "If start is equals or greater than stop, stop is start+24H\n" +
62 " -host host (optional)";
63
64 protected final R66Future future;
65 protected final boolean purgeLog;
66 protected final Timestamp start;
67 protected final Timestamp stop;
68 protected final boolean clean;
69 protected final NetworkTransaction networkTransaction;
70 protected DbHostAuth host;
71
72 public LogExport(final R66Future future, final boolean purgeLog,
73 final boolean clean, final Timestamp start,
74 final Timestamp stop,
75 final NetworkTransaction networkTransaction) {
76 this.future = future;
77 this.purgeLog = purgeLog;
78 this.clean = clean;
79 this.start = start;
80 this.stop = stop;
81 this.networkTransaction = networkTransaction;
82 host = Configuration.configuration.getHostSslAuth();
83 if (logger == null) {
84 logger = WaarpLoggerFactory.getLogger(LogExport.class);
85 }
86 }
87
88 public void setHost(final DbHostAuth host) {
89 this.host = host;
90 }
91
92
93
94
95
96
97 @Override
98 public void run() {
99 if (logger == null) {
100 logger = WaarpLoggerFactory.getLogger(LogExport.class);
101 }
102 final String lstart = start != null? start.toString() : null;
103 final String lstop = stop != null? stop.toString() : null;
104 final byte type = purgeLog? LocalPacketFactory.LOGPURGEPACKET :
105 LocalPacketFactory.LOGPACKET;
106 final ValidPacket valid = new ValidPacket(lstart, lstop, type);
107 final LocalChannelReference localChannelReference =
108 AbstractTransfer.tryConnect(host, future, networkTransaction);
109 if (localChannelReference == null) {
110 return;
111 }
112
113
114 if (clean &&
115 (host.getHostid().equals(Configuration.configuration.getHostId()) ||
116 host.getHostid().equals(Configuration.configuration.getHostSslId()))) {
117
118
119 try {
120 DbTaskRunner.changeFinishedToDone();
121 } catch (final WaarpDatabaseNoConnectionException e) {
122 logger.warn("Clean cannot be done {}", e.getMessage());
123 }
124 }
125 localChannelReference.sessionNewState(R66FiniteDualStates.VALIDOTHER);
126 AbstractTransfer.sendValidPacket(host, localChannelReference, valid,
127 future);
128 logger.info(
129 "Request done with " + (future.isSuccess()? "success" : "error"));
130 }
131
132 protected static boolean spurgeLog;
133 protected static Timestamp sstart;
134 protected static Timestamp sstop;
135 protected static boolean sclean;
136 protected static String stohost;
137
138 protected static boolean getParams(final String[] args) {
139 if (logger == null) {
140 logger = WaarpLoggerFactory.getLogger(LogExport.class);
141 }
142 if (args.length < 1) {
143 logger.error(INFO_ARGS);
144 return false;
145 }
146 if (!FileBasedConfiguration.setClientConfigurationFromXml(
147 Configuration.configuration, args[0])) {
148 logger.error(INFO_ARGS);
149 return false;
150 }
151 String ssstart = null;
152 String ssstop = null;
153 for (int i = 1; i < args.length; i++) {
154 if ("-purge".equalsIgnoreCase(args[i])) {
155 spurgeLog = true;
156 } else if ("-clean".equalsIgnoreCase(args[i])) {
157 sclean = true;
158 } else if ("-start".equalsIgnoreCase(args[i])) {
159 i++;
160 ssstart = args[i];
161 } else if ("-stop".equalsIgnoreCase(args[i])) {
162 i++;
163 ssstop = args[i];
164 } else if ("-host".equalsIgnoreCase(args[i])) {
165 i++;
166 stohost = args[i];
167 }
168 }
169 if (ssstart != null) {
170 final Timestamp tstart = WaarpStringUtils.fixDate(ssstart);
171 if (tstart != null) {
172 sstart = tstart;
173 }
174 }
175 if (ssstop != null) {
176 final Timestamp tstop = WaarpStringUtils.fixDate(ssstop, sstart);
177 if (tstop != null) {
178 sstop = tstop;
179 }
180 }
181 if (ssstart == null && ssstop == null) {
182 sstop = WaarpStringUtils.getTodayMidnight();
183 }
184 return true;
185 }
186
187 public static void main(final String[] args) {
188 WaarpLoggerFactory.setDefaultFactoryIfNotSame(
189 new WaarpSlf4JLoggerFactory(null));
190 if (logger == null) {
191 logger = WaarpLoggerFactory.getLogger(LogExport.class);
192 }
193 if (!getParams(args)) {
194 logger.error("Wrong initialization");
195 if (admin != null) {
196 admin.close();
197 }
198 System.exit(1);
199 }
200 final long time1 = System.currentTimeMillis();
201 final R66Future future = new R66Future(true);
202
203 Configuration.configuration.pipelineInit();
204 final NetworkTransaction networkTransaction = new NetworkTransaction();
205 try {
206 final LogExport transaction =
207 new LogExport(future, spurgeLog, sclean, sstart, sstop,
208 networkTransaction);
209 if (stohost != null) {
210 try {
211 transaction.setHost(new DbHostAuth(stohost));
212 } catch (final WaarpDatabaseException e) {
213 logger.error(
214 "LogExport in FAILURE since Host is not found: " + stohost,
215 e);
216 networkTransaction.closeAll();
217 System.exit(10);
218 }
219 } else {
220 stohost = Configuration.configuration.getHostSslId();
221 }
222 transaction.run();
223 future.awaitOrInterruptible();
224 final long time2 = System.currentTimeMillis();
225 final long delay = time2 - time1;
226 final R66Result result = future.getResult();
227 if (future.isSuccess()) {
228 if (result.getCode() == ErrorCode.Warning) {
229 logger.warn("WARNED on file: " + (result.getOther() != null?
230 ((ValidPacket) result.getOther()).getSheader() : "no file") +
231 " delay: " + delay);
232 } else {
233 logger.warn("SUCCESS on Final file: " +
234 (result.getOther() != null?
235 ((ValidPacket) result.getOther()).getSheader() :
236 "no file") + " delay: " + delay);
237 }
238 } else {
239 if (result.getCode() == ErrorCode.Warning) {
240 logger.warn("LogExport is WARNED" + " : {}",
241 future.getCause() != null?
242 future.getCause().getMessage() : "");
243 } else {
244 logger.error("LogExport in FAILURE" + " : {}",
245 future.getCause() != null?
246 future.getCause().getMessage() : "");
247 }
248 networkTransaction.closeAll();
249 System.exit(result.getCode().ordinal());
250 }
251 } finally {
252 networkTransaction.closeAll();
253 System.exit(0);
254 }
255 }
256
257 }