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.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.shared.util.ClassUtils;
30  
31  public class TomcatAnnotationLifecycleProvider implements 
32      DiscoverableLifecycleProvider, LifecycleProvider2
33  {
34      //private static Log log = LogFactory.getLog(TomcatAnnotationLifecycleProvider.class);
35      private static Logger log = Logger.getLogger(TomcatAnnotationLifecycleProvider.class.getName());
36  
37      private ExternalContext externalContext;
38      private org.apache.AnnotationProcessor annotationProcessor;
39  
40      public TomcatAnnotationLifecycleProvider(ExternalContext externalContext)
41      {
42          this.externalContext = externalContext;
43      }
44  
45  
46      public Object newInstance(String className)
47              throws InstantiationException, IllegalAccessException, InvocationTargetException,
48              NamingException, ClassNotFoundException
49      {
50          Class<?> clazz = ClassUtils.classForName(className);
51          if (log.isLoggable(Level.FINEST))
52          {
53              log.finest("Creating instance of " + className);
54          }
55          Object object = clazz.newInstance();
56          annotationProcessor.processAnnotations(object);
57          //annotationProcessor.postConstruct(object);
58          return object;
59      }
60  
61  
62      public void destroyInstance(Object o) throws IllegalAccessException, InvocationTargetException
63      {
64          if (log.isLoggable(Level.FINEST))
65          {
66              log.info("Destroy instance of " + o.getClass().getName());
67          }
68          annotationProcessor.preDestroy(o);
69      }
70  
71      public boolean isAvailable()
72      {
73          try
74          {
75              annotationProcessor =  (org.apache.AnnotationProcessor) ((ServletContext)
76                       externalContext.getContext()).getAttribute(org.apache.AnnotationProcessor.class.getName());
77              return annotationProcessor != null;
78          }
79          catch (Throwable e)
80          {
81              // ignore
82          }
83          return false;
84      }
85  
86  
87      public void postConstruct(Object o) throws IllegalAccessException,
88              InvocationTargetException
89      {
90          annotationProcessor.postConstruct(o);
91      }
92  
93  }