View Javadoc

1   // ========================================================================
2   // Copyright (c) 2006-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.deploy;
15  
16  import java.util.ArrayList;
17  
18  import org.eclipse.jetty.deploy.providers.ScanningAppProvider;
19  import org.eclipse.jetty.server.Handler;
20  import org.eclipse.jetty.server.handler.ContextHandler;
21  import org.eclipse.jetty.server.handler.ContextHandlerCollection;
22  import org.eclipse.jetty.server.handler.HandlerCollection;
23  import org.eclipse.jetty.util.AttributesMap;
24  import org.eclipse.jetty.util.URIUtil;
25  import org.eclipse.jetty.util.component.AbstractLifeCycle;
26  import org.eclipse.jetty.util.log.Log;
27  import org.eclipse.jetty.util.resource.Resource;
28  import org.eclipse.jetty.webapp.WebAppContext;
29  
30  /**
31   * Legacy Web Application Deployer.
32   * 
33   * <p>
34   * Note: The WebAppDeployer is being phased out of Jetty in favor of the {@link DeploymentManager} and
35   * {@link org.eclipse.jetty.deploy.providers.WebAppProvider} implementation.
36   * 
37   * <p>
38   * The class searches a directory for and deploys standard web application. At startup, the directory specified by
39   * {@link #setWebAppDir(String)} is searched for subdirectories (excluding hidden and CVS) or files ending with ".zip"
40   * or "*.war". For each webapp discovered is passed to a new instance of {@link WebAppContext} (or a subclass specified
41   * by {@link #getContexts()}. {@link ContextHandlerCollection#getContextClass()}
42   * 
43   * <p>
44   * This deployer does not do hot deployment or undeployment. Nor does it support per web application configuration. For
45   * these features see {@link ContextDeployer}.
46   * 
47   * @deprecated
48   * @see DeploymentManager
49   * @see ScanningAppProvider
50   * @see ContextDeployer
51   */
52  @SuppressWarnings("unchecked")
53  public class WebAppDeployer extends AbstractLifeCycle
54  {
55      private HandlerCollection _contexts;
56      private String _webAppDir;
57      private String _defaultsDescriptor;
58      private String[] _configurationClasses;
59      private boolean _extract;
60      private boolean _parentLoaderPriority;
61      private boolean _allowDuplicates;
62      private ArrayList _deployed;
63      private AttributesMap _contextAttributes = new AttributesMap();
64      
65      
66      public WebAppDeployer()
67      {
68          Log.warn("WebAppDeployer is deprecated. Use WebAppProvider");
69      }
70      
71      public String[] getConfigurationClasses()
72      {
73          return _configurationClasses;
74      }
75  
76      public void setConfigurationClasses(String[] configurationClasses)
77      {
78          _configurationClasses=configurationClasses;
79      }
80  
81      public HandlerCollection getContexts()
82      {
83          return _contexts;
84      }
85  
86      public void setContexts(HandlerCollection contexts)
87      {
88          _contexts=contexts;
89      }
90  
91      public String getDefaultsDescriptor()
92      {
93          return _defaultsDescriptor;
94      }
95  
96      public void setDefaultsDescriptor(String defaultsDescriptor)
97      {
98          _defaultsDescriptor=defaultsDescriptor;
99      }
100 
101     public boolean isExtract()
102     {
103         return _extract;
104     }
105 
106     public void setExtract(boolean extract)
107     {
108         _extract=extract;
109     }
110 
111     public boolean isParentLoaderPriority()
112     {
113         return _parentLoaderPriority;
114     }
115 
116     public void setParentLoaderPriority(boolean parentPriorityClassLoading)
117     {
118         _parentLoaderPriority=parentPriorityClassLoading;
119     }
120 
121     public String getWebAppDir()
122     {
123         return _webAppDir;
124     }
125 
126     public void setWebAppDir(String dir)
127     {
128         _webAppDir=dir;
129     }
130 
131     public boolean getAllowDuplicates()
132     {
133         return _allowDuplicates;
134     }
135 
136     /* ------------------------------------------------------------ */
137     /**
138      * @param allowDuplicates If false, do not deploy webapps that have already been deployed or duplicate context path
139      */
140     public void setAllowDuplicates(boolean allowDuplicates)
141     {
142         _allowDuplicates=allowDuplicates;
143     }
144 
145     
146     /**
147      * Set a contextAttribute that will be set for every Context deployed by this deployer.
148      * @param name
149      * @param value
150      */
151     public void setAttribute (String name, Object value)
152     {
153         _contextAttributes.setAttribute(name,value);
154     }
155     
156     
157     /**
158      * Get a contextAttribute that will be set for every Context deployed by this deployer.
159      * @param name
160      * @return the attribute value
161      */
162     public Object getAttribute (String name)
163     {
164         return _contextAttributes.getAttribute(name);
165     }
166     
167     
168     /**
169      * Remove a contextAttribute that will be set for every Context deployed by this deployer.
170      * @param name
171      */
172     public void removeAttribute(String name)
173     {
174         _contextAttributes.removeAttribute(name);
175     }
176 
177     /* ------------------------------------------------------------ */
178     /**
179      * @throws Exception 
180      */
181     @Override
182     public void doStart() throws Exception
183     {
184         _deployed=new ArrayList();
185         
186         scan();
187         
188     }
189     
190     /* ------------------------------------------------------------ */
191     /** Scan for webapplications.
192      * 
193      * @throws Exception
194      */
195     public void scan() throws Exception
196     {
197         if (_contexts==null)
198             throw new IllegalArgumentException("No HandlerContainer");
199 
200         Resource r=Resource.newResource(_webAppDir);
201         if (!r.exists())
202             throw new IllegalArgumentException("No such webapps resource "+r);
203 
204         if (!r.isDirectory())
205             throw new IllegalArgumentException("Not directory webapps resource "+r);
206 
207         String[] files=r.list();
208 
209         files: for (int f=0; files!=null&&f<files.length; f++)
210         {
211             String context=files[f];
212 
213             if (context.equalsIgnoreCase("CVS/")||context.equalsIgnoreCase("CVS")||context.startsWith("."))
214                 continue;
215 
216             Resource app=r.addPath(r.encode(context));
217 
218             if (context.toLowerCase().endsWith(".war")||context.toLowerCase().endsWith(".jar"))
219             {
220                 context=context.substring(0,context.length()-4);
221                 Resource unpacked=r.addPath(context);
222                 if (unpacked!=null&&unpacked.exists()&&unpacked.isDirectory())
223                     continue;
224             }
225             else if (!app.isDirectory())
226                 continue;
227 
228             if (context.equalsIgnoreCase("root")||context.equalsIgnoreCase("root/"))
229                 context=URIUtil.SLASH;
230             else
231                 context="/"+context;
232             if (context.endsWith("/")&&context.length()>0)
233                 context=context.substring(0,context.length()-1);
234 
235             // Check the context path has not already been added or the webapp itself is not already deployed
236             if (!_allowDuplicates)
237             {
238                 Handler[] installed=_contexts.getChildHandlersByClass(ContextHandler.class);
239                 for (int i=0; i<installed.length; i++)
240                 {
241                     ContextHandler c = (ContextHandler)installed[i];
242 
243                     if (context.equals(c.getContextPath()))
244                         continue files;
245 
246                     
247                     try
248                     {
249                         String path = null;
250                         if (c instanceof WebAppContext)
251                             path = Resource.newResource(((WebAppContext)c).getWar()).getFile().getCanonicalPath();
252                         else if (c.getBaseResource() != null)
253                             path = c.getBaseResource().getFile().getCanonicalPath();
254 
255                         if (path != null && path.equals(app.getFile().getCanonicalPath()))
256                         {
257                             Log.debug("Already deployed:"+path);
258                             continue files;
259                         }
260                     }
261                     catch (Exception e)
262                     {
263                         Log.ignore(e);
264                     }
265 
266                 }
267             }
268 
269             // create a webapp
270             WebAppContext wah=null;
271             if (_contexts instanceof ContextHandlerCollection && 
272                 WebAppContext.class.isAssignableFrom(((ContextHandlerCollection)_contexts).getContextClass()))
273             {
274                 try
275                 {
276                     wah=(WebAppContext)((ContextHandlerCollection)_contexts).getContextClass().newInstance();
277                 }
278                 catch (Exception e)
279                 {
280                     throw new Error(e);
281                 }
282             }
283             else
284             {
285                 wah=new WebAppContext();
286             }
287             
288             // configure it
289             wah.setContextPath(context);
290             if (_configurationClasses!=null)
291                 wah.setConfigurationClasses(_configurationClasses);
292             if (_defaultsDescriptor!=null)
293                 wah.setDefaultsDescriptor(_defaultsDescriptor);
294             wah.setExtractWAR(_extract);
295             wah.setWar(app.toString());
296             wah.setParentLoaderPriority(_parentLoaderPriority);
297             
298             //set up any contextAttributes
299             wah.setAttributes(new AttributesMap(_contextAttributes));
300             
301             // add it
302             _contexts.addHandler(wah);
303             _deployed.add(wah);
304             
305             if (_contexts.isStarted())
306                 wah.start();  // TODO Multi exception
307         }
308     }
309     
310     @Override
311     public void doStop() throws Exception
312     {
313         for (int i=_deployed.size();i-->0;)
314         {
315             ContextHandler wac = (ContextHandler)_deployed.get(i);
316             wac.stop();// TODO Multi exception
317         }
318     }
319 }