1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.waarp.ftp.core.config;
21
22 import io.netty.channel.Channel;
23 import io.netty.handler.traffic.GlobalChannelTrafficShapingHandler;
24 import io.netty.util.AttributeKey;
25 import org.waarp.common.file.FileParameterInterface;
26 import org.waarp.common.logging.SysErrLogger;
27 import org.waarp.common.logging.WaarpLogger;
28 import org.waarp.common.logging.WaarpLoggerFactory;
29 import org.waarp.common.utility.WaarpShutdownHook.ShutdownConfiguration;
30 import org.waarp.ftp.core.control.BusinessHandler;
31 import org.waarp.ftp.core.data.handler.DataBusinessHandler;
32 import org.waarp.ftp.core.exception.FtpNoConnectionException;
33 import org.waarp.ftp.core.exception.FtpUnknownFieldException;
34 import org.waarp.ftp.core.session.FtpSession;
35
36 import java.io.File;
37 import java.net.InetAddress;
38 import java.net.InetSocketAddress;
39 import java.util.HashMap;
40 import java.util.concurrent.locks.ReentrantLock;
41
42
43
44
45 public abstract class FtpConfiguration {
46 public static final String FTP_SESSION_ATTRIBUTE_KEY_NAME = "FtpSession";
47 public static final AttributeKey<FtpSession> FTP_SESSION_ATTRIBUTE_KEY =
48 AttributeKey.newInstance(FTP_SESSION_ATTRIBUTE_KEY_NAME);
49 private static final String STOU = ".stou";
50 private static final String PROPERTY_HAS_NO_VALUE = "Property has no value: ";
51
52
53
54 private static final WaarpLogger logger =
55 WaarpLoggerFactory.getLogger(FtpConfiguration.class);
56
57
58
59 public static Object ftpConfiguration;
60
61
62
63
64
65 static final long DEFAULT_SESSION_LIMIT = 0x800000L;
66
67
68
69
70 static final long DEFAULT_GLOBAL_LIMIT = 0x4000000L;
71
72
73
74
75 private static long dataTimeoutCon = 5000;
76
77
78
79
80 private static final String FTP_PASSWORD = "FTP_PASSWORD";
81
82
83
84
85
86 private final FtpInternalConfiguration internalConfiguration;
87
88
89
90
91 private int serverPort = 21;
92
93
94
95 private String serverAddress;
96
97
98
99
100 private String baseDirectory;
101
102
103
104
105 private final FileParameterInterface fileParameter;
106
107
108
109
110 private boolean isShutdown;
111
112
113
114
115
116
117
118
119 private int serverThread;
120
121
122
123
124 private int clientThread = 80;
125
126
127
128
129 final Class<?> fromClass;
130
131
132
133
134 final Class<? extends DataBusinessHandler> dataBusinessHandler;
135
136
137
138
139 final Class<? extends BusinessHandler> businessHandler;
140
141
142
143
144 private final ReentrantLock lock = new ReentrantLock();
145
146
147
148
149 private long timeoutCon = 30000;
150
151
152
153
154
155
156 private int blocksize = 0x10000;
157
158
159
160
161 protected long serverGlobalWriteLimit = DEFAULT_GLOBAL_LIMIT;
162
163
164
165
166 protected long serverGlobalReadLimit = DEFAULT_GLOBAL_LIMIT;
167
168
169
170
171 protected long serverChannelWriteLimit = DEFAULT_SESSION_LIMIT;
172
173
174
175
176 protected long serverChannelReadLimit = DEFAULT_SESSION_LIMIT;
177
178
179
180
181 protected long delayLimit = 1000;
182
183
184
185
186
187 private boolean deleteOnAbort;
188
189
190
191
192 private int maxGlobalMemory = 1073741824;
193
194
195
196
197 private int activePassiveMode = 0;
198
199
200
201
202 private final HashMap<String, Object> properties =
203 new HashMap<String, Object>();
204
205
206
207
208 private final ShutdownConfiguration shutdownConfiguration =
209 new ShutdownConfiguration();
210
211
212
213
214
215
216
217
218
219
220
221 protected FtpConfiguration(final Class<?> classtype,
222 final Class<? extends BusinessHandler> businessHandler,
223 final Class<? extends DataBusinessHandler> dataBusinessHandler,
224 final FileParameterInterface fileParameter) {
225 fromClass = classtype;
226 this.dataBusinessHandler = dataBusinessHandler;
227 this.businessHandler = businessHandler;
228 internalConfiguration = new FtpInternalConfiguration(this);
229 this.fileParameter = fileParameter;
230 ftpConfiguration = this;
231 }
232
233
234
235
236
237
238
239
240 public final String getStringProperty(final String key)
241 throws FtpUnknownFieldException {
242 final String s = (String) properties.get(key);
243 if (s == null) {
244 throw new FtpUnknownFieldException(PROPERTY_HAS_NO_VALUE + key);
245 }
246 return s;
247 }
248
249
250
251
252
253
254
255
256 public final int getIntProperty(final String key)
257 throws FtpUnknownFieldException {
258 final Integer i = (Integer) properties.get(key);
259 if (i == null) {
260 throw new FtpUnknownFieldException(PROPERTY_HAS_NO_VALUE + key);
261 }
262 return i;
263 }
264
265
266
267
268
269
270
271
272 public final File getFileProperty(final String key)
273 throws FtpUnknownFieldException {
274 final File f = (File) properties.get(key);
275 if (f == null) {
276 throw new FtpUnknownFieldException(PROPERTY_HAS_NO_VALUE + key);
277 }
278 return f;
279 }
280
281
282
283
284
285
286
287
288 public final Object getProperty(final String key)
289 throws FtpUnknownFieldException {
290 final Object o = properties.get(key);
291 if (o == null) {
292 throw new FtpUnknownFieldException(PROPERTY_HAS_NO_VALUE + key);
293 }
294 return o;
295 }
296
297
298
299
300 public final int getServerPort() {
301 return serverPort;
302 }
303
304
305
306
307 public final String getServerAddress() {
308 return serverAddress;
309 }
310
311
312
313
314 public final long getServerGlobalWriteLimit() {
315 return serverGlobalWriteLimit;
316 }
317
318
319
320
321
322 public final long getServerChannelWriteLimit() {
323 return serverChannelWriteLimit;
324 }
325
326
327
328
329 public final long getServerGlobalReadLimit() {
330 return serverGlobalReadLimit;
331 }
332
333
334
335
336
337 public final long getServerChannelReadLimit() {
338 return serverChannelReadLimit;
339 }
340
341
342
343
344 public final long getDelayLimit() {
345 return delayLimit;
346 }
347
348
349
350
351
352
353
354
355 public boolean checkPassword(final String password) {
356 final String serverpassword;
357 try {
358 serverpassword = getStringProperty(FTP_PASSWORD);
359 } catch (final FtpUnknownFieldException e) {
360 return false;
361 }
362 return serverpassword.equals(password);
363 }
364
365
366
367
368
369
370 public abstract int getNextRangePort();
371
372
373
374
375 public final String getBaseDirectory() {
376 return baseDirectory;
377 }
378
379
380
381
382
383 public final void setStringProperty(final String key, final String s) {
384 properties.put(key, s);
385 }
386
387
388
389
390
391 public final void setIntProperty(final String key, final int i) {
392 properties.put(key, i);
393 }
394
395
396
397
398
399 public final void setFileProperty(final String key, final File f) {
400 properties.put(key, f);
401 }
402
403
404
405
406
407 public final void setProperty(final String key, final Object o) {
408 properties.put(key, o);
409 }
410
411
412
413
414 public final void setServerPort(final int port) {
415 serverPort = port;
416 }
417
418
419
420
421 public final void setServerAddress(final String address) {
422 serverAddress = address;
423 }
424
425
426
427
428 public final void setBaseDirectory(final String dir) {
429 baseDirectory = dir;
430 }
431
432
433
434
435 public final void setPassword(final String password) {
436 setStringProperty(FTP_PASSWORD, password);
437 }
438
439
440
441
442 public final Class<? extends DataBusinessHandler> getDataBusinessHandler() {
443 return dataBusinessHandler;
444 }
445
446
447
448
449
450
451 public final void serverStartup() throws FtpNoConnectionException {
452 logger.debug("Server Startup");
453 internalConfiguration.serverStartup();
454 logger.debug("Server Startup done");
455 }
456
457
458
459
460
461
462
463
464
465
466 public final void changeNetworkLimit(final long writeLimit,
467 final long readLimit) {
468 long newWriteLimit = writeLimit > 1024? writeLimit : serverGlobalWriteLimit;
469 if (writeLimit <= 0) {
470 newWriteLimit = 0;
471 }
472 long newReadLimit = readLimit > 1024? readLimit : serverGlobalReadLimit;
473 if (readLimit <= 0) {
474 newReadLimit = 0;
475 }
476 final FtpGlobalTrafficShapingHandler fgts =
477 internalConfiguration.getGlobalTrafficShapingHandler();
478 fgts.configure(newWriteLimit, newReadLimit);
479 serverChannelReadLimit = newReadLimit / 10;
480 serverChannelWriteLimit = newWriteLimit / 10;
481 if (fgts instanceof GlobalChannelTrafficShapingHandler) {
482 fgts.configureChannel(serverChannelWriteLimit, serverChannelReadLimit);
483 }
484 }
485
486
487
488
489
490
491 public final void computeNbThreads() {
492 int nb = Runtime.getRuntime().availableProcessors() * 2 + 1;
493 if (nb > 32) {
494 nb = Runtime.getRuntime().availableProcessors() + 1;
495 }
496 if (getServerThread() < nb) {
497 setServerThread(nb);
498 setClientThread(getServerThread() * 10);
499 } else if (getClientThread() < nb) {
500 setClientThread(nb * 10);
501 }
502 }
503
504
505
506
507 public final void bindLock() {
508 lock.lock();
509 }
510
511
512
513
514 public final void bindUnlock() {
515 lock.unlock();
516 }
517
518
519
520
521 public final FtpInternalConfiguration getFtpInternalConfiguration() {
522 return internalConfiguration;
523 }
524
525
526
527
528
529
530
531
532 public final void setNewFtpSession(final InetAddress ipOnly,
533 final InetSocketAddress fullIp,
534 final FtpSession session) {
535 internalConfiguration.setNewFtpSession(ipOnly, fullIp, session);
536 }
537
538
539
540
541
542
543
544
545
546 public final FtpSession getFtpSession(final Channel channel,
547 final boolean active) {
548 FtpSession session = null;
549 for (int i = 0; i < FtpInternalConfiguration.RETRYNB * 2; i++) {
550 if (active) {
551 session = channel.attr(FTP_SESSION_ATTRIBUTE_KEY).get();
552 if (session != null) {
553 return session;
554 }
555 try {
556 Thread.sleep(FtpInternalConfiguration.RETRYINMS * 10);
557 } catch (final InterruptedException e1) {
558 SysErrLogger.FAKE_LOGGER.ignoreLog(e1);
559 }
560 } else {
561 session = internalConfiguration.getFtpSession(channel);
562 if (session == null) {
563 logger.debug("Session not found at try " + i);
564 try {
565 Thread.sleep(FtpInternalConfiguration.RETRYINMS * 10);
566 } catch (final InterruptedException e1) {
567 SysErrLogger.FAKE_LOGGER.ignoreLog(e1);
568 }
569 } else {
570 return session;
571 }
572 }
573 }
574 if (session == null) {
575 if (active) {
576 session = channel.attr(FTP_SESSION_ATTRIBUTE_KEY).get();
577 if (session != null) {
578 return session;
579 }
580 } else {
581
582 session = internalConfiguration.findPassiveFtpSession(channel);
583 if (session != null) {
584 logger.debug("Found from port Passive? {}", channel.localAddress());
585 return session;
586 }
587 }
588 }
589 if (session == null) {
590 logger.error("Could not find {} session for {}",
591 active? "Active" : "Passive", channel);
592 }
593 return session;
594 }
595
596
597
598
599
600
601
602 public final void delFtpSession(final InetAddress ipOnly,
603 final InetSocketAddress fullIp) {
604 internalConfiguration.delFtpSession(ipOnly, fullIp);
605 }
606
607
608
609
610 public final FileParameterInterface getFileParameter() {
611 return fileParameter;
612 }
613
614 public final String getUniqueExtension() {
615
616 return STOU;
617 }
618
619
620
621
622 public void releaseResources() {
623 internalConfiguration.releaseResources();
624 }
625
626
627
628
629 public abstract void inShutdownProcess();
630
631
632
633
634 public final boolean isShutdown() {
635 return isShutdown;
636 }
637
638
639
640
641 public final void setShutdown(final boolean isShutdown) {
642 this.isShutdown = isShutdown;
643 }
644
645
646
647
648 public final int getServerThread() {
649 return serverThread;
650 }
651
652
653
654
655 public final void setServerThread(final int serverThread0) {
656 serverThread = serverThread0;
657 }
658
659
660
661
662 public final int getClientThread() {
663 return clientThread;
664 }
665
666
667
668
669 public final void setClientThread(final int clientThread0) {
670 clientThread = clientThread0;
671 }
672
673
674
675
676 public final long getTimeoutCon() {
677 return timeoutCon;
678 }
679
680
681
682
683 public final void setTimeoutCon(final long tIMEOUTCON) {
684 timeoutCon = tIMEOUTCON;
685 }
686
687
688
689
690 public final int getBlocksize() {
691 return blocksize;
692 }
693
694
695
696
697 public final void setBlocksize(final int bLOCKSIZE) {
698 blocksize = bLOCKSIZE;
699 }
700
701
702
703
704 public final boolean isDeleteOnAbort() {
705 return deleteOnAbort;
706 }
707
708
709
710
711 public final void setDeleteOnAbort(final boolean deleteOnAbort) {
712 this.deleteOnAbort = deleteOnAbort;
713 }
714
715
716
717
718 public static long getDataTimeoutCon() {
719 return dataTimeoutCon;
720 }
721
722
723
724
725 public static void setDataTimeoutCon(final long dATATIMEOUTCON) {
726 dataTimeoutCon = dATATIMEOUTCON;
727 }
728
729
730
731
732 public final int getActivePassiveMode() {
733 return activePassiveMode;
734 }
735
736
737
738
739 public final void setActivePassiveMode(final int activePassiveModeArg) {
740 activePassiveMode =
741 activePassiveModeArg < 0? -1 : (activePassiveModeArg > 0? 1 : 0);
742 }
743
744
745
746
747 public final int getMaxGlobalMemory() {
748 return maxGlobalMemory;
749 }
750
751
752
753
754 public final void setMaxGlobalMemory(final int maxGlobalMemory) {
755 this.maxGlobalMemory = maxGlobalMemory;
756 }
757
758
759
760
761 public final ShutdownConfiguration getShutdownConfiguration() {
762 return shutdownConfiguration;
763 }
764 }