View Javadoc

1   //========================================================================
2   //$Id: WebAppDeployer.java 20437 2010-12-02 11:58:20Z dirk.olmes $
3   //Copyright 2006 Mort Bay Consulting Pty. Ltd.
4   //------------------------------------------------------------------------
5   //Licensed under the Apache License, Version 2.0 (the "License");
6   //you may not use this file except in compliance with the License.
7   //You may obtain a copy of the License at 
8   //http://www.apache.org/licenses/LICENSE-2.0
9   //Unless required by applicable law or agreed to in writing, software
10  //distributed under the License is distributed on an "AS IS" BASIS,
11  //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  //See the License for the specific language governing permissions and
13  //limitations under the License.
14  //========================================================================
15  
16  package org.mule.transport.servlet.jetty;
17  
18  import java.util.ArrayList;
19  
20  import org.mortbay.component.AbstractLifeCycle;
21  import org.mortbay.jetty.Handler;
22  import org.mortbay.jetty.HandlerContainer;
23  import org.mortbay.jetty.handler.ContextHandler;
24  import org.mortbay.jetty.handler.ContextHandlerCollection;
25  import org.mortbay.jetty.webapp.WebAppContext;
26  import org.mortbay.log.Log;
27  import org.mortbay.resource.Resource;
28  import org.mortbay.util.URIUtil;
29  
30  /**
31   * A repackaged version of the Jetty WebAppDeployer which makes it possible to 
32   * override the server classes of the WebAppContext;
33   */
34  public class WebAppDeployer extends AbstractLifeCycle
35  {
36      private HandlerContainer _contexts;
37      private String _webAppDir;
38      private String _defaultsDescriptor;
39      private String[] _configurationClasses;
40      private boolean _extract;
41      private boolean _parentLoaderPriority;
42      private boolean _allowDuplicates;
43      private ArrayList _deployed;
44      private String[] serverClasses;
45      private String[] systemClasses;
46      
47      public String[] getConfigurationClasses()
48      {
49          return _configurationClasses;
50      }
51  
52      public void setConfigurationClasses(String[] configurationClasses)
53      {
54          _configurationClasses=configurationClasses;
55      }
56  
57      public HandlerContainer getContexts()
58      {
59          return _contexts;
60      }
61  
62      public void setContexts(HandlerContainer contexts)
63      {
64          _contexts=contexts;
65      }
66  
67      public String getDefaultsDescriptor()
68      {
69          return _defaultsDescriptor;
70      }
71  
72      public void setDefaultsDescriptor(String defaultsDescriptor)
73      {
74          _defaultsDescriptor=defaultsDescriptor;
75      }
76  
77      public boolean isExtract()
78      {
79          return _extract;
80      }
81  
82      public void setExtract(boolean extract)
83      {
84          _extract=extract;
85      }
86  
87      public boolean isParentLoaderPriority()
88      {
89          return _parentLoaderPriority;
90      }
91  
92      public void setParentLoaderPriority(boolean parentPriorityClassLoading)
93      {
94          _parentLoaderPriority=parentPriorityClassLoading;
95      }
96  
97      public String getWebAppDir()
98      {
99          return _webAppDir;
100     }
101 
102     public void setWebAppDir(String dir)
103     {
104         _webAppDir=dir;
105     }
106 
107     public boolean getAllowDuplicates()
108     {
109         return _allowDuplicates;
110     }
111 
112     /* ------------------------------------------------------------ */
113     /**
114      * @param allowDuplicates If false, do not deploy webapps that have already been deployed or duplicate context path
115      */
116     public void setAllowDuplicates(boolean allowDuplicates)
117     {
118         _allowDuplicates=allowDuplicates;
119     }
120 
121     public String[] getServerClasses()
122     {
123         return serverClasses;
124     }
125 
126     public void setServerClasses(String[] serverClasses)
127     {
128         this.serverClasses = serverClasses;
129     }
130 
131     public String[] getSystemClasses()
132     {
133         return systemClasses;
134     }
135 
136     public void setSystemClasses(String[] systemClasses)
137     {
138         this.systemClasses = systemClasses;
139     }
140 
141     /* ------------------------------------------------------------ */
142     /**
143      * @throws Exception 
144      */
145     public void doStart() throws Exception
146     {
147         _deployed=new ArrayList();
148         
149         scan();
150         
151     }
152     
153     /* ------------------------------------------------------------ */
154     /** Scan for webapplications.
155      * 
156      * @throws Exception
157      */
158     public void scan() throws Exception
159     {
160         if (_contexts==null)
161             throw new IllegalArgumentException("No HandlerContainer");
162 
163         Resource r=Resource.newResource(_webAppDir);
164         if (!r.exists())
165             throw new IllegalArgumentException("No such webapps resource "+r);
166 
167         if (!r.isDirectory())
168             throw new IllegalArgumentException("Not directory webapps resource "+r);
169 
170         String[] files=r.list();
171 
172         files: for (int f=0; files!=null&&f<files.length; f++)
173         {
174             String context=files[f];
175 
176             if (context.equalsIgnoreCase("CVS/")||context.equalsIgnoreCase("CVS")||context.startsWith("."))
177                 continue;
178 
179             Resource app=r.addPath(r.encode(context));
180 
181             if (context.toLowerCase().endsWith(".war")||context.toLowerCase().endsWith(".jar"))
182             {
183                 context=context.substring(0,context.length()-4);
184                 Resource unpacked=r.addPath(context);
185                 if (unpacked!=null&&unpacked.exists()&&unpacked.isDirectory())
186                     continue;
187             }
188             else if (!app.isDirectory())
189                 continue;
190 
191             if (context.equalsIgnoreCase("root")||context.equalsIgnoreCase("root/"))
192                 context=URIUtil.SLASH;
193             else
194                 context="/"+context;
195             if (context.endsWith("/")&&context.length()>0)
196                 context=context.substring(0,context.length()-1);
197 
198             // Check the context path has not already been added or the webapp itself is not already deployed
199             if (!_allowDuplicates)
200             {
201                 Handler[] installed=_contexts.getChildHandlersByClass(ContextHandler.class);
202                 for (int i=0; i<installed.length; i++)
203                 {
204                     ContextHandler c=(ContextHandler)installed[i];
205         
206                     if (context.equals(c.getContextPath()))
207                         continue files;
208 
209                     try
210                     {
211                         String path=null;
212                         if (c instanceof WebAppContext)
213                             path = Resource.newResource(((WebAppContext)c).getWar()).getFile().getAbsolutePath();
214                         else if (c.getBaseResource()!=null)
215                             path = c.getBaseResource().getFile().getAbsolutePath();
216 
217                         if (path!=null && path.equals(app.getFile().getAbsolutePath()))
218                             continue files;
219                     }
220                     catch (Exception e)
221                     {
222                         Log.ignore(e);
223                     }
224                 }
225             }
226 
227             // create a webapp
228             WebAppContext wah=null;
229             if (_contexts instanceof ContextHandlerCollection && 
230                 WebAppContext.class.isAssignableFrom(((ContextHandlerCollection)_contexts).getContextClass()))
231             {
232                 try
233                 {
234                     wah=(WebAppContext)((ContextHandlerCollection)_contexts).getContextClass().newInstance();
235                 }
236                 catch (Exception e)
237                 {
238                     throw new Error(e);
239                 }
240             }
241             else
242             {
243                 wah=new WebAppContext();
244             }
245             
246             // configure it
247             wah.setContextPath(context);
248             if (_configurationClasses!=null)
249                 wah.setConfigurationClasses(_configurationClasses);
250             if (_defaultsDescriptor!=null)
251                 wah.setDefaultsDescriptor(_defaultsDescriptor);
252             wah.setExtractWAR(_extract);
253             wah.setWar(app.toString());
254             wah.setParentLoaderPriority(_parentLoaderPriority);
255 
256             if (serverClasses != null)
257             {
258                 wah.setServerClasses(serverClasses);
259             }
260 
261             if (systemClasses != null)
262             {
263                 wah.setSystemClasses(systemClasses);
264             }
265             
266             // add it
267             _contexts.addHandler(wah);
268             _deployed.add(wah);
269             
270             if (_contexts.isStarted())
271                 _contexts.start();  // TODO Multi exception
272         }
273     }
274     
275     public void doStop() throws Exception
276     {
277         for (int i=_deployed.size();i-->0;)
278         {
279             ContextHandler wac = (ContextHandler)_deployed.get(i);
280             wac.stop();// TODO Multi exception
281         }
282     }
283 }