1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package goldengate.ftp.core.config;
22
23 import goldengate.common.file.FileParameterInterface;
24 import goldengate.ftp.core.control.BusinessHandler;
25 import goldengate.ftp.core.data.handler.DataBusinessHandler;
26 import goldengate.ftp.core.exception.FtpUnknownFieldException;
27 import goldengate.ftp.core.session.FtpSession;
28
29 import java.io.File;
30 import java.net.InetAddress;
31 import java.net.InetSocketAddress;
32 import java.util.HashMap;
33 import java.util.concurrent.locks.Lock;
34 import java.util.concurrent.locks.ReentrantLock;
35
36 import org.jboss.netty.channel.Channel;
37
38
39
40
41
42
43
44 public abstract class FtpConfiguration {
45
46
47
48 public static final boolean USEJDK6 = true;
49
50
51
52
53 public static long DEFAULT_SESSION_LIMIT = 0x800000L;
54
55
56
57
58 public static long DEFAULT_GLOBAL_LIMIT = 0x4000000L;
59
60
61
62
63
64 private static final String FTP_PASSWORD = "FTP_PASSWORD";
65
66
67
68
69
70 private final FtpInternalConfiguration internalConfiguration;
71
72
73
74
75 private int SERVER_PORT = 21;
76
77
78
79 private String SERVER_ADDRESS = null;
80
81
82
83
84 private String BASE_DIRECTORY = null;
85
86
87
88
89 private final FileParameterInterface fileParameter;
90
91
92
93
94 public volatile boolean isShutdown = false;
95
96
97
98
99
100
101 public int SERVER_THREAD = 8;
102
103
104
105
106 public int CLIENT_THREAD = 80;
107
108
109
110
111 public Class<?> fromClass = null;
112
113
114
115
116 public Class<? extends DataBusinessHandler> dataBusinessHandler = null;
117
118
119
120
121 public Class<? extends BusinessHandler> businessHandler = null;
122
123
124
125
126 private ReentrantLock lock = new ReentrantLock();
127
128
129
130
131 public long TIMEOUTCON = 30000;
132
133
134
135
136
137 public int BLOCKSIZE = 0x10000;
138
139
140
141
142 protected long serverGlobalWriteLimit = DEFAULT_GLOBAL_LIMIT;
143
144
145
146
147 protected long serverGlobalReadLimit = DEFAULT_GLOBAL_LIMIT;
148
149
150
151
152 protected long serverChannelWriteLimit = DEFAULT_SESSION_LIMIT;
153
154
155
156
157 protected long serverChannelReadLimit = DEFAULT_SESSION_LIMIT;
158
159
160
161
162 protected long delayLimit = 1000;
163
164
165
166
167
168 public boolean deleteOnAbort = false;
169
170
171
172
173 public long maxGlobalMemory = 0x100000000L;
174
175
176
177
178 private final HashMap<String, Object> properties = new HashMap<String, Object>();
179
180
181
182
183
184
185
186
187
188
189
190
191
192 public FtpConfiguration(Class<?> classtype,
193 Class<? extends BusinessHandler> businessHandler,
194 Class<? extends DataBusinessHandler> dataBusinessHandler,
195 FileParameterInterface fileParameter) {
196 fromClass = classtype;
197 this.dataBusinessHandler = dataBusinessHandler;
198 this.businessHandler = businessHandler;
199 internalConfiguration = new FtpInternalConfiguration(this);
200 this.fileParameter = fileParameter;
201 }
202
203
204
205
206
207
208
209 public String getStringProperty(String key) throws FtpUnknownFieldException {
210 String s = (String) properties.get(key);
211 if (s == null) {
212 throw new FtpUnknownFieldException("Property has no value: " + key);
213 }
214 return s;
215 }
216
217
218
219
220
221
222
223 public int getIntProperty(String key) throws FtpUnknownFieldException {
224 Integer i = (Integer) properties.get(key);
225 if (i == null) {
226 throw new FtpUnknownFieldException("Property has no value: " + key);
227 }
228 return i;
229 }
230
231
232
233
234
235
236
237 public File getFileProperty(String key) throws FtpUnknownFieldException {
238 File f = (File) properties.get(key);
239 if (f == null) {
240 throw new FtpUnknownFieldException("Property has no value: " + key);
241 }
242 return f;
243 }
244
245
246
247
248
249
250
251 public Object getProperty(String key) throws FtpUnknownFieldException {
252 Object o = properties.get(key);
253 if (o == null) {
254 throw new FtpUnknownFieldException("Property has no value: " + key);
255 }
256 return o;
257 }
258
259
260
261
262
263 public int getServerPort() {
264 return SERVER_PORT;
265 }
266
267
268
269
270 public String getServerAddress() {
271 return SERVER_ADDRESS;
272 }
273
274
275
276
277
278 public long getServerGlobalWriteLimit() {
279 return serverGlobalWriteLimit;
280 }
281
282
283
284
285
286
287 public long getServerChannelWriteLimit() {
288 return serverChannelWriteLimit;
289 }
290
291
292
293
294
295 public long getServerGlobalReadLimit() {
296 return serverGlobalReadLimit;
297 }
298
299
300
301
302
303
304 public long getServerChannelReadLimit() {
305 return serverChannelReadLimit;
306 }
307
308
309
310
311 public long getDelayLimit() {
312 return delayLimit;
313 }
314
315
316
317
318
319
320
321 public boolean checkPassword(String password) {
322 String serverpassword;
323 try {
324 serverpassword = getStringProperty(FTP_PASSWORD);
325 } catch (FtpUnknownFieldException e) {
326 return false;
327 }
328 return serverpassword.equals(password);
329 }
330
331
332
333
334
335
336 public abstract int getNextRangePort();
337
338
339
340
341
342 public String getBaseDirectory() {
343 return BASE_DIRECTORY;
344 }
345
346
347
348
349
350
351 public void setStringProperty(String key, String s) {
352 properties.put(key, s);
353 }
354
355
356
357
358
359
360 public void setIntProperty(String key, int i) {
361 properties.put(key, Integer.valueOf(i));
362 }
363
364
365
366
367
368
369 public void setFileProperty(String key, File f) {
370 properties.put(key, f);
371 }
372
373
374
375
376
377
378 public void setProperty(String key, Object o) {
379 properties.put(key, o);
380 }
381
382
383
384
385
386 public void setServerPort(int port) {
387 SERVER_PORT = port;
388 }
389
390
391
392
393
394 public void setServerAddress(String address) {
395 SERVER_ADDRESS = address;
396 }
397
398
399
400
401 public void setBaseDirectory(String dir) {
402 BASE_DIRECTORY = dir;
403 }
404
405
406
407
408
409 public void setPassword(String password) {
410 setStringProperty(FTP_PASSWORD, password);
411 }
412
413
414
415
416 public Class<? extends DataBusinessHandler> getDataBusinessHandler() {
417 return dataBusinessHandler;
418 }
419
420
421
422
423
424 public void serverStartup() {
425 internalConfiguration.serverStartup();
426 }
427
428
429
430
431
432
433
434
435 public void changeNetworkLimit(long writeLimit, long readLimit) {
436 long newWriteLimit = writeLimit > 1024? writeLimit
437 : serverGlobalWriteLimit;
438 if (writeLimit <= 0) {
439 newWriteLimit = 0;
440 }
441 long newReadLimit = readLimit > 1024? readLimit : serverGlobalReadLimit;
442 if (readLimit <= 0) {
443 newReadLimit = 0;
444 }
445 internalConfiguration.getGlobalTrafficShapingHandler().configure(
446 newWriteLimit, newReadLimit);
447 serverChannelReadLimit = newReadLimit / 10;
448 serverChannelWriteLimit = newWriteLimit / 10;
449 }
450
451
452
453
454
455
456
457 public void computeNbThreads() {
458 int nb = Runtime.getRuntime().availableProcessors() * 2 + 1;
459 if (nb > 32) {
460 nb = Runtime.getRuntime().availableProcessors() + 1;
461 }
462 if (SERVER_THREAD < nb) {
463 SERVER_THREAD = nb;
464 CLIENT_THREAD = SERVER_THREAD*10;
465 }
466 }
467
468
469
470
471
472 public Lock getLock() {
473 return lock;
474 }
475
476
477
478 public void bindLock() {
479 lock.lock();
480 }
481
482
483
484 public void bindUnlock() {
485 lock.unlock();
486 }
487
488
489
490
491 public FtpInternalConfiguration getFtpInternalConfiguration() {
492 return internalConfiguration;
493 }
494
495
496
497
498
499
500
501
502 public void setNewFtpSession(InetAddress ipOnly, InetSocketAddress fullIp,
503 FtpSession session) {
504 internalConfiguration.setNewFtpSession(ipOnly, fullIp, session);
505 }
506
507
508
509
510
511
512
513
514 public FtpSession getFtpSession(Channel channel, boolean active) {
515 return internalConfiguration.getFtpSession(channel, active);
516 }
517
518
519
520
521
522
523
524 public void delFtpSession(InetAddress ipOnly, InetSocketAddress fullIp) {
525 internalConfiguration.delFtpSession(ipOnly, fullIp);
526 }
527
528
529
530
531
532
533
534
535 public boolean hasFtpSession(InetAddress ipOnly, InetSocketAddress fullIp) {
536 return internalConfiguration.hasFtpSession(ipOnly, fullIp);
537 }
538
539
540
541
542 public FileParameterInterface getFileParameter() {
543 return fileParameter;
544 }
545
546 public String getUniqueExtension() {
547
548 return ".stou";
549 }
550
551
552
553 public void releaseResources() {
554 internalConfiguration.releaseResources();
555 }
556
557
558
559 public abstract void inShutdownProcess();
560 }