KickJava   Java API By Example, From Geeks To Geeks.

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


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.ConfigException;
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.ExpandDeployGenerator;
38 import com.caucho.vfs.CaseInsensitive;
39 import com.caucho.vfs.Path;
40
41 import java.util.ArrayList JavaDoc;
42 import java.util.HashMap JavaDoc;
43 import java.util.Set JavaDoc;
44 import java.util.logging.Level JavaDoc;
45 import java.util.logging.Logger JavaDoc;
46
47 /**
48  * The generator for the web-app deploy
49  */

50 public class WebAppExpandDeployGenerator extends ExpandDeployGenerator<WebAppController>
51   implements EnvironmentListener {
52   private static final Logger JavaDoc log = Log.open(WebAppExpandDeployGenerator.class);
53
54   private final WebAppExpandDeployGeneratorAdmin _admin;
55
56   private WebAppContainer _container;
57
58   private WebAppController _parent;
59
60   private String JavaDoc _urlPrefix = "";
61
62   private ArrayList JavaDoc<WebAppConfig> _webAppDefaults
63     = new ArrayList JavaDoc<WebAppConfig>();
64
65   private HashMap JavaDoc<Path,WebAppConfig> _webAppConfigMap
66     = new HashMap JavaDoc<Path,WebAppConfig>();
67
68   // Maps from the context-path to the webapps directory
69
private HashMap JavaDoc<String JavaDoc,Path> _contextPathMap
70     = new HashMap JavaDoc<String JavaDoc,Path>();
71
72   private ClassLoader JavaDoc _parentLoader;
73
74   /**
75    * Creates the new expand deploy.
76    */

77   public WebAppExpandDeployGenerator(DeployContainer<WebAppController> container,
78                                      WebAppContainer webAppContainer)
79   {
80     super(container, webAppContainer.getRootDirectory());
81
82     _container = webAppContainer;
83
84     _parentLoader = webAppContainer.getClassLoader();
85
86     try {
87       setExtension(".war");
88     } catch (Exception JavaDoc e) {
89       log.log(Level.WARNING, e.toString(), e);
90     }
91
92     _admin = new WebAppExpandDeployGeneratorAdmin(this);
93   }
94
95   /**
96    * Gets the webApp container.
97    */

98   public WebAppContainer getContainer()
99   {
100     return _container;
101   }
102
103   /**
104    * Sets the parent webApp.
105    */

106   public void setParent(WebAppController parent)
107   {
108     _parent = parent;
109   }
110
111   /**
112    * Sets the parent loader.
113    */

114   public void setParentClassLoader(ClassLoader JavaDoc loader)
115   {
116     _parentLoader = loader;
117   }
118
119   /**
120    * Sets the url prefix.
121    */

122   public void setURLPrefix(String JavaDoc prefix)
123   {
124     if (prefix.equals("")) {
125     }
126
127     while (prefix.endsWith("/"))
128       prefix = prefix.substring(0, prefix.length() - 1);
129
130     _urlPrefix = prefix;
131
132   }
133
134   /**
135    * Gets the url prefix.
136    */

137   public String JavaDoc getURLPrefix()
138   {
139     return _urlPrefix;
140   }
141
142   /**
143    * Sets true for a lazy-init.
144    */

145   public void setLazyInit(boolean lazyInit)
146     throws ConfigException
147   {
148     log.config("lazy-init is deprecated. Use <startup>lazy</startup> instead.");
149     if (lazyInit)
150       setStartupMode("lazy");
151     else
152       setStartupMode("automatic");
153   }
154
155   /**
156    * Adds an overriding web-app
157    */

158   public void addWebApp(WebAppConfig config)
159   {
160     String JavaDoc docDir = config.getDocumentDirectory();
161
162     Path appDir = getExpandDirectory().lookup(docDir);
163
164     _webAppConfigMap.put(appDir, config);
165
166     if (config.getContextPath() != null)
167       _contextPathMap.put(config.getContextPath(), appDir);
168   }
169
170   /**
171    * Adds a default.
172    */

173   public void addWebAppDefault(WebAppConfig config)
174   {
175     _webAppDefaults.add(config);
176   }
177
178   @Override JavaDoc
179   protected void initImpl()
180   {
181     super.initImpl();
182   }
183
184   /**
185    * Returns the log.
186    */

187   @Override JavaDoc
188   protected Logger JavaDoc getLog()
189   {
190     return log;
191   }
192
193   /**
194    * Returns the deployed keys.
195    */

196   @Override JavaDoc
197   protected void fillDeployedKeys(Set JavaDoc<String JavaDoc> keys)
198   {
199     super.fillDeployedKeys(keys);
200
201     for (WebAppConfig cfg : _webAppConfigMap.values()) {
202       if (cfg.getContextPath() != null)
203         keys.add(cfg.getContextPath());
204     }
205   }
206
207   /**
208    * Start the deploy.
209    */

210   @Override JavaDoc
211   protected void startImpl()
212   {
213     super.startImpl();
214
215     Environment.addEnvironmentListener(this, _parentLoader);
216
217     _admin.register();
218   }
219
220   /**
221    * Returns the new controller.
222    */

223   @Override JavaDoc
224   protected WebAppController createController(String JavaDoc name)
225   {
226     if (! name.startsWith(_urlPrefix))
227       return null;
228
229     String JavaDoc segmentName = name.substring(_urlPrefix.length());
230
231     Path webAppRoot = _contextPathMap.get(segmentName);
232
233     if (webAppRoot != null)
234       segmentName = "/" + webAppRoot.getTail();
235     else if (segmentName.indexOf('/', 1) > 0)
236       return null;
237
238     if (segmentName.equals("")) {
239       if (CaseInsensitive.isCaseInsensitive())
240         segmentName = "/root";
241       else
242         segmentName = "/ROOT";
243     }
244
245     String JavaDoc expandName = getExpandName(segmentName.substring(1));
246
247     String JavaDoc archiveName = segmentName + ".war";
248     Path jarPath = getArchiveDirectory().lookup("." + archiveName);
249
250     Path rootDirectory;
251
252     if (jarPath.isDirectory()) {
253       rootDirectory = getExpandDirectory().lookup("." + archiveName);
254       jarPath = null;
255     }
256     else {
257       // server/003j
258
rootDirectory = getExpandDirectory().lookup("./" + expandName);
259     }
260
261     if (! rootDirectory.isDirectory() &&
262         (jarPath == null || ! jarPath.isFile()))
263       return null;
264     else if (rootDirectory.isDirectory() &&
265              ! isValidDirectory(rootDirectory, segmentName.substring(1)))
266       return null;
267
268     WebAppConfig cfg = _webAppConfigMap.get(rootDirectory);
269
270     if (cfg != null && cfg.getContextPath() != null)
271       name = cfg.getContextPath();
272
273     WebAppController controller
274       = new WebAppController(name, rootDirectory, _container);
275
276     controller.setWarName(segmentName.substring(1));
277
278     controller.setParentWebApp(_parent);
279
280     controller.setDynamicDeploy(true);
281     controller.setSourceType("expand");
282
283     return controller;
284   }
285
286
287   /**
288    * Returns the current array of webApp entries.
289    */

290   @Override JavaDoc
291   protected WebAppController mergeController(WebAppController controller,
292                                              String JavaDoc key)
293   {
294     try {
295       Path expandDirectory = getExpandDirectory();
296       Path rootDirectory = controller.getRootDirectory();
297
298       if (! expandDirectory.equals(rootDirectory.getParent()))
299         return controller;
300
301       controller = super.mergeController(controller, key);
302
303       if (controller.getArchivePath() == null) {
304         String JavaDoc archiveName = rootDirectory.getTail() + ".war";
305
306         Path jarPath = getArchiveDirectory().lookup(archiveName);
307
308         if (! jarPath.isDirectory()) {
309           controller.setArchivePath(jarPath);
310           controller.addDepend(jarPath);
311         }
312       }
313
314       controller.setStartupMode(getStartupMode());
315       // controller.setRedeployMode(getRedeployMode());
316

317       for (int i = 0; i < _webAppDefaults.size(); i++)
318         controller.addConfigDefault(_webAppDefaults.get(i));
319
320       WebAppConfig cfg = _webAppConfigMap.get(rootDirectory);
321
322       if (cfg != null)
323         controller.addConfigDefault(cfg);
324     } catch (ConfigException e) {
325       controller.setConfigException(e);
326
327       log.log(Level.FINER, e.toString(), e);
328       log.warning(e.toString());
329     } catch (Throwable JavaDoc e) {
330       controller.setConfigException(e);
331
332       log.log(Level.WARNING, e.toString(), e);
333     }
334
335     return controller;
336   }
337
338   /**
339    * Converts the name.
340    */

341   @Override JavaDoc
342   protected String JavaDoc pathNameToEntryName(String JavaDoc name)
343   {
344     String JavaDoc entryName = super.pathNameToEntryName(name);
345
346     if (entryName == null)
347       return null;
348
349     if (CaseInsensitive.isCaseInsensitive()) {
350       try {
351         String JavaDoc []list = getExpandDirectory().list();
352
353         String JavaDoc matchName = null;
354
355         for (int i = 0; i < list.length; i++) {
356           if (list[i].equalsIgnoreCase(entryName))
357             matchName = list[i];
358         }
359
360         if (matchName == null)
361           matchName = entryName.toLowerCase();
362       } catch (Exception JavaDoc e) {
363         entryName = entryName.toLowerCase();
364       }
365     }
366
367     if (entryName.equalsIgnoreCase("root"))
368       return _urlPrefix;
369     else
370       return _urlPrefix + "/" + entryName;
371   }
372
373   @Override JavaDoc
374   protected String JavaDoc entryNameToArchiveName(String JavaDoc entryName)
375   {
376     String JavaDoc prefix = _urlPrefix + "/";
377
378     if (entryName.equals(_urlPrefix))
379       return "ROOT" + getExtension();
380     else if (entryName.startsWith(prefix))
381       return entryName.substring(prefix.length()) + getExtension();
382     else
383       return null;
384   }
385
386   /**
387    * Destroy the deployment.
388    */

389   @Override JavaDoc
390   protected void destroyImpl()
391   {
392     _admin.unregister();
393
394     _container.removeWebAppDeploy(this);
395
396     Environment.removeEnvironmentListener(this, _parentLoader);
397
398     super.destroyImpl();
399   }
400 }
401
Popular Tags