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.Collections;
22  import java.util.HashSet;
23  import java.util.Set;
24  import java.util.concurrent.TimeUnit;
25  
26  import javax.servlet.ServletContainerInitializer;
27  
28  import org.eclipse.jetty.util.ConcurrentHashSet;
29  import org.eclipse.jetty.util.Loader;
30  import org.eclipse.jetty.util.log.Log;
31  import org.eclipse.jetty.util.log.Logger;
32  import org.eclipse.jetty.webapp.WebAppContext;
33  
34  public class ContainerInitializer
35  {
36      private static final Logger LOG = Log.getLogger(ContainerInitializer.class);
37      
38      final protected ServletContainerInitializer _target;
39      final protected Class[] _interestedTypes;
40      protected Set<String> _applicableTypeNames = new ConcurrentHashSet<String>();
41      protected Set<String> _annotatedTypeNames = new ConcurrentHashSet<String>();
42  
43  
44      public ContainerInitializer (ServletContainerInitializer target, Class[] classes)
45      {
46          _target = target;
47          _interestedTypes = classes;
48      }
49  
50      public ServletContainerInitializer getTarget ()
51      {
52          return _target;
53      }
54  
55      public Class[] getInterestedTypes ()
56      {
57          return _interestedTypes;
58      }
59  
60  
61      /**
62       * A class has been found that has an annotation of interest
63       * to this initializer.
64       * @param className
65       */
66      public void addAnnotatedTypeName (String className)
67      {
68          _annotatedTypeNames.add(className);
69      }
70  
71      public Set<String> getAnnotatedTypeNames ()
72      {
73          return Collections.unmodifiableSet(_annotatedTypeNames);
74      }
75  
76      public void addApplicableTypeName (String className)
77      {
78          _applicableTypeNames.add(className);
79      }
80  
81      public Set<String> getApplicableTypeNames ()
82      {
83          return Collections.unmodifiableSet(_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                  for (String s : _applicableTypeNames)
100                     classes.add(Loader.loadClass(context.getClass(), s));
101 
102                 context.getServletContext().setExtendedListenerTypes(true);
103                 if (LOG.isDebugEnabled())
104                 {
105                     long start = System.nanoTime();
106                     _target.onStartup(classes, context.getServletContext());
107                     LOG.debug("ContainerInitializer {} called in {}ms", _target.getClass().getName(), TimeUnit.MILLISECONDS.convert(System.nanoTime()-start, TimeUnit.NANOSECONDS));
108                 }
109                 else
110                     _target.onStartup(classes, context.getServletContext());
111             }
112             finally
113             { 
114                 context.getServletContext().setExtendedListenerTypes(false);
115                 Thread.currentThread().setContextClassLoader(oldLoader);
116             }
117         }
118     }
119 }