view src/kryshen/catalina/startup/UserConfig2.java @ 2:d11df03af52a

Fixed version of HomeUserDatabase.
author Mikhail Kryshen <mikhail@kryshen.net>
date Fri, 30 Oct 2009 03:54:56 +0300
parents bd283605f2ae
children
line wrap: on
line source

package kryshen.catalina.startup;

import java.io.File;
import java.util.Enumeration;
import org.apache.catalina.startup.UserDatabase;
import org.apache.catalina.startup.HostConfig;
import org.apache.catalina.startup.UserConfig;

/**
 *
 * @author Mikhail Kryshen
 */
public class UserConfig2 extends HostConfig {

    /**
     * The directory name to be searched for within each user home directory.
     */
    private String directoryName = "public_webapps";

    /**
     * The base directory containing user home directories.
     */
    private String homeBase = null;

    /**
     * The Java class name of the user database class we should use.
     */
    private String userClass =
            "org.apache.catalina.startup.PasswdUserDatabase";

    /**
     * UserConfig instance is required by UserDatabase.
     */
    private UserConfig userConfig = new UserConfig();

    /**
     * Return the directory name for user web applications.
     */
    public String getDirectoryName() {
        return (this.directoryName);
    }

    /**
     * Set the directory name for user web applications.
     *
     * @param directoryName The new directory name
     */
    public void setDirectoryName(String directoryName) {
        this.directoryName = directoryName;
        userConfig.setDirectoryName(directoryName);
    }

    /**
     * Return the base directory containing user home directories.
     */
    public String getHomeBase() {
        return (this.homeBase);
    }

    /**
     * Set the base directory containing user home directories.
     *
     * @param homeBase The new base directory
     */
    public void setHomeBase(String homeBase) {
        this.homeBase = homeBase;
        userConfig.setHomeBase(homeBase);
    }

    /** 
     * Return the user database class name for this component. 
     */
    public String getUserClass() {
        return (this.userClass);
    }

    /** 
     * Set the user database class name for this component. 
     */
    public void setUserClass(String userClass) {
        this.userClass = userClass;
        userConfig.setUserClass(userClass);
    }

    @Override
    protected void deployApps() {       
        // Load the user database object for this host
        UserDatabase database = null;
        try {
            Class clazz = Class.forName(userClass);
            database = (UserDatabase) clazz.newInstance();
            database.setUserConfig(userConfig);
        } catch (Exception e) {
            host.getLogger().error(sm.getString("userConfig.database"), e);
            return;
        }

        // Deploy the web application (if any) for each defined user
        Enumeration users = database.getUsers();
        while (users.hasMoreElements()) {
            String user = (String) users.nextElement();
            String home = database.getHome(user);
            deployUserApps(user, home);
        }
    }

    @Override
    protected void deployApps(String name) {
        throw new UnsupportedOperationException
                ("deployApps(String) is not supported.");
    }

    protected void deployUserApps(String user, String home) {
        File base = new File(home, directoryName);

        if (!base.exists() || !base.isDirectory()) {
            return;
        }

// TODO: deployWARs
//        // Deploy WARs, and loop if additional descriptors are found
//        deployWARs(appBase, appBase.list());

        // Deploy expanded folders
        deployDirectories(user, base, base.list());
    }

    /**
     * Deploy user webapp directories.
     */
    protected void deployDirectories(String user, File base, String[] files) {

        if (files == null) {
            return;
        }

        for (int i = 0; i < files.length; i++) {
            if (files[i].equalsIgnoreCase("META-INF")) {
                continue;
            }

            if (files[i].equalsIgnoreCase("WEB-INF")) {
                continue;
            }

            File dir = new File(base, files[i]);
            if (dir.isDirectory()) {
                String contextPath;
                
                if (files[i].equals("ROOT")) {
                    contextPath = "/~" + user;
                } else {
                    contextPath = "/~" + user + '/' + files[i].replace('#', '/');
                }

                if (isServiced(contextPath)) {
                    continue;
                }

                deployDirectory(contextPath, dir, dir.getAbsolutePath());
            }
        }
    }
}