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.logging.WaarpLogger;
24 import org.waarp.common.logging.WaarpLoggerFactory;
25 import org.waarp.common.logging.WaarpSlf4JLoggerFactory;
26 import org.waarp.openr66.client.AbstractTransfer;
27 import org.waarp.openr66.configuration.FileBasedConfiguration;
28 import org.waarp.openr66.context.ErrorCode;
29 import org.waarp.openr66.context.R66FiniteDualStates;
30 import org.waarp.openr66.context.R66Result;
31 import org.waarp.openr66.database.data.DbHostAuth;
32 import org.waarp.openr66.protocol.configuration.Configuration;
33 import org.waarp.openr66.protocol.configuration.PartnerConfiguration;
34 import org.waarp.openr66.protocol.localhandler.LocalChannelReference;
35 import org.waarp.openr66.protocol.localhandler.packet.AbstractLocalPacket;
36 import org.waarp.openr66.protocol.localhandler.packet.JsonCommandPacket;
37 import org.waarp.openr66.protocol.localhandler.packet.LocalPacketFactory;
38 import org.waarp.openr66.protocol.localhandler.packet.ValidPacket;
39 import org.waarp.openr66.protocol.localhandler.packet.json.ConfigImportJsonPacket;
40 import org.waarp.openr66.protocol.networkhandler.NetworkTransaction;
41 import org.waarp.openr66.protocol.utils.R66Future;
42
43 import static org.waarp.common.database.DbConstant.*;
44
45
46
47
48 public class ConfigImport implements Runnable {
49
50
51
52 static volatile WaarpLogger logger;
53
54 protected static final String INFO_ARGS =
55 "Need at least the configuration file as first argument then at least one from\n" +
56 " -hosts file\n" + " -rules file\n" +
57 " -business file (if compatible)\n" +
58 " -alias file (if compatible)\n" +
59 " -roles file (if compatible)\n" + " -purgehosts\n" +
60 " -purgerules\n" + " -purgebusiness (if compatible)\n" +
61 " -purgealias (if compatible)\n" +
62 " -purgeroles (if compatible)\n" +
63 " -hostid file transfer id (if compatible)\n" +
64 " -ruleid file transfer id (if compatible)\n" +
65 " -businessid file transfer id (if compatible)\n" +
66 " -aliasid file transfer id (if compatible)\n" +
67 " -roleid file transfer id (if compatible)\n" +
68 " -host host (optional)";
69
70 protected final R66Future future;
71 protected final String host;
72 protected final boolean hostPurge;
73 protected final String rule;
74 protected final boolean rulePurge;
75 protected final String business;
76 protected final boolean businessPurge;
77 protected final String alias;
78 protected final boolean aliasPurge;
79 protected final String role;
80 protected final boolean rolePurge;
81 protected long hostid = ILLEGALVALUE;
82 protected long ruleid = ILLEGALVALUE;
83 protected long businessid = ILLEGALVALUE;
84 protected long aliasid = ILLEGALVALUE;
85 protected long roleid = ILLEGALVALUE;
86 protected final NetworkTransaction networkTransaction;
87 protected DbHostAuth dbhost;
88
89 public ConfigImport(final R66Future future, final boolean hostPurge,
90 final boolean rulePurge, final String host,
91 final String rule,
92 final NetworkTransaction networkTransaction) {
93 this.future = future;
94 this.host = host;
95 this.rule = rule;
96 this.hostPurge = hostPurge;
97 this.rulePurge = rulePurge;
98 business = null;
99 businessPurge = false;
100 alias = null;
101 aliasPurge = false;
102 role = null;
103 rolePurge = false;
104 this.networkTransaction = networkTransaction;
105 dbhost = Configuration.configuration.getHostSslAuth();
106 if (logger == null) {
107 logger = WaarpLoggerFactory.getLogger(ConfigImport.class);
108 }
109 }
110
111 public ConfigImport(final R66Future future, final boolean hostPurge,
112 final boolean rulePurge, final boolean businessPurge,
113 final boolean aliasPurge, final boolean rolePurge,
114 final String host, final String rule,
115 final String business, final String alias,
116 final String role,
117 final NetworkTransaction networkTransaction) {
118 this.future = future;
119 this.host = host;
120 this.rule = rule;
121 this.hostPurge = hostPurge;
122 this.rulePurge = rulePurge;
123 this.business = business;
124 this.businessPurge = businessPurge;
125 this.alias = alias;
126 this.aliasPurge = aliasPurge;
127 this.role = role;
128 this.rolePurge = rolePurge;
129 this.networkTransaction = networkTransaction;
130 dbhost = Configuration.configuration.getHostSslAuth();
131 if (logger == null) {
132 logger = WaarpLoggerFactory.getLogger(ConfigImport.class);
133 }
134 }
135
136
137
138
139
140
141
142
143
144
145 public final void setSpecialIds(final long hostid, final long ruleid,
146 final long businessid, final long aliasid,
147 final long roleid) {
148 this.hostid = hostid;
149 this.ruleid = ruleid;
150 this.businessid = businessid;
151 this.aliasid = aliasid;
152 this.roleid = roleid;
153 }
154
155 public final void setHost(final DbHostAuth host) {
156 dbhost = host;
157 }
158
159
160
161
162
163
164 @Override
165 public void run() {
166 if (logger == null) {
167 logger = WaarpLoggerFactory.getLogger(ConfigImport.class);
168 }
169 final LocalChannelReference localChannelReference =
170 AbstractTransfer.tryConnect(dbhost, future, networkTransaction);
171 if (localChannelReference == null) {
172 return;
173 }
174 localChannelReference.sessionNewState(R66FiniteDualStates.VALIDOTHER);
175 final boolean useJson = PartnerConfiguration.useJson(dbhost.getHostid());
176 logger.debug("UseJson: {}", useJson);
177 final AbstractLocalPacket valid;
178 if (useJson) {
179 final ConfigImportJsonPacket node = new ConfigImportJsonPacket();
180 node.setHost(host);
181 node.setRule(rule);
182 node.setBusiness(business);
183 node.setAlias(alias);
184 node.setRoles(role);
185 node.setPurgehost(hostPurge);
186 node.setPurgerule(rulePurge);
187 node.setPurgebusiness(businessPurge);
188 node.setPurgealias(aliasPurge);
189 node.setPurgeroles(rolePurge);
190 node.setHostid(hostid);
191 node.setRuleid(ruleid);
192 node.setBusinessid(businessid);
193 node.setAliasid(aliasid);
194 node.setRolesid(roleid);
195 valid = new JsonCommandPacket(node, LocalPacketFactory.CONFIMPORTPACKET);
196 } else {
197 valid = new ValidPacket((hostPurge? "1 " : "0 ") + host,
198 (rulePurge? "1 " : "0 ") + rule,
199 LocalPacketFactory.CONFIMPORTPACKET);
200 }
201 AbstractTransfer.sendValidPacket(dbhost, localChannelReference, valid,
202 future);
203 logger.debug("Request done with {}",
204 (future.isSuccess()? "success" : "error"));
205 }
206
207 protected static String shost;
208 protected static String srule;
209 protected static String sbusiness;
210 protected static String salias;
211 protected static String srole;
212 protected static boolean shostpurge;
213 protected static boolean srulepurge;
214 protected static boolean sbusinesspurge;
215 protected static boolean saliaspurge;
216 protected static boolean srolepurge;
217 protected static String stohost;
218 protected static long lhost = ILLEGALVALUE;
219 protected static long lrule = ILLEGALVALUE;
220 protected static long lbusiness = ILLEGALVALUE;
221 protected static long lalias = ILLEGALVALUE;
222 protected static long lrole = ILLEGALVALUE;
223
224 protected static boolean getParams(final String[] args) {
225 if (logger == null) {
226 logger = WaarpLoggerFactory.getLogger(ConfigImport.class);
227 }
228 if (args.length < 3) {
229 logger.error(INFO_ARGS);
230 return false;
231 }
232 if (!FileBasedConfiguration.setClientConfigurationFromXml(
233 Configuration.configuration, args[0])) {
234 logger.error(INFO_ARGS);
235 return false;
236 }
237 for (int i = 1; i < args.length; i++) {
238 if ("-hosts".equalsIgnoreCase(args[i])) {
239 i++;
240 if (args.length <= i) {
241 return false;
242 }
243 shost = args[i];
244 } else if ("-rules".equalsIgnoreCase(args[i])) {
245 i++;
246 if (args.length <= i) {
247 return false;
248 }
249 srule = args[i];
250 } else if ("-business".equalsIgnoreCase(args[i])) {
251 i++;
252 if (args.length <= i) {
253 return false;
254 }
255 sbusiness = args[i];
256 } else if ("-alias".equalsIgnoreCase(args[i])) {
257 i++;
258 if (args.length <= i) {
259 return false;
260 }
261 salias = args[i];
262 } else if ("-roles".equalsIgnoreCase(args[i])) {
263 i++;
264 if (args.length <= i) {
265 return false;
266 }
267 srole = args[i];
268 } else if ("-purgehosts".equalsIgnoreCase(args[i])) {
269 shostpurge = true;
270 } else if ("-purgerules".equalsIgnoreCase(args[i])) {
271 srulepurge = true;
272 } else if ("-purgebusiness".equalsIgnoreCase(args[i])) {
273 sbusinesspurge = true;
274 } else if ("-purgealias".equalsIgnoreCase(args[i])) {
275 saliaspurge = true;
276 } else if ("-purgeroles".equalsIgnoreCase(args[i])) {
277 srolepurge = true;
278 } else if ("-host".equalsIgnoreCase(args[i])) {
279 i++;
280 stohost = args[i];
281 } else if ("-hostid".equalsIgnoreCase(args[i])) {
282 i++;
283 if (args.length <= i) {
284 return false;
285 }
286 try {
287 lhost = Long.parseLong(args[i]);
288 } catch (final NumberFormatException e) {
289 return false;
290 }
291 } else if ("-ruleid".equalsIgnoreCase(args[i])) {
292 i++;
293 if (args.length <= i) {
294 return false;
295 }
296 try {
297 lrule = Long.parseLong(args[i]);
298 } catch (final NumberFormatException e) {
299 return false;
300 }
301 } else if ("-businessid".equalsIgnoreCase(args[i])) {
302 i++;
303 if (args.length <= i) {
304 return false;
305 }
306 try {
307 lbusiness = Long.parseLong(args[i]);
308 } catch (final NumberFormatException e) {
309 return false;
310 }
311 } else if ("-aliasid".equalsIgnoreCase(args[i])) {
312 i++;
313 if (args.length <= i) {
314 return false;
315 }
316 try {
317 lalias = Long.parseLong(args[i]);
318 } catch (final NumberFormatException e) {
319 return false;
320 }
321 } else if ("-roleid".equalsIgnoreCase(args[i])) {
322 i++;
323 if (args.length <= i) {
324 return false;
325 }
326 try {
327 lrole = Long.parseLong(args[i]);
328 } catch (final NumberFormatException e) {
329 return false;
330 }
331 }
332 }
333 return true;
334 }
335
336 public static void main(final String[] args) {
337 WaarpLoggerFactory.setDefaultFactoryIfNotSame(
338 new WaarpSlf4JLoggerFactory(null));
339 if (logger == null) {
340 logger = WaarpLoggerFactory.getLogger(ConfigImport.class);
341 }
342 if (!getParams(args)) {
343 logger.error("Wrong initialization");
344 if (admin != null) {
345 admin.close();
346 }
347 System.exit(1);
348 }
349 final long time1 = System.currentTimeMillis();
350 final R66Future future = new R66Future(true);
351
352 Configuration.configuration.pipelineInit();
353 final NetworkTransaction networkTransaction = new NetworkTransaction();
354 try {
355 final ConfigImport transaction =
356 new ConfigImport(future, shostpurge, srulepurge, sbusinesspurge,
357 saliaspurge, srolepurge, shost, srule, sbusiness,
358 salias, srole, networkTransaction);
359 transaction.setSpecialIds(lhost, lrule, lbusiness, lalias, lrole);
360 if (stohost != null) {
361 try {
362 transaction.setHost(new DbHostAuth(stohost));
363 } catch (final WaarpDatabaseException e) {
364 logger.error(
365 "ConfigImport in FAILURE since Host is not found: " + stohost,
366 e);
367 networkTransaction.closeAll();
368 System.exit(10);
369 }
370 } else {
371 stohost = Configuration.configuration.getHostSslId();
372 }
373 transaction.run();
374 future.awaitOrInterruptible();
375 final long time2 = System.currentTimeMillis();
376 final long delay = time2 - time1;
377 final R66Result result = future.getResult();
378 if (future.isSuccess()) {
379 final boolean useJson = PartnerConfiguration.useJson(stohost);
380 logger.debug("UseJson: {}", useJson);
381 final String message;
382 if (useJson) {
383 message = result.getOther() != null?
384 ((JsonCommandPacket) result.getOther()).getRequest() : "no file";
385 } else {
386 message = result.getOther() != null?
387 ((ValidPacket) result.getOther()).getSheader() : "no file";
388 }
389 if (result.getCode() == ErrorCode.Warning) {
390 logger.warn(
391 "WARNED on import: " + message + " delay: " + delay);
392 } else {
393 logger.warn(
394 "SUCCESS on import: " + message + " delay: " + delay);
395 }
396 } else {
397 if (result.getCode() == ErrorCode.Warning) {
398 logger.warn("ConfigImport is WARNED" + " : {}",
399 future.getCause() != null?
400 future.getCause().getMessage() : "");
401 } else {
402 logger.error("ConfigImport in FAILURE" + " : {}",
403 future.getCause() != null?
404 future.getCause().getMessage() : "");
405 }
406 networkTransaction.closeAll();
407 System.exit(result.getCode().ordinal());
408 }
409 } finally {
410 networkTransaction.closeAll();
411 System.exit(0);
412 }
413 }
414
415 }