KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > catalina > container > CatalinaContainer


1 /*
2  * $Id: CatalinaContainer.java 7208 2006-04-05 21:28:25Z jonesde $
3  *
4  * Copyright (c) 2004 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.catalina.container;
26
27 import java.io.File JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.util.Collection JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Map JavaDoc;
35 import javax.naming.InitialContext JavaDoc;
36 import javax.naming.NamingException JavaDoc;
37 import javax.xml.parsers.ParserConfigurationException JavaDoc;
38
39 import org.ofbiz.base.component.ComponentConfig;
40 import org.ofbiz.base.container.ClassLoaderContainer;
41 import org.ofbiz.base.container.Container;
42 import org.ofbiz.base.container.ContainerConfig;
43 import org.ofbiz.base.container.ContainerException;
44 import org.ofbiz.base.util.Debug;
45 import org.ofbiz.base.util.UtilURL;
46 import org.ofbiz.base.util.UtilValidate;
47 import org.ofbiz.base.util.UtilXml;
48 import org.ofbiz.entity.GenericDelegator;
49
50 import org.apache.catalina.Cluster;
51 import org.apache.catalina.Context;
52 import org.apache.catalina.Engine;
53 import org.apache.catalina.Host;
54 import org.apache.catalina.LifecycleException;
55 import org.apache.catalina.Manager;
56 import org.apache.catalina.ServerFactory;
57 import org.apache.catalina.cluster.mcast.McastService;
58 import org.apache.catalina.cluster.tcp.ReplicationListener;
59 import org.apache.catalina.cluster.tcp.ReplicationTransmitter;
60 import org.apache.catalina.cluster.tcp.ReplicationValve;
61 import org.apache.catalina.cluster.tcp.SimpleTcpCluster;
62 import org.apache.catalina.connector.Connector;
63 import org.apache.catalina.core.StandardContext;
64 import org.apache.catalina.core.StandardEngine;
65 import org.apache.catalina.core.StandardServer;
66 import org.apache.catalina.core.StandardWrapper;
67 import org.apache.catalina.realm.MemoryRealm;
68 import org.apache.catalina.session.StandardManager;
69 import org.apache.catalina.startup.Embedded;
70 import org.apache.catalina.util.ServerInfo;
71 import org.apache.catalina.valves.AccessLogValve;
72 import org.apache.catalina.valves.RequestDumperValve;
73 import org.apache.coyote.ProtocolHandler;
74 import org.apache.coyote.http11.Http11Protocol;
75 import org.w3c.dom.Document JavaDoc;
76 import org.w3c.dom.Element JavaDoc;
77 import org.xml.sax.SAXException JavaDoc;
78
79 /*
80  * --- Access Log Pattern Information - From Tomcat 5 AccessLogValve.java
81  * <p>Patterns for the logged message may include constant text or any of the
82  * following replacement strings, for which the corresponding information
83  * from the specified Response is substituted:</p>
84  * <ul>
85  * <li><b>%a</b> - Remote IP address
86  * <li><b>%A</b> - Local IP address
87  * <li><b>%b</b> - Bytes sent, excluding HTTP headers, or '-' if no bytes
88  * were sent
89  * <li><b>%B</b> - Bytes sent, excluding HTTP headers
90  * <li><b>%h</b> - Remote host name
91  * <li><b>%H</b> - Request protocol
92  * <li><b>%l</b> - Remote logical username from identd (always returns '-')
93  * <li><b>%m</b> - Request method
94  * <li><b>%p</b> - Local port
95  * <li><b>%q</b> - Query string (prepended with a '?' if it exists, otherwise
96  * an empty string
97  * <li><b>%r</b> - First line of the request
98  * <li><b>%s</b> - HTTP status code of the response
99  * <li><b>%S</b> - User session ID
100  * <li><b>%t</b> - Date and time, in Common Log Format format
101  * <li><b>%u</b> - Remote user that was authenticated
102  * <li><b>%U</b> - Requested URL path
103  * <li><b>%v</b> - Local server name
104  * <li><b>%D</b> - Time taken to process the request, in millis
105  * <li><b>%T</b> - Time taken to process the request, in seconds
106  * </ul>
107  * <p>In addition, the caller can specify one of the following aliases for
108  * commonly utilized patterns:</p>
109  * <ul>
110  * <li><b>common</b> - <code>%h %l %u %t "%r" %s %b</code>
111  * <li><b>combined</b> -
112  * <code>%h %l %u %t "%r" %s %b "%{Referer}i" "%{User-Agent}i"</code>
113  * </ul>
114  *
115  * <p>
116  * There is also support to write information from the cookie, incoming
117  * header, the Session or something else in the ServletRequest.<br/>
118  * It is modeled after the apache syntax:
119  * <ul>
120  * <li><code>%{xxx}i</code> for incoming headers
121  * <li><code>%{xxx}c</code> for a specific cookie
122  * <li><code>%{xxx}r</code> xxx is an attribute in the ServletRequest
123  * <li><code>%{xxx}s</code> xxx is an attribute in the HttpSession
124  * </ul>
125  * </p>
126  */

127
128 /**
129  * CatalinaContainer - Tomcat 5
130  *
131  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
132  * @version $Rev: 7208 $
133  * @since 3.1
134  */

135 public class CatalinaContainer implements Container {
136
137     public static final String JavaDoc CATALINA_HOSTS_HOME = System.getProperty("ofbiz.home") + "/framework/catalina/hosts";
138     public static final String JavaDoc J2EE_SERVER = "OFBiz Container 3.1";
139     public static final String JavaDoc J2EE_APP = "OFBiz";
140     public static final String JavaDoc module = CatalinaContainer.class.getName();
141     protected static Map JavaDoc mimeTypes = new HashMap JavaDoc();
142
143     protected GenericDelegator delegator = null;
144     protected Embedded embedded = null;
145     protected Map JavaDoc clusterConfig = new HashMap JavaDoc();
146     protected Map JavaDoc engines = new HashMap JavaDoc();
147     protected Map JavaDoc hosts = new HashMap JavaDoc();
148
149     protected boolean contextReloadable = false;
150     protected boolean crossContext = false;
151     protected boolean distribute = false;
152
153     protected boolean enableDefaultMimeTypes = true;
154
155     /**
156      * @see org.ofbiz.base.container.Container#init(java.lang.String[], java.lang.String)
157      */

158     public void init(String JavaDoc[] args, String JavaDoc configFile) throws ContainerException {
159         // set catalina_home
160
System.setProperty("catalina.home", System.getProperty("ofbiz.home") + "/framework/catalina");
161
162         // get the container config
163
ContainerConfig.Container cc = ContainerConfig.getContainer("catalina-container", configFile);
164         if (cc == null) {
165             throw new ContainerException("No catalina-container configuration found in container config!");
166         }
167
168         // embedded properties
169
boolean useNaming = ContainerConfig.getPropertyValue(cc, "use-naming", false);
170         //int debug = ContainerConfig.getPropertyValue(cc, "debug", 0);
171

172         // grab some global context settings
173
this.delegator = GenericDelegator.getGenericDelegator(ContainerConfig.getPropertyValue(cc, "delegator-name", "default"));
174         this.contextReloadable = ContainerConfig.getPropertyValue(cc, "apps-context-reloadable", false);
175         this.crossContext = ContainerConfig.getPropertyValue(cc, "apps-cross-context", true);
176         this.distribute = ContainerConfig.getPropertyValue(cc, "apps-distributable", true);
177
178         // configure JNDI in the StandardServer
179
StandardServer server = (StandardServer) ServerFactory.getServer();
180         try {
181             server.setGlobalNamingContext(new InitialContext JavaDoc());
182         } catch (NamingException JavaDoc e) {
183             throw new ContainerException(e);
184         }
185
186         // create the instance of Embedded
187
embedded = new Embedded();
188         embedded.setUseNaming(useNaming);
189
190         // create the engines
191
List JavaDoc engineProps = cc.getPropertiesWithValue("engine");
192         if (engineProps == null && engineProps.size() == 0) {
193             throw new ContainerException("Cannot load CatalinaContainer; no engines defined!");
194         }
195         Iterator JavaDoc ei = engineProps.iterator();
196         while (ei.hasNext()) {
197             ContainerConfig.Container.Property engineProp = (ContainerConfig.Container.Property) ei.next();
198             createEngine(engineProp);
199         }
200
201         // load the web applications
202
loadComponents();
203
204         // create the connectors
205
List JavaDoc connectorProps = cc.getPropertiesWithValue("connector");
206         if (connectorProps == null && connectorProps.size() == 0) {
207             throw new ContainerException("Cannot load CatalinaContainer; no connectors defined!");
208         }
209         Iterator JavaDoc ci = connectorProps.iterator();
210         while (ci.hasNext()) {
211             ContainerConfig.Container.Property connectorProp = (ContainerConfig.Container.Property) ci.next();
212             createConnector(connectorProp);
213         }
214
215         try {
216             embedded.initialize();
217         } catch (LifecycleException e) {
218             throw new ContainerException(e);
219         }
220     }
221
222     public boolean start() throws ContainerException {
223         // Start the embedded server
224
try {
225             embedded.start();
226         } catch (LifecycleException e) {
227             throw new ContainerException(e);
228         }
229
230         Connector[] cons = embedded.findConnectors();
231         for (int i = 0; i < cons.length; i++) {
232             ProtocolHandler ph = cons[i].getProtocolHandler();
233             if (ph instanceof Http11Protocol) {
234                 Http11Protocol hph = (Http11Protocol) ph;
235                 Debug.logInfo("Connector " + hph.getProtocol() + " @ " + hph.getPort() + " - " +
236                     (hph.getSecure() ? "secure" : "not-secure") + " [" + cons[i].getProtocolHandlerClassName() + "] started.", module);
237             } else {
238                 Debug.logInfo("Connector " + cons[i].getProtocol() + " @ " + cons[i].getPort() + " - " +
239                     (cons[i].getSecure() ? "secure" : "not-secure") + " [" + cons[i].getProtocolHandlerClassName() + "] started.", module);
240             }
241         }
242         Debug.logInfo("Started " + ServerInfo.getServerInfo(), module);
243         return true;
244     }
245
246     protected Engine createEngine(ContainerConfig.Container.Property engineConfig) throws ContainerException {
247         if (embedded == null) {
248             throw new ContainerException("Cannot create Engine without Embedded instance!");
249         }
250
251         ContainerConfig.Container.Property defaultHostProp = engineConfig.getProperty("default-host");
252         if (defaultHostProp == null) {
253             throw new ContainerException("default-host element of server property is required for catalina!");
254         }
255
256         String JavaDoc engineName = engineConfig.name;
257         String JavaDoc hostName = defaultHostProp.value;
258
259         StandardEngine engine = (StandardEngine) embedded.createEngine();
260         engine.setName(engineName);
261         engine.setDefaultHost(hostName);
262
263         // set the JVM Route property (JK/JK2)
264
String JavaDoc jvmRoute = ContainerConfig.getPropertyValue(engineConfig, "jvm-route", null);
265         if (jvmRoute != null) {
266             engine.setJvmRoute(jvmRoute);
267         }
268
269         // create the default realm -- TODO: make this configurable
270
String JavaDoc dbConfigPath = "config/catalina-users.xml";
271         MemoryRealm realm = new MemoryRealm();
272         realm.setPathname(dbConfigPath);
273         engine.setRealm(realm);
274
275         // cache the engine
276
engines.put(engine.getName(), engine);
277
278         // create a default virtual host; others will be created as needed
279
Host host = createHost(engine, hostName);
280         hosts.put(engineName + "._DEFAULT", host);
281
282         // configure clustering
283
List JavaDoc clusterProps = engineConfig.getPropertiesWithValue("cluster");
284         if (clusterProps != null && clusterProps.size() > 1) {
285             throw new ContainerException("Only one cluster configuration allowed per engine");
286         }
287
288         if (clusterProps != null && clusterProps.size() > 0) {
289             ContainerConfig.Container.Property clusterProp = (ContainerConfig.Container.Property) clusterProps.get(0);
290             createCluster(clusterProp, host);
291             clusterConfig.put(engineName, clusterProp);
292         }
293
294         // request dumper valve
295
boolean enableRequestDump = ContainerConfig.getPropertyValue(engineConfig, "enable-request-dump", false);
296         if (enableRequestDump) {
297             RequestDumperValve rdv = new RequestDumperValve();
298             engine.addValve(rdv);
299         }
300
301         // configure the access log valve
302
String JavaDoc logDir = ContainerConfig.getPropertyValue(engineConfig, "access-log-dir", null);
303         AccessLogValve al = null;
304         if (logDir != null) {
305             al = new AccessLogValve();
306             if (!logDir.startsWith("/")) {
307                 logDir = System.getProperty("ofbiz.home") + "/" + logDir;
308             }
309             File JavaDoc logFile = new File JavaDoc(logDir);
310             if (!logFile.isDirectory()) {
311                 throw new ContainerException("Log directory [" + logDir + "] is not available; make sure the directory is created");
312             }
313             al.setDirectory(logFile.getAbsolutePath());
314         }
315
316         String JavaDoc alp2 = ContainerConfig.getPropertyValue(engineConfig, "access-log-pattern", null);
317         if (al != null && !UtilValidate.isEmpty(alp2)) {
318             al.setPattern(alp2);
319         }
320
321         String JavaDoc alp3 = ContainerConfig.getPropertyValue(engineConfig, "access-log-prefix", null);
322         if (al != null && !UtilValidate.isEmpty(alp3)) {
323             al.setPrefix(alp3);
324         }
325
326
327         boolean alp4 = ContainerConfig.getPropertyValue(engineConfig, "access-log-resolve", true);
328         if (al != null) {
329             al.setResolveHosts(alp4);
330         }
331
332         boolean alp5 = ContainerConfig.getPropertyValue(engineConfig, "access-log-rotate", false);
333         if (al != null) {
334             al.setRotatable(alp5);
335         }
336
337         if (al != null) {
338             engine.addValve(al);
339         }
340
341         embedded.addEngine(engine);
342         return engine;
343     }
344
345     protected Host createHost(Engine engine, String JavaDoc hostName) throws ContainerException {
346         if (embedded == null) {
347             throw new ContainerException("Cannot create Host without Embedded instance!");
348         }
349
350         Host host = embedded.createHost(hostName, CATALINA_HOSTS_HOME);
351         host.setDeployOnStartup(true);
352         host.setAutoDeploy(true);
353         host.setRealm(engine.getRealm());
354         engine.addChild(host);
355         hosts.put(engine.getName() + hostName, host);
356
357         return host;
358     }
359
360     protected Cluster createCluster(ContainerConfig.Container.Property clusterProps, Host host) throws ContainerException {
361         String JavaDoc defaultValveFilter = ".*.gif;.*.js;.*.jpg;.*.htm;.*.html;.*.txt;";
362
363         ReplicationValve clusterValve = new ReplicationValve();
364         clusterValve.setFilter(ContainerConfig.getPropertyValue(clusterProps, "rep-valve-filter", defaultValveFilter));
365
366         String JavaDoc mcb = ContainerConfig.getPropertyValue(clusterProps, "mcast-bind-addr", null);
367         String JavaDoc mca = ContainerConfig.getPropertyValue(clusterProps, "mcast-addr", null);
368         int mcp = ContainerConfig.getPropertyValue(clusterProps, "mcast-port", -1);
369         int mcd = ContainerConfig.getPropertyValue(clusterProps, "mcast-freq", 500);
370         int mcf = ContainerConfig.getPropertyValue(clusterProps, "mcast-drop-time", 3000);
371
372         if (mca == null || mcp == -1) {
373             throw new ContainerException("Cluster configuration requires mcast-addr and mcast-port properties");
374         }
375
376         McastService mcast = new McastService();
377         if (mcb != null) {
378             mcast.setMcastBindAddress(mcb);
379         }
380
381         mcast.setMcastAddr(mca);
382         mcast.setMcastPort(mcp);
383         mcast.setMcastDropTime(mcd);
384         mcast.setMcastFrequency(mcf);
385
386         String JavaDoc tla = ContainerConfig.getPropertyValue(clusterProps, "tcp-listen-host", "auto");
387         int tlp = ContainerConfig.getPropertyValue(clusterProps, "tcp-listen-port", 4001);
388         int tlt = ContainerConfig.getPropertyValue(clusterProps, "tcp-sector-timeout", 100);
389         int tlc = ContainerConfig.getPropertyValue(clusterProps, "tcp-thread-count", 6);
390         //String tls = getPropertyValue(clusterProps, "", "");
391

392         if (tlp == -1) {
393             throw new ContainerException("Cluster configuration requires tcp-listen-port property");
394         }
395
396         ReplicationListener listener = new ReplicationListener();
397         listener.setTcpListenAddress(tla);
398         listener.setTcpListenPort(tlp);
399         listener.setTcpSelectorTimeout(tlt);
400         listener.setTcpThreadCount(tlc);
401         //listener.setIsSenderSynchronized(false);
402

403         ReplicationTransmitter trans = new ReplicationTransmitter();
404         trans.setReplicationMode(ContainerConfig.getPropertyValue(clusterProps, "replication-mode", "pooled"));
405
406         String JavaDoc mgrClassName = ContainerConfig.getPropertyValue(clusterProps, "manager-class", "org.apache.catalina.cluster.session.DeltaManager");
407         //int debug = ContainerConfig.getPropertyValue(clusterProps, "debug", 0);
408
boolean expireSession = ContainerConfig.getPropertyValue(clusterProps, "expire-session", false);
409         boolean useDirty = ContainerConfig.getPropertyValue(clusterProps, "use-dirty", true);
410
411         SimpleTcpCluster cluster = new SimpleTcpCluster();
412         cluster.setClusterName(clusterProps.name);
413         cluster.setManagerClassName(mgrClassName);
414         //cluster.setDebug(debug);
415
cluster.setExpireSessionsOnShutdown(expireSession);
416         cluster.setUseDirtyFlag(useDirty);
417
418         cluster.setClusterReceiver(listener);
419         cluster.setClusterSender(trans);
420         cluster.setMembershipService(mcast);
421         cluster.addValve(clusterValve);
422         cluster.setPrintToScreen(true);
423
424         // set the cluster to the host
425
host.setCluster(cluster);
426         Debug.logInfo("Catalina Cluster [" + cluster.getClusterName() + "] configured for host - " + host.getName(), module);
427
428         return cluster;
429     }
430
431     protected Connector createConnector(ContainerConfig.Container.Property connectorProp) throws ContainerException {
432         if (embedded == null) {
433             throw new ContainerException("Cannot create Connector without Embedded instance!");
434         }
435
436         // need some standard properties
437
String JavaDoc protocol = ContainerConfig.getPropertyValue(connectorProp, "protocol", "HTTP/1.1");
438         String JavaDoc address = ContainerConfig.getPropertyValue(connectorProp, "address", "0.0.0.0");
439         int port = ContainerConfig.getPropertyValue(connectorProp, "port", 0);
440         boolean secure = ContainerConfig.getPropertyValue(connectorProp, "secure", false);
441         if (protocol.toLowerCase().startsWith("ajp")) {
442             protocol = "ajp";
443         } else if ("memory".equals(protocol.toLowerCase())) {
444             protocol = "memory";
445         } else if (secure) {
446             protocol = "https";
447         } else {
448             protocol = "http";
449         }
450
451         Connector connector = null;
452         if (connectorProp.properties != null && connectorProp.properties.size() > 0) {
453             connector = embedded.createConnector(address, port, protocol);
454             try {
455                 Iterator JavaDoc i = connectorProp.properties.values().iterator();
456                 while (i.hasNext()) {
457                     ContainerConfig.Container.Property prop = (ContainerConfig.Container.Property) i.next();
458                     connector.setProperty(prop.name, prop.value);
459                     //connector.setAttribute(prop.name, prop.value);
460
}
461                 embedded.addConnector(connector);
462             } catch (Exception JavaDoc e) {
463                 throw new ContainerException(e);
464             }
465         }
466         return connector;
467     }
468
469     protected Context JavaDoc createContext(ComponentConfig.WebappInfo appInfo) throws ContainerException {
470         // webapp settings
471
Map JavaDoc initParameters = appInfo.getInitParameters();
472         List JavaDoc virtualHosts = appInfo.getVirtualHosts();
473         Engine engine = (Engine) engines.get(appInfo.server);
474         if (engine == null) {
475             Debug.logWarning("Server with name [" + appInfo.server + "] not found; not mounting [" + appInfo.name + "]", module);
476             return null;
477         }
478
479         // set the root location (make sure we set the paths correctly)
480
String JavaDoc location = appInfo.componentConfig.getRootLocation() + appInfo.location;
481         location = location.replace('\\', '/');
482         if (location.endsWith("/")) {
483             location = location.substring(0, location.length() - 1);
484         }
485
486         // get the mount point
487
String JavaDoc mount = appInfo.mountPoint;
488         if (mount.endsWith("/*")) {
489             mount = mount.substring(0, mount.length() - 2);
490         }
491
492         // configure persistent sessions
493
Manager sessionMgr = new StandardManager();
494
495         // create the web application context
496
StandardContext context = (StandardContext) embedded.createContext(mount, location);
497         context.setJ2EEApplication(J2EE_APP);
498         context.setJ2EEServer(J2EE_SERVER);
499         context.setLoader(embedded.createLoader(ClassLoaderContainer.getClassLoader()));
500
501         context.setDisplayName(appInfo.name);
502         context.setDocBase(location);
503         context.setAllowLinking(true);
504
505         context.setReloadable(contextReloadable);
506         context.setDistributable(distribute);
507         context.setCrossContext(crossContext);
508         context.setManager(sessionMgr);
509         context.getServletContext().setAttribute("_serverId", appInfo.server);
510
511         // create the Default Servlet instance to mount
512
StandardWrapper defaultServlet = new StandardWrapper();
513         defaultServlet.setServletClass("org.apache.catalina.servlets.DefaultServlet");
514         defaultServlet.setServletName("default");
515         defaultServlet.setLoadOnStartup(1);
516         defaultServlet.addInitParameter("debug", "0");
517         defaultServlet.addInitParameter("listing", "true");
518         defaultServlet.addMapping("/");
519         context.addChild(defaultServlet);
520         context.addServletMapping("/", "default");
521
522         // create the Jasper Servlet instance to mount
523
StandardWrapper jspServlet = new StandardWrapper();
524         jspServlet.setServletClass("org.apache.jasper.servlet.JspServlet");
525         jspServlet.setServletName("jsp");
526         jspServlet.setLoadOnStartup(1);
527         jspServlet.addInitParameter("fork", "false");
528         jspServlet.addInitParameter("xpoweredBy", "true");
529         jspServlet.addMapping("*.jsp");
530         jspServlet.addMapping("*.jspx");
531         context.addChild(jspServlet);
532         context.addServletMapping("*.jsp", "jsp");
533
534         // default mime-type mappings
535
configureMimeTypes(context);
536
537         // set the init parameters
538
Iterator JavaDoc ip = initParameters.keySet().iterator();
539         while (ip.hasNext()) {
540             String JavaDoc paramName = (String JavaDoc) ip.next();
541             context.addParameter(paramName, (String JavaDoc) initParameters.get(paramName));
542         }
543
544         if (UtilValidate.isEmpty(virtualHosts)) {
545             Host host = (Host) hosts.get(engine.getName() + "._DEFAULT");
546             context.setRealm(host.getRealm());
547             host.addChild(context);
548             context.getMapper().setDefaultHostName(host.getName());
549         } else {
550             // assume that the first virtual-host will be the default; additional virtual-hosts will be aliases
551
Iterator JavaDoc vhi = virtualHosts.iterator();
552             String JavaDoc hostName = (String JavaDoc) vhi.next();
553
554             boolean newHost = false;
555             Host host = (Host) hosts.get(engine.getName() + "." + hostName);
556             if (host == null) {
557                 host = createHost(engine, hostName);
558                 newHost = true;
559             }
560             while (vhi.hasNext()) {
561                 host.addAlias((String JavaDoc) vhi.next());
562             }
563             context.setRealm(host.getRealm());
564             host.addChild(context);
565             context.getMapper().setDefaultHostName(host.getName());
566
567             if (newHost) {
568                 hosts.put(engine.getName() + "." + hostName, host);
569             }
570         }
571
572         return context;
573     }
574
575     protected void loadComponents() throws ContainerException {
576         if (embedded == null) {
577             throw new ContainerException("Cannot load web applications without Embedded instance!");
578         }
579
580         // load the applications
581
Collection JavaDoc componentConfigs = ComponentConfig.getAllComponents();
582         if (componentConfigs != null) {
583             Iterator JavaDoc components = componentConfigs.iterator();
584             while (components.hasNext()) {
585                 ComponentConfig component = (ComponentConfig) components.next();
586                 Iterator JavaDoc appInfos = component.getWebappInfos().iterator();
587                 while (appInfos.hasNext()) {
588                     ComponentConfig.WebappInfo appInfo = (ComponentConfig.WebappInfo) appInfos.next();
589                     createContext(appInfo);
590                 }
591             }
592         }
593
594     }
595
596     public void stop() throws ContainerException {
597         try {
598             embedded.stop();
599         } catch (LifecycleException e) {
600             // don't throw this; or it will kill the rest of the shutdown process
601
Debug.logError(e, module);
602         }
603     }
604
605     protected void configureMimeTypes(Context JavaDoc context) throws ContainerException {
606         Map JavaDoc mimeTypes = CatalinaContainer.getMimeTypes();
607         if (mimeTypes != null && mimeTypes.size() > 0) {
608             Iterator JavaDoc i = mimeTypes.entrySet().iterator();
609             while (i.hasNext()) {
610                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
611                 context.addMimeMapping((String JavaDoc)entry.getKey(), (String JavaDoc)entry.getValue());
612             }
613         }
614     }
615
616     protected static synchronized Map JavaDoc getMimeTypes() throws ContainerException {
617         if (mimeTypes != null && mimeTypes.size() > 0) {
618             return mimeTypes;
619         }
620
621         if (mimeTypes == null) mimeTypes = new HashMap JavaDoc();
622         URL JavaDoc xmlUrl = UtilURL.fromResource("mime-type.xml");
623
624         // read the document
625
Document JavaDoc mimeTypeDoc = null;
626         try {
627             mimeTypeDoc = UtilXml.readXmlDocument(xmlUrl, true);
628         } catch (SAXException JavaDoc e) {
629             throw new ContainerException("Error reading the mime-type.xml config file: " + xmlUrl, e);
630         } catch (ParserConfigurationException JavaDoc e) {
631             throw new ContainerException("Error reading the mime-type.xml config file: " + xmlUrl, e);
632         } catch (IOException JavaDoc e) {
633             throw new ContainerException("Error reading the mime-type.xml config file: " + xmlUrl, e);
634         }
635
636         if (mimeTypeDoc == null) {
637             Debug.logError("Null document returned for mime-type.xml", module);
638             return null;
639         }
640
641         // root element
642
Element JavaDoc root = mimeTypeDoc.getDocumentElement();
643
644         // mapppings
645
Iterator JavaDoc elementIter = UtilXml.childElementList(root, "mime-mapping").iterator();
646         while (elementIter.hasNext()) {
647             Element JavaDoc curElement = (Element JavaDoc) elementIter.next();
648             String JavaDoc extension = UtilXml.childElementValue(curElement, "extension");
649             String JavaDoc type = UtilXml.childElementValue(curElement, "mime-type");
650             mimeTypes.put(extension, type);
651         }
652
653         return mimeTypes;
654     }
655 }
656
Popular Tags