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.plus.annotation;
20  
21  import java.util.HashSet;
22  import java.util.Set;
23  
24  import javax.servlet.ServletContainerInitializer;
25  import javax.servlet.ServletContext;
26  
27  import org.eclipse.jetty.util.Loader;
28  import org.eclipse.jetty.webapp.WebAppContext;
29  
30  public class ContainerInitializer
31  {
32      protected ServletContainerInitializer _target;
33      protected Class[] _interestedTypes;
34      protected Set<String> _applicableTypeNames;
35      protected Set<String> _annotatedTypeNames;
36  
37      
38      public void setTarget (ServletContainerInitializer target)
39      {
40          _target = target;
41      }
42      
43      public ServletContainerInitializer getTarget ()
44      {
45          return _target;
46      }
47  
48      public Class[] getInterestedTypes ()
49      {
50          return _interestedTypes;
51      }
52      
53      public void setInterestedTypes (Class[] interestedTypes)
54      {
55          _interestedTypes = interestedTypes;
56      }
57      
58      /**
59       * A class has been found that has an annotation of interest 
60       * to this initializer.
61       * @param className
62       */
63      public void addAnnotatedTypeName (String className)
64      {
65          if (_annotatedTypeNames == null)
66              _annotatedTypeNames = new HashSet<String>();
67          _annotatedTypeNames.add(className);
68      }
69      
70      public Set<String> getAnnotatedTypeNames ()
71      {
72          return _annotatedTypeNames;
73      }
74      
75      public void addApplicableTypeName (String className)
76      {
77          if (_applicableTypeNames == null)
78              _applicableTypeNames = new HashSet<String>();
79          _applicableTypeNames.add(className);
80      }
81      
82      public Set<String> getApplicableTypeNames ()
83      {
84          return _applicableTypeNames;
85      }
86      
87      
88      public void callStartup(WebAppContext context)
89      throws Exception
90      {
91          if (_target != null)
92          {
93              Set<Class<?>> classes = new HashSet<Class<?>>();
94            
95              ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
96              Thread.currentThread().setContextClassLoader(context.getClassLoader());
97  
98              try
99              {
100                 if (_applicableTypeNames != null)
101                 {
102                     for (String s : _applicableTypeNames)
103                         classes.add(Loader.loadClass(context.getClass(), s));
104                 }
105 
106                 _target.onStartup(classes, context.getServletContext());
107             }
108             finally
109             {
110                 Thread.currentThread().setContextClassLoader(oldLoader);
111             }
112         }
113     }
114 }