You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by ng...@apache.org on 2008/11/16 22:05:01 UTC

svn commit: r718118 - in /mina/ftpserver/trunk/core/src: main/java/org/apache/ftpserver/usermanager/impl/PropertiesUserManager.java test/java/org/apache/ftpserver/usermanager/impl/PropertiesUserManagerTest.java

Author: ngn
Date: Sun Nov 16 13:05:00 2008
New Revision: 718118

URL: http://svn.apache.org/viewvc?rev=718118&view=rev
Log:
Suggestion to enable user properties file to be updated without restarting server (FTPSERVER-218)

Modified:
    mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/usermanager/impl/PropertiesUserManager.java
    mina/ftpserver/trunk/core/src/test/java/org/apache/ftpserver/usermanager/impl/PropertiesUserManagerTest.java

Modified: mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/usermanager/impl/PropertiesUserManager.java
URL: http://svn.apache.org/viewvc/mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/usermanager/impl/PropertiesUserManager.java?rev=718118&r1=718117&r2=718118&view=diff
==============================================================================
--- mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/usermanager/impl/PropertiesUserManager.java (original)
+++ mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/usermanager/impl/PropertiesUserManager.java Sun Nov 16 13:05:00 2008
@@ -126,8 +126,8 @@
 
     private File userDataFile;
 
+    private URL userUrl;
 
-    
     /**
      * Internal constructor, do not use directly. Use {@link PropertiesUserManagerFactory} instead.
      */
@@ -135,15 +135,29 @@
             File userDataFile, String adminName) {
         super(adminName, passwordEncryptor);
 
+        loadFromFile(userDataFile);
+    }
+
+    /**
+     * Internal constructor, do not use directly. Use {@link PropertiesUserManagerFactory} instead.
+     */
+    public PropertiesUserManager(PasswordEncryptor passwordEncryptor,
+            URL userDataPath, String adminName) {
+        super(adminName, passwordEncryptor);
+
+        loadFromUrl(userDataPath);
+    }
+
+    private void loadFromFile(File userDataFile) {
         try {
             userDataProp = new BaseProperties();
 
             if (userDataFile != null) {
                 LOG.debug("File configured, will try loading");
-                
-                if(userDataFile.exists()) {
+
+                if (userDataFile.exists()) {
                     this.userDataFile = userDataFile;
-                    
+
                     LOG.debug("File found on file system");
                     FileInputStream fis = null;
                     try {
@@ -154,11 +168,13 @@
                     }
                 } else {
                     // try loading it from the classpath
-                    LOG.debug("File not found on file system, try loading from classpath");
-                    
-                    InputStream is = getClass().getClassLoader().getResourceAsStream(userDataFile.getPath());
-                    
-                    if(is != null) {
+                    LOG
+                            .debug("File not found on file system, try loading from classpath");
+
+                    InputStream is = getClass().getClassLoader()
+                            .getResourceAsStream(userDataFile.getPath());
+
+                    if (is != null) {
                         try {
                             userDataProp.load(is);
                         } finally {
@@ -166,49 +182,61 @@
                         }
                     } else {
                         throw new FtpServerConfigurationException(
-                                "User data file specified but could not be located, " +
-                                "neither on the file system or in the classpath: "
-                                        + userDataFile.getPath());                    
+                                "User data file specified but could not be located, "
+                                        + "neither on the file system or in the classpath: "
+                                        + userDataFile.getPath());
                     }
                 }
-            } 
+            }
         } catch (IOException e) {
             throw new FtpServerConfigurationException(
-                    "Error loading user data file : "
-                            + userDataFile, e);
+                    "Error loading user data file : " + userDataFile, e);
         }
     }
 
-    /**
-     * Internal constructor, do not use directly. Use {@link PropertiesUserManagerFactory} instead.
-     */
-    public PropertiesUserManager(PasswordEncryptor passwordEncryptor,
-            URL userDataPath, String adminName) {
-        super(adminName, passwordEncryptor);
-
+    private void loadFromUrl(URL userDataPath) {
         try {
             userDataProp = new BaseProperties();
 
             if (userDataPath != null) {
                 LOG.debug("URL configured, will try loading");
-                
+
+                userUrl = userDataPath;
                 InputStream is = null;
-                
+
                 is = userDataPath.openStream();
-                    
+
                 try {
                     userDataProp.load(is);
                 } finally {
                     IoUtils.close(is);
                 }
-            } 
+            }
         } catch (IOException e) {
             throw new FtpServerConfigurationException(
-                    "Error loading user data resource : "
-                            + userDataPath, e);
+                    "Error loading user data resource : " + userDataPath, e);
         }
     }
+
+    /**
+     * Reloads the contents of the user.properties file. This allows any manual modifications to the file to be recognised by the running server.
+     */
+    public void refresh() {
+        synchronized (userDataProp) {
+            if (userDataFile != null) {
+                LOG.debug("Refreshing user manager using file: "
+                        + userDataFile.getAbsolutePath());
+                loadFromFile(userDataFile);
     
+            } else {
+                //file is null, must have been created using URL
+                LOG.debug("Refreshing user manager using URL: "
+                        + userUrl.toString());
+                loadFromUrl(userUrl);
+            }
+        }
+    }
+
     /**
      * Retrive the file backing this user manager
      * @return The file
@@ -217,7 +245,6 @@
         return userDataFile;
     }
 
-    
     /**
      * Save user data. Store the properties.
      */
@@ -279,10 +306,10 @@
      * @throws FtpException
      */
     private void saveUserData() throws FtpException {
-        if(userDataFile == null) {
+        if (userDataFile == null) {
             return;
         }
-        
+
         File dir = userDataFile.getAbsoluteFile().getParentFile();
         if (dir != null && !dir.exists() && !dir.mkdirs()) {
             String dirName = dir.getAbsolutePath();
@@ -452,10 +479,10 @@
                 password = "";
             }
 
-            String storedPassword = userDataProp.getProperty(PREFIX + user + '.'
-                    + ATTR_PASSWORD);
-            
-            if(storedPassword == null) {
+            String storedPassword = userDataProp.getProperty(PREFIX + user
+                    + '.' + ATTR_PASSWORD);
+
+            if (storedPassword == null) {
                 // user does not exist
                 throw new AuthenticationFailedException("Authentication failed");
             }
@@ -487,4 +514,5 @@
             userDataProp = null;
         }
     }
+
 }

Modified: mina/ftpserver/trunk/core/src/test/java/org/apache/ftpserver/usermanager/impl/PropertiesUserManagerTest.java
URL: http://svn.apache.org/viewvc/mina/ftpserver/trunk/core/src/test/java/org/apache/ftpserver/usermanager/impl/PropertiesUserManagerTest.java?rev=718118&r1=718117&r2=718118&view=diff
==============================================================================
--- mina/ftpserver/trunk/core/src/test/java/org/apache/ftpserver/usermanager/impl/PropertiesUserManagerTest.java (original)
+++ mina/ftpserver/trunk/core/src/test/java/org/apache/ftpserver/usermanager/impl/PropertiesUserManagerTest.java Sun Nov 16 13:05:00 2008
@@ -20,11 +20,14 @@
 package org.apache.ftpserver.usermanager.impl;
 
 import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.util.Properties;
 
 import org.apache.ftpserver.ftplet.FtpException;
+import org.apache.ftpserver.ftplet.User;
 import org.apache.ftpserver.usermanager.ClearTextPasswordEncryptor;
 import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;
 import org.apache.ftpserver.usermanager.UserManagerFactory;
@@ -89,4 +92,25 @@
         IoUtils.delete(TEST_DIR);
     }
 
+    public void testRefresh() throws FileNotFoundException, IOException {
+    	Properties users = new Properties();
+    	users.load(new FileInputStream(USERS_FILE));
+    	String originalSetting = users.getProperty("ftpserver.user.user1.homedirectory");
+    	users.setProperty("ftpserver.user.user1.homedirectory", "refresh_test");
+    	users.store(new FileOutputStream(USERS_FILE), null);
+    	
+    	PropertiesUserManager pum = (PropertiesUserManager) userManager;
+    	pum.refresh();
+    	User modifiedUser = pum.getUserByName("user1");
+    	assertEquals("Home directory should have been \"refresh_test\" after call to refresh().","refresh_test",modifiedUser.getHomeDirectory());
+    	// set everything back again
+    	users.load(new FileInputStream(USERS_FILE));
+    	users.setProperty("ftpserver.user.user1.homedirectory", originalSetting);
+    	users.store(new FileOutputStream(USERS_FILE), null);
+    	
+    	pum.refresh();
+    	//check everything is back again
+    	modifiedUser = pum.getUserByName("user1");
+    	assertEquals("Home directory should have reset back to \""+originalSetting+"\" after second call to refresh().",originalSetting,modifiedUser.getHomeDirectory());
+    }
 }