incubator-ftpserver-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From n..@apache.org
Subject svn commit: r490790 - in /incubator/ftpserver/trunk/core/src: java/org/apache/ftpserver/ test/org/apache/ftpserver/
Date Thu, 28 Dec 2006 20:46:13 GMT
Author: ngn
Date: Thu Dec 28 12:46:12 2006
New Revision: 490790

URL: http://svn.apache.org/viewvc?view=rev&rev=490790
Log:
Refactored parsing and reservation of passive ports into its own class. This is the first
step towards solving FTPSERVER-53

Added:
    incubator/ftpserver/trunk/core/src/java/org/apache/ftpserver/PassivePorts.java   (with
props)
    incubator/ftpserver/trunk/core/src/test/org/apache/ftpserver/PassivePortsTest.java   (with
props)
Modified:
    incubator/ftpserver/trunk/core/src/java/org/apache/ftpserver/DefaultDataConnectionConfig.java
    incubator/ftpserver/trunk/core/src/java/org/apache/ftpserver/DefaultListener.java
    incubator/ftpserver/trunk/core/src/java/org/apache/ftpserver/Listener.java
    incubator/ftpserver/trunk/core/src/java/org/apache/ftpserver/RequestHandler.java

Modified: incubator/ftpserver/trunk/core/src/java/org/apache/ftpserver/DefaultDataConnectionConfig.java
URL: http://svn.apache.org/viewvc/incubator/ftpserver/trunk/core/src/java/org/apache/ftpserver/DefaultDataConnectionConfig.java?view=diff&rev=490790&r1=490789&r2=490790
==============================================================================
--- incubator/ftpserver/trunk/core/src/java/org/apache/ftpserver/DefaultDataConnectionConfig.java
(original)
+++ incubator/ftpserver/trunk/core/src/java/org/apache/ftpserver/DefaultDataConnectionConfig.java
Thu Dec 28 12:46:12 2006
@@ -20,7 +20,6 @@
 package org.apache.ftpserver;
 
 import java.net.InetAddress;
-import java.util.StringTokenizer;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -48,9 +47,10 @@
     private int activeLocalPort;
     
     private InetAddress passiveAddress;
-    private int passivePorts[][];
     
     private Ssl ssl;
+    
+    private PassivePorts passivePorts;
 
     
     /**
@@ -99,12 +99,8 @@
             }
             
             String pasvPorts = passiveConf.getString("ports", "0");
-            StringTokenizer st = new StringTokenizer(pasvPorts, " ,;\t\n\r\f");
-            passivePorts = new int[st.countTokens()][2];
-            for(int i=0; i<passivePorts.length; i++) {
-                passivePorts[i][0] = Integer.parseInt(st.nextToken());
-                passivePorts[i][1] = 0;
-            }
+            
+            passivePorts = PassivePorts.parse(pasvPorts);
             
             // get SSL parameters if available 
             Configuration sslConf = conf.subset("ssl");
@@ -177,15 +173,7 @@
         while( (dataPort==-1) && (--loopTimes >= 0)  && (!currThread.isInterrupted())
) {
 
             // search for a free port            
-            for(int i=0; i<passivePorts.length; i++) {
-                if(passivePorts[i][1] == 0) {
-                    if(passivePorts[i][0] != 0) {
-                        passivePorts[i][1] = 1;
-                    }
-                    dataPort = passivePorts[i][0];
-                    break;
-                }
-            }
+            dataPort = passivePorts.reserveNextPort();
 
             // no available free port - wait for the release notification
             if(dataPort == -1) {
@@ -203,12 +191,8 @@
      * Release data port
      */
     public synchronized void releasePassivePort(int port) {
-        for(int i=0; i<passivePorts.length; i++) {
-            if(passivePorts[i][0] == port) {
-                passivePorts[i][1] = 0;
-                break;
-            }
-        }
+        passivePorts.releasePort(port);
+
         notify();
     }
     

Modified: incubator/ftpserver/trunk/core/src/java/org/apache/ftpserver/DefaultListener.java
URL: http://svn.apache.org/viewvc/incubator/ftpserver/trunk/core/src/java/org/apache/ftpserver/DefaultListener.java?view=diff&rev=490790&r1=490789&r2=490790
==============================================================================
--- incubator/ftpserver/trunk/core/src/java/org/apache/ftpserver/DefaultListener.java (original)
+++ incubator/ftpserver/trunk/core/src/java/org/apache/ftpserver/DefaultListener.java Thu
Dec 28 12:46:12 2006
@@ -1,3 +1,22 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */ 
+
 package org.apache.ftpserver;
 
 import java.io.IOException;

Modified: incubator/ftpserver/trunk/core/src/java/org/apache/ftpserver/Listener.java
URL: http://svn.apache.org/viewvc/incubator/ftpserver/trunk/core/src/java/org/apache/ftpserver/Listener.java?view=diff&rev=490790&r1=490789&r2=490790
==============================================================================
--- incubator/ftpserver/trunk/core/src/java/org/apache/ftpserver/Listener.java (original)
+++ incubator/ftpserver/trunk/core/src/java/org/apache/ftpserver/Listener.java Thu Dec 28
12:46:12 2006
@@ -1,3 +1,22 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */ 
+
 package org.apache.ftpserver;
 
 import org.apache.ftpserver.interfaces.Connection;
@@ -50,7 +69,7 @@
 
     /**
      * Checks if the listener is currently suspended
-     * @return
+     * @return True if the listener is suspended
      */
     boolean isSuspended();
 }

Added: incubator/ftpserver/trunk/core/src/java/org/apache/ftpserver/PassivePorts.java
URL: http://svn.apache.org/viewvc/incubator/ftpserver/trunk/core/src/java/org/apache/ftpserver/PassivePorts.java?view=auto&rev=490790
==============================================================================
--- incubator/ftpserver/trunk/core/src/java/org/apache/ftpserver/PassivePorts.java (added)
+++ incubator/ftpserver/trunk/core/src/java/org/apache/ftpserver/PassivePorts.java Thu Dec
28 12:46:12 2006
@@ -0,0 +1,81 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */ 
+
+package org.apache.ftpserver;
+
+import java.util.StringTokenizer;
+
+
+/**
+ * Provides support for parsing a passive ports string as well as
+ * keeping track of reserved passive ports.
+ */
+public class PassivePorts {
+
+    private int[][] passivePorts;
+    
+    /**
+     * Parse a string containing passive ports
+     * @param portsString A string of passive ports, can contain a single
+     *   port (as an integer) or multiple ports seperated by commas
+     * @return An instance of {@link PassivePorts} based on the parsed string
+     * @throws NumberFormatException If any of of the ports in the string is
+     *   invalid (e.g. not an integer) 
+     */
+    public static PassivePorts parse(String portsString) {
+        int[][] passivePorts;
+        
+        StringTokenizer st = new StringTokenizer(portsString, " ,;\t\n\r\f");
+        passivePorts = new int[st.countTokens()][2];
+        for(int i=0; i<passivePorts.length; i++) {
+            passivePorts[i][0] = Integer.parseInt(st.nextToken());
+            passivePorts[i][1] = 0;
+        }
+        
+        return new PassivePorts(passivePorts);
+    }
+    
+    private PassivePorts(int[][] passivePorts) {
+        this.passivePorts = passivePorts;
+    }
+    
+    public int reserveNextPort() {
+        // search for a free port            
+        for(int i=0; i<passivePorts.length; i++) {
+            if(passivePorts[i][1] == 0) {
+                if(passivePorts[i][0] != 0) {
+                    passivePorts[i][1] = 1;
+                }
+                return passivePorts[i][0];
+            }
+        }
+        
+        return -1;
+    }
+    
+    public void releasePort(int port) {
+        for(int i=0; i<passivePorts.length; i++) {
+            if(passivePorts[i][0] == port) {
+                passivePorts[i][1] = 0;
+                break;
+            }
+        }
+    }
+
+}
\ No newline at end of file

Propchange: incubator/ftpserver/trunk/core/src/java/org/apache/ftpserver/PassivePorts.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: incubator/ftpserver/trunk/core/src/java/org/apache/ftpserver/RequestHandler.java
URL: http://svn.apache.org/viewvc/incubator/ftpserver/trunk/core/src/java/org/apache/ftpserver/RequestHandler.java?view=diff&rev=490790&r1=490789&r2=490790
==============================================================================
--- incubator/ftpserver/trunk/core/src/java/org/apache/ftpserver/RequestHandler.java (original)
+++ incubator/ftpserver/trunk/core/src/java/org/apache/ftpserver/RequestHandler.java Thu Dec
28 12:46:12 2006
@@ -46,11 +46,10 @@
 import org.apache.ftpserver.interfaces.Connection;
 import org.apache.ftpserver.interfaces.ConnectionManager;
 import org.apache.ftpserver.interfaces.ConnectionObserver;
-import org.apache.ftpserver.interfaces.IpRestrictor;
 import org.apache.ftpserver.interfaces.FtpServerContext;
+import org.apache.ftpserver.interfaces.IpRestrictor;
 import org.apache.ftpserver.interfaces.ServerFtpStatistics;
 import org.apache.ftpserver.interfaces.Ssl;
-import org.apache.ftpserver.listing.DirectoryLister;
 import org.apache.ftpserver.util.IoUtils;
 
 

Added: incubator/ftpserver/trunk/core/src/test/org/apache/ftpserver/PassivePortsTest.java
URL: http://svn.apache.org/viewvc/incubator/ftpserver/trunk/core/src/test/org/apache/ftpserver/PassivePortsTest.java?view=auto&rev=490790
==============================================================================
--- incubator/ftpserver/trunk/core/src/test/org/apache/ftpserver/PassivePortsTest.java (added)
+++ incubator/ftpserver/trunk/core/src/test/org/apache/ftpserver/PassivePortsTest.java Thu
Dec 28 12:46:12 2006
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */  
+
+package org.apache.ftpserver;
+
+import junit.framework.TestCase;
+
+
+public class PassivePortsTest extends TestCase {
+
+    public void testParseSingleValue() {
+        PassivePorts ports = PassivePorts.parse("123");
+        
+        assertEquals(123, ports.reserveNextPort());
+        assertEquals(-1, ports.reserveNextPort());
+    }
+
+    public void testParseListOfValues() {
+        PassivePorts ports = PassivePorts.parse("123, 456,789");
+
+        
+        assertEquals(123, ports.reserveNextPort());
+        assertEquals(456, ports.reserveNextPort());
+        assertEquals(789, ports.reserveNextPort());
+        assertEquals(-1, ports.reserveNextPort());
+    }
+
+    public void testParseRelease() {
+        PassivePorts ports = PassivePorts.parse("123, 456,789");
+        
+        
+        assertEquals(123, ports.reserveNextPort());
+        assertEquals(456, ports.reserveNextPort());
+        ports.releasePort(456);
+        assertEquals(456, ports.reserveNextPort());
+        
+        assertEquals(789, ports.reserveNextPort());
+        assertEquals(-1, ports.reserveNextPort());
+    }
+    
+}

Propchange: incubator/ftpserver/trunk/core/src/test/org/apache/ftpserver/PassivePortsTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message