01: package com.coldcore.coloradoftp.plugin.intellipack.command;
02:
03: import com.coldcore.coloradoftp.command.Command;
04: import com.coldcore.coloradoftp.command.Reply;
05: import com.coldcore.coloradoftp.command.impl.CommandWrapper;
06: import com.coldcore.coloradoftp.connection.Connection;
07: import com.coldcore.coloradoftp.connection.ConnectionPool;
08: import com.coldcore.coloradoftp.connection.DataConnection;
09: import com.coldcore.coloradoftp.factory.ObjectFactory;
10: import com.coldcore.coloradoftp.factory.ObjectName;
11: import org.apache.log4j.Logger;
12:
13: import java.util.Set;
14:
15: /**
16: * This class ensures that a number of simultaneously opened files stays below the limit.
17: * It checks if there are too many data connections already open before command execution.
18: *
19: * This class merely delegates all calls to a command of your choise. But because of the
20: * reply it sends back the internal command must be a command that creates a new data
21: * connection.
22: *
23: * Known commands that create data connections to files: STOR, STOU, RETR, PASV.
24: */
25: public class IntelFileCommandWrapper extends CommandWrapper {
26:
27: private static Logger log = Logger
28: .getLogger(IntelFileCommandWrapper.class);
29: protected int maxDataConnections;
30:
31: public IntelFileCommandWrapper(Command command) {
32: super (command);
33: }
34:
35: /** Get number of total simultaneous data connections
36: * @return Max amount of data connections
37: */
38: public int getMaxDataConnections() {
39: return maxDataConnections;
40: }
41:
42: /** Set number of total simultaneous data connections
43: * @param maxDataConnections Max amount of data connections
44: */
45: public void setMaxDataConnections(int maxDataConnections) {
46: this .maxDataConnections = maxDataConnections;
47: }
48:
49: public Reply execute() {
50: ConnectionPool dataConnectionPool = (ConnectionPool) ObjectFactory
51: .getObject(ObjectName.DATA_CONNECTION_POOL);
52:
53: //Test if data connection is already open and waiting
54: boolean open = false;
55: Set<Connection> set = dataConnectionPool.list();
56: for (Connection dc : set) {
57: if (((DataConnection) dc).getControlConnection() == command
58: .getConnection()) {
59: log.debug("Data connection already open and waiting");
60: open = true;
61: break;
62: }
63: }
64:
65: //If no data connection then test if one can be created
66: int total = dataConnectionPool.size();
67: if (total > maxDataConnections && maxDataConnections > 0
68: && !open) {
69: log
70: .debug("Too many data connections (total " + total
71: + ")");
72: Reply reply = (Reply) ObjectFactory
73: .getObject(ObjectName.REPLY);
74: reply.setCode("425");
75: reply
76: .setText("Too many data connections already, try again later.");
77: return reply;
78: }
79:
80: //Continue as normal
81: return command.execute();
82: }
83:
84: }
|