KickJava   Java API By Example, From Geeks To Geeks.

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


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.loader.Environment;
34 import com.caucho.loader.EnvironmentListener;
35 import com.caucho.log.Log;
36 import com.caucho.server.deploy.DeployContainer;
37 import com.caucho.server.deploy.DeployGenerator;
38 import com.caucho.vfs.Path;
39
40 import java.util.ArrayList JavaDoc;
41 import java.util.Set JavaDoc;
42 import java.util.logging.Level JavaDoc;
43 import java.util.logging.Logger JavaDoc;
44
45 /**
46  * The generator for the web-app deploy
47  */

48 public class WebAppSingleDeployGenerator
49   extends DeployGenerator<WebAppController>
50   implements EnvironmentListener {
51   private static final Logger JavaDoc log = Log.open(WebAppSingleDeployGenerator.class);
52
53   private WebAppContainer _container;
54   
55   private WebAppController _parentWebApp;
56
57   private String JavaDoc _urlPrefix = "";
58
59   private Path _archivePath;
60   private Path _rootDirectory;
61
62   private ArrayList JavaDoc<WebAppConfig> _defaultList = new ArrayList JavaDoc<WebAppConfig>();
63   private WebAppConfig _config;
64
65   private ClassLoader JavaDoc _parentLoader;
66
67   private WebAppController _controller;
68
69   /**
70    * Creates the new host deploy.
71    */

72   public WebAppSingleDeployGenerator(DeployContainer<WebAppController> deployContainer)
73   {
74     super(deployContainer);
75   }
76
77   /**
78    * Creates the new web-app deploy.
79    */

80   public WebAppSingleDeployGenerator(DeployContainer<WebAppController> deployContainer,
81                      WebAppContainer container,
82                      WebAppConfig config)
83     throws Exception JavaDoc
84   {
85     super(deployContainer);
86     
87     setContainer(container);
88
89     String JavaDoc contextPath = config.getContextPath();
90
91     if (contextPath.equals("/"))
92       contextPath = "";
93     
94     setURLPrefix(config.getContextPath());
95
96     _config = config;
97   }
98
99   /**
100    * Gets the webApp container.
101    */

102   public WebAppContainer getContainer()
103   {
104     return _container;
105   }
106
107   /**
108    * Sets the webApp container.
109    */

110   public void setContainer(WebAppContainer container)
111   {
112     _container = container;
113
114     if (_parentLoader == null)
115       _parentLoader = container.getClassLoader();
116   }
117   /**
118    * Sets the parent webApp.
119    */

120   public void setParentWebApp(WebAppController parent)
121   {
122     _parentWebApp = parent;
123   }
124
125   /**
126    * Sets the parent loader.
127    */

128   public void setParentClassLoader(ClassLoader JavaDoc loader)
129   {
130     _parentLoader = loader;
131   }
132
133   /**
134    * Sets the url prefix.
135    */

136   public void setURLPrefix(String JavaDoc prefix)
137   {
138     if (! prefix.startsWith("/"))
139       prefix = "/" + prefix;
140     
141     while (prefix.endsWith("/")) {
142       prefix = prefix.substring(0, prefix.length() - 1);
143     }
144     
145     _urlPrefix = prefix;
146   }
147
148   /**
149    * Gets the url prefix.
150    */

151   public String JavaDoc getURLPrefix()
152   {
153     return _urlPrefix;
154   }
155
156   /**
157    * Sets the root directory.
158    */

159   public void setRootDirectory(Path rootDirectory)
160   {
161     _rootDirectory = rootDirectory;
162   }
163
164   /**
165    * Adds a default.
166    */

167   public void addWebAppDefault(WebAppConfig config)
168   {
169     _defaultList.add(config);
170   }
171
172   /**
173    * Returns the log.
174    */

175   protected Logger JavaDoc getLog()
176   {
177     return log;
178   }
179
180   /**
181    * Initializes the controller.
182    */

183   @Override JavaDoc
184   protected void initImpl()
185   {
186     super.initImpl();
187
188     if (_controller != null)
189       return;
190
191     String JavaDoc appDir = _config.getDocumentDirectory();
192
193     if (appDir == null)
194       appDir = "./" + _urlPrefix;
195
196     if (_rootDirectory == null) {
197       _rootDirectory = PathBuilder.lookupPath(appDir, null,
198                           _container.getDocumentDirectory());
199     }
200
201     String JavaDoc archivePath = _config.getArchivePath();
202
203     if (archivePath != null) {
204       _archivePath = PathBuilder.lookupPath(archivePath, null,
205                           _container.getRootDirectory());
206     }
207     
208     _controller = new WebAppController(_urlPrefix, _rootDirectory, _container);
209
210     _controller.setArchivePath(_archivePath);
211
212     if (_archivePath != null)
213       _controller.addDepend(_archivePath);
214     
215     _controller.setParentWebApp(_parentWebApp);
216
217     for (WebAppConfig config : _defaultList)
218       _controller.addConfigDefault(config);
219     
220     // _controller.setConfig(_config);
221
// server/1h13
222
_controller.addConfigDefault(_config);
223
224     _controller.setPrologue(_config.getPrologue());
225
226     _controller.setSourceType("single");
227
228     Environment.addEnvironmentListener(this, _parentLoader);
229   }
230
231   /**
232    * Returns the deployed keys.
233    */

234   protected void fillDeployedKeys(Set JavaDoc<String JavaDoc> keys)
235   {
236     keys.add(_controller.getContextPath());
237   }
238   
239   /**
240    * Returns the current array of webApp entries.
241    */

242   public WebAppController generateController(String JavaDoc name)
243   {
244     if (name.equals(_controller.getContextPath())) {
245       WebAppController webApp;
246       
247       webApp = new WebAppController(_urlPrefix, _rootDirectory, _container);
248
249       webApp.setArchivePath(_controller.getArchivePath());
250
251       return webApp;
252     }
253     else
254       return null;
255   }
256   
257   /**
258    * Merges the controllers.
259    */

260   public WebAppController mergeController(WebAppController controller,
261                       String JavaDoc name)
262   {
263     // if directory matches, merge the two controllers. The
264
// last controller has priority.
265
if (controller.getRootDirectory().equals(_controller.getRootDirectory())) {
266       // server/1h10
267
controller.setContextPath(_controller.getContextPath());
268       
269       controller.setDynamicDeploy(false);
270       
271       return controller.merge(_controller);
272     }
273     // else if the names don't match, return the new controller
274
else if (! _controller.isNameMatch(name))
275       return controller;
276     // otherwise, the single deploy overrides
277
else
278       return _controller;
279   }
280
281   /**
282    * Initialize the deployment.
283    */

284   public void deploy()
285   {
286     try {
287       init();
288     } catch (Exception JavaDoc e) {
289       log.log(Level.WARNING, e.toString(), e);
290     }
291   }
292
293   public Throwable JavaDoc getConfigException()
294   {
295     Throwable JavaDoc configException = super.getConfigException();
296
297     if (configException == null && _controller != null)
298       configException = _controller.getConfigException();
299
300     return configException;
301   }
302
303   /**
304    * Destroy the deployment.
305    */

306   @Override JavaDoc
307   protected void destroyImpl()
308   {
309     Environment.removeEnvironmentListener(this, _parentLoader);
310
311     _container.removeWebAppDeploy(this);
312
313     super.destroyImpl();
314   }
315   
316   public String JavaDoc toString()
317   {
318     return "WebAppSingleDeployGenerator[" + _urlPrefix + "]";
319   }
320 }
321
Popular Tags