view src/kryshen/catalina/startup/HomesUserDatabase.java @ 4:6ef7cbf5c8d6

Cleanup.
author Mikhail Kryshen <mikhail@kryshen.net>
date Fri, 30 Oct 2009 05:15:03 +0300
parents 3ab011b6e6e8
children 64c05808a046
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.HashMap;
import java.util.Map;

/**
 * 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.
 *
 * @author Craig R. McClanahan
 * @author Mikhail Kryshen
 */

public final class HomesUserDatabase implements UserDatabase {

    // --------------------------------------------------- Instance Variables

    /**
     * The set of home directories for all defined users, keyed by username.
     */
    private Map<String, File> homes = new HashMap<String, File>();


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


    // ----------------------------------------------------------- Properties


    /**
     * 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;
        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.
     */
    private void init() {
        String homeBase = userConfig.getHomeBase();
        File homeBaseDir = new File(homeBase);

        if (!homeBaseDir.exists() || !homeBaseDir.isDirectory()) {
            return;
        }
        
        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);
        }
    }
}