View Javadoc

1   /*
2    * Copyright (c) 2005 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the LGPL licence
5    * which accompanies this distribution, and is available at
6    * http://www.gnu.org/copyleft/lesser.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
11   *
12   */
13  package org.abstracthorizon.spring.server.deployment.jetty;
14  
15  import java.net.MalformedURLException;
16  import java.net.URI;
17  import java.net.URISyntaxException;
18  import java.net.URL;
19  
20  import org.abstracthorizon.extend.server.deployment.ModuleId;
21  import org.abstracthorizon.extend.server.support.URLUtils;
22  import org.abstracthorizon.extend.support.spring.deployment.AbstractApplicationContextModule;
23  import org.abstracthorizon.extend.support.spring.service.ServiceModuleLoader;
24  import org.springframework.beans.BeansException;
25  import org.springframework.context.ApplicationContext;
26  import org.springframework.context.ConfigurableApplicationContext;
27  
28  /**
29   * <p>
30   * Jetty war archive (or directory) module loader (deployer).
31   * It checks if &quot;WEB-INF&quot; directory is present and if so
32   * deploys archive with (embedded) Jetty.
33   * </p>
34   * <p>
35   * Current implementation creates context and deploys it with
36   * current host (defined under &quot;jetty.host&quot; name in
37   * jetty service archive).
38   * </p>
39   *
40   * @author Daniel Sendula
41   */
42  public class JettyWarModuleLoader extends ServiceModuleLoader {
43  
44      /**
45       * Empty constructor.
46       */
47      public JettyWarModuleLoader() {
48      }
49  
50      /**
51       * Returns <code>true</code> if &quot;WEB-INF&quot; exists in specified directory.
52       * @param uri URI
53       * @return <code>true</code> if &quot;WEB-INF&quot; exists in specified directory.
54       */
55      public boolean canLoad(URI uri) {
56          if (super.canLoad(uri)) {
57              if (URLUtils.isFolder(uri)) {
58                  try {
59                      URI webINFURL = URLUtils.add(uri, "WEB-INF");
60                      return URLUtils.exists(webINFURL);
61                  } catch (URISyntaxException ignore) {
62                  }
63              } else {
64                  // TODO it's archive - so it must be OK. Right?
65                  return true;
66              }
67          }
68          return false;
69      }
70  
71      /**
72       * Creates new module {@link JettyWebApplicationContext} module
73       * @param url url for the module
74       * @return newly created module
75       */
76      protected AbstractApplicationContextModule createModule(URL url) {
77          return new JettyWebApplicationContext(ModuleId.createModuleIdFromFileName(url.getPath()));
78      }
79  
80      /**
81       * Called before module is pronounced defined
82       * @param service service
83       */
84      protected void preDefinitionProcessing(AbstractApplicationContextModule service) {
85          JettyWebApplicationContext jettyContext = (JettyWebApplicationContext)service;
86          jettyContext.setWebServerContext(root);
87          jettyContext.setParent(root.getParent());
88      }
89  
90      /**
91       * Sets application context
92       * @param applicationContext application context this bean is created under
93       */
94      public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
95          this.root = (ConfigurableApplicationContext)applicationContext;
96      }
97  
98      /**
99       * Hook method that collects all important files from newly created module and
100      * adds then to the URL redeployment scanner.
101      * @param module module
102      */
103     protected void addFilesForScanning(AbstractApplicationContextModule module) {
104         super.addFilesForScanning(module);
105         if (redeployURLScanner != null) {
106             // TODO should this be set in case of working location only?!
107 
108             URL locationURL = module.getOriginalLocation();
109             if (locationURL.equals(module.getWorkingLocation())) {
110                 URL serviceFileURL = module.getServiceFile();
111                 redeployURLScanner.addURL(serviceFileURL, module);
112                 try {
113                     URL url = URLUtils.add(locationURL, "WEB-INF/web.xml");
114                     if (URLUtils.exists(url)) {
115                         redeployURLScanner.addURL(url, module);
116                     }
117                 } catch (MalformedURLException ignore) {
118                 }
119             }
120         }
121     }
122 }