KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > webapp > WebAppController


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.webapp;
31
32 import com.caucho.config.types.PathBuilder;
33 import com.caucho.log.Log;
34 import com.caucho.management.j2ee.J2EEManagedObject;
35 import com.caucho.management.j2ee.WebModule;
36 import com.caucho.server.deploy.DeployConfig;
37 import com.caucho.server.deploy.DeployControllerAdmin;
38 import com.caucho.server.deploy.EnvironmentDeployController;
39 import com.caucho.server.host.Host;
40 import com.caucho.server.util.CauchoSystem;
41 import com.caucho.util.L10N;
42 import com.caucho.vfs.Path;
43
44 import javax.servlet.jsp.el.ELException JavaDoc;
45 import java.io.IOException JavaDoc;
46 import java.util.ArrayList JavaDoc;
47 import java.util.Map JavaDoc;
48 import java.util.logging.Logger JavaDoc;
49 import java.util.regex.Matcher JavaDoc;
50 import java.util.regex.Pattern JavaDoc;
51 /**
52  * A configuration entry for a web-app.
53  */

54 public class WebAppController
55   extends EnvironmentDeployController<WebApp,WebAppConfig> {
56   private static final L10N L = new L10N(WebAppController.class);
57   private static final Logger JavaDoc log = Log.open(WebAppController.class);
58
59   private WebAppContainer _container;
60
61   private WebAppController _parent;
62
63   // The context path is the URL prefix for the web-app
64
private String JavaDoc _contextPath;
65
66   private String JavaDoc _warName;
67
68   // regexp values
69
private ArrayList JavaDoc<String JavaDoc> _regexpValues;
70
71   private boolean _isInheritSession;
72   private boolean _isDynamicDeploy;
73
74   private ArrayList JavaDoc<Path> _dependPathList = new ArrayList JavaDoc<Path>();
75
76   private String JavaDoc _sourceType = "unknown";
77
78   private final Object JavaDoc _statisticsLock = new Object JavaDoc();
79
80   private volatile long _lifetimeConnectionCount;
81   private volatile long _lifetimeConnectionTime;
82   private volatile long _lifetimeReadBytes;
83   private volatile long _lifetimeWriteBytes;
84   private volatile long _lifetimeClientDisconnectCount;
85
86   private WebAppAdmin _admin = new WebAppAdmin(this);
87
88   public WebAppController()
89   {
90     this("/", null, null);
91   }
92
93   public WebAppController(String JavaDoc contextPath,
94               Path rootDirectory,
95               WebAppContainer container)
96   {
97     super(contextPath, rootDirectory);
98
99     _container = container;
100
101     setContextPath(contextPath);
102
103     getVariableMap().put("app", new Var());
104     getVariableMap().put("webApp", new Var());
105   }
106
107   /**
108    * Returns the webApp's context path
109    */

110   public String JavaDoc getContextPath()
111   {
112     return _contextPath;
113   }
114
115   /**
116    * Sets the webApp's context path
117    */

118   public void setContextPath(String JavaDoc contextPath)
119   {
120     if (! contextPath.equals("") && ! contextPath.startsWith("/"))
121       contextPath = "/" + contextPath;
122
123     if (contextPath.endsWith("/"))
124       contextPath = contextPath.substring(0, contextPath.length() - 1);
125
126     _contextPath = contextPath;
127   }
128
129   /**
130    * Returns the webApp's context path
131    */

132   public String JavaDoc getContextPath(String JavaDoc uri)
133   {
134     if (getConfig() == null || getConfig().getURLRegexp() == null)
135       return getContextPath();
136
137     Pattern JavaDoc regexp = getConfig().getURLRegexp();
138     Matcher JavaDoc matcher = regexp.matcher(uri);
139
140     int tail = 0;
141     while (tail >= 0 && tail <= uri.length()) {
142       String JavaDoc prefix = uri.substring(0, tail);
143
144       matcher.reset(prefix);
145
146       if (matcher.find() && matcher.start() == 0)
147     return matcher.group();
148
149       if (tail < uri.length()) {
150     tail = uri.indexOf('/', tail + 1);
151     if (tail < 0)
152       tail = uri.length();
153       }
154       else
155     break;
156     }
157
158     return _contextPath;
159   }
160
161   /**
162    * Sets the war name prefix.
163    */

164   public void setWarName(String JavaDoc warName)
165   {
166     _warName = warName;
167   }
168
169   /**
170    * Gets the war name prefix.
171    */

172   public String JavaDoc getWarName()
173   {
174     return _warName;
175   }
176
177   /**
178    * Gets the URL
179    */

180   public String JavaDoc getURL()
181   {
182     if (_container != null)
183       return _container.getURL() + _contextPath;
184     else
185       return _contextPath;
186   }
187
188   /**
189    * Returns the parent controller.
190    */

191   public WebAppController getParent()
192   {
193     return _parent;
194   }
195
196   /**
197    * Returns the web-app container.
198    */

199   public WebAppContainer getContainer()
200   {
201     return _container;
202   }
203
204   /**
205    * Sets the parent controller.
206    */

207   public void setParentWebApp(WebAppController parent)
208   {
209     _parent = parent;
210   }
211
212   /**
213    * Returns the containing host.
214    */

215   public Host getHost()
216   {
217     if (_container != null)
218       return _container.getHost();
219     else
220       return null;
221   }
222
223   /**
224    * Returns the source (for backwards compatibility)
225    */

226   public String JavaDoc getSourceType()
227   {
228     return _sourceType;
229   }
230
231   /**
232    * Sets the source (for backwards compatibility)
233    */

234   public void setSourceType(String JavaDoc type)
235   {
236     _sourceType = type;
237   }
238
239   /**
240    * Sets the regexp values.
241    */

242   public void setRegexpValues(ArrayList JavaDoc<String JavaDoc> values)
243   {
244     _regexpValues = values;
245   }
246
247   /**
248    * True for inherit-session webApps.
249    */

250   public boolean isInheritSession()
251   {
252     return _isInheritSession;
253   }
254
255   /**
256    * True for inherit-session
257    */

258   public void setInheritSession(boolean inheritSession)
259   {
260     _isInheritSession = inheritSession;
261   }
262
263   /**
264    * Returns the webApp object.
265    */

266   public WebApp getWebApp()
267   {
268     return getDeployInstance();
269   }
270
271   /**
272    * Set true for a dynamically deployed webApp.
273    */

274   public void setDynamicDeploy(boolean isDynamicDeploy)
275   {
276     _isDynamicDeploy = isDynamicDeploy;
277   }
278
279   /**
280    * Returns true for a dynamically deployed webApp.
281    */

282   public boolean isDynamicDeploy()
283   {
284     return _isDynamicDeploy;
285   }
286
287   protected String JavaDoc getMBeanTypeName()
288   {
289     return "WebApp";
290   }
291
292   protected String JavaDoc getMBeanId()
293   {
294     String JavaDoc name = _contextPath;
295     if (_contextPath.equals(""))
296       name = "/";
297
298     return name;
299   }
300
301   /**
302    * Returns the deploy admin.
303    */

304   @Override JavaDoc
305   protected DeployControllerAdmin getDeployAdmin()
306   {
307     return _admin;
308   }
309
310   @Override JavaDoc
311   protected void initEnd()
312   {
313     super.initEnd();
314     
315     J2EEManagedObject.register(new WebModule(this));
316   }
317
318   /**
319    * Returns the admin.
320    */

321   public WebAppAdmin getAdmin()
322   {
323     return _admin;
324   }
325
326   /**
327    * Returns true if the controller matches.
328    */

329   public boolean isNameMatch(String JavaDoc url)
330   {
331     if (CauchoSystem.isCaseInsensitive())
332       return url.equalsIgnoreCase(_contextPath);
333     else
334       return url.equals(_contextPath);
335   }
336
337   /**
338    * Merges two entries.
339    */

340   protected WebAppController merge(WebAppController newController)
341   {
342     if (getConfig() != null && getConfig().getURLRegexp() != null)
343       return newController;
344     else if (newController.getConfig() != null &&
345          newController.getConfig().getURLRegexp() != null)
346       return this;
347     else {
348       Thread JavaDoc thread = Thread.currentThread();
349       ClassLoader JavaDoc oldLoader = thread.getContextClassLoader();
350
351       try {
352     thread.setContextClassLoader(getParentClassLoader());
353
354     // The contextPath comes from current web-app
355
WebAppController mergedController
356       = new WebAppController(getContextPath(),
357                  getRootDirectory(),
358                  _container);
359
360     // server/1h1{2,3}
361
// This controller overrides configuration from the new controller
362
mergedController.mergeController(this);
363     mergedController.mergeController(newController);
364
365     return mergedController;
366       } finally {
367     thread.setContextClassLoader(oldLoader);
368       }
369     }
370   }
371
372   /**
373    * Returns the webApp object.
374    */

375   public boolean destroy()
376   {
377     if (! super.destroy())
378       return false;
379
380     if (_container != null)
381       _container.removeWebApp(this);
382
383     return true;
384   }
385
386   /**
387    * Any extra steps needed to deploy the webApp.
388    */

389   protected void protectedWebApp()
390     throws Exception JavaDoc
391   {
392     Path root = getRootDirectory();
393     // XXX: need to re-add to control.
394
root.lookup("WEB-INF").chmod(0750);
395     root.lookup("META-INF").chmod(0750);
396   }
397
398   /**
399    * Adding any dependencies.
400    */

401   protected void addDependencies()
402     throws Exception JavaDoc
403   {
404   }
405
406   /**
407    * Adds a dependent file.
408    */

409   public void addDepend(Path path)
410   {
411     _dependPathList.add(path);
412   }
413
414   /**
415    * Initialize the controller.
416    */

417   protected void initBegin()
418   {
419     getVariableMap().put("app-dir", getRootDirectory());
420
421     super.initBegin();
422   }
423
424   protected void fillInitList(ArrayList JavaDoc<DeployConfig> initList)
425   {
426     if (_container != null) {
427       for (WebAppConfig config : _container.getWebAppDefaultList())
428     initList.add(config);
429     }
430
431     super.fillInitList(initList);
432   }
433
434   /**
435    * Instantiate the webApp.
436    */

437   protected WebApp instantiateDeployInstance()
438   {
439     return new Application(this);
440   }
441
442   /**
443    * Creates the webApp.
444    */

445   protected void configureInstanceVariables(WebApp app)
446     throws Throwable JavaDoc
447   {
448     app.setRegexp(_regexpValues);
449     app.setDynamicDeploy(isDynamicDeploy());
450
451     super.configureInstanceVariables(app);
452   }
453   
454   protected void extendJMXContext(Map JavaDoc<String JavaDoc,String JavaDoc> context)
455   {
456     context.put("WebApp", getMBeanId());
457   }
458
459   protected Path calculateRootDirectory()
460     throws ELException JavaDoc
461   {
462     Path appDir = null;
463
464     if (appDir == null && getConfig() != null) {
465       String JavaDoc path = getConfig().getRootDirectory();
466
467       if (path != null)
468         appDir = PathBuilder.lookupPath(path);
469     }
470
471     if (appDir == null && _container != null)
472       appDir = _container.getDocumentDirectory().lookup("./" + _contextPath);
473
474     if (appDir == null && getDeployInstance() != null)
475       appDir = getDeployInstance().getAppDir();
476
477     return appDir;
478   }
479
480   /**
481    * Override to prevent removing of special files.
482    */

483   protected void removeExpandFile(Path path, String JavaDoc relPath)
484     throws IOException JavaDoc
485   {
486     if (relPath.equals("./WEB-INF/resin-web.xml"))
487       return;
488
489     super.removeExpandFile(path, relPath);
490   }
491
492   public long getLifetimeConnectionCount()
493   {
494     synchronized (_statisticsLock) {
495       return _lifetimeConnectionCount;
496     }
497   }
498
499   public long getLifetimeConnectionTime()
500   {
501     synchronized (_statisticsLock) {
502       return _lifetimeConnectionTime;
503     }
504   }
505
506   public long getLifetimeReadBytes()
507   {
508     synchronized (_statisticsLock) {
509       return _lifetimeReadBytes;
510     }
511   }
512
513   public long getLifetimeWriteBytes()
514   {
515     synchronized (_statisticsLock) {
516       return _lifetimeWriteBytes;
517     }
518   }
519
520   public long getLifetimeClientDisconnectCount()
521   {
522     synchronized (_statisticsLock) {
523       return _lifetimeClientDisconnectCount;
524     }
525   }
526
527   /**
528    * Update statistics with the results of one request.
529    *
530    * @param milliseconds the number of millesconds for the request
531    * @param readBytes the number of bytes read
532    * @param writeBytes the number of bytes written
533    * @param isClientDisconnect true if the request ended with a client DisconnectException
534    */

535   public void updateStatistics(long milliseconds,
536                                int readBytes,
537                                int writeBytes,
538                                boolean isClientDisconnect)
539   {
540     synchronized (_statisticsLock) {
541       _lifetimeConnectionCount++;
542       _lifetimeConnectionTime += milliseconds;
543       _lifetimeReadBytes += readBytes;
544       _lifetimeWriteBytes += writeBytes;
545       if (isClientDisconnect)
546         _lifetimeClientDisconnectCount++;
547     }
548   }
549   /**
550    * Returns a printable view.
551    */

552   public String JavaDoc toString()
553   {
554     return "WebAppController$" + System.identityHashCode(this) + "[" + getId() + "]";
555   }
556
557   /**
558    * EL variables for the app.
559    */

560   public class Var {
561     public String JavaDoc getUrl()
562     {
563       return WebAppController.this.getURL();
564     }
565
566     public String JavaDoc getId()
567     {
568       String JavaDoc id = WebAppController.this.getId();
569
570       if (id != null)
571         return id;
572       else
573         return WebAppController.this.getContextPath();
574     }
575
576     public String JavaDoc getName()
577     {
578       if (getWarName() != null)
579         return getWarName();
580       else
581         return getId();
582     }
583
584     public Path getAppDir()
585     {
586       return WebAppController.this.getRootDirectory();
587     }
588
589     public Path getDocDir()
590     {
591       return WebAppController.this.getRootDirectory();
592     }
593
594     public Path getRoot()
595     {
596       return WebAppController.this.getRootDirectory();
597     }
598
599     public Path getRootDir()
600     {
601       return WebAppController.this.getRootDirectory();
602     }
603
604     public String JavaDoc getContextPath()
605     {
606       return WebAppController.this.getContextPath();
607     }
608
609     public ArrayList JavaDoc<String JavaDoc> getRegexp()
610     {
611       return _regexpValues;
612     }
613
614     public String JavaDoc toString()
615     {
616       return "WebApp[" + getURL() + "]";
617     }
618   }
619 }
620
Popular Tags