changeset 5:64c05808a046

Refactored UserDatabase.
author Mikhail Kryshen <mikhail@kryshen.net>
date Sun, 01 Nov 2009 02:19:04 +0300
parents 6ef7cbf5c8d6
children 66a0db4d54aa
files nbproject/project.properties src/kryshen/catalina/startup/HomesUserDatabase.java src/kryshen/catalina/startup/UserConfig.java src/kryshen/catalina/startup/UserDatabase.java
diffstat 4 files changed, 85 insertions(+), 61 deletions(-) [+]
line wrap: on
line diff
--- a/nbproject/project.properties	Fri Oct 30 05:15:03 2009 +0300
+++ b/nbproject/project.properties	Sun Nov 01 02:19:04 2009 +0300
@@ -32,7 +32,7 @@
     ${file.reference.catalina.jar}:\
     ${file.reference.tomcat-juli.jar}
 # Space-separated list of extra javac options
-javac.compilerargs=
+javac.compilerargs=-Xlint:unchecked
 javac.deprecation=false
 javac.source=1.5
 javac.target=1.5
@@ -54,7 +54,7 @@
 javadoc.version=false
 javadoc.windowtitle=
 jnlp.codebase.type=local
-jnlp.codebase.url=file:/home/kryshen/projects/own/tomcat-userconfig/dist
+jnlp.codebase.url=file:/home/mikhail/projects/own/tomcat-userconfig/dist/
 jnlp.descriptor=application
 jnlp.enabled=false
 jnlp.offline-allowed=false
--- a/src/kryshen/catalina/startup/HomesUserDatabase.java	Fri Oct 30 05:15:03 2009 +0300
+++ b/src/kryshen/catalina/startup/HomesUserDatabase.java	Sun Nov 01 02:19:04 2009 +0300
@@ -21,37 +21,84 @@
 
 import java.io.File;
 import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
+import java.util.Iterator;
 
 /**
- * Concrete implementation of the <strong>UserDatabase</code> interface
- * considers all directories in a directory whose pathname is specified
- * to our constructor to be "home" directories for those users.
+ * Implementation of the <code>UserDatabase</code> interface
+ * lists all users by their home directories found in the specified
+ * base directory.
  *
- * @author Craig R. McClanahan
  * @author Mikhail Kryshen
  */
 
 public final class HomesUserDatabase implements UserDatabase {
 
-    // --------------------------------------------------- Instance Variables
+    /**
+     * Iterate over users with accessible home directories.
+     */
+    private class UserIterator implements Iterator<User> {
+        final File homeBase;
+        final String[] homes;
+
+        User next;
+        int index = 0;
+
+        UserIterator(File homeBase) {
+            this.homeBase = homeBase;
+            this.homes = homeBase.list();
+
+            findNext();
+        }
+
+        /**
+         * Find the next accessible home directory in the list and
+         * create appropriate User to be returned by <code>next()</code>.
+         */
+        void findNext() {
+            next = null;
+
+            while (next == null && index < homes.length) {
+                final String name = homes[index];
+                final File home = new File(homeBase, name);               
 
-    /**
-     * The set of home directories for all defined users, keyed by username.
-     */
-    private Map<String, File> homes = new HashMap<String, File>();
+                index++;
+                
+                if (!home.isDirectory() || !home.canExecute()) {
+                    continue;
+                }
+
+                next = new User() {
+                    public String getName() {
+                        return name;
+                    }
 
+                    public File getHome() {
+                        return home;
+                    }
+                };                
+            }
+        }
+
+        public boolean hasNext() {
+            return next != null;
+        }
+
+        public User next() {
+            User current = next;
+            findNext();
+            return current;
+        }
+
+        public void remove() {
+            throw new UnsupportedOperationException("Not supported yet.");
+        }
+    }
 
     /**
      * The UserConfig listener with which we are associated.
      */
     private UserConfig userConfig = null;
 
-
-    // ----------------------------------------------------------- Properties
-
-
     /**
      * Return the UserConfig listener with which we are associated.
      */
@@ -59,7 +106,6 @@
         return (this.userConfig);
     }
 
-
     /**
      * Set the UserConfig listener with which we are associated.
      *
@@ -67,45 +113,19 @@
      */
     public void setUserConfig(UserConfig userConfig) {
         this.userConfig = userConfig;
-        init();
-
     }
 
-
-    // ------------------------------------------------------- Public Methods
-
-
-    /**
-     * Return mapping between usernames and home directories.
-     */
-    public Map<String, File> getUserHomes() {
-        return Collections.unmodifiableMap(homes);
-    }
-
-    // ------------------------------------------------------ Private Methods
-
-
     /**
-     * Initialize our set of users and home directories.
+     * Returns Iterator for the list of users.
      */
-    private void init() {
-        String homeBase = userConfig.getHomeBase();
-        File homeBaseDir = new File(homeBase);
+    public Iterator<User> iterator() {
+        File homeBase = new File(userConfig.getHomeBase());
 
-        if (!homeBaseDir.exists() || !homeBaseDir.isDirectory()) {
-            return;
+        if (!homeBase.exists() || !homeBase.isDirectory()) {
+            // PENDING: throw Exception instead?
+            return Collections.<User>emptyList().iterator();
         }
-        
-        String homeBaseFiles[] = homeBaseDir.list();
 
-        for (int i = 0; i < homeBaseFiles.length; i++) {
-            File homeDir = new File(homeBaseDir, homeBaseFiles[i]);
-
-            if (!homeDir.isDirectory() || !homeDir.canExecute()) {
-                continue;
-            }
-
-            homes.put(homeBaseFiles[i], homeDir);
-        }
+        return new UserIterator(homeBase);
     }
 }
--- a/src/kryshen/catalina/startup/UserConfig.java	Fri Oct 30 05:15:03 2009 +0300
+++ b/src/kryshen/catalina/startup/UserConfig.java	Sun Nov 01 02:19:04 2009 +0300
@@ -1,7 +1,6 @@
 package kryshen.catalina.startup;
 
 import java.io.File;
-import java.util.Map;
 import org.apache.catalina.startup.HostConfig;
 
 /**
@@ -86,9 +85,8 @@
         }
 
         // Deploy the web application (if any) for each defined user
-        Map<String, File> userHomes = database.getUserHomes();
-        for (Map.Entry<String, File> entry :userHomes.entrySet()) {
-            deployUserApps(entry.getKey(), entry.getValue());
+        for (UserDatabase.User user : database) {
+            deployUserApps(user.getName(), user.getHome());
         }
     }
 
--- a/src/kryshen/catalina/startup/UserDatabase.java	Fri Oct 30 05:15:03 2009 +0300
+++ b/src/kryshen/catalina/startup/UserDatabase.java	Sun Nov 01 02:19:04 2009 +0300
@@ -1,28 +1,34 @@
 package kryshen.catalina.startup;
 
 import java.io.File;
-import java.util.Map;
+import java.util.Iterator;
 
 /**
  *
  * @author Mikhail Kryshen
  */
-public interface UserDatabase {
+public interface UserDatabase extends Iterable<UserDatabase.User> {
+
+    interface User {
+        String getName();
+
+        File getHome();
+    }
 
     /**
      * Return the UserConfig listener with which we are associated.
      */
-    public UserConfig getUserConfig();
+    UserConfig getUserConfig();
 
     /**
      * Set the UserConfig listener with which we are associated.
      *
      * @param userConfig The new UserConfig listener
      */
-    public void setUserConfig(UserConfig userConfig);
+    void setUserConfig(UserConfig userConfig);
 
     /**
-     * Return mapping between usernames and homes.
+     * Returns Iterator for the list of users.
      */
-     Map<String, File> getUserHomes();
+    Iterator<User> iterator();
 }