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.tide.ejb;
023
024import java.lang.annotation.Annotation;
025import java.lang.reflect.Method;
026import java.util.Collections;
027import java.util.Set;
028
029import javax.naming.InitialContext;
030import javax.servlet.ServletContext;
031
032import org.granite.config.ConfigProvider;
033import org.granite.gravity.Gravity;
034import org.granite.logging.Logger;
035import org.granite.messaging.service.ServiceFactory;
036
037
038public class EjbConfigProvider implements ConfigProvider {
039
040    private static final Logger log = Logger.getLogger(EjbConfigProvider.class);
041
042    private ServletContext servletContext;
043    
044    public EjbConfigProvider(ServletContext servletContext) {
045        this.servletContext = servletContext;
046    }
047        
048        public Boolean useTide() {
049                return true;
050        }
051
052        public String getType() {
053                return "server";
054        }
055
056        public Class<? extends ServiceFactory> getFactoryClass() {
057                return EjbServiceFactory.class;
058        }
059
060        public <T> T findInstance(Class<T> type) {
061                return null;
062        }
063
064        public <T> Set<T> findInstances(Class<T> type) {
065                return Collections.emptySet();
066        }
067        
068        public Class<?>[] getTideInterfaces() {
069                return new Class<?>[] { EjbIdentity.class };
070        }
071
072        @SuppressWarnings("unchecked")
073        public Class<? extends Annotation>[] getTideAnnotations() {
074                return new Class[0];
075        }
076
077    public void initGravity(Gravity gravity) {
078        try {
079            // Set reference in EJB proxy if found
080            InitialContext ic = new InitialContext();
081            org.granite.ejb.Gravity gravityEJB = (org.granite.ejb.Gravity)ic.lookup("java:global" + servletContext.getContextPath() + "/org.granite.ejb.Gravity");
082            gravityEJB.setGravity(gravity);
083            log.info("Registered Gravity EJB");
084        }
085        catch (Exception e) {
086            log.debug(e, "Gravity EJB not found");
087        }
088    }
089
090}