View Javadoc

1   // ========================================================================
2   // Copyright (c) 2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  package org.eclipse.jetty.plus.annotation;
14  
15  import java.util.HashSet;
16  import java.util.Set;
17  
18  import javax.servlet.ServletContainerInitializer;
19  import javax.servlet.ServletContext;
20  
21  import org.eclipse.jetty.util.Loader;
22  import org.eclipse.jetty.webapp.WebAppContext;
23  
24  public class ContainerInitializer
25  {
26      protected ServletContainerInitializer _target;
27      protected Class[] _interestedTypes;
28      protected Set<String> _applicableTypeNames;
29      protected Set<String> _annotatedTypeNames;
30  
31      
32      public void setTarget (ServletContainerInitializer target)
33      {
34          _target = target;
35      }
36      
37      public ServletContainerInitializer getTarget ()
38      {
39          return _target;
40      }
41  
42      public Class[] getInterestedTypes ()
43      {
44          return _interestedTypes;
45      }
46      
47      public void setInterestedTypes (Class[] interestedTypes)
48      {
49          _interestedTypes = interestedTypes;
50      }
51      
52      /**
53       * A class has been found that has an annotation of interest 
54       * to this initializer.
55       * @param className
56       */
57      public void addAnnotatedTypeName (String className)
58      {
59          if (_annotatedTypeNames == null)
60              _annotatedTypeNames = new HashSet<String>();
61          _annotatedTypeNames.add(className);
62      }
63      
64      public Set<String> getAnnotatedTypeNames ()
65      {
66          return _annotatedTypeNames;
67      }
68      
69      public void addApplicableTypeName (String className)
70      {
71          if (_applicableTypeNames == null)
72              _applicableTypeNames = new HashSet<String>();
73          _applicableTypeNames.add(className);
74      }
75      
76      public Set<String> getApplicableTypeNames ()
77      {
78          return _applicableTypeNames;
79      }
80      
81      
82      public void callStartup(WebAppContext context)
83      throws Exception
84      {
85          if (_target != null)
86          {
87              Set<Class<?>> classes = new HashSet<Class<?>>();
88            
89              ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
90              Thread.currentThread().setContextClassLoader(context.getClassLoader());
91  
92              try
93              {
94                  if (_applicableTypeNames != null)
95                  {
96                      for (String s : _applicableTypeNames)
97                          classes.add(Loader.loadClass(context.getClass(), s));
98                  }
99  
100                 _target.onStartup(classes, context.getServletContext());
101             }
102             finally
103             {
104                 Thread.currentThread().setContextClassLoader(oldLoader);
105             }
106         }
107     }
108 }