KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > web > jsf > integration > injection > JBossInjectionProvider


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2006, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22
23 package org.jboss.web.jsf.integration.injection;
24
25 import com.sun.faces.spi.InjectionProvider;
26 import com.sun.faces.spi.InjectionProviderException;
27 import javax.naming.Context JavaDoc;
28 import javax.naming.InitialContext JavaDoc;
29 import org.apache.catalina.util.DefaultAnnotationProcessor;
30 import org.jboss.logging.Logger;
31
32 /**
33  * Provides interface between JSF RI and Tomcat Catalina for injection of managed beans as
34  * per JSF 1.2 Spec section 5.4.
35  *
36  * @author Stan Silvert
37  */

38 public class JBossInjectionProvider implements InjectionProvider {
39     private static final Logger LOG = Logger.getLogger(JBossInjectionProvider.class);
40     private static final String JavaDoc NAMING_DISABLED = "Injection of naming resources into JSF managed beans disabled.";
41
42     private Context JavaDoc namingContext;
43     private DefaultAnnotationProcessor annotationProcessor = null;
44     
45     /**
46      * Uses the default naming context for injection of resources into managed beans.
47      */

48     public JBossInjectionProvider() {
49         try {
50             this.namingContext = new InitialContext JavaDoc();
51             this.annotationProcessor = new DefaultAnnotationProcessor(this.namingContext);
52         } catch (Exception JavaDoc e) {
53             LOG.warn(NAMING_DISABLED, e);
54         }
55     }
56     
57     /**
58      * This constructor allows a subclass to override the default naming
59      * context.
60      *
61      * @param namingContext The naming context to use for injection of managed beans.
62      * If this param is null then injection of resources will be
63      * disabled and JBoss will only call @PostConstruct and
64      * @PreDestroy methods.
65      */

66     protected JBossInjectionProvider(Context JavaDoc namingContext) {
67         if (namingContext == null) {
68             LOG.warn(NAMING_DISABLED);
69         }
70         
71         this.namingContext = namingContext;
72         this.annotationProcessor = new DefaultAnnotationProcessor(this.namingContext);
73     }
74     
75     /**
76      * Call methods on a managed bean that are annotated with @PreDestroy.
77      */

78     public void invokePreDestroy(Object JavaDoc managedBean) throws InjectionProviderException {
79         try {
80             annotationProcessor.preDestroy(managedBean);
81         } catch (Exception JavaDoc e) {
82             LOG.error("PreDestroy failed on managed bean.", e);
83         }
84     }
85
86     /**
87      * Inject naming resources into a managed bean and then call methods
88      * annotated with @PostConstruct.
89      */

90     public void inject(Object JavaDoc managedBean) throws InjectionProviderException {
91         if (this.namingContext != null) {
92             try {
93                 annotationProcessor.processAnnotations(managedBean);
94             } catch (Exception JavaDoc e) {
95                 LOG.error("Injection failed on managed bean.", e);
96             }
97         }
98         
99         try {
100             annotationProcessor.postConstruct(managedBean);
101         } catch (Exception JavaDoc e) {
102             LOG.error("PostConstruct failed on managed bean.", e);
103         }
104     }
105     
106 }
107
Popular Tags