view src/kryshen/catalina/startup/UserConfig.java @ 6:66a0db4d54aa

Minor.
author Mikhail Kryshen <mikhail@kryshen.net>
date Sun, 01 Nov 2009 02:27:18 +0300
parents 64c05808a046
children ca0b81d28307
line wrap: on
line source

package kryshen.catalina.startup;

import java.io.File;
import org.apache.catalina.startup.HostConfig;

/**
 *
 * @author Mikhail Kryshen
 */
public class UserConfig 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";

    /**
     * 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;
    }

    /**
     * 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;
    }

    /** 
     * 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;
    }

    @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(this);
        } catch (Exception e) {
            host.getLogger().error(sm.getString("userConfig.database"), e);
            return;
        }

        // Deploy the web application (if any) for each defined user
        for (UserDatabase.User user : database) {
            deployUserApps(user.getName(), user.getHome());
        }
    }

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

    protected void deployUserApps(String user, File 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());
            }
        }
    }
}