View Javadoc

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