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, 88 insertions(+), 64 deletions(-) [+]
line diff
     1.1 --- a/nbproject/project.properties	Fri Oct 30 05:15:03 2009 +0300
     1.2 +++ b/nbproject/project.properties	Sun Nov 01 02:19:04 2009 +0300
     1.3 @@ -32,7 +32,7 @@
     1.4      ${file.reference.catalina.jar}:\
     1.5      ${file.reference.tomcat-juli.jar}
     1.6  # Space-separated list of extra javac options
     1.7 -javac.compilerargs=
     1.8 +javac.compilerargs=-Xlint:unchecked
     1.9  javac.deprecation=false
    1.10  javac.source=1.5
    1.11  javac.target=1.5
    1.12 @@ -54,7 +54,7 @@
    1.13  javadoc.version=false
    1.14  javadoc.windowtitle=
    1.15  jnlp.codebase.type=local
    1.16 -jnlp.codebase.url=file:/home/kryshen/projects/own/tomcat-userconfig/dist
    1.17 +jnlp.codebase.url=file:/home/mikhail/projects/own/tomcat-userconfig/dist/
    1.18  jnlp.descriptor=application
    1.19  jnlp.enabled=false
    1.20  jnlp.offline-allowed=false
     2.1 --- a/src/kryshen/catalina/startup/HomesUserDatabase.java	Fri Oct 30 05:15:03 2009 +0300
     2.2 +++ b/src/kryshen/catalina/startup/HomesUserDatabase.java	Sun Nov 01 02:19:04 2009 +0300
     2.3 @@ -21,37 +21,84 @@
     2.4  
     2.5  import java.io.File;
     2.6  import java.util.Collections;
     2.7 -import java.util.HashMap;
     2.8 -import java.util.Map;
     2.9 +import java.util.Iterator;
    2.10  
    2.11  /**
    2.12 - * Concrete implementation of the <strong>UserDatabase</code> interface
    2.13 - * considers all directories in a directory whose pathname is specified
    2.14 - * to our constructor to be "home" directories for those users.
    2.15 + * Implementation of the <code>UserDatabase</code> interface
    2.16 + * lists all users by their home directories found in the specified
    2.17 + * base directory.
    2.18   *
    2.19 - * @author Craig R. McClanahan
    2.20   * @author Mikhail Kryshen
    2.21   */
    2.22  
    2.23  public final class HomesUserDatabase implements UserDatabase {
    2.24  
    2.25 -    // --------------------------------------------------- Instance Variables
    2.26 +    /**
    2.27 +     * Iterate over users with accessible home directories.
    2.28 +     */
    2.29 +    private class UserIterator implements Iterator<User> {
    2.30 +        final File homeBase;
    2.31 +        final String[] homes;
    2.32  
    2.33 -    /**
    2.34 -     * The set of home directories for all defined users, keyed by username.
    2.35 -     */
    2.36 -    private Map<String, File> homes = new HashMap<String, File>();
    2.37 +        User next;
    2.38 +        int index = 0;
    2.39  
    2.40 +        UserIterator(File homeBase) {
    2.41 +            this.homeBase = homeBase;
    2.42 +            this.homes = homeBase.list();
    2.43 +
    2.44 +            findNext();
    2.45 +        }
    2.46 +
    2.47 +        /**
    2.48 +         * Find the next accessible home directory in the list and
    2.49 +         * create appropriate User to be returned by <code>next()</code>.
    2.50 +         */
    2.51 +        void findNext() {
    2.52 +            next = null;
    2.53 +
    2.54 +            while (next == null && index < homes.length) {
    2.55 +                final String name = homes[index];
    2.56 +                final File home = new File(homeBase, name);               
    2.57 +
    2.58 +                index++;
    2.59 +                
    2.60 +                if (!home.isDirectory() || !home.canExecute()) {
    2.61 +                    continue;
    2.62 +                }
    2.63 +
    2.64 +                next = new User() {
    2.65 +                    public String getName() {
    2.66 +                        return name;
    2.67 +                    }
    2.68 +
    2.69 +                    public File getHome() {
    2.70 +                        return home;
    2.71 +                    }
    2.72 +                };                
    2.73 +            }
    2.74 +        }
    2.75 +
    2.76 +        public boolean hasNext() {
    2.77 +            return next != null;
    2.78 +        }
    2.79 +
    2.80 +        public User next() {
    2.81 +            User current = next;
    2.82 +            findNext();
    2.83 +            return current;
    2.84 +        }
    2.85 +
    2.86 +        public void remove() {
    2.87 +            throw new UnsupportedOperationException("Not supported yet.");
    2.88 +        }
    2.89 +    }
    2.90  
    2.91      /**
    2.92       * The UserConfig listener with which we are associated.
    2.93       */
    2.94      private UserConfig userConfig = null;
    2.95  
    2.96 -
    2.97 -    // ----------------------------------------------------------- Properties
    2.98 -
    2.99 -
   2.100      /**
   2.101       * Return the UserConfig listener with which we are associated.
   2.102       */
   2.103 @@ -59,7 +106,6 @@
   2.104          return (this.userConfig);
   2.105      }
   2.106  
   2.107 -
   2.108      /**
   2.109       * Set the UserConfig listener with which we are associated.
   2.110       *
   2.111 @@ -67,45 +113,19 @@
   2.112       */
   2.113      public void setUserConfig(UserConfig userConfig) {
   2.114          this.userConfig = userConfig;
   2.115 -        init();
   2.116 -
   2.117      }
   2.118  
   2.119 +    /**
   2.120 +     * Returns Iterator for the list of users.
   2.121 +     */
   2.122 +    public Iterator<User> iterator() {
   2.123 +        File homeBase = new File(userConfig.getHomeBase());
   2.124  
   2.125 -    // ------------------------------------------------------- Public Methods
   2.126 +        if (!homeBase.exists() || !homeBase.isDirectory()) {
   2.127 +            // PENDING: throw Exception instead?
   2.128 +            return Collections.<User>emptyList().iterator();
   2.129 +        }
   2.130  
   2.131 -
   2.132 -    /**
   2.133 -     * Return mapping between usernames and home directories.
   2.134 -     */
   2.135 -    public Map<String, File> getUserHomes() {
   2.136 -        return Collections.unmodifiableMap(homes);
   2.137 -    }
   2.138 -
   2.139 -    // ------------------------------------------------------ Private Methods
   2.140 -
   2.141 -
   2.142 -    /**
   2.143 -     * Initialize our set of users and home directories.
   2.144 -     */
   2.145 -    private void init() {
   2.146 -        String homeBase = userConfig.getHomeBase();
   2.147 -        File homeBaseDir = new File(homeBase);
   2.148 -
   2.149 -        if (!homeBaseDir.exists() || !homeBaseDir.isDirectory()) {
   2.150 -            return;
   2.151 -        }
   2.152 -        
   2.153 -        String homeBaseFiles[] = homeBaseDir.list();
   2.154 -
   2.155 -        for (int i = 0; i < homeBaseFiles.length; i++) {
   2.156 -            File homeDir = new File(homeBaseDir, homeBaseFiles[i]);
   2.157 -
   2.158 -            if (!homeDir.isDirectory() || !homeDir.canExecute()) {
   2.159 -                continue;
   2.160 -            }
   2.161 -
   2.162 -            homes.put(homeBaseFiles[i], homeDir);
   2.163 -        }
   2.164 +        return new UserIterator(homeBase);
   2.165      }
   2.166  }
     3.1 --- a/src/kryshen/catalina/startup/UserConfig.java	Fri Oct 30 05:15:03 2009 +0300
     3.2 +++ b/src/kryshen/catalina/startup/UserConfig.java	Sun Nov 01 02:19:04 2009 +0300
     3.3 @@ -1,7 +1,6 @@
     3.4  package kryshen.catalina.startup;
     3.5  
     3.6  import java.io.File;
     3.7 -import java.util.Map;
     3.8  import org.apache.catalina.startup.HostConfig;
     3.9  
    3.10  /**
    3.11 @@ -86,9 +85,8 @@
    3.12          }
    3.13  
    3.14          // Deploy the web application (if any) for each defined user
    3.15 -        Map<String, File> userHomes = database.getUserHomes();
    3.16 -        for (Map.Entry<String, File> entry :userHomes.entrySet()) {
    3.17 -            deployUserApps(entry.getKey(), entry.getValue());
    3.18 +        for (UserDatabase.User user : database) {
    3.19 +            deployUserApps(user.getName(), user.getHome());
    3.20          }
    3.21      }
    3.22  
     4.1 --- a/src/kryshen/catalina/startup/UserDatabase.java	Fri Oct 30 05:15:03 2009 +0300
     4.2 +++ b/src/kryshen/catalina/startup/UserDatabase.java	Sun Nov 01 02:19:04 2009 +0300
     4.3 @@ -1,28 +1,34 @@
     4.4  package kryshen.catalina.startup;
     4.5  
     4.6  import java.io.File;
     4.7 -import java.util.Map;
     4.8 +import java.util.Iterator;
     4.9  
    4.10  /**
    4.11   *
    4.12   * @author Mikhail Kryshen
    4.13   */
    4.14 -public interface UserDatabase {
    4.15 +public interface UserDatabase extends Iterable<UserDatabase.User> {
    4.16 +
    4.17 +    interface User {
    4.18 +        String getName();
    4.19 +
    4.20 +        File getHome();
    4.21 +    }
    4.22  
    4.23      /**
    4.24       * Return the UserConfig listener with which we are associated.
    4.25       */
    4.26 -    public UserConfig getUserConfig();
    4.27 +    UserConfig getUserConfig();
    4.28  
    4.29      /**
    4.30       * Set the UserConfig listener with which we are associated.
    4.31       *
    4.32       * @param userConfig The new UserConfig listener
    4.33       */
    4.34 -    public void setUserConfig(UserConfig userConfig);
    4.35 +    void setUserConfig(UserConfig userConfig);
    4.36  
    4.37      /**
    4.38 -     * Return mapping between usernames and homes.
    4.39 +     * Returns Iterator for the list of users.
    4.40       */
    4.41 -     Map<String, File> getUserHomes();
    4.42 +    Iterator<User> iterator();
    4.43  }