001: /*
002: * Copyright 2003-2006 Rick Knowles <winstone-devel at lists sourceforge net>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: */
007: package winstone;
008:
009: import java.io.File;
010: import java.io.IOException;
011: import java.util.HashSet;
012: import java.util.Hashtable;
013: import java.util.Iterator;
014: import java.util.Map;
015: import java.util.Set;
016:
017: /**
018: * Manages the references to individual hosts within the container. This object handles
019: * the mapping of ip addresses and hostnames to groups of webapps, and init and
020: * shutdown of any hosts it manages.
021: *
022: * @author <a href="mailto:rick_knowles@hotmail.com">Rick Knowles</a>
023: * @version $Id: HostGroup.java,v 1.4 2006/03/24 17:24:21 rickknowles Exp $
024: */
025: public class HostGroup {
026:
027: private final static String DEFAULT_HOSTNAME = "default";
028:
029: // private Map args;
030: private Map hostConfigs;
031: private String defaultHostName;
032:
033: public HostGroup(Cluster cluster, ObjectPool objectPool,
034: ClassLoader commonLibCL, File commonLibCLPaths[], Map args)
035: throws IOException {
036: // this.args = args;
037: this .hostConfigs = new Hashtable();
038:
039: // Is this the single or multiple configuration ? Check args
040: String hostDirName = (String) args.get("hostsDir");
041: String webappsDirName = (String) args.get("webappsDir");
042:
043: // If host mode
044: if (hostDirName == null) {
045: initHost(webappsDirName, DEFAULT_HOSTNAME, cluster,
046: objectPool, commonLibCL, commonLibCLPaths, args);
047: this .defaultHostName = DEFAULT_HOSTNAME;
048: Logger.log(Logger.DEBUG, Launcher.RESOURCES,
049: "HostGroup.InitSingleComplete", new String[] {
050: this .hostConfigs.size() + "",
051: this .hostConfigs.keySet() + "" });
052: }
053: // Otherwise multi-webapp mode
054: else {
055: initMultiHostDir(hostDirName, cluster, objectPool,
056: commonLibCL, commonLibCLPaths, args);
057: Logger.log(Logger.DEBUG, Launcher.RESOURCES,
058: "HostGroup.InitMultiComplete", new String[] {
059: this .hostConfigs.size() + "",
060: this .hostConfigs.keySet() + "" });
061: }
062: }
063:
064: public HostConfiguration getHostByName(String hostname) {
065: if ((hostname != null) && (this .hostConfigs.size() > 1)) {
066: HostConfiguration host = (HostConfiguration) this .hostConfigs
067: .get(hostname);
068: if (host != null) {
069: return host;
070: }
071: }
072: return (HostConfiguration) this .hostConfigs
073: .get(this .defaultHostName);
074: }
075:
076: public void destroy() {
077: Set hostnames = new HashSet(this .hostConfigs.keySet());
078: for (Iterator i = hostnames.iterator(); i.hasNext();) {
079: String hostname = (String) i.next();
080: HostConfiguration host = (HostConfiguration) this .hostConfigs
081: .get(hostname);
082: host.destroy();
083: this .hostConfigs.remove(hostname);
084: }
085: this .hostConfigs.clear();
086: }
087:
088: protected void initHost(String webappsDirName, String hostname,
089: Cluster cluster, ObjectPool objectPool,
090: ClassLoader commonLibCL, File commonLibCLPaths[], Map args)
091: throws IOException {
092: Logger.log(Logger.DEBUG, Launcher.RESOURCES,
093: "HostGroup.DeployingHost", hostname);
094: HostConfiguration config = new HostConfiguration(hostname,
095: cluster, objectPool, commonLibCL, commonLibCLPaths,
096: args, webappsDirName);
097: this .hostConfigs.put(hostname, config);
098: }
099:
100: protected void initMultiHostDir(String hostsDirName,
101: Cluster cluster, ObjectPool objectPool,
102: ClassLoader commonLibCL, File commonLibCLPaths[], Map args)
103: throws IOException {
104: if (hostsDirName == null) {
105: hostsDirName = "hosts";
106: }
107: File hostsDir = new File(hostsDirName);
108: if (!hostsDir.exists()) {
109: throw new WinstoneException(Launcher.RESOURCES.getString(
110: "HostGroup.HostsDirNotFound", hostsDirName));
111: } else if (!hostsDir.isDirectory()) {
112: throw new WinstoneException(Launcher.RESOURCES.getString(
113: "HostGroup.HostsDirIsNotDirectory", hostsDirName));
114: } else {
115: File children[] = hostsDir.listFiles();
116: if ((children == null) || (children.length == 0)) {
117: throw new WinstoneException(Launcher.RESOURCES
118: .getString("HostGroup.HostsDirIsEmpty",
119: hostsDirName));
120: }
121: for (int n = 0; n < children.length; n++) {
122: String childName = children[n].getName();
123:
124: // Mount directories as host dirs
125: if (children[n].isDirectory()) {
126: if (!this.hostConfigs.containsKey(childName)) {
127: initHost(children[n].getCanonicalPath(),
128: childName, cluster, objectPool,
129: commonLibCL, commonLibCLPaths, args);
130: }
131: }
132: if ((defaultHostName == null)
133: || childName.equals(DEFAULT_HOSTNAME)) {
134: this.defaultHostName = childName;
135: }
136: }
137: }
138: }
139: }
|