<commit_before>/*
 * Copyright 2003-2006 <NAME> <winstone-devel at lists sourceforge net>
 * Distributed under the terms of either:
 * - the common development and distribution license (CDDL), v1.0; or
 * - the GNU Lesser General Public License, v2.1 or later
 */
package winstone;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import net.winstone.cluster.Cluster;

import net.winstone.WinstoneException;
import org.slf4j.LoggerFactory;

/**
 * Manages the references to individual hosts within the container. This object handles the mapping of ip addresses and hostnames to groups
 * of webapps, and init and shutdown of any hosts it manages.
 * 
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version $Id: HostGroup.java,v 1.4 2006/03/24 17:24:21 rickknowles Exp $
 */
public class HostGroup {

    protected static org.slf4j.Logger logger = LoggerFactory.getLogger(HostGroup.class);
    private final static transient String DEFAULT_HOSTNAME = "default";
    /** map of host configuration */
    private final Map<String, HostConfiguration> hostConfigs;
    /** default host name if host mode is on */
    private String defaultHostName;

    /**
     * Build a new instance of HostGroup.
     * 
     * @param commonLibCL
     * @param commonLibCLPaths
     * @param args
     * @throws IOException
     */
    public HostGroup(final ClassLoader commonLibCL, final String jspClasspath, final Map<String, String> args) throws IOException {
        this(null, null, commonLibCL, jspClasspath, args);
    }

    public HostGroup(final Cluster cluster, final ObjectPool objectPool, final ClassLoader commonLibCL, final String jspClasspath, final Map<String, String> args) throws IOException {
        super();
        this.hostConfigs = new HashMap<String, HostConfiguration>();

        // Is this the single or multiple configuration ? Check args
        String hostDirName = (String) args.get("hostsDir");
        String webappsDirName = (String) args.get("webappsDir");

        // If host mode
        if (hostDirName == null) {
            addHostConfiguration(webappsDirName, DEFAULT_HOSTNAME, cluster, objectPool, commonLibCL, jspClasspath, args);
            this.defaultHostName = DEFAULT_HOSTNAME;
            logger.debug("Initialized in non-virtual-host mode");
        } // Otherwise multi-host mode
        else {
            initMultiHostDir(hostDirName, cluster, objectPool, commonLibCL, jspClasspath, args);
            logger.debug("Initialized in virtual host mode with {} hosts: hostnames - {}", this.hostConfigs.size() + "", this.hostConfigs.keySet() + "");
        }
    }

    public HostConfiguration getHostByName(String hostname) {
        HostConfiguration host = (HostConfiguration) this.hostConfigs.get(hostname);
        return host != null ? host : this.hostConfigs.get(this.defaultHostName);
    }

    public void destroy() {
        Set<String> hostnames = new HashSet<String>(this.hostConfigs.keySet());
        for (Iterator<String> i = hostnames.iterator(); i.hasNext();) {
            String hostname = i.next();
            HostConfiguration host = (HostConfiguration) this.hostConfigs.get(hostname);
            host.destroy();
            this.hostConfigs.remove(hostname);
        }
        this.hostConfigs.clear();
    }

    /**
     * Initialize an host.
     * 
     * @param webappsDirName
     * @param hostname
     * @param cluster
     * @param objectPool
     * @param commonLibCL
     * @param commonLibCLPaths
     * @param args
     * @throws IOException
     */
    protected final void addHostConfiguration(String webappsDirName, String hostname, Cluster cluster, ObjectPool objectPool,
            ClassLoader commonLibCL, String jspClasspath, Map<String, String> args) throws IOException {
        logger.debug("Deploying host found at {}", hostname);
        HostConfiguration config = new HostConfiguration(hostname, cluster, objectPool, commonLibCL, jspClasspath, args, webappsDirName);
        this.hostConfigs.put(hostname, config);
    }

    /**
     * Initialize a group host.
     * 
     * @param hostsDirName
     * @param cluster
     * @param objectPool
     * @param commonLibCL
     * @param commonLibCLPaths
     * @param args
     * @throws IOException<commit_msg>check some issue<commit_after>/*
 * Copyright 2003-2006 <NAME> <winstone-devel at lists sourceforge net>
 * Distributed under the terms of either:
 * - the common development and distribution license (CDDL), v1.0; or
 * - the GNU Lesser General Public License, v2.1 or later
 */
package winstone;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import net.winstone.cluster.Cluster;

import net.winstone.WinstoneException;
import org.slf4j.LoggerFactory;

/**
 * Manages the references to individual hosts within the container. This object handles the mapping of ip addresses and hostnames to groups
 * of webapps, and init and shutdown of any hosts it manages.
 * 
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version $Id: HostGroup.java,v 1.4 2006/03/24 17:24:21 rickknowles Exp $
 */
public class HostGroup {

    protected static org.slf4j.Logger logger = LoggerFactory.getLogger(HostGroup.class);
    private final static transient String DEFAULT_HOSTNAME = "default";
    /** map of host configuration */
    private final Map<String, HostConfiguration> hostConfigs;
    /** default host name if host mode is on */
    private String defaultHostName;

    /**
     * Build a new instance of HostGroup.
     * 
     * @param commonLibCL
     * @param commonLibCLPaths
     * @param args
     * @throws IOException
     */
    public HostGroup(final ClassLoader commonLibCL, final String jspClasspath, final Map<String, String> args) throws IOException {
        this(null, null, commonLibCL, jspClasspath, args);
    }

    public HostGroup(final Cluster cluster, final ObjectPool objectPool, final ClassLoader commonLibCL, final String jspClasspath, final Map<String, String> args) throws IOException {
        super();
        this.hostConfigs = new HashMap<String, HostConfiguration>();

        // Is this the single or multiple configuration ? Check args
        String hostDirName = (String) args.get("hostsDir");
        String webappsDirName = (String) args.get("webappsDir");

        // If host mode
        if (hostDirName == null) {
            addHostConfiguration(webappsDirName, DEFAULT_HOSTNAME, cluster, objectPool, commonLibCL, jspClasspath, args);
            this.defaultHostName = DEFAULT_HOSTNAME;
            logger.debug("Initialized in non-virtual-host mode");
        } // Otherwise multi-host mode
        else {
            initMultiHostDir(hostDirName, cluster, objectPool, commonLibCL, jspClasspath, args);
            logger.debug("Initialized in virtual host mode with {} hosts: hostnames - {}", this.hostConfigs.size() + "", this.hostConfigs.keySet() + "");
        }
    }

    public HostConfiguration getHostByName(final String hostname) {
        HostConfiguration host = this.hostConfigs.get(hostname);
        return host != null ? host : this.hostConfigs.get(this.defaultHostName);
    }

    public void destroy() {
        if (this.hostConfigs != null) {
            // obtain a copy of name
            Set<String> hostnames = new HashSet<String>(this.hostConfigs.keySet());
            for (Iterator<String> i = hostnames.iterator(); i.hasNext();) {
                String hostname = i.next();
                hostConfigs.get(hostname).destroy();
                hostConfigs.remove(hostname);
            }
            this.hostConfigs.clear();
        }
    }

    /**
     * Initialize an host.
     * 
     * @param webappsDirName
     * @param hostname
     * @param cluster
     * @param objectPool
     * @param commonLibCL
     * @param commonLibCLPaths
     * @param args
     * @throws IOException
     */
    protected final void addHostConfiguration(String webappsDirName, String hostname, Cluster cluster, ObjectPool objectPool,
            ClassLoader commonLibCL, String jspClasspath, Map<String, String> args) throws IOException {
        logger.debug("Deploying host found at {}", hostname);
        HostConfiguration config = new HostConfiguration(hostname, cluster, objectPool, commonLibCL, jspClasspath, args, webappsDirName);
        this.hostConfigs.put(hostname, config);
    }

    /**
     * Initialize a group host.
     * 
     * @param hostsDirName
     * @param cluster
     * @param objectPool
     * @param commonLibCL
     * @param commonLibCLPaths
     * @param args
     * @throws IOException
{'fraction_non_alphanumeric': 0.09830276796901698, 'fraction_numerical': 0.006834491399931655, 'mean_word_length': 3.0367816091954025, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7651622', 'n_tokens_mistral': 2447, 'n_tokens_neox': 2381, 'n_words': 713}