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 org.apache.myfaces.shared_impl.util.ClassUtils;
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  
25  import javax.naming.NamingException;
26  import javax.faces.context.ExternalContext;
27  import javax.servlet.ServletContext;
28  import java.lang.reflect.InvocationTargetException;
29  
30  public class TomcatAnnotationLifecycleProvider implements 
31      DiscoverableLifecycleProvider, LifecycleProvider2
32  {
33      private static Log log = LogFactory.getLog(TomcatAnnotationLifecycleProvider.class);
34  
35      private ExternalContext externalContext;
36      private org.apache.AnnotationProcessor annotationProcessor;
37  
38      public TomcatAnnotationLifecycleProvider(ExternalContext externalContext)
39      {
40          this.externalContext = externalContext;
41      }
42  
43  
44      public Object newInstance(String className)
45              throws InstantiationException, IllegalAccessException, InvocationTargetException, NamingException, ClassNotFoundException {
46          Class clazz = ClassUtils.classForName(className);
47          log.info("Creating instance of " + className);
48          Object object = clazz.newInstance();
49          annotationProcessor.processAnnotations(object);
50          //annotationProcessor.postConstruct(object);
51          return object;
52      }
53  
54  
55      public void destroyInstance(Object o) throws IllegalAccessException, InvocationTargetException
56      {
57          log.info("Destroy instance of " + o.getClass().getName());
58          annotationProcessor.preDestroy(o);
59      }
60  
61      public boolean isAvailable()
62      {
63          try
64          {
65              annotationProcessor =  (org.apache.AnnotationProcessor) ((ServletContext)
66                       externalContext.getContext()).getAttribute(org.apache.AnnotationProcessor.class.getName());
67              return annotationProcessor != null;
68          } catch (Exception e) {
69              // ignore
70          }
71          return false;
72      }
73  
74  
75      public void postConstruct(Object o) throws IllegalAccessException,
76              InvocationTargetException
77      {
78          annotationProcessor.postConstruct(o);
79      }
80  
81  }