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      @Override
43      protected void checkMethodAnnotation(Method method, Object instance)
44              throws NamingException, IllegalAccessException, InvocationTargetException
45      {
46          super.checkMethodAnnotation(method, instance);
47          if (method.isAnnotationPresent(Resource.class))
48          {
49              Resource annotation =  method.getAnnotation(Resource.class);
50              lookupMethodResource(context, instance, method, annotation.name());
51          }
52          if (method.isAnnotationPresent(EJB.class))
53          {
54              EJB annotation =  method.getAnnotation(EJB.class);
55              lookupMethodResource(context, instance, method, annotation.name());
56          }
57          // TODO where i find WebServiceRef?
58          /*if (method.isAnnotationPresent(WebServiceRef.class)) {
59              WebServiceRef annotation =
60                  (WebServiceRef) method.getAnnotation(WebServiceRef.class);
61              lookupMethodResource(context, instance, methods, annotation.name());
62          }*/
63          if (method.isAnnotationPresent(PersistenceContext.class))
64          {
65              PersistenceContext annotation = method.getAnnotation(PersistenceContext.class);
66              lookupMethodResource(context, instance, method, annotation.name());
67          }
68          if (method.isAnnotationPresent(PersistenceUnit.class))
69          {
70              PersistenceUnit annotation = method.getAnnotation(PersistenceUnit.class);
71              lookupMethodResource(context, instance, method, annotation.name());
72          }
73      }
74  
75      @Override
76      protected void checkFieldAnnotation(Field field, Object instance)
77              throws NamingException, IllegalAccessException
78      {
79          super.checkFieldAnnotation(field, instance);
80          if (field.isAnnotationPresent(EJB.class))
81          {
82              EJB annotation = field.getAnnotation(EJB.class);
83              lookupFieldResource(context, instance, field, annotation.name());
84          }
85          /*if (field.isAnnotationPresent(WebServiceRef.class)) {
86              WebServiceRef annotation =
87                  (WebServiceRef) field.getAnnotation(WebServiceRef.class);
88              lookupFieldResource(context, instance, field, annotation.name());
89          }*/
90          if (field.isAnnotationPresent(PersistenceContext.class))
91          {
92              PersistenceContext annotation = field.getAnnotation(PersistenceContext.class);
93              lookupFieldResource(context, instance, field, annotation.name());
94          }
95          if (field.isAnnotationPresent(PersistenceUnit.class))
96          {
97              PersistenceUnit annotation = field.getAnnotation(PersistenceUnit.class);
98              lookupFieldResource(context, instance, field, annotation.name());
99          }
100     }
101 }