001 /*
002 GRANITE DATA SERVICES
003 Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
004
005 This file is part of Granite Data Services.
006
007 Granite Data Services is free software; you can redistribute it and/or modify
008 it under the terms of the GNU Library General Public License as published by
009 the Free Software Foundation; either version 2 of the License, or (at your
010 option) any later version.
011
012 Granite Data Services is distributed in the hope that it will be useful, but
013 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015 for more details.
016
017 You should have received a copy of the GNU Library General Public License
018 along with this library; if not, see <http://www.gnu.org/licenses/>.
019 */
020
021 package org.granite.tide.cdi;
022
023 import java.io.Serializable;
024 import java.lang.reflect.Type;
025 import java.util.HashMap;
026 import java.util.Map;
027 import java.util.Map.Entry;
028
029 import javax.enterprise.context.ApplicationScoped;
030 import javax.enterprise.inject.spi.Bean;
031
032
033 /**
034 * @author William DRAI
035 */
036 @ApplicationScoped
037 public class TideInstrumentedBeans implements Serializable {
038
039 private static final long serialVersionUID = 1L;
040
041
042 private Map<Type, Bean<?>> beans = null;
043 private Map<Object, Type> producedBeans = null;
044
045 public void setBeans(Map<Type, Bean<?>> beans) {
046 this.beans = beans;
047 }
048 public Bean<?> getBean(Type type) {
049 Bean<?> bean = beans.get(type);
050 if (bean != null)
051 return bean;
052 if (type instanceof Class<?>) {
053 Class<?> clazz = (Class<?>)type;
054 if (clazz.getSuperclass() != null)
055 bean = beans.get(clazz.getSuperclass());
056 if (bean != null)
057 return bean;
058 for (Class<?> i : clazz.getInterfaces()) {
059 bean = beans.get(i);
060 if (bean != null)
061 return bean;
062 }
063 }
064 return null;
065 }
066
067
068 public void setProducedBeans(Map<Object, Type> producedBeans) {
069 this.producedBeans = new HashMap<Object, Type>();
070 for (Entry<Object, Type> me : producedBeans.entrySet()) {
071 if (beans.containsKey(me.getValue()))
072 this.producedBeans.put(me.getKey(), me.getValue());
073 }
074 }
075 public boolean isProducedBy(Object name, Type clazz) {
076 Class<?> beanClass = ((Class<?>)producedBeans.get(name));
077 return beanClass != null ? beanClass.isAssignableFrom((Class<?>)clazz) : false;
078 }
079 }