KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > host > HostContainer


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.server.host;
31
32 import com.caucho.lifecycle.Lifecycle;
33 import com.caucho.loader.EnvironmentClassLoader;
34 import com.caucho.log.Log;
35 import com.caucho.make.AlwaysModified;
36 import com.caucho.server.cluster.Server;
37 import com.caucho.server.deploy.DeployContainer;
38 import com.caucho.server.dispatch.DispatchBuilder;
39 import com.caucho.server.dispatch.DispatchServer;
40 import com.caucho.server.dispatch.ErrorFilterChain;
41 import com.caucho.server.dispatch.Invocation;
42 import com.caucho.server.dispatch.ServletMapping;
43 import com.caucho.server.e_app.EarConfig;
44 import com.caucho.server.webapp.RewriteInvocation;
45 import com.caucho.server.webapp.WebApp;
46 import com.caucho.server.webapp.WebAppConfig;
47 import com.caucho.util.L10N;
48 import com.caucho.vfs.Path;
49 import com.caucho.vfs.Vfs;
50
51 import javax.servlet.FilterChain JavaDoc;
52 import java.util.ArrayList JavaDoc;
53 import java.util.HashMap JavaDoc;
54 import java.util.logging.Level JavaDoc;
55 import java.util.logging.Logger JavaDoc;
56
57 /**
58  * Resin's host container implementation.
59  */

60 public class HostContainer implements DispatchBuilder {
61   static final Logger JavaDoc log = Log.open(HostContainer.class);
62   static final L10N L = new L10N(HostContainer.class);
63
64   // The environment class loader
65
private EnvironmentClassLoader _classLoader;
66
67   private DispatchServer _dispatchServer;
68
69   private WebApp _errorWebApp;
70
71   private String JavaDoc _url = "";
72   
73   // The root directory.
74
private Path _rootDir;
75
76   // dispatch mapping
77
private RewriteInvocation _rewriteInvocation;
78
79   // List of default host configurations
80
private ArrayList JavaDoc<HostConfig> _hostDefaultList = new ArrayList JavaDoc<HostConfig>();
81
82   // The host deploy
83
private DeployContainer<HostController> _hostDeploy
84     = new DeployContainer<HostController>();
85   
86   // Cache of hosts
87
private HashMap JavaDoc<String JavaDoc,HostController> _hostMap
88     = new HashMap JavaDoc<String JavaDoc,HostController>();
89
90   // Regexp host
91
private ArrayList JavaDoc<HostConfig> _hostRegexpList = new ArrayList JavaDoc<HostConfig>();
92
93   // List of default webApp configurations
94
private ArrayList JavaDoc<WebAppConfig> _webAppDefaultList
95     = new ArrayList JavaDoc<WebAppConfig>();
96   
97   // List of default ear configurations
98
private ArrayList JavaDoc<EarConfig> _earDefaultList
99     = new ArrayList JavaDoc<EarConfig>();
100
101   // The configure exception
102
private Throwable JavaDoc _configException;
103
104   // lifecycle
105
private final Lifecycle _lifecycle = new Lifecycle();
106
107   /**
108    * Creates the webApp with its environment loader.
109    */

110   public HostContainer()
111   {
112     _classLoader = new EnvironmentClassLoader();
113
114     _rootDir = Vfs.lookup();
115   }
116
117   /**
118    * Gets the environment class loader.
119    */

120   public ClassLoader JavaDoc getClassLoader()
121   {
122     return _classLoader;
123   }
124
125   /**
126    * Gets the environment class loader.
127    */

128   public void setClassLoader(EnvironmentClassLoader classLoader)
129   {
130     _classLoader = classLoader;
131   }
132
133   /**
134    * Sets the URL for the default server.
135    */

136   public void setURL(String JavaDoc url)
137   {
138     _url = url;
139   }
140
141   /**
142    * Gets the URL for the default server.
143    */

144   public String JavaDoc getURL()
145   {
146     return _url;
147   }
148
149   /**
150    * Sets the dispatch server.
151    */

152   public void setDispatchServer(DispatchServer server)
153   {
154     _dispatchServer = server;
155   }
156
157   /**
158    * Gets the dispatch server.
159    */

160   public DispatchServer getDispatchServer()
161   {
162     return _dispatchServer;
163   }
164
165   /**
166    * Gets the root directory.
167    */

168   public Path getRootDirectory()
169   {
170     return _rootDir;
171   }
172
173   /**
174    * Sets the root directory.
175    */

176   public void setRootDirectory(Path path)
177   {
178     _rootDir = path;
179   }
180
181   /**
182    * Sets the root directory (obsolete).
183    * @deprecated
184    */

185   public void setRootDir(Path path)
186   {
187     setRootDirectory(path);
188   }
189
190   /**
191    * Adds a host default
192    */

193   public void addHostDefault(HostConfig init)
194   {
195     _hostDefaultList.add(init);
196   }
197
198   /**
199    * Returns the list of host defaults
200    */

201   public ArrayList JavaDoc<HostConfig> getHostDefaultList()
202   {
203     return _hostDefaultList;
204   }
205
206   /**
207    * Creates a host deploy
208    */

209   public HostExpandDeployGenerator createHostDeploy()
210   {
211     return new HostExpandDeployGenerator(_hostDeploy, this);
212   }
213
214   /**
215    * Adds a host deploy
216    */

217   public void addHostDeploy(HostExpandDeployGenerator hostDeploy)
218   {
219     _hostDeploy.add(hostDeploy);
220   }
221
222   /**
223    * Adds a host.
224    */

225   public void addHost(HostConfig hostConfig)
226     throws Exception JavaDoc
227   {
228     if (hostConfig.getRegexp() != null) {
229       _hostDeploy.add(new HostRegexpDeployGenerator(_hostDeploy, this, hostConfig));
230       return;
231     }
232
233     HostSingleDeployGenerator deploy;
234     deploy = new HostSingleDeployGenerator(_hostDeploy, this, hostConfig);
235     
236     _hostDeploy.add(deploy);
237   }
238
239   /**
240    * Adds a web-app default
241    */

242   public void addWebAppDefault(WebAppConfig init)
243   {
244     _webAppDefaultList.add(init);
245   }
246
247   /**
248    * Returns the list of web-app defaults
249    */

250   public ArrayList JavaDoc<WebAppConfig> getWebAppDefaultList()
251   {
252     return _webAppDefaultList;
253   }
254
255   /**
256    * Adds an ear default
257    */

258   public void addEarDefault(EarConfig init)
259   {
260     _earDefaultList.add(init);
261   }
262
263   /**
264    * Returns the list of ear defaults
265    */

266   public ArrayList JavaDoc<EarConfig> getEarDefaultList()
267   {
268     return _earDefaultList;
269   }
270
271   /**
272    * Adds rewrite-dispatch.
273    */

274   public RewriteInvocation createRewriteDispatch()
275   {
276     if (_rewriteInvocation == null) {
277       _rewriteInvocation = new RewriteInvocation();
278     }
279
280     return _rewriteInvocation;
281   }
282
283   /**
284    * Clears the cache.
285    */

286   public void clearCache()
287   {
288     _hostMap.clear();
289     _dispatchServer.clearCache();
290   }
291
292   /**
293    * Creates the invocation.
294    */

295   public void buildInvocation(Invocation invocation)
296     throws Throwable JavaDoc
297   {
298     String JavaDoc rawHost = invocation.getHost();
299     int rawPort = invocation.getPort();
300
301     String JavaDoc hostName;
302
303     if (rawHost == null)
304       hostName = "";
305     else
306       hostName = DomainName.fromAscii(rawHost);
307
308     invocation.setHostName(hostName);
309
310     if (_rewriteInvocation != null) {
311       String JavaDoc url;
312
313       if (invocation.isSecure())
314     url = "https://" + hostName + invocation.getURI();
315       else
316     url = "http://" + hostName + invocation.getURI();
317       
318       FilterChain chain = _rewriteInvocation.map(url, invocation);
319
320       if (chain != null) {
321     Server server = (Server) _dispatchServer;
322     invocation.setWebApp(server.getErrorWebApp());
323     invocation.setFilterChain(chain);
324     return;
325       }
326     }
327
328     Host host = getHost(hostName, rawPort);
329
330     if (host != null) {
331       host.buildInvocation(invocation);
332     }
333     else {
334       FilterChain chain = new ErrorFilterChain(404);
335       invocation.setFilterChain(chain);
336       invocation.setWebApp(getErrorWebApp());
337       invocation.setDependency(AlwaysModified.create());
338     }
339   }
340
341   public ArrayList JavaDoc<HostController> getHostList()
342   {
343     return _hostDeploy.getControllers();
344   }
345
346   /**
347    * Returns the matching host.
348    */

349   public Host getHost(String JavaDoc hostName, int port)
350   {
351     try {
352       HostController controller = findHost(hostName, port);
353
354       if (controller != null)
355         return controller.request();
356       else
357         return null;
358     } catch (Throwable JavaDoc e) {
359       log.log(Level.WARNING, e.toString(), e);
360       
361       return null;
362     }
363   }
364
365   /**
366    * Finds the best matching host entry.
367    */

368   private HostController findHost(String JavaDoc rawHost, int rawPort)
369     throws Exception JavaDoc
370   {
371     if (rawHost == null)
372       rawHost = "";
373
374     int p = rawHost.indexOf(':');
375
376     String JavaDoc shortHost = rawHost;
377
378     if (p > 0)
379       shortHost = rawHost.substring(0, p);
380     
381     String JavaDoc fullHost = shortHost + ':' + rawPort;
382
383     HostController hostController = null;
384     
385     synchronized (_hostMap) {
386       hostController = _hostMap.get(fullHost);
387
388       if (hostController != null && ! hostController.isDestroyed())
389         return hostController;
390     }
391
392     if (hostController == null || hostController.isDestroyed())
393       hostController = _hostMap.get(shortHost);
394
395     if (hostController == null || hostController.isDestroyed())
396       hostController = findHostController(fullHost);
397
398     if (hostController == null || hostController.isDestroyed())
399       hostController = findHostController(shortHost);
400
401     if (hostController == null || hostController.isDestroyed())
402       hostController = findHostController("");
403
404     synchronized (_hostMap) {
405       if (hostController != null && ! hostController.isDestroyed())
406     _hostMap.put(fullHost, hostController);
407       else {
408     hostController = null;
409     _hostMap.remove(fullHost);
410       }
411     }
412
413     return hostController;
414   }
415
416   /**
417    * Returns the HostController based on a host name. The canonical name
418    * and the host aliases are tested for the match.
419    *
420    * @param hostName name to match on
421    * @return the host entry or null if none are found.
422    */

423   private HostController findHostController(String JavaDoc hostName)
424     throws Exception JavaDoc
425   {
426     return _hostDeploy.findController(hostName);
427   }
428
429   /**
430    * Returns the error webApp during startup.
431    */

432   public WebApp getErrorWebApp()
433   {
434     if (_errorWebApp == null) {
435       Thread JavaDoc thread = Thread.currentThread();
436       ClassLoader JavaDoc loader = thread.getContextClassLoader();
437       try {
438     thread.setContextClassLoader(_classLoader);
439
440     _errorWebApp = new WebApp();
441     _errorWebApp.setAppDir(getRootDirectory());
442     com.caucho.server.dispatch.ServletMapping file;
443     file = new ServletMapping();
444     file.addURLPattern("/");
445     file.setServletName("resin-file");
446     file.setServletClass("com.caucho.servlets.FileServlet");
447     file.init();
448     _errorWebApp.addServletMapping(file);
449     
450     for (WebAppConfig config : _webAppDefaultList) {
451       try {
452         config.getBuilderProgram().configure(_errorWebApp);
453       } catch (Throwable JavaDoc e) {
454         log.log(Level.WARNING, e.toString(), e);
455       }
456     }
457     
458     _errorWebApp.init();
459     _errorWebApp.start();
460       } catch (Throwable JavaDoc e) {
461     log.log(Level.WARNING, e.toString(), e);
462       } finally {
463     thread.setContextClassLoader(loader);
464       }
465     }
466
467     return _errorWebApp;
468   }
469
470   /**
471    * Starts the container.
472    */

473   public void start()
474   {
475     if (! _lifecycle.toStarting())
476       return;
477
478     _classLoader.start();
479
480     _lifecycle.toActive();
481
482     _hostDeploy.start();
483   }
484
485   /**
486    * Stops the container.
487    */

488   public void stop()
489   {
490     if (! _lifecycle.toStop())
491       return;
492
493     _hostDeploy.stop();
494     
495     _classLoader.stop();
496   }
497
498   /**
499    * Closes the container.
500    */

501   public void destroy()
502   {
503     stop();
504
505     if (! _lifecycle.toDestroy())
506       return;
507
508     _hostDeploy.destroy();
509
510     _classLoader.destroy();
511   }
512 }
513
Popular Tags