View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.maven.plugin;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.Iterator;
25  import java.util.List;
26  import java.util.Locale;
27  
28  
29  import org.eclipse.jetty.util.log.Log;
30  import org.eclipse.jetty.util.log.Logger;
31  import org.eclipse.jetty.util.resource.Resource;
32  import org.eclipse.jetty.util.resource.ResourceCollection;
33  import org.eclipse.jetty.webapp.WebAppClassLoader;
34  import org.eclipse.jetty.webapp.WebAppContext;
35  import org.eclipse.jetty.webapp.WebInfConfiguration;
36  
37  
38  
39  /**
40   * MavenWebInfConfiguration
41   * 
42   * WebInfConfiguration to take account of overlaid wars expressed as project dependencies and
43   * potential configured via the maven-war-plugin.
44   */
45  public class MavenWebInfConfiguration extends WebInfConfiguration
46  {
47      private static final Logger LOG = Log.getLogger(MavenWebInfConfiguration.class);
48  
49      public static final String RESOURCE_BASES_POST_OVERLAY = "org.eclipse.jetty.resource.postOverlay";
50      protected static int COUNTER = 0; 
51      protected Resource _originalResourceBase;
52      protected List<Resource>  _unpackedOverlayResources;
53      
54      /** 
55       * @see org.eclipse.jetty.webapp.WebInfConfiguration#configure(org.eclipse.jetty.webapp.WebAppContext)
56       */
57      public void configure(WebAppContext context) throws Exception
58      {
59          JettyWebAppContext jwac = (JettyWebAppContext)context;
60          
61          //put the classes dir and all dependencies into the classpath
62          if (jwac.getClassPathFiles() != null)
63          {
64              if (LOG.isDebugEnabled()) LOG.debug("Setting up classpath ...");
65              Iterator itor = jwac.getClassPathFiles().iterator();
66              while (itor.hasNext())
67                  ((WebAppClassLoader)context.getClassLoader()).addClassPath(((File)itor.next()).getCanonicalPath());
68          }
69          
70          super.configure(context);
71          
72          // knock out environmental maven and plexus classes from webAppContext
73          String[] existingServerClasses = context.getServerClasses();
74          String[] newServerClasses = new String[2+(existingServerClasses==null?0:existingServerClasses.length)];
75          newServerClasses[0] = "org.apache.maven.";
76          newServerClasses[1] = "org.codehaus.plexus.";
77          System.arraycopy( existingServerClasses, 0, newServerClasses, 2, existingServerClasses.length );
78          if (LOG.isDebugEnabled())
79          {
80              LOG.debug("Server classes:");
81              for (int i=0;i<newServerClasses.length;i++)
82                  LOG.debug(newServerClasses[i]);
83          }
84          context.setServerClasses( newServerClasses ); 
85      }
86  
87      
88      
89  
90      /** 
91       * @see org.eclipse.jetty.webapp.WebInfConfiguration#preConfigure(org.eclipse.jetty.webapp.WebAppContext)
92       */
93      public void preConfigure(WebAppContext context) throws Exception
94      {
95          super.preConfigure(context);
96  
97      }
98      
99      
100     
101     
102     /** 
103      * @see org.eclipse.jetty.webapp.AbstractConfiguration#postConfigure(org.eclipse.jetty.webapp.WebAppContext)
104      */
105     public void postConfigure(WebAppContext context) throws Exception
106     {
107         super.postConfigure(context);
108     }
109 
110 
111     
112     
113     /** 
114      * @see org.eclipse.jetty.webapp.WebInfConfiguration#deconfigure(org.eclipse.jetty.webapp.WebAppContext)
115      */
116     public void deconfigure(WebAppContext context) throws Exception
117     {   
118         super.deconfigure(context);
119         //restore whatever the base resource was before we might have included overlaid wars
120         context.setBaseResource(_originalResourceBase);
121         //undo the setting of the overlayed resources
122         context.removeAttribute(RESOURCE_BASES_POST_OVERLAY);
123     }
124 
125     
126     
127 
128     /** 
129      * @see org.eclipse.jetty.webapp.WebInfConfiguration#unpack(org.eclipse.jetty.webapp.WebAppContext)
130      */
131     @Override
132     public void unpack(WebAppContext context) throws IOException
133     {
134         //Unpack and find base resource as normal
135         super.unpack(context);
136         
137         //Get the base resource for the "virtual" webapp
138         _originalResourceBase = context.getBaseResource();
139 
140         JettyWebAppContext jwac = (JettyWebAppContext)context;
141 
142         //determine sequencing of overlays
143         _unpackedOverlayResources = new ArrayList<Resource>();
144 
145        
146 
147         if (jwac.getOverlays() != null && !jwac.getOverlays().isEmpty())
148         {
149             List<Resource> resourceBaseCollection = new ArrayList<Resource>();
150 
151             for (Overlay o:jwac.getOverlays())
152             {
153                 //can refer to the current project in list of overlays for ordering purposes
154                 if (o.getConfig() != null && o.getConfig().isCurrentProject() && _originalResourceBase.exists())
155                 {
156                     resourceBaseCollection.add(_originalResourceBase); 
157                     LOG.debug("Adding virtual project to resource base list");
158                     continue;
159                 }
160 
161                 Resource unpacked = unpackOverlay(jwac,o);
162                 _unpackedOverlayResources.add(unpacked); //remember the unpacked overlays for later so we can delete the tmp files
163                 resourceBaseCollection.add(unpacked); //add in the selectively unpacked overlay in the correct order to the webapps resource base
164                 LOG.debug("Adding "+unpacked+" to resource base list");
165             }
166 
167             if (!resourceBaseCollection.contains(_originalResourceBase) && _originalResourceBase.exists())
168             {
169                 if (jwac.getBaseAppFirst())
170                 {
171                     LOG.debug("Adding virtual project first in resource base list");
172                     resourceBaseCollection.add(0, _originalResourceBase);
173                 }
174                 else
175                 {
176                     LOG.debug("Adding virtual project last in resource base list");
177                     resourceBaseCollection.add(_originalResourceBase);
178                 }
179             }
180             jwac.setBaseResource(new ResourceCollection(resourceBaseCollection.toArray(new Resource[resourceBaseCollection.size()])));
181         }
182         
183         jwac.setAttribute(RESOURCE_BASES_POST_OVERLAY, jwac.getBaseResource());
184     }
185 
186 
187     /**
188      * Get the jars to examine from the files from which we have
189      * synthesized the classpath. Note that the classpath is not
190      * set at this point, so we cannot get them from the classpath.
191      * @param context the web app context
192      * @return the list of jars found
193      */
194     @Override
195     protected List<Resource> findJars (WebAppContext context)
196     throws Exception
197     {
198         List<Resource> list = new ArrayList<Resource>();
199         JettyWebAppContext jwac = (JettyWebAppContext)context;
200         if (jwac.getClassPathFiles() != null)
201         {
202             for (File f: jwac.getClassPathFiles())
203             {
204                 if (f.getName().toLowerCase(Locale.ENGLISH).endsWith(".jar"))
205                 {
206                     try
207                     {
208                         list.add(Resource.newResource(f.toURI()));
209                     }
210                     catch (Exception e)
211                     {
212                         LOG.warn("Bad url ", e);
213                     }
214                 }
215             }
216         }
217 
218         List<Resource> superList = super.findJars(context);
219         if (superList != null)
220             list.addAll(superList);
221         return list;
222     }
223     
224     
225     
226     
227 
228     /** 
229      * Add in the classes dirs from test/classes and target/classes
230      * @see org.eclipse.jetty.webapp.WebInfConfiguration#findClassDirs(org.eclipse.jetty.webapp.WebAppContext)
231      */
232     @Override
233     protected List<Resource> findClassDirs(WebAppContext context) throws Exception
234     {
235         List<Resource> list = new ArrayList<Resource>();
236         
237         JettyWebAppContext jwac = (JettyWebAppContext)context;
238         if (jwac.getClassPathFiles() != null)
239         {
240             for (File f: jwac.getClassPathFiles())
241             {
242                 if (f.exists() && f.isDirectory())
243                 {
244                     try
245                     {
246                         list.add(Resource.newResource(f.toURI()));
247                     }
248                     catch (Exception e)
249                     {
250                         LOG.warn("Bad url ", e);
251                     }
252                 }
253             }
254         }
255         
256         List<Resource> classesDirs = super.findClassDirs(context);
257         if (classesDirs != null)
258             list.addAll(classesDirs);
259         return list;
260     }
261 
262 
263 
264 
265 
266     protected  Resource unpackOverlay (WebAppContext context, Overlay overlay)
267     throws IOException
268     {
269         LOG.debug("Unpacking overlay: " + overlay);
270         
271         if (overlay.getResource() == null)
272             return null; //nothing to unpack
273    
274         //Get the name of the overlayed war and unpack it to a dir of the
275         //same name in the temporary directory
276         String name = overlay.getResource().getName();
277         if (name.endsWith("!/"))
278             name = name.substring(0,name.length()-2);
279         int i = name.lastIndexOf('/');
280         if (i>0)
281             name = name.substring(i+1,name.length());
282         name = name.replace('.', '_');
283         name = name+(++COUNTER); //add some digits to ensure uniqueness
284         File dir = new File(context.getTempDirectory(), name); 
285         
286         //if specified targetPath, unpack to that subdir instead
287         File unpackDir = dir;
288         if (overlay.getConfig() != null && overlay.getConfig().getTargetPath() != null)
289             unpackDir = new File (dir, overlay.getConfig().getTargetPath());
290         
291         overlay.getResource().copyTo(unpackDir);
292         //use top level of unpacked content
293         Resource unpackedOverlay = Resource.newResource(dir.getCanonicalPath());
294         
295         LOG.debug("Unpacked overlay: "+overlay+" to "+unpackedOverlay);
296         return  unpackedOverlay;
297     }
298 }