KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > winstone > HostGroup


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

7 package winstone;
8
9 import java.io.File JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.util.HashSet JavaDoc;
12 import java.util.Hashtable JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.Map JavaDoc;
15 import java.util.Set JavaDoc;
16
17 /**
18  * Manages the references to individual hosts within the container. This object handles
19  * the mapping of ip addresses and hostnames to groups of webapps, and init and
20  * shutdown of any hosts it manages.
21  *
22  * @author <a HREF="mailto:rick_knowles@hotmail.com">Rick Knowles</a>
23  * @version $Id: HostGroup.java,v 1.4 2006/03/24 17:24:21 rickknowles Exp $
24  */

25 public class HostGroup {
26     
27     private final static String JavaDoc DEFAULT_HOSTNAME = "default";
28     
29 // private Map args;
30
private Map JavaDoc hostConfigs;
31     private String JavaDoc defaultHostName;
32     
33     public HostGroup(Cluster cluster,
34             ObjectPool objectPool, ClassLoader JavaDoc commonLibCL,
35             File JavaDoc commonLibCLPaths[], Map JavaDoc args) throws IOException JavaDoc {
36 // this.args = args;
37
this.hostConfigs = new Hashtable JavaDoc();
38         
39         // Is this the single or multiple configuration ? Check args
40
String JavaDoc hostDirName = (String JavaDoc) args.get("hostsDir");
41         String JavaDoc webappsDirName = (String JavaDoc) args.get("webappsDir");
42
43         // If host mode
44
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 JavaDoc[] {this.hostConfigs.size() + "", this.hostConfigs.keySet() + ""});
50         }
51         // Otherwise multi-webapp mode
52
else {
53             initMultiHostDir(hostDirName, cluster, objectPool, commonLibCL,
54                     commonLibCLPaths, args);
55             Logger.log(Logger.DEBUG, Launcher.RESOURCES, "HostGroup.InitMultiComplete",
56                     new String JavaDoc[] {this.hostConfigs.size() + "", this.hostConfigs.keySet() + ""});
57         }
58     }
59
60     public HostConfiguration getHostByName(String JavaDoc 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 JavaDoc hostnames = new HashSet JavaDoc(this.hostConfigs.keySet());
72         for (Iterator JavaDoc i = hostnames.iterator(); i.hasNext(); ) {
73             String JavaDoc hostname = (String JavaDoc) 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 JavaDoc webappsDirName, String JavaDoc hostname, Cluster cluster,
82             ObjectPool objectPool, ClassLoader JavaDoc commonLibCL,
83             File JavaDoc commonLibCLPaths[], Map JavaDoc args) throws IOException JavaDoc {
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 JavaDoc hostsDirName, Cluster cluster,
91             ObjectPool objectPool, ClassLoader JavaDoc commonLibCL,
92             File JavaDoc commonLibCLPaths[], Map JavaDoc args) throws IOException JavaDoc {
93         if (hostsDirName == null) {
94             hostsDirName = "hosts";
95         }
96         File JavaDoc hostsDir = new File JavaDoc(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 JavaDoc 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 JavaDoc childName = children[n].getName();
108
109                 // Mount directories as host dirs
110
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