KickJava   Java API By Example, From Geeks To Geeks.

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


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.config.ConfigException;
33 import com.caucho.config.SchemaBean;
34 import com.caucho.lifecycle.Lifecycle;
35 import com.caucho.loader.EnvironmentBean;
36 import com.caucho.loader.EnvironmentClassLoader;
37 import com.caucho.loader.EnvironmentLocal;
38 import com.caucho.make.AlwaysModified;
39 import com.caucho.management.server.HostMXBean;
40 import com.caucho.server.cluster.Cluster;
41 import com.caucho.server.cluster.Server;
42 import com.caucho.server.deploy.EnvironmentDeployInstance;
43 import com.caucho.server.dispatch.DispatchServer;
44 import com.caucho.server.dispatch.ExceptionFilterChain;
45 import com.caucho.server.dispatch.Invocation;
46 import com.caucho.server.port.Port;
47 import com.caucho.server.webapp.WebAppContainer;
48 import com.caucho.util.L10N;
49 import com.caucho.vfs.Dependency;
50 import com.caucho.vfs.Path;
51
52 import java.util.ArrayList JavaDoc;
53 import java.util.logging.Level JavaDoc;
54 import java.util.logging.Logger JavaDoc;
55
56 /**
57  * Resin's virtual host implementation.
58  */

59 public class Host extends WebAppContainer
60   implements EnvironmentBean, Dependency, SchemaBean,
61          EnvironmentDeployInstance
62 {
63   private static final Logger JavaDoc log = Logger.getLogger(Host.class.getName());
64   private static final L10N L = new L10N(Host.class);
65
66   private static EnvironmentLocal<Host> _hostLocal
67     = new EnvironmentLocal<Host>("caucho.host");
68
69   private HostContainer _parent;
70
71   // The Host entry
72
private HostController _controller;
73
74   // The canonical host name. The host name may include the port.
75
private String JavaDoc _hostName = "";
76   // The canonical URL
77
private String JavaDoc _url;
78
79   private String JavaDoc _serverName = "";
80   private int _serverPort = 0;
81
82   // The secure host
83
private String JavaDoc _secureHostName;
84
85   private boolean _isDefaultHost;
86
87   // Alises
88
private ArrayList JavaDoc<String JavaDoc> _aliasList = new ArrayList JavaDoc<String JavaDoc>();
89
90   private Throwable JavaDoc _configException;
91   
92   private boolean _isRootDirSet;
93   private boolean _isDocDirSet;
94
95   private final Lifecycle _lifecycle;
96
97   private String JavaDoc _configETag = null;
98   
99   /**
100    * Creates the webApp with its environment loader.
101    */

102   public Host(HostContainer parent, HostController controller, String JavaDoc hostName)
103   {
104     super(new EnvironmentClassLoader());
105
106     try {
107       _controller = controller;
108
109       setParent(parent);
110       setHostName(hostName);
111
112       _hostLocal.set(this, getClassLoader());
113     } catch (Throwable JavaDoc e) {
114       _configException = e;
115     } finally {
116       _lifecycle = new Lifecycle(log, toString(), Level.INFO);
117     }
118   }
119
120   /**
121    * Returns the local host.
122    */

123   public static Host getLocal()
124   {
125     return _hostLocal.get();
126   }
127
128   /**
129    * Sets the canonical host name.
130    */

131   private void setHostName(String JavaDoc name)
132     throws ConfigException
133   {
134     _hostName = name;
135
136     if (name.equals(""))
137       _isDefaultHost = true;
138
139     addHostAlias(name);
140
141     getEnvironmentClassLoader().setId("host:" + name);
142     
143     // _jmxContext.put("J2EEServer", name);
144

145     int p = name.indexOf("://");
146     
147     if (p >= 0)
148       name = name.substring(p + 3);
149
150     _serverName = name;
151
152     p = name.lastIndexOf(':');
153     if (p > 0) {
154       _serverName = name.substring(0, p);
155       
156       boolean isPort = true;
157       int port = 0;
158       for (p++; p < name.length(); p++) {
159     char ch = name.charAt(p);
160
161     if ('0' <= ch && ch <= '9')
162       port = 10 * port + ch - '0';
163     else
164           isPort = false;
165       }
166
167       if (isPort)
168     _serverPort = port;
169     }
170   }
171
172   /**
173    * Returns the entry name
174    */

175   public String JavaDoc getName()
176   {
177     return _controller.getName();
178   }
179
180   /**
181    * Returns the canonical host name. The canonical host name may include
182    * the port.
183    */

184   public String JavaDoc getHostName()
185   {
186     return _hostName;
187   }
188
189   /**
190    * Returns the host (as an webApp container)
191    */

192   public Host getHost()
193   {
194     return this;
195   }
196
197   /**
198    * Returns the secure host name. Used for redirects.
199    */

200   public String JavaDoc getSecureHostName()
201   {
202     return _secureHostName;
203   }
204
205   /**
206    * Sets the secure host name. Used for redirects.
207    */

208   public void setSecureHostName(String JavaDoc secureHostName)
209   {
210     _secureHostName = secureHostName;
211   }
212
213   /**
214    * Returns the relax schema.
215    */

216   public String JavaDoc getSchema()
217   {
218     return "com/caucho/server/host/host.rnc";
219   }
220   
221   /**
222    * Returns the URL for the container.
223    */

224   public String JavaDoc getURL()
225   {
226     if (_url != null)
227       return _url;
228     else if (_hostName == null || _hostName.equals("")) {
229       Server server = getServer();
230
231       if (server == null)
232     return "";
233
234       for (Port port : server.getPorts()) {
235     if ("http".equals(port.getProtocolName())) {
236       String JavaDoc address = port.getAddress();
237       if (address == null)
238         address = "localhost";
239       
240       return "http://" + address + ":" + port.getPort();
241     }
242       }
243
244       for (Port port : server.getPorts()) {
245     if ("https".equals(port.getProtocolName())) {
246       String JavaDoc address = port.getAddress();
247       if (address == null)
248         address = "localhost";
249       
250       return "https://" + address + ":" + port.getPort();
251     }
252       }
253
254       return "";
255     }
256     else if (_hostName.startsWith("http:") ||
257              _hostName.startsWith("https:"))
258       return _hostName;
259     else
260       return "http://" + _hostName;
261   }
262
263   /**
264    * Adds an alias.
265    */

266   public void addHostAlias(String JavaDoc name)
267   {
268     name = name.toLowerCase();
269
270     if (! _aliasList.contains(name))
271       _aliasList.add(name);
272
273     if (name.equals("") || name.equals("*"))
274       _isDefaultHost = true;
275
276
277     _controller.addExtHostAlias(name);
278   }
279
280   /**
281    * Gets the alias list.
282    */

283   public ArrayList JavaDoc<String JavaDoc> getAliasList()
284   {
285     return _aliasList;
286   }
287
288   /**
289    * Returns true if matches the default host.
290    */

291   public boolean isDefaultHost()
292   {
293     return _isDefaultHost;
294   }
295
296   /**
297    * Sets the parent container.
298    */

299   private void setParent(HostContainer parent)
300   {
301     _parent = parent;
302
303     setDispatchServer(parent.getDispatchServer());
304
305     if (! _isRootDirSet) {
306       setRootDirectory(parent.getRootDirectory());
307       _isRootDirSet = false;
308     }
309   }
310
311   /**
312    * Gets the environment class loader.
313    */

314   public EnvironmentClassLoader getEnvironmentClassLoader()
315   {
316     return (EnvironmentClassLoader) getClassLoader();
317   }
318
319   /**
320    * Sets the root dir.
321    */

322   public void setRootDirectory(Path rootDir)
323   {
324     super.setRootDirectory(rootDir);
325     _isRootDirSet = true;
326
327     if (! _isDocDirSet) {
328       setDocumentDirectory(rootDir);
329       _isDocDirSet = false;
330     }
331   }
332
333   /**
334    * Sets the doc dir.
335    */

336   public void setDocumentDirectory(Path docDir)
337   {
338     super.setDocumentDirectory(docDir);
339     _isDocDirSet = true;
340   }
341
342   /**
343    * Sets the config exception.
344    */

345   public void setConfigException(Throwable JavaDoc e)
346   {
347     if (e != null) {
348       // XXX:
349
_configException = e;
350       getEnvironmentClassLoader().addDependency(AlwaysModified.create());
351     }
352   }
353
354   /**
355    * Gets the config exception.
356    */

357   public Throwable JavaDoc getConfigException()
358   {
359     return _configException;
360   }
361
362   /**
363    * Returns the owning server.
364    */

365   public Server getServer()
366   {
367     if (_parent != null) {
368       DispatchServer server = _parent.getDispatchServer();
369
370       if (server instanceof Server)
371     return (Server) server;
372     }
373
374     return null;
375   }
376   
377   /**
378    * Returns the current cluster.
379    */

380   public Cluster getCluster()
381   {
382     Server server = getServer();
383
384     if (server != null)
385       return server.getCluster();
386     else
387       return null;
388   }
389
390   /**
391    * Returns the config etag.
392    */

393   public String JavaDoc getConfigETag()
394   {
395     return _configETag;
396   }
397
398   /**
399    * Returns the config etag.
400    */

401   public void setConfigETag(String JavaDoc etag)
402   {
403     _configETag = etag;
404   }
405
406   /**
407    * Returns the admin.
408    */

409   public HostMXBean getAdmin()
410   {
411     return _controller.getAdmin();
412   }
413
414   /**
415    * Starts the host.
416    */

417   public void start()
418   {
419     if (! _lifecycle.toStarting())
420       return;
421     
422     if (getURL().equals("") && _parent != null) {
423       _url = _parent.getURL();
424     }
425
426     EnvironmentClassLoader loader;
427     loader = getEnvironmentClassLoader();
428     
429     loader.setId("host:" + getURL());
430                        
431     Thread JavaDoc thread = Thread.currentThread();
432     ClassLoader JavaDoc oldLoader = thread.getContextClassLoader();
433
434     try {
435       thread.setContextClassLoader(loader);
436       
437       super.start();
438       
439       loader.start();
440
441       if (_parent != null)
442     _parent.clearCache();
443     } finally {
444       _lifecycle.toActive();
445       
446       thread.setContextClassLoader(oldLoader);
447     }
448   }
449
450   /**
451    * Clears the cache
452    */

453   public void clearCache()
454   {
455     super.clearCache();
456
457     setConfigETag(null);
458   }
459
460   /**
461    * Builds the invocation for the host.
462    */

463   public void buildInvocation(Invocation invocation)
464     throws Exception JavaDoc
465   {
466     invocation.setHostName(_serverName);
467     invocation.setPort(_serverPort);
468
469     Thread JavaDoc thread = Thread.currentThread();
470     ClassLoader JavaDoc oldLoader = thread.getContextClassLoader();
471
472     try {
473       thread.setContextClassLoader(getClassLoader());
474
475       if (_configException == null)
476         super.buildInvocation(invocation);
477       else {
478         invocation.setFilterChain(new ExceptionFilterChain(_configException));
479     invocation.setDependency(AlwaysModified.create());
480       }
481     } finally {
482       thread.setContextClassLoader(oldLoader);
483     }
484   }
485
486   /**
487    * Returns true if the host is modified.
488    */

489   public boolean isModified()
490   {
491     return (isDestroyed() || getEnvironmentClassLoader().isModified());
492   }
493
494   /**
495    * Returns true if the host is modified.
496    */

497   public boolean isModifiedNow()
498   {
499     return (isDestroyed() || getEnvironmentClassLoader().isModifiedNow());
500   }
501
502   /**
503    * Returns true if the host deploy was an error
504    */

505   public boolean isDeployError()
506   {
507     return _configException != null;
508   }
509
510   /**
511    * Returns true if the host is idle
512    */

513   public boolean isDeployIdle()
514   {
515     return false;
516   }
517   
518   /**
519    * Stops the host.
520    */

521   public boolean stop()
522   {
523     Thread JavaDoc thread = Thread.currentThread();
524     ClassLoader JavaDoc oldLoader = thread.getContextClassLoader();
525
526     try {
527       EnvironmentClassLoader envLoader = getEnvironmentClassLoader();
528       thread.setContextClassLoader(envLoader);
529
530       if (! _lifecycle.toStopping())
531     return false;
532       
533       super.stop();
534       
535       envLoader.stop();
536
537       return true;
538     } finally {
539       _lifecycle.toStop();
540       
541       thread.setContextClassLoader(oldLoader);
542     }
543   }
544   
545   /**
546    * Closes the host.
547    */

548   public void destroy()
549   {
550     stop();
551     
552     if (isDestroyed())
553       return;
554
555     Thread JavaDoc thread = Thread.currentThread();
556     ClassLoader JavaDoc oldLoader = thread.getContextClassLoader();
557     EnvironmentClassLoader classLoader = getEnvironmentClassLoader();
558     
559     thread.setContextClassLoader(classLoader);
560     
561     try {
562       super.destroy();
563     } finally {
564       thread.setContextClassLoader(oldLoader);
565
566       classLoader.destroy();
567     }
568   }
569
570   public String JavaDoc toString()
571   {
572     return "Host[" + getHostName() + "]";
573   }
574 }
575
Popular Tags