View Javadoc

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