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  
14  package org.eclipse.jetty.annotations;
15  
16  import java.lang.reflect.Method;
17  import java.lang.reflect.Modifier;
18  
19  import javax.annotation.PostConstruct;
20  
21  import org.eclipse.jetty.annotations.AnnotationIntrospector.AbstractIntrospectableAnnotationHandler;
22  import org.eclipse.jetty.plus.annotation.LifeCycleCallbackCollection;
23  import org.eclipse.jetty.plus.annotation.PostConstructCallback;
24  import org.eclipse.jetty.webapp.MetaData;
25  import org.eclipse.jetty.webapp.WebAppContext;
26  
27  public class PostConstructAnnotationHandler extends AbstractIntrospectableAnnotationHandler
28  {
29      protected WebAppContext _context;
30  
31      public PostConstructAnnotationHandler (WebAppContext wac)
32      {
33          super(true);
34          _context = wac;
35      }
36  
37  
38      public void doHandle(Class clazz)
39      {  
40          //Check that the PostConstruct is on a class that we're interested in
41          if (Util.isServletType(clazz))
42          {
43              Method[] methods = clazz.getDeclaredMethods();
44              for (int i=0; i<methods.length; i++)
45              {
46                  Method m = (Method)methods[i];
47                  if (m.isAnnotationPresent(PostConstruct.class))
48                  {
49                      if (m.getParameterTypes().length != 0)
50                          throw new IllegalStateException(m+" has parameters");
51                      if (m.getReturnType() != Void.TYPE)
52                          throw new IllegalStateException(m+" is not void");
53                      if (m.getExceptionTypes().length != 0)
54                          throw new IllegalStateException(m+" throws checked exceptions");
55                      if (Modifier.isStatic(m.getModifiers()))
56                          throw new IllegalStateException(m+" is static");
57                     
58                      //ServletSpec 3.0 p80 If web.xml declares even one post-construct then all post-constructs
59                      //in fragments must be ignored. Otherwise, they are additive.
60                      MetaData metaData = _context.getMetaData();
61                      MetaData.Origin origin = metaData.getOrigin("post-construct");
62                      if (origin != null && 
63                          (origin == MetaData.Origin.WebXml ||
64                           origin == MetaData.Origin.WebDefaults || 
65                           origin == MetaData.Origin.WebOverride))
66                          return;
67                                          
68                      PostConstructCallback callback = new PostConstructCallback();
69                      callback.setTarget(clazz.getName(), m.getName());
70                      LifeCycleCallbackCollection lifecycles = (LifeCycleCallbackCollection)_context.getAttribute(LifeCycleCallbackCollection.LIFECYCLE_CALLBACK_COLLECTION);
71                      if (lifecycles == null)
72                      {
73                          lifecycles = new LifeCycleCallbackCollection();
74                          _context.setAttribute(LifeCycleCallbackCollection.LIFECYCLE_CALLBACK_COLLECTION,lifecycles);
75                      }
76                      lifecycles.add(callback);
77                  }
78              }
79          }
80      }
81  }