01: package org.apache.myfaces.config.annotation;
02:
03: /*
04: * Licensed to the Apache Software Foundation (ASF) under one or more
05: * contributor license agreements. See the NOTICE file distributed with
06: * this work for additional information regarding copyright ownership.
07: * The ASF licenses this file to You under the Apache License, Version 2.0
08: * (the "License"); you may not use this file except in compliance with
09: * the License. You may obtain a copy of the License at
10: *
11: * http://www.apache.org/licenses/LICENSE-2.0
12: *
13: * Unless required by applicable law or agreed to in writing, software
14: * distributed under the License is distributed on an "AS IS" BASIS,
15: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16: * See the License for the specific language governing permissions and
17: * limitations under the License.
18: */
19:
20: import org.apache.myfaces.shared_impl.util.ClassUtils;
21: import org.apache.commons.logging.Log;
22: import org.apache.commons.logging.LogFactory;
23:
24: import javax.naming.NamingException;
25: import javax.faces.context.ExternalContext;
26: import javax.servlet.ServletContext;
27: import java.lang.reflect.InvocationTargetException;
28:
29: public class TomcatAnnotationLifecycleProvider implements
30: DiscoverableLifecycleProvider {
31: private static Log log = LogFactory
32: .getLog(TomcatAnnotationLifecycleProvider.class);
33:
34: private ExternalContext externalContext;
35: private org.apache.AnnotationProcessor annotationProcessor;
36:
37: public TomcatAnnotationLifecycleProvider(
38: ExternalContext externalContext) {
39: this .externalContext = externalContext;
40: }
41:
42: public Object newInstance(String className)
43: throws InstantiationException, IllegalAccessException,
44: InvocationTargetException, NamingException,
45: 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: public void destroyInstance(Object o)
55: throws IllegalAccessException, InvocationTargetException {
56: log.info("Destroy instance of " + o.getClass().getName());
57: annotationProcessor.preDestroy(o);
58: }
59:
60: public boolean isAvailable() {
61: try {
62: annotationProcessor = (org.apache.AnnotationProcessor) ((ServletContext) externalContext
63: .getContext())
64: .getAttribute(org.apache.AnnotationProcessor.class
65: .getName());
66: return annotationProcessor != null;
67: } catch (Exception e) {
68: // ignore
69: }
70: return false;
71: }
72:
73: }
|