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.cdi;
023
024import java.io.Serializable;
025import java.lang.reflect.Type;
026import java.util.HashMap;
027import java.util.Map;
028import java.util.Map.Entry;
029
030import javax.enterprise.context.ApplicationScoped;
031import javax.enterprise.inject.spi.Bean;
032
033
034/**
035 * @author William DRAI
036 */
037@ApplicationScoped
038public class TideInstrumentedBeans implements Serializable {
039
040    private static final long serialVersionUID = 1L;
041    
042    
043    private Map<Type, Bean<?>> beans = null;
044    private Map<Object, Type> producedBeans = null;
045    
046    public void setBeans(Map<Type, Bean<?>> beans) {
047        this.beans = beans;
048    }
049    public Bean<?> getBean(Type type) {
050        Bean<?> bean = beans.get(type);
051        if (bean != null)
052                return bean;
053        if (type instanceof Class<?>) {
054                Class<?> clazz = (Class<?>)type;
055                if (clazz.getSuperclass() != null)
056                        bean = beans.get(clazz.getSuperclass());
057                if (bean != null)
058                        return bean;
059                for (Class<?> i : clazz.getInterfaces()) {
060                        bean = beans.get(i);
061                        if (bean != null)
062                                return bean;
063                }
064        }
065        return null;
066    }
067    
068    
069    public void setProducedBeans(Map<Object, Type> producedBeans) {
070        this.producedBeans = new HashMap<Object, Type>();
071        for (Entry<Object, Type> me : producedBeans.entrySet()) {
072                if (beans.containsKey(me.getValue()))
073                        this.producedBeans.put(me.getKey(), me.getValue());
074        }
075    }
076    public boolean isProducedBy(Object name, Type clazz) {
077        Class<?> beanClass = ((Class<?>)producedBeans.get(name)); 
078        return beanClass != null ? beanClass.isAssignableFrom((Class<?>)clazz) : false;
079    }
080}