001: /*
002: * JBoss, Home of Professional Open Source
003: * Copyright 2006, JBoss Inc., and individual contributors as indicated
004: * by the @authors tag. See the copyright.txt in the distribution for a
005: * full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022:
023: package org.jboss.web.jsf.integration.injection;
024:
025: import com.sun.faces.spi.InjectionProvider;
026: import com.sun.faces.spi.InjectionProviderException;
027: import javax.naming.Context;
028: import javax.naming.InitialContext;
029: import org.apache.catalina.util.DefaultAnnotationProcessor;
030: import org.jboss.logging.Logger;
031:
032: /**
033: * Provides interface between JSF RI and Tomcat Catalina for injection of managed beans as
034: * per JSF 1.2 Spec section 5.4.
035: *
036: * @author Stan Silvert
037: */
038: public class JBossInjectionProvider implements InjectionProvider {
039: private static final Logger LOG = Logger
040: .getLogger(JBossInjectionProvider.class);
041: private static final String NAMING_DISABLED = "Injection of naming resources into JSF managed beans disabled.";
042:
043: private Context namingContext;
044: private DefaultAnnotationProcessor annotationProcessor = null;
045:
046: /**
047: * Uses the default naming context for injection of resources into managed beans.
048: */
049: public JBossInjectionProvider() {
050: try {
051: this .namingContext = new InitialContext();
052: this .annotationProcessor = new DefaultAnnotationProcessor(
053: this .namingContext);
054: } catch (Exception e) {
055: LOG.warn(NAMING_DISABLED, e);
056: }
057: }
058:
059: /**
060: * This constructor allows a subclass to override the default naming
061: * context.
062: *
063: * @param namingContext The naming context to use for injection of managed beans.
064: * If this param is null then injection of resources will be
065: * disabled and JBoss will only call @PostConstruct and
066: * @PreDestroy methods.
067: */
068: protected JBossInjectionProvider(Context namingContext) {
069: if (namingContext == null) {
070: LOG.warn(NAMING_DISABLED);
071: }
072:
073: this .namingContext = namingContext;
074: this .annotationProcessor = new DefaultAnnotationProcessor(
075: this .namingContext);
076: }
077:
078: /**
079: * Call methods on a managed bean that are annotated with @PreDestroy.
080: */
081: public void invokePreDestroy(Object managedBean)
082: throws InjectionProviderException {
083: try {
084: annotationProcessor.preDestroy(managedBean);
085: } catch (Exception e) {
086: LOG.error("PreDestroy failed on managed bean.", e);
087: }
088: }
089:
090: /**
091: * Call methods on a managed bean that are annotated with @PostConstruct.
092: */
093: public void invokePostConstruct(Object managedBean)
094: throws InjectionProviderException {
095: try {
096: annotationProcessor.postConstruct(managedBean);
097: } catch (Exception e) {
098: LOG.error("PostConstruct failed on managed bean.", e);
099: }
100: }
101:
102: /**
103: * Inject naming resources into a managed bean and then call methods
104: * annotated with @PostConstruct.
105: */
106: public void inject(Object managedBean)
107: throws InjectionProviderException {
108: if (this .namingContext != null) {
109: try {
110: annotationProcessor.processAnnotations(managedBean);
111: } catch (Exception e) {
112: LOG.error("Injection failed on managed bean.", e);
113: }
114: }
115:
116: }
117:
118: }
|