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