1 7 package winstone; 8 9 import java.io.File ; 10 import java.io.IOException ; 11 import java.util.HashSet ; 12 import java.util.Hashtable ; 13 import java.util.Iterator ; 14 import java.util.Map ; 15 import java.util.Set ; 16 17 25 public class HostGroup { 26 27 private final static String DEFAULT_HOSTNAME = "default"; 28 29 private Map hostConfigs; 31 private String defaultHostName; 32 33 public HostGroup(Cluster cluster, 34 ObjectPool objectPool, ClassLoader commonLibCL, 35 File commonLibCLPaths[], Map args) throws IOException { 36 this.hostConfigs = new Hashtable (); 38 39 String hostDirName = (String ) args.get("hostsDir"); 41 String webappsDirName = (String ) args.get("webappsDir"); 42 43 if (hostDirName == null) { 45 initHost(webappsDirName, DEFAULT_HOSTNAME, cluster, objectPool, commonLibCL, 46 commonLibCLPaths, args); 47 this.defaultHostName = DEFAULT_HOSTNAME; 48 Logger.log(Logger.DEBUG, Launcher.RESOURCES, "HostGroup.InitSingleComplete", 49 new String [] {this.hostConfigs.size() + "", this.hostConfigs.keySet() + ""}); 50 } 51 else { 53 initMultiHostDir(hostDirName, cluster, objectPool, commonLibCL, 54 commonLibCLPaths, args); 55 Logger.log(Logger.DEBUG, Launcher.RESOURCES, "HostGroup.InitMultiComplete", 56 new String [] {this.hostConfigs.size() + "", this.hostConfigs.keySet() + ""}); 57 } 58 } 59 60 public HostConfiguration getHostByName(String hostname) { 61 if ((hostname != null) && (this.hostConfigs.size() > 1)) { 62 HostConfiguration host = (HostConfiguration) this.hostConfigs.get(hostname); 63 if (host != null) { 64 return host; 65 } 66 } 67 return (HostConfiguration) this.hostConfigs.get(this.defaultHostName); 68 } 69 70 public void destroy() { 71 Set hostnames = new HashSet (this.hostConfigs.keySet()); 72 for (Iterator i = hostnames.iterator(); i.hasNext(); ) { 73 String hostname = (String ) i.next(); 74 HostConfiguration host = (HostConfiguration) this.hostConfigs.get(hostname); 75 host.destroy(); 76 this.hostConfigs.remove(hostname); 77 } 78 this.hostConfigs.clear(); 79 } 80 81 protected void initHost(String webappsDirName, String hostname, Cluster cluster, 82 ObjectPool objectPool, ClassLoader commonLibCL, 83 File commonLibCLPaths[], Map args) throws IOException { 84 Logger.log(Logger.DEBUG, Launcher.RESOURCES, "HostGroup.DeployingHost", hostname); 85 HostConfiguration config = new HostConfiguration(hostname, cluster, objectPool, commonLibCL, 86 commonLibCLPaths, args, webappsDirName); 87 this.hostConfigs.put(hostname, config); 88 } 89 90 protected void initMultiHostDir(String hostsDirName, Cluster cluster, 91 ObjectPool objectPool, ClassLoader commonLibCL, 92 File commonLibCLPaths[], Map args) throws IOException { 93 if (hostsDirName == null) { 94 hostsDirName = "hosts"; 95 } 96 File hostsDir = new File (hostsDirName); 97 if (!hostsDir.exists()) { 98 throw new WinstoneException(Launcher.RESOURCES.getString("HostGroup.HostsDirNotFound", hostsDirName)); 99 } else if (!hostsDir.isDirectory()) { 100 throw new WinstoneException(Launcher.RESOURCES.getString("HostGroup.HostsDirIsNotDirectory", hostsDirName)); 101 } else { 102 File children[] = hostsDir.listFiles(); 103 if ((children == null) || (children.length == 0)) { 104 throw new WinstoneException(Launcher.RESOURCES.getString("HostGroup.HostsDirIsEmpty", hostsDirName)); 105 } 106 for (int n = 0; n < children.length; n++) { 107 String childName = children[n].getName(); 108 109 if (children[n].isDirectory()) { 111 if (!this.hostConfigs.containsKey(childName)) { 112 initHost(children[n].getCanonicalPath(), childName, cluster, 113 objectPool, commonLibCL, commonLibCLPaths, args); 114 } 115 } 116 if ((defaultHostName == null) || childName.equals(DEFAULT_HOSTNAME)) { 117 this.defaultHostName = childName; 118 } 119 } 120 } 121 } 122 } 123 | Popular Tags |