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.ant;
20  
21  import java.io.File;
22  import java.net.URI;
23  import java.net.URISyntaxException;
24  import java.net.URL;
25  import java.net.URLClassLoader;
26  import java.util.List;
27  import java.util.regex.Pattern;
28  
29  import org.apache.tools.ant.AntClassLoader;
30  import org.eclipse.jetty.util.PatternMatcher;
31  import org.eclipse.jetty.util.resource.Resource;
32  import org.eclipse.jetty.webapp.WebAppClassLoader;
33  import org.eclipse.jetty.webapp.WebAppContext;
34  import org.eclipse.jetty.webapp.WebInfConfiguration;
35  import org.eclipse.jetty.webapp.WebXmlConfiguration;
36  
37  public class AntWebInfConfiguration extends WebInfConfiguration
38  {
39  
40      
41      @Override
42      public void preConfigure(final WebAppContext context) throws Exception
43      {        
44          //Make a temp directory for the webapp if one is not already set
45          resolveTempDirectory(context);
46          
47          //Extract webapp if necessary
48          unpack (context);
49  
50          
51          //Apply an initial ordering to the jars which governs which will be scanned for META-INF
52          //info and annotations. The ordering is based on inclusion patterns.       
53          String tmp = (String)context.getAttribute(WEBINF_JAR_PATTERN);
54          Pattern webInfPattern = (tmp==null?null:Pattern.compile(tmp));
55          tmp = (String)context.getAttribute(CONTAINER_JAR_PATTERN);
56          Pattern containerPattern = (tmp==null?null:Pattern.compile(tmp));
57  
58          //Apply ordering to container jars - if no pattern is specified, we won't
59          //match any of the container jars
60          PatternMatcher containerJarNameMatcher = new PatternMatcher ()
61          {
62              public void matched(URI uri) throws Exception
63              {
64                  context.getMetaData().addContainerResource(Resource.newResource(uri));
65              }      
66          };
67          ClassLoader loader = context.getClassLoader();
68          if (loader != null)
69          {
70              loader = loader.getParent();
71              if (loader != null)
72              {
73                  URI[] containerUris = null; 
74             
75                  if (loader instanceof URLClassLoader)
76                  {
77                      URL[] urls = ((URLClassLoader)loader).getURLs();
78                      if (urls != null)
79                      {
80                          containerUris = new URI[urls.length];
81                          int i=0;
82                          for (URL u : urls)
83                          {
84                              try 
85                              {
86                                  containerUris[i] = u.toURI();
87                              }
88                              catch (URISyntaxException e)
89                              {
90                                  containerUris[i] = new URI(u.toString().replaceAll(" ", "%20"));
91                              }  
92                              i++;
93                          }
94                      }
95                  }
96                  else if (loader instanceof AntClassLoader)
97                  {
98                      AntClassLoader antLoader = (AntClassLoader)loader;     
99                      String[] paths = antLoader.getClasspath().split(new String(new char[]{File.pathSeparatorChar}));
100                     if (paths != null)
101                     {
102                         containerUris = new URI[paths.length];
103                         int i=0;
104                         for (String p:paths)
105                         {
106                             File f = new File(p);
107                             containerUris[i] = f.toURI();
108                             i++;
109                         }
110                     }
111                 }
112 
113                 containerJarNameMatcher.match(containerPattern, containerUris, false);
114             }
115         }
116         
117         //Apply ordering to WEB-INF/lib jars
118         PatternMatcher webInfJarNameMatcher = new PatternMatcher ()
119         {
120             @Override
121             public void matched(URI uri) throws Exception
122             {
123                 context.getMetaData().addWebInfJar(Resource.newResource(uri));
124             }      
125         };
126         List<Resource> jars = findJars(context);
127        
128         //Convert to uris for matching
129         URI[] uris = null;
130         if (jars != null)
131         {
132             uris = new URI[jars.size()];
133             int i=0;
134             for (Resource r: jars)
135             {
136                 uris[i++] = r.getURI();
137             }
138         }
139         webInfJarNameMatcher.match(webInfPattern, uris, true); //null is inclusive, no pattern == all jars match 
140         
141         //No pattern to appy to classes, just add to metadata
142         context.getMetaData().setWebInfClassesDirs(findClassDirs(context));
143     }
144     
145 
146     /**
147      * Adds classpath files into web application classloader, and
148      * sets web.xml and base directory for the configured web application.
149      *
150      * @see WebXmlConfiguration#configure(WebAppContext)
151      */
152     public void configure(WebAppContext context) throws Exception
153     {
154         if (context instanceof AntWebAppContext)
155         {
156             List<File> classPathFiles = ((AntWebAppContext)context).getClassPathFiles();
157             if (classPathFiles != null)
158             {
159                 for (File cpFile:classPathFiles)
160                 {
161                     if (cpFile.exists())
162                     {
163                         ((WebAppClassLoader) context.getClassLoader()).addClassPath(cpFile.getCanonicalPath());
164                     }
165                 }
166             }
167         }
168         super.configure(context);
169     }
170 }