001/** 002 * GRANITE DATA SERVICES 003 * Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S. 004 * 005 * This file is part of the Granite Data Services Platform. 006 * 007 * Granite Data Services is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * Granite Data Services 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 Lesser 015 * 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 library; if not, write to the Free Software 019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 020 * USA, or see <http://www.gnu.org/licenses/>. 021 */ 022package org.granite.cdi; 023 024import javax.enterprise.context.ApplicationScoped; 025import javax.enterprise.inject.Default; 026import javax.enterprise.inject.Instance; 027import javax.enterprise.inject.Produces; 028import javax.enterprise.util.AnnotationLiteral; 029import javax.inject.Inject; 030import javax.servlet.ServletContext; 031 032import org.granite.context.GraniteContext; 033import org.granite.gravity.Gravity; 034import org.granite.gravity.GravityManager; 035import org.granite.gravity.GravityProxy; 036import org.granite.messaging.webapp.ServletGraniteContext; 037 038 039/** 040 * TideGravity produces the Gravity instance for injection in CDI beans 041 * 042 * @author William DRAI 043 */ 044@ApplicationScoped 045public class GravityFactory { 046 047 private Gravity gravity; 048 049 private GravityProxy gravityProxy = new CDIGravityProxy(); 050 051 @Inject 052 Instance<ServletContext> servletContextInstance; 053 054 @SuppressWarnings("serial") 055 private static final AnnotationLiteral<Default> DEFAULT_LITERAL = new AnnotationLiteral<Default>() {}; 056 057 public void setGravity(Gravity gravity) { 058 this.gravity = gravity; 059 } 060 061 @Produces 062 public Gravity getGravity() { 063 return gravityProxy; 064 } 065 066 private class CDIGravityProxy extends GravityProxy { 067 068 @Override 069 protected Gravity getGravity() { 070 if (gravity != null) 071 return gravity; 072 073 ServletContext servletContext = null; 074 075 if (!servletContextInstance.isUnsatisfied()) { 076 // Use ServletContext exposed with @Default qualifier (by Apache DeltaSpike Servlet for example) 077 servletContext = servletContextInstance.select(DEFAULT_LITERAL).get(); 078 } 079 if (servletContext == null) { 080 // If not found, lookup in GraniteContext 081 GraniteContext graniteContext = GraniteContext.getCurrentInstance(); 082 if (graniteContext != null && graniteContext instanceof ServletGraniteContext) 083 servletContext = ((ServletGraniteContext)graniteContext).getServletContext(); 084 } 085 086 return servletContext != null ? GravityManager.getGravity(servletContext) : null; 087 } 088 089 } 090}