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.spi.impl;
20  
21  import java.lang.reflect.InvocationTargetException;
22  import java.util.logging.Level;
23  import java.util.logging.Logger;
24  
25  import javax.faces.context.ExternalContext;
26  import javax.naming.NamingException;
27  import javax.servlet.ServletContext;
28  
29  import org.apache.myfaces.spi.InjectionProvider;
30  import org.apache.myfaces.spi.InjectionProviderException;
31  
32  public class TomcatAnnotationInjectionProvider extends InjectionProvider
33  {
34      private static Logger log = Logger.getLogger(TomcatAnnotationInjectionProvider.class.getName());
35  
36      private ExternalContext externalContext;
37      private org.apache.AnnotationProcessor annotationProcessor;
38  
39      public TomcatAnnotationInjectionProvider(ExternalContext externalContext)
40      {
41          this.externalContext = externalContext;
42      }
43  
44      @Override
45      public Object inject(Object instance) throws InjectionProviderException
46      {
47          try
48          {
49              annotationProcessor.processAnnotations(instance);
50          }
51          catch (IllegalAccessException ex)
52          {
53              throw new InjectionProviderException(ex);
54          }
55          catch (InvocationTargetException ex)
56          {
57              throw new InjectionProviderException(ex);
58          }
59          catch (NamingException ex)
60          {
61              throw new InjectionProviderException(ex);
62          }
63          return null;
64      }
65  
66      @Override
67      public void preDestroy(Object instance, Object creationMetaData) throws InjectionProviderException
68      {
69          if (log.isLoggable(Level.FINEST))
70          {
71              log.info("Destroy instance of " + instance.getClass().getName());
72          }
73          try
74          {
75              annotationProcessor.preDestroy(instance);
76          }
77          catch (IllegalAccessException ex)
78          {
79              throw new InjectionProviderException(ex);
80          }
81          catch (InvocationTargetException ex)
82          {
83              throw new InjectionProviderException(ex);
84          }
85      }
86  
87      @Override
88      public boolean isAvailable()
89      {
90          try
91          {
92              annotationProcessor =  (org.apache.AnnotationProcessor) ((ServletContext)
93                       externalContext.getContext()).getAttribute(org.apache.AnnotationProcessor.class.getName());
94              return annotationProcessor != null;
95          }
96          catch (Throwable e)
97          {
98              // ignore
99          }
100         return false;
101     }
102 
103     @Override
104     public void postConstruct(Object instance, Object creationMetaData) throws InjectionProviderException
105     {
106         try
107         {
108             annotationProcessor.postConstruct(instance);
109         }
110         catch (IllegalAccessException ex)
111         {
112             throw new InjectionProviderException(ex);
113         }
114         catch (InvocationTargetException ex)
115         {
116             throw new InjectionProviderException(ex);
117         }
118     }
119 }