view src/kryshen/catalina/startup/HomesUserDatabase.java @ 7:ca0b81d28307

Compatibility fixes.
author Mikhail Kryshen <mikhail@kryshen.net>
date Sun, 01 Nov 2009 04:25:39 +0300
parents 66a0db4d54aa
children
line wrap: on
line source

/*
 * 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 kryshen.catalina.startup;


import java.io.File;
import java.util.Collections;
import java.util.Iterator;

/**
 * Implementation of the <code>UserDatabase</code> interface
 * lists all users by their home directories found in the specified
 * base directory.
 *
 * @author Mikhail Kryshen
 */

public final class HomesUserDatabase implements UserDatabase {

    /**
     * Iterates 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);               

                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.");
        }
    }

    /**
     * The UserConfig listener with which we are associated.
     */
    private UserConfig userConfig = null;

    /**
     * Return the UserConfig listener with which we are associated.
     */
    public UserConfig getUserConfig() {
        return (this.userConfig);
    }

    /**
     * Set the UserConfig listener with which we are associated.
     *
     * @param userConfig The new UserConfig listener
     */
    public void setUserConfig(UserConfig userConfig) {
        this.userConfig = userConfig;
    }

    /**
     * Returns Iterator for the list of users.
     */
    public Iterator<User> iterator() {
        File homeBase = new File(userConfig.getHomeBase());

        if (!homeBase.exists() || !homeBase.isDirectory()) {
            // PENDING: throw Exception instead?
            return Collections.<User>emptyList().iterator();
        }

        return new UserIterator(homeBase);
    }
}