View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 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  
23  import org.eclipse.jetty.annotations.AbstractDiscoverableAnnotationHandler;
24  import org.eclipse.jetty.annotations.AnnotationConfiguration;
25  import org.eclipse.jetty.annotations.AnnotationParser;
26  import org.eclipse.jetty.annotations.AnnotationParser.DiscoverableAnnotationHandler;
27  import org.eclipse.jetty.annotations.ClassNameResolver;
28  import org.eclipse.jetty.util.log.Log;
29  import org.eclipse.jetty.util.log.Logger;
30  import org.eclipse.jetty.util.resource.Resource;
31  import org.eclipse.jetty.webapp.MetaData;
32  import org.eclipse.jetty.webapp.WebAppContext;
33  
34  public class MavenAnnotationConfiguration extends AnnotationConfiguration
35  {
36      private static final Logger LOG = Log.getLogger(MavenAnnotationConfiguration.class);
37  
38      /* ------------------------------------------------------------ */
39      @Override
40      public void parseWebInfClasses(final WebAppContext context, final AnnotationParser parser) throws Exception
41      {
42          JettyWebAppContext jwac = (JettyWebAppContext)context;
43         if (jwac.getClassPathFiles() == null || jwac.getClassPathFiles().size() == 0)
44              super.parseWebInfClasses (context, parser);
45          else
46          {
47              LOG.debug("Scanning classes ");
48              //Look for directories on the classpath and process each one of those
49              
50              MetaData metaData = context.getMetaData();
51              if (metaData == null)
52                 throw new IllegalStateException ("No metadata");
53  
54              parser.clearHandlers();
55              for (DiscoverableAnnotationHandler h:_discoverableAnnotationHandlers)
56              {
57                  if (h instanceof AbstractDiscoverableAnnotationHandler)
58                      ((AbstractDiscoverableAnnotationHandler)h).setResource(null); //
59              }
60              parser.registerHandlers(_discoverableAnnotationHandlers);
61              parser.registerHandler(_classInheritanceHandler);
62              parser.registerHandlers(_containerInitializerAnnotationHandlers);
63  
64  
65              for (File f:jwac.getClassPathFiles())
66              {
67                  //scan the equivalent of the WEB-INF/classes directory that has been synthesised by the plugin
68                  if (f.isDirectory() && f.exists())
69                  {
70                      doParse(context, parser, Resource.newResource(f.toURL()));
71                  }
72              }
73  
74              //if an actual WEB-INF/classes directory also exists (eg because of overlayed wars) then scan that
75              //too
76              if (context.getWebInf() != null && context.getWebInf().exists())
77              {
78                  Resource classesDir = context.getWebInf().addPath("classes/");
79                  if (classesDir.exists())
80                  {
81                      doParse(context, parser, classesDir);
82                  }
83              }
84          }
85      }
86      
87      
88      public void doParse (final WebAppContext context, final AnnotationParser parser, Resource resource)
89      throws Exception
90      { 
91              parser.parse(resource, new ClassNameResolver()
92              {
93                  public boolean isExcluded (String name)
94                  {
95                      if (context.isSystemClass(name)) return true;
96                      if (context.isServerClass(name)) return false;
97                      return false;
98                  }
99  
100                 public boolean shouldOverride (String name)
101                 {
102                     //looking at webapp classpath, found already-parsed class of same name - did it come from system or duplicate in webapp?
103                     if (context.isParentLoaderPriority())
104                         return false;
105                     return true;
106                 }
107             });            
108     }
109 }