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