/*
 * Copyright 2009 Mikhail Kryshen
 *
 * Licensed 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.userconfig;

import java.io.File;
import org.apache.juli.logging.LogFactory;

/**
 * Event listener for Host that deploys and updates applications 
 * provided by users. Files in the specified base directory are 
 * considered tp be user home directories.
 *
 * @author Mikhail Kryshen
 */
public class HomesUserConfig extends UserConfig {

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


    public HomesUserConfig() {
        log = LogFactory.getLog(HomesUserConfig.class);
    }

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

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

    @Override
    protected void deployUserApps() {
        File homeBaseFile = new File(homeBase);

        if (!homeBaseFile.isDirectory()) {
            log.error("Invalid home base.");
            return;
        }

        String[] homes = homeBaseFile.list();

        for (String name : homes) {
            File home = new File(homeBaseFile, name);
            
            if (!home.isDirectory() /* || !home.canExecute() */) {
                continue;
            }

            deployUserApps(name, home);
        }
    }
}