Author: ngn
Date: Sun Jul 13 04:51:10 2008
New Revision: 676304

URL: http://svn.apache.org/viewvc?rev=676304&view=rev
Log:
Make sure the user managers are configured before using
Add missing configuration for SQL statements for DbUserManager

Modified:
    
mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/config/spring/SpringUtil.java
    
mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/config/spring/UserManagerBeanDefinitionParser.java
    
mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/usermanager/DbUserManager.java
    
mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/usermanager/PropertiesUserManager.java

Modified: 
mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/config/spring/SpringUtil.java
URL: 
http://svn.apache.org/viewvc/mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/config/spring/SpringUtil.java?rev=676304&r1=676303&r2=676304&view=diff
==============================================================================
--- 
mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/config/spring/SpringUtil.java
 (original)
+++ 
mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/config/spring/SpringUtil.java
 Sun Jul 13 04:51:10 2008
@@ -29,6 +29,7 @@
 import org.springframework.beans.factory.support.BeanDefinitionBuilder;
 import org.springframework.beans.factory.xml.ParserContext;
 import org.springframework.util.StringUtils;
+import org.springframework.util.xml.DomUtils;
 import org.w3c.dom.Element;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
@@ -78,6 +79,29 @@
     }
 
     /**
+     * Get the text context of first child element matching the local name and 
namespace
+     * @param parent The element for which to locate the child
+     * @param ns The namespace to match, or null for any namespace
+     * @param localName The local name to match, or null for any local name
+     * @return The text content of the first child matching the criteria 
+     *         or null if element not found
+     */
+    public static String getChildElementText(Element parent, String ns, String 
localName) {
+        List<Element> elements = getChildElements(parent);
+        
+        for(Element element : elements) {
+            if((ns == null || ns.equals(element.getNamespaceURI()) &&
+                    (localName == null || 
localName.equals(element.getLocalName())))) {
+                return DomUtils.getTextValue(element);
+            }
+        }
+        
+        return null;
+    }
+    
+    
+    
+    /**
      * Parse specific Spring elements, bean and ref
      * @param parent The element in which we will look for Spring elements
      * @param parserContext The Spring parser context

Modified: 
mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/config/spring/UserManagerBeanDefinitionParser.java
URL: 
http://svn.apache.org/viewvc/mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/config/spring/UserManagerBeanDefinitionParser.java?rev=676304&r1=676303&r2=676304&view=diff
==============================================================================
--- 
mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/config/spring/UserManagerBeanDefinitionParser.java
 (original)
+++ 
mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/config/spring/UserManagerBeanDefinitionParser.java
 Sun Jul 13 04:51:10 2008
@@ -26,6 +26,7 @@
 import 
org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
 import org.springframework.beans.factory.xml.ParserContext;
 import org.springframework.util.StringUtils;
+import org.springframework.util.xml.DomUtils;
 import org.w3c.dom.Element;
 
 /**
@@ -51,6 +52,7 @@
                     element.getAttribute("encrypt-passwords").equals("true")) {
                 builder.addPropertyValue("encryptPasswords", true);
             }
+            builder.setInitMethodName("configure");
         } else {
             Element dsElm = SpringUtil.getChildElement(element, 
                     FtpServerNamespaceHandler.FTPSERVER_NS, "data-source");
@@ -66,6 +68,20 @@
 
             }
             builder.addPropertyValue("dataSource", o);
+            
+            builder.addPropertyValue("sqlUserInsert",       getSql(element, 
"insert-user"));
+            builder.addPropertyValue("sqlUserUpdate",       getSql(element, 
"update-user"));
+            builder.addPropertyValue("sqlUserDelete",       getSql(element, 
"delete-user"));
+            builder.addPropertyValue("sqlUserSelect",       getSql(element, 
"select-user"));
+            builder.addPropertyValue("sqlUserSelectAll",    getSql(element, 
"select-all-users"));
+            builder.addPropertyValue("SqlUserAdmin",        getSql(element, 
"is-admin"));
+            builder.addPropertyValue("sqlUserAuthenticate", getSql(element, 
"authenticate"));
+            
+            builder.setInitMethodName("configure");
         }
     }
+    
+    private String getSql(Element element, String elmName) {
+        return SpringUtil.getChildElementText(element, 
FtpServerNamespaceHandler.FTPSERVER_NS, elmName);    
+    }
 }

Modified: 
mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/usermanager/DbUserManager.java
URL: 
http://svn.apache.org/viewvc/mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/usermanager/DbUserManager.java?rev=676304&r1=676303&r2=676304&view=diff
==============================================================================
--- 
mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/usermanager/DbUserManager.java
 (original)
+++ 
mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/usermanager/DbUserManager.java
 Sun Jul 13 04:51:10 2008
@@ -62,6 +62,10 @@
 
     private DataSource dataSource;
     private Connection cachedConnection;
+
+    // Set to true when the user manager has been configured, 
+    // used for lazy init.
+    private boolean configured = false;
     
     /**
      * Retrive the data source used by the user manager
@@ -192,9 +196,19 @@
     }
     
     /**
+     * Lazy init the user manager
+     */
+    private void lazyInit() {
+        if(!configured) {
+            configure();
+        }
+    }
+    
+    /**
      * Configure user manager.
      */
     public void configure() {
+        configured = true;
         
         if(dataSource == null) {
             throw new FtpServerConfigurationException("Required data source 
not provided");
@@ -328,6 +342,7 @@
      * Delete user. Delete the row from the table.
      */
     public synchronized void delete(String name) throws FtpException {
+        lazyInit();
         
         // create sql query
         HashMap<String, Object> map = new HashMap<String, Object>();
@@ -361,6 +376,7 @@
      * Save user. If new insert a new row, else update the existing row.
      */
     public synchronized void save(User user) throws FtpException {
+        lazyInit();
         
         // null value check
         if(user.getName() == null) {
@@ -511,6 +527,8 @@
      * User existance check.
      */
     public synchronized boolean doesExist(String name) throws FtpException {
+        lazyInit();
+        
         Statement stmt = null;
         ResultSet rs = null;
         try {
@@ -555,6 +573,8 @@
      */
     public synchronized String[] getAllUserNames() throws FtpException {
         
+        lazyInit();
+        
         Statement stmt = null;
         ResultSet rs = null;
         try {
@@ -662,6 +682,8 @@
      * User authentication.
      */
     public synchronized User authenticate(Authentication authentication) 
throws AuthenticationFailedException {
+        lazyInit();
+        
         if(authentication instanceof UsernamePasswordAuthentication) {
             UsernamePasswordAuthentication upauth = 
(UsernamePasswordAuthentication) authentication;
             

Modified: 
mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/usermanager/PropertiesUserManager.java
URL: 
http://svn.apache.org/viewvc/mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/usermanager/PropertiesUserManager.java?rev=676304&r1=676303&r2=676304&view=diff
==============================================================================
--- 
mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/usermanager/PropertiesUserManager.java
 (original)
+++ 
mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/usermanager/PropertiesUserManager.java
 Sun Jul 13 04:51:10 2008
@@ -106,9 +106,18 @@
     }
     
     /**
+     * Lazy init the user manager
+     */
+    private void lazyInit() {
+        if(!isConfigured) {
+            configure();
+        }
+    }
+    
+    /**
      * Configure user manager.
      */
-public void configure() {
+    public void configure() {
         isConfigured  = true;
         File dir = userDataFile.getParentFile();
         if( (!dir.exists()) && (!dir.mkdirs()) ) {
@@ -163,6 +172,7 @@
      * Save user data. Store the properties.
      */
     public synchronized void save(User usr) throws FtpException {
+        lazyInit();
         
        // null value check
        if(usr.getName() == null) {
@@ -236,6 +246,7 @@
      * After removing the corresponding from the properties, save the data.
      */
     public synchronized void delete(String usrName) throws FtpException {
+        lazyInit();
         
         // remove entries from properties
         String thisPrefix = PREFIX + usrName + '.';
@@ -297,7 +308,8 @@
      * Get all user names.
      */
     public synchronized String[] getAllUserNames() {
-
+        lazyInit();
+        
         // get all user names
         String suffix = '.' + ATTR_HOME;
         ArrayList<String> ulst = new ArrayList<String>();
@@ -322,6 +334,7 @@
      * Load user data.
      */
     public synchronized User getUserByName(String userName) {
+        lazyInit();
         
         if (!doesExist(userName)) {
             return null;
@@ -360,6 +373,8 @@
      * User existance check
      */
     public synchronized boolean doesExist(String name) {
+        lazyInit();
+        
         String key = PREFIX + name + '.' + ATTR_HOME;
         return userDataProp.containsKey(key);
     }
@@ -368,6 +383,7 @@
      * User authenticate method
      */
     public synchronized User authenticate(Authentication authentication) 
throws AuthenticationFailedException {
+        lazyInit();
         
         if(authentication instanceof UsernamePasswordAuthentication) {
             UsernamePasswordAuthentication upauth = 
(UsernamePasswordAuthentication) authentication;


Reply via email to