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 javax.naming.NamingException;
22  import javax.naming.Context;
23  import javax.annotation.Resource;
24  import javax.ejb.EJB;
25  import javax.persistence.PersistenceContext;
26  import javax.persistence.PersistenceUnit;
27  
28  
29  import java.lang.reflect.Method;
30  import java.lang.reflect.InvocationTargetException;
31  import java.lang.reflect.Field;
32  
33  // TODO @EJBs
34  public class AllAnnotationLifecycleProvider extends ResourceAnnotationLifecycleProvider
35  {
36  
37      public AllAnnotationLifecycleProvider(Context context)
38      {
39          super(context);
40      }
41  
42      protected void checkMethodAnnotation(Method method, Object instance)
43              throws NamingException, IllegalAccessException, InvocationTargetException
44      {
45          super.checkMethodAnnotation(method, instance);
46          if (method.isAnnotationPresent(Resource.class))
47          {
48              Resource annotation =  method.getAnnotation(Resource.class);
49              lookupMethodResource(context, instance, method, annotation.name());
50          }
51          if (method.isAnnotationPresent(EJB.class))
52          {
53              EJB annotation =  method.getAnnotation(EJB.class);
54              lookupMethodResource(context, instance, method, annotation.name());
55          }
56          // TODO where i find WebServiceRef?
57          /*if (method.isAnnotationPresent(WebServiceRef.class)) {
58              WebServiceRef annotation =
59                  (WebServiceRef) method.getAnnotation(WebServiceRef.class);
60              lookupMethodResource(context, instance, methods, annotation.name());
61          }*/
62          if (method.isAnnotationPresent(PersistenceContext.class))
63          {
64              PersistenceContext annotation = method.getAnnotation(PersistenceContext.class);
65              lookupMethodResource(context, instance, method, annotation.name());
66          }
67          if (method.isAnnotationPresent(PersistenceUnit.class))
68          {
69              PersistenceUnit annotation = method.getAnnotation(PersistenceUnit.class);
70              lookupMethodResource(context, instance, method, annotation.name());
71          }
72      }
73  
74      protected void checkFieldAnnotation(Field field, Object instance)
75              throws NamingException, IllegalAccessException
76      {
77          super.checkFieldAnnotation(field, instance);
78          if (field.isAnnotationPresent(EJB.class))
79          {
80              EJB annotation = field.getAnnotation(EJB.class);
81              lookupFieldResource(context, instance, field, annotation.name());
82          }
83          /*if (field.isAnnotationPresent(WebServiceRef.class)) {
84              WebServiceRef annotation =
85                  (WebServiceRef) field.getAnnotation(WebServiceRef.class);
86              lookupFieldResource(context, instance, field, annotation.name());
87          }*/
88          if (field.isAnnotationPresent(PersistenceContext.class))
89          {
90              PersistenceContext annotation = field.getAnnotation(PersistenceContext.class);
91              lookupFieldResource(context, instance, field, annotation.name());
92          }
93          if (field.isAnnotationPresent(PersistenceUnit.class))
94          {
95              PersistenceUnit annotation = field.getAnnotation(PersistenceUnit.class);
96              lookupFieldResource(context, instance, field, annotation.name());
97          }
98      }
99  }