KickJava   Java API By Example, From Geeks To Geeks.

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


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.types.PathBuilder;
34 import com.caucho.el.EL;
35 import com.caucho.log.Log;
36 import com.caucho.management.server.HostMXBean;
37 import com.caucho.server.deploy.DeployController;
38 import com.caucho.server.deploy.DeployControllerAdmin;
39 import com.caucho.server.deploy.EnvironmentDeployController;
40 import com.caucho.server.e_app.EarConfig;
41 import com.caucho.server.webapp.WebAppConfig;
42 import com.caucho.util.L10N;
43 import com.caucho.vfs.Depend;
44 import com.caucho.vfs.Dependency;
45 import com.caucho.vfs.Path;
46 import com.caucho.vfs.Vfs;
47
48 import java.util.ArrayList JavaDoc;
49 import java.util.HashMap JavaDoc;
50 import java.util.Map JavaDoc;
51 import java.util.logging.Level JavaDoc;
52 import java.util.logging.Logger JavaDoc;
53 import java.util.regex.Matcher JavaDoc;
54 import java.util.regex.Pattern JavaDoc;
55
56 /**
57  * A configuration entry for a host
58  */

59 public class HostController
60   extends EnvironmentDeployController<Host,HostConfig>
61 {
62   private static final Logger JavaDoc log = Log.open(HostController.class);
63   private static final L10N L = new L10N(HostController.class);
64   
65   private HostContainer _container;
66
67   // The host name is the canonical name
68
private String JavaDoc _hostName;
69   // The regexp name is the matching name of the regexp
70
private String JavaDoc _regexpName;
71
72   private Pattern JavaDoc _regexp;
73   private String JavaDoc _rootDirectoryPattern;
74
75   // Any host aliases.
76
private ArrayList JavaDoc<String JavaDoc> _entryHostAliases
77     = new ArrayList JavaDoc<String JavaDoc>();
78   private ArrayList JavaDoc<Pattern JavaDoc> _entryHostAliasRegexps
79     = new ArrayList JavaDoc<Pattern JavaDoc>();
80   
81   private ArrayList JavaDoc<String JavaDoc> _hostAliases = new ArrayList JavaDoc<String JavaDoc>();
82
83   // The host variables.
84
private final Var _hostVar = new Var();
85   private final HostAdmin _admin = new HostAdmin(this);
86
87   private ArrayList JavaDoc<Dependency> _dependList = new ArrayList JavaDoc<Dependency>();
88
89   HostController(String JavaDoc id,
90          HostConfig config,
91          HostContainer container,
92          Map JavaDoc<String JavaDoc,Object JavaDoc> varMap)
93   {
94     super(id, config);
95
96     setHostName(id);
97
98     if (varMap != null)
99       getVariableMap().putAll(varMap);
100     
101     getVariableMap().put("host", _hostVar);
102
103     setContainer(container);
104     
105     setRootDirectory(config.calculateRootDirectory(getVariableMap()));
106   }
107
108   HostController(String JavaDoc id, Path rootDirectory, HostContainer container)
109   {
110     super(id, rootDirectory);
111
112     addHostAlias(id);
113     setHostName(id);
114
115     getVariableMap().put("name", id);
116     getVariableMap().put("host", _hostVar);
117     
118     setContainer(container);
119   }
120
121   public void setContainer(HostContainer container)
122   {
123     _container = container;
124     
125     if (_container != null) {
126       for (HostConfig defaultConfig : _container.getHostDefaultList())
127     addConfigDefault(defaultConfig);
128     }
129   }
130
131   /**
132    * Returns the Resin host name.
133    */

134   public String JavaDoc getName()
135   {
136     String JavaDoc name = super.getId();
137     
138     if (name != null)
139       return name;
140     else
141       return getHostName();
142   }
143
144   /**
145    * Returns the host's canonical name
146    */

147   public String JavaDoc getHostName()
148   {
149     return _hostName;
150   }
151
152   /**
153    * Sets the host's canonical name
154    */

155   public void setHostName(String JavaDoc name)
156   {
157     if (name != null)
158       name = name.trim();
159     
160     if (name == null || name.equals("*"))
161       name = "";
162     
163     name = name.toLowerCase();
164
165     _hostName = name;
166   }
167
168   /**
169    * Returns the host's canonical name
170    */

171   public void setRegexpName(String JavaDoc name)
172   {
173     _regexpName = name.toLowerCase();
174   }
175   
176   /**
177    * Adds a host alias.
178    */

179   public void addHostAlias(String JavaDoc name)
180   {
181     if (name != null)
182       name = name.trim();
183     
184     if (name == null || name.equals("*"))
185       name = ""; // XXX: default?
186

187     name = name.toLowerCase();
188
189     if (! _entryHostAliases.contains(name))
190       _entryHostAliases.add(name);
191
192     addExtHostAlias(name);
193   }
194
195   /**
196    * Adds an extension host alias, e.g. from a resin:import
197    */

198   public void addExtHostAlias(String JavaDoc name)
199   {
200     if (! _hostAliases.contains(name))
201       _hostAliases.add(name);
202   }
203
204   /**
205    * Returns the host aliases.
206    */

207   public ArrayList JavaDoc<String JavaDoc> getHostAliases()
208   {
209     return _hostAliases;
210   }
211
212   /**
213    * Sets the regexp pattern
214    */

215   public void setRegexp(Pattern JavaDoc regexp)
216   {
217     _regexp = regexp;
218   }
219
220   /**
221    * Sets the root directory pattern
222    */

223   public void setRootDirectoryPattern(String JavaDoc rootDirectoryPattern)
224   {
225     _rootDirectoryPattern = rootDirectoryPattern;
226   }
227
228   /**
229    * Adds a dependent file.
230    */

231   public void addDepend(Path depend)
232   {
233     if (! _dependList.contains(depend))
234       _dependList.add(new Depend(depend));
235   }
236
237   /**
238    * Returns the host admin.
239    */

240   public HostMXBean getAdmin()
241   {
242     return _admin;
243   }
244
245   /**
246    * Returns the deploy admin.
247    */

248   protected DeployControllerAdmin getDeployAdmin()
249   {
250     return _admin;
251   }
252
253   /**
254    * Initialize the entry.
255    */

256   protected void initBegin()
257   {
258     try {
259       try {
260     if (getConfig() == null || getHostName() != null) {
261     }
262     else if (getConfig().getHostName() != null)
263       setHostName(EL.evalString(getConfig().getHostName(),
264                     EL.getEnvironment()));
265       } catch (Exception JavaDoc e) {
266     log.log(Level.WARNING, e.toString(), e);
267       }
268
269       if (_regexpName != null && _hostName == null)
270     _hostName = _regexpName;
271
272       if (_hostName == null)
273     _hostName = "";
274
275       ArrayList JavaDoc<String JavaDoc> aliases = null;
276
277       if (getConfig() != null) {
278     aliases = getConfig().getHostAliases();
279
280     _entryHostAliasRegexps.addAll(getConfig().getHostAliasRegexps());
281       }
282       
283       for (int i = 0; aliases != null && i < aliases.size(); i++) {
284     String JavaDoc alias = aliases.get(i);
285
286     alias = EL.evalString(alias, EL.getEnvironment());
287     
288     addHostAlias(alias);
289       }
290     } catch (Exception JavaDoc e) {
291       log.log(Level.WARNING, e.toString(), e);
292     }
293
294     super.initBegin();
295   }
296
297   /**
298    * Returns the "name" property.
299    */

300   protected String JavaDoc getMBeanId()
301   {
302     String JavaDoc name = _hostName;
303     
304     if (name == null)
305       name = "";
306     else if (name.indexOf(':') >= 0)
307       name = name.replace(':', '-');
308
309     if (name.equals(""))
310       return "default";
311     else
312       return name;
313   }
314
315   /**
316    * Returns true for a matching name.
317    */

318   public boolean isNameMatch(String JavaDoc name)
319   {
320     if (_hostName.equalsIgnoreCase(name))
321       return true;
322
323     for (int i = _hostAliases.size() - 1; i >= 0; i--) {
324       if (name.equalsIgnoreCase(_hostAliases.get(i)))
325     return true;
326     }
327
328     for (int i = _entryHostAliasRegexps.size() - 1; i >= 0; i--) {
329       Pattern JavaDoc alias = _entryHostAliasRegexps.get(i);
330
331       if (alias.matcher(name).find())
332     return true;
333     }
334
335     if (_regexp != null) {
336       // server/0523
337

338       Matcher JavaDoc matcher = _regexp.matcher(name);
339
340       if (matcher.matches()) {
341     Path rootDirectory = calculateRoot(matcher);
342
343     if (getRootDirectory().equals(rootDirectory))
344       return true;
345       }
346     }
347     
348     return false;
349   }
350
351   private Path calculateRoot(Matcher JavaDoc matcher)
352   {
353     // XXX: duplicates HostRegexp
354

355     Thread JavaDoc thread = Thread.currentThread();
356     ClassLoader JavaDoc oldLoader = thread.getContextClassLoader();
357
358     try {
359       thread.setContextClassLoader(getParentClassLoader());
360
361       if (_rootDirectoryPattern == null) {
362     // server/129p
363
return Vfs.lookup();
364       }
365       
366       int length = matcher.end() - matcher.start();
367
368       ArrayList JavaDoc<String JavaDoc> vars = new ArrayList JavaDoc<String JavaDoc>();
369
370       HashMap JavaDoc<String JavaDoc,Object JavaDoc> varMap = new HashMap JavaDoc<String JavaDoc,Object JavaDoc>();
371         
372       for (int j = 0; j <= matcher.groupCount(); j++) {
373     vars.add(matcher.group(j));
374     varMap.put("host" + j, matcher.group(j));
375       }
376
377       varMap.put("regexp", vars);
378       varMap.put("host", new TestVar(matcher.group(0), vars));
379
380       Path path = PathBuilder.lookupPath(_rootDirectoryPattern, varMap);
381       
382       return path;
383     } catch (Exception JavaDoc e) {
384       log.log(Level.FINE, e.toString(), e);
385
386       // XXX: not quite right
387
return Vfs.lookup(_rootDirectoryPattern);
388     } finally {
389       thread.setContextClassLoader(oldLoader);
390     }
391   }
392
393   /**
394    * Merges two entries.
395    */

396   protected HostController merge(HostController newController)
397   {
398     if (getConfig() != null && getConfig().getRegexp() != null)
399       return newController;
400     else if (newController.getConfig() != null &&
401          newController.getConfig().getRegexp() != null)
402       return this;
403     else {
404       Thread JavaDoc thread = Thread.currentThread();
405       ClassLoader JavaDoc oldLoader = thread.getContextClassLoader();
406
407       try {
408     thread.setContextClassLoader(getParentClassLoader());
409
410     HostController mergedController
411       = new HostController(newController.getHostName(),
412                    getRootDirectory(),
413                    _container);
414
415     mergedController.mergeController(this);
416     mergedController.mergeController(newController);
417
418     if (! getHostName().equals(newController.getHostName())) {
419       ConfigException e;
420
421       e = new ConfigException(L.l("Illegal merge of {0} and {1}. Both hosts have the same root-directory '{2}'.",
422                     getHostName(),
423                     newController.getHostName(),
424                     getRootDirectory()));
425
426       log.log(Level.FINE, e.toString(), e);
427
428       mergedController.setConfigException(e);
429     }
430
431     return mergedController;
432       } catch (Throwable JavaDoc e) {
433     log.log(Level.FINE, e.toString(), e);
434     
435     return null;
436       } finally {
437     thread.setContextClassLoader(oldLoader);
438       }
439     }
440   }
441
442   /**
443    * Merges with the old controller.
444    */

445   protected void mergeController(DeployController oldControllerV)
446   {
447     super.mergeController(oldControllerV);
448
449     HostController oldController = (HostController) oldControllerV;
450     
451     _entryHostAliases.addAll(oldController._entryHostAliases);
452     if (! oldController.getHostName().equals(""))
453       _entryHostAliases.add(oldController.getHostName());
454     _entryHostAliasRegexps.addAll(oldController._entryHostAliasRegexps);
455     _hostAliases.addAll(oldController._hostAliases);
456
457     if (_regexp == null) {
458       _regexp = oldController._regexp;
459       _rootDirectoryPattern = oldController._rootDirectoryPattern;
460     }
461   }
462
463   /**
464    * Creates a new instance of the host object.
465    */

466   protected Host instantiateDeployInstance()
467   {
468     return new Host(_container, this, _hostName);
469   }
470
471   /**
472    * Creates the host.
473    */

474   protected void configureInstance(Host host)
475     throws Throwable JavaDoc
476   {
477     _hostAliases.clear();
478     _hostAliases.addAll(_entryHostAliases);
479
480     getVariableMap().put("host-root", getRootDirectory());
481
482     if (_container != null) {
483       for (EarConfig config : _container.getEarDefaultList())
484     host.addEarDefault(config);
485
486       for (WebAppConfig config : _container.getWebAppDefaultList())
487     host.addWebAppDefault(config);
488     }
489
490     super.configureInstance(host);
491   }
492
493   protected void extendJMXContext(Map JavaDoc<String JavaDoc,String JavaDoc> context)
494   {
495     context.put("Host", getMBeanId());
496   }
497
498   /**
499    * Returns equality.
500    */

501   public boolean equals(Object JavaDoc o)
502   {
503     if (! (o instanceof HostController))
504       return false;
505
506     HostController entry = (HostController) o;
507
508     return _hostName.equals(entry._hostName);
509   }
510
511   /**
512    * Returns a printable view.
513    */

514   public String JavaDoc toString()
515   {
516     return "HostController[" + getName() + "]";
517   }
518
519   /**
520    * EL variables for the host.
521    */

522   public class Var {
523     public String JavaDoc getName()
524     {
525       return HostController.this.getName();
526     }
527     
528     public String JavaDoc getHostName()
529     {
530       return HostController.this.getHostName();
531     }
532     
533     public String JavaDoc getUrl()
534     {
535       Host host = getDeployInstance();
536       
537       if (host != null)
538     return host.getURL();
539       else if (_hostName.equals(""))
540     return "";
541       else if (_hostName.startsWith("http:") ||
542            _hostName.startsWith("https:"))
543     return _hostName;
544       else
545     return "http://" + _hostName;
546     }
547
548     public ArrayList JavaDoc getRegexp()
549     {
550       return (ArrayList JavaDoc) getVariableMap().get("regexp");
551     }
552     
553     public Path getRoot()
554     {
555       Host host = getDeployInstance();
556       
557       if (host != null)
558     return host.getRootDirectory();
559       else
560     return HostController.this.getRootDirectory();
561     }
562     
563     /**
564      * @deprecated
565      */

566     public Path getRootDir()
567     {
568       return getRoot();
569     }
570
571     /**
572      * @deprecated
573      */

574     public Path getRootDirectory()
575     {
576       return getRoot();
577     }
578     
579     public Path getDocumentDirectory()
580     {
581       Host host = getDeployInstance();
582       
583       if (host != null)
584     return host.getDocumentDirectory();
585       else
586     return null;
587     }
588     
589     public Path getDocDir()
590     {
591       return getDocumentDirectory();
592     }
593     
594     public Path getWarDirectory()
595     {
596       Host host = getDeployInstance();
597       
598       if (host != null)
599     return host.getWarDir();
600       else
601     return null;
602     }
603     
604     public Path getWarDir()
605     {
606       return getWarDirectory();
607     }
608     
609     public Path getWarExpandDirectory()
610     {
611       Host host = getDeployInstance();
612       
613       if (host != null)
614     return host.getWarExpandDir();
615       else
616     return null;
617     }
618     
619     public Path getWarExpandDir()
620     {
621       return getWarExpandDirectory();
622     }
623     
624     public String JavaDoc toString()
625     {
626       return "Host[" + getId() + "]";
627     }
628   }
629
630   /**
631    * EL variables for the host, when testing for regexp identity .
632    */

633   public class TestVar {
634     private String JavaDoc _name;
635     private ArrayList JavaDoc<String JavaDoc> _regexp;
636
637     TestVar(String JavaDoc name, ArrayList JavaDoc<String JavaDoc> regexp)
638     {
639       _name = name;
640       _regexp = regexp;
641     }
642     
643     public String JavaDoc getName()
644     {
645       return _name;
646     }
647     
648     public String JavaDoc getHostName()
649     {
650       return _name;
651     }
652     
653     public ArrayList JavaDoc<String JavaDoc> getRegexp()
654     {
655       // server/13t0
656
return _regexp;
657     }
658   }
659 }
660
Popular Tags