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.webapp;
20  
21  import org.eclipse.jetty.util.Loader;
22  import org.eclipse.jetty.util.log.Log;
23  import org.eclipse.jetty.util.log.Logger;
24  import org.eclipse.jetty.util.resource.Resource;
25  
26  /**
27   * DiscoveredAnnotation
28   *
29   * Represents an annotation that has been discovered
30   * by scanning source code of WEB-INF/classes and WEB-INF/lib jars.
31   * 
32   */
33  public abstract class DiscoveredAnnotation
34  {
35      private static final Logger LOG = Log.getLogger(DiscoveredAnnotation.class);
36  
37      protected WebAppContext _context;
38      protected String _className;
39      protected Class<?> _clazz;
40      protected Resource _resource; //resource it was discovered on, can be null (eg from WEB-INF/classes)
41      
42      public abstract void apply();
43      
44      public DiscoveredAnnotation (WebAppContext context, String className)
45      {
46          this(context,className, null);
47      } 
48      
49      
50      public DiscoveredAnnotation(WebAppContext context, String className, Resource resource)
51      {
52          _context = context;
53          _className = className;
54          _resource = resource;
55      }
56      
57      public Resource getResource ()
58      {
59          return _resource;
60      }
61      
62      public Class<?> getTargetClass()
63      {
64          if (_clazz != null)
65              return _clazz;
66          
67          loadClass();
68          
69          return _clazz;
70      }
71      
72      private void loadClass ()
73      {
74          if (_clazz != null)
75              return;
76          
77          if (_className == null)
78              return;
79          
80          try
81          {
82              _clazz = Loader.loadClass(null, _className);
83          }
84          catch (Exception e)
85          {
86              LOG.warn(e);
87          }
88      }  
89  }