View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.config.annotation;
20  
21  import java.lang.reflect.InvocationTargetException;
22  import java.lang.reflect.Method;
23  import java.lang.reflect.Modifier;
24  
25  import javax.annotation.PostConstruct;
26  import javax.annotation.PreDestroy;
27  import javax.naming.NamingException;
28  
29  import org.apache.myfaces.shared_impl.util.ClassUtils;
30  
31  /**
32   * See SRV.14.5 Servlet Specification Version 2.5 JSR 154
33   * and Common Annotations for the Java Platform JSR 250
34  
35   */
36  
37  public class NoInjectionAnnotationLifecycleProvider implements LifecycleProvider2
38  {
39  
40  
41      public Object newInstance(String className)
42             throws InstantiationException, IllegalAccessException, NamingException, InvocationTargetException, ClassNotFoundException
43      {
44          Class clazz = ClassUtils.classForName(className);
45          Object object = clazz.newInstance();
46          processAnnotations(object);
47          //postConstruct(object);
48          return object;
49      }
50  
51      /**
52       * Call postConstruct method on the specified instance.
53       */
54      public void postConstruct(Object instance)
55              throws IllegalAccessException, InvocationTargetException
56      {
57  
58          // TODO the servlet spec is not clear about searching in superclass??
59  
60          Method[] methods = instance.getClass().getDeclaredMethods();
61          Method postConstruct = null;
62          for (Method method : methods)
63          {
64              if (method.isAnnotationPresent(PostConstruct.class))
65              {
66                  // a method that does not take any arguments
67                  // the method must not be static
68                  // must not throw any checked expections
69                  // the return value must be void
70                  // the method may be public, protected, package private or private
71  
72                  if ((postConstruct != null)
73                          || (method.getParameterTypes().length != 0)
74                          || (Modifier.isStatic(method.getModifiers()))
75                          || (method.getExceptionTypes().length > 0)
76                          || (!method.getReturnType().getName().equals("void")))
77                  {
78                      throw new IllegalArgumentException("Invalid PostConstruct annotation");
79                  }
80                  postConstruct = method;
81              }
82          }
83  
84          invokeAnnotatedMethod(postConstruct, instance);
85  
86      }
87  
88      public void destroyInstance(Object instance)
89              throws IllegalAccessException, InvocationTargetException
90      {
91  
92          // TODO the servlet spec is not clear about searching in superclass??
93          // May be only check non private fields and methods
94          Method[] methods = instance.getClass().getDeclaredMethods();
95          Method preDestroy = null;
96          for (Method method : methods)
97          {
98              if (method.isAnnotationPresent(PreDestroy.class))
99              {
100                 // must not throw any checked expections
101                 // the method must not be static
102                 // must not throw any checked expections
103                 // the return value must be void
104                 // the method may be public, protected, package private or private
105 
106                 if ((preDestroy != null)
107                         || (method.getParameterTypes().length != 0)
108                         || (Modifier.isStatic(method.getModifiers()))
109                         || (method.getExceptionTypes().length > 0)
110                         || (!method.getReturnType().getName().equals("void")))
111                 {
112                     throw new IllegalArgumentException("Invalid PreDestroy annotation");
113                 }
114                 preDestroy = method;
115             }
116         }
117 
118         invokeAnnotatedMethod(preDestroy, instance);
119 
120     }
121 
122     private void invokeAnnotatedMethod(Method method, Object instance)
123                 throws IllegalAccessException, InvocationTargetException
124     {
125         // At the end the annotated
126         // method is invoked
127         if (method != null)
128         {
129             boolean accessibility = method.isAccessible();
130             method.setAccessible(true);
131             method.invoke(instance);
132             method.setAccessible(accessibility);
133         }
134     }
135 
136      /**
137      * Inject resources in specified instance.
138      */
139     protected void processAnnotations(Object instance)
140             throws IllegalAccessException, InvocationTargetException, NamingException
141     {
142 
143     }
144 
145 }