| <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../../jacoco-resources/report.gif" type="image/gif"/><title>BeanTypeListener.java</title><link rel="stylesheet" href="../../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../../index.html" class="el_report">Apache Shiro :: All (aggregate jar)</a> > <a href="../index.html" class="el_bundle">shiro-guice</a> > <a href="index.source.html" class="el_package">org.apache.shiro.guice</a> > <span class="el_source">BeanTypeListener.java</span></div><h1>BeanTypeListener.java</h1><pre class="source lang-java linenums">/* |
| * Licensed to the Apache Software Foundation (ASF) under one |
| * or more contributor license agreements. See the NOTICE file |
| * distributed with this work for additional information |
| * regarding copyright ownership. The ASF licenses this file |
| * to you under the Apache License, Version 2.0 (the |
| * "License"); you may not use this file except in compliance |
| * with the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, |
| * software distributed under the License is distributed on an |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| * KIND, either express or implied. See the License for the |
| * specific language governing permissions and limitations |
| * under the License. |
| */ |
| package org.apache.shiro.guice; |
| |
| import java.beans.PropertyDescriptor; |
| import java.lang.reflect.InvocationTargetException; |
| import java.lang.reflect.Modifier; |
| import java.lang.reflect.Type; |
| import java.util.Arrays; |
| import java.util.HashMap; |
| import java.util.HashSet; |
| import java.util.Map; |
| import java.util.Set; |
| |
| import com.google.inject.Binder; |
| import com.google.inject.ConfigurationException; |
| import com.google.inject.Injector; |
| import com.google.inject.Key; |
| import com.google.inject.MembersInjector; |
| import com.google.inject.Provider; |
| import com.google.inject.TypeLiteral; |
| import com.google.inject.matcher.Matcher; |
| import com.google.inject.matcher.Matchers; |
| import com.google.inject.multibindings.MapBinder; |
| import com.google.inject.name.Names; |
| import com.google.inject.spi.TypeEncounter; |
| import com.google.inject.spi.TypeListener; |
| import com.google.inject.util.Types; |
| |
| import org.apache.commons.beanutils.BeanUtilsBean; |
| import org.apache.commons.beanutils.SuppressPropertiesBeanIntrospector; |
| import org.apache.shiro.SecurityUtils; |
| |
| /** |
| * TypeListener that injects setter methods on Shiro objects. |
| */ |
| class BeanTypeListener implements TypeListener { |
| <span class="fc" id="L54"> public static final Package SHIRO_GUICE_PACKAGE = ShiroModule.class.getPackage();</span> |
| <span class="fc" id="L55"> public static final Package SHIRO_PACKAGE = SecurityUtils.class.getPackage();</span> |
| |
| <span class="fc" id="L57"> private static Matcher<Class> shiroMatcher = Matchers.inSubpackage(SHIRO_PACKAGE.getName());</span> |
| <span class="fc" id="L58"> private static Matcher<Class> shiroGuiceMatcher = Matchers.inSubpackage(SHIRO_GUICE_PACKAGE.getName());</span> |
| |
| <span class="fc" id="L60"> private static Matcher<Class> classMatcher = ShiroMatchers.ANY_PACKAGE.and(shiroMatcher.and(Matchers.not(shiroGuiceMatcher)));</span> |
| |
| <span class="fc" id="L62"> public static final Matcher<TypeLiteral> MATCHER = ShiroMatchers.typeLiteral(classMatcher);</span> |
| |
| private static final String BEAN_TYPE_MAP_NAME = "__SHIRO_BEAN_TYPES__"; |
| <span class="fc" id="L65"> static final Key<?> MAP_KEY = Key.get(Types.mapOf(TypeLiteral.class, BeanTypeKey.class), Names.named(BEAN_TYPE_MAP_NAME));</span> |
| |
| /** |
| * @since 1.4 |
| */ |
| private final BeanUtilsBean beanUtilsBean; |
| |
| <span class="fc" id="L72"> private static final Set<Class<?>> WRAPPER_TYPES = new HashSet<Class<?>>(Arrays.asList(</span> |
| Byte.class, |
| Boolean.class, |
| Character.class, |
| Double.class, |
| Float.class, |
| Integer.class, |
| Long.class, |
| Short.class, |
| Void.class)); |
| |
| <span class="fc" id="L83"> public BeanTypeListener() {</span> |
| // SHIRO-619 |
| <span class="fc" id="L85"> beanUtilsBean = new BeanUtilsBean();</span> |
| <span class="fc" id="L86"> beanUtilsBean.getPropertyUtils().addBeanIntrospector(</span> |
| SuppressPropertiesBeanIntrospector.SUPPRESS_CLASS); |
| <span class="fc" id="L88"> }</span> |
| |
| public <I> void hear(TypeLiteral<I> type, final TypeEncounter<I> encounter) { |
| <span class="fc" id="L91"> PropertyDescriptor propertyDescriptors[] = beanUtilsBean.getPropertyUtils().getPropertyDescriptors(type.getRawType());</span> |
| <span class="fc" id="L92"> final Map<PropertyDescriptor, Key<?>> propertyDependencies = new HashMap<PropertyDescriptor, Key<?>>(propertyDescriptors.length);</span> |
| <span class="fc" id="L93"> final Provider<Injector> injectorProvider = encounter.getProvider(Injector.class);</span> |
| <span class="fc bfc" id="L94" title="All 2 branches covered."> for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {</span> |
| <span class="pc bpc" id="L95" title="1 of 4 branches missed."> if (propertyDescriptor.getWriteMethod() != null && Modifier.isPublic(propertyDescriptor.getWriteMethod().getModifiers())) {</span> |
| <span class="fc" id="L96"> Type propertyType = propertyDescriptor.getWriteMethod().getGenericParameterTypes()[0];</span> |
| <span class="fc" id="L97"> propertyDependencies.put(propertyDescriptor, createDependencyKey(propertyDescriptor, propertyType));</span> |
| } |
| } |
| <span class="fc" id="L100"> encounter.register(new MembersInjector<I>() {</span> |
| public void injectMembers(I instance) { |
| <span class="fc bfc" id="L102" title="All 2 branches covered."> for (Map.Entry<PropertyDescriptor, Key<?>> dependency : propertyDependencies.entrySet()) {</span> |
| try { |
| <span class="fc" id="L104"> final Injector injector = injectorProvider.get();</span> |
| |
| <span class="fc" id="L106"> Object value = injector.getInstance(getMappedKey(injector, dependency.getValue()));</span> |
| <span class="fc" id="L107"> dependency.getKey().getWriteMethod().invoke(instance, value);</span> |
| |
| <span class="fc" id="L109"> } catch (ConfigurationException e) {</span> |
| // This is ok, it simply means that we can't fulfill this dependency. |
| // Is there a better way to do this? |
| <span class="nc" id="L112"> } catch (InvocationTargetException e) {</span> |
| <span class="nc" id="L113"> throw new RuntimeException("Couldn't set property " + dependency.getKey().getDisplayName(), e);</span> |
| <span class="nc" id="L114"> } catch (IllegalAccessException e) {</span> |
| <span class="nc" id="L115"> throw new RuntimeException("We shouldn't have ever reached this point, we don't try to inject to non-accessible methods.", e);</span> |
| <span class="fc" id="L116"> }</span> |
| <span class="fc" id="L117"> }</span> |
| |
| <span class="fc" id="L119"> }</span> |
| }); |
| <span class="fc" id="L121"> }</span> |
| |
| private static Key<?> getMappedKey(Injector injector, Key<?> key) { |
| <span class="fc" id="L124"> Map<TypeLiteral, BeanTypeKey> beanTypeMap = getBeanTypeMap(injector);</span> |
| <span class="fc bfc" id="L125" title="All 4 branches covered."> if(key.getAnnotation() == null && beanTypeMap.containsKey(key.getTypeLiteral())) {</span> |
| <span class="fc" id="L126"> return beanTypeMap.get(key.getTypeLiteral()).key;</span> |
| } else { |
| <span class="fc" id="L128"> return key;</span> |
| } |
| } |
| |
| @SuppressWarnings({"unchecked"}) |
| private static Map<TypeLiteral, BeanTypeKey> getBeanTypeMap(Injector injector) { |
| <span class="fc" id="L134"> return (Map<TypeLiteral, BeanTypeKey>) injector.getInstance(MAP_KEY);</span> |
| } |
| |
| private static Key<?> createDependencyKey(PropertyDescriptor propertyDescriptor, Type propertyType) { |
| <span class="fc bfc" id="L138" title="All 2 branches covered."> if(requiresName(propertyType)) {</span> |
| <span class="fc" id="L139"> return Key.get(propertyType, Names.named("shiro." + propertyDescriptor.getName()));</span> |
| } else { |
| <span class="fc" id="L141"> return Key.get(propertyType);</span> |
| } |
| } |
| |
| private static boolean requiresName(Type propertyType) { |
| <span class="fc bfc" id="L146" title="All 2 branches covered."> if (propertyType instanceof Class) {</span> |
| <span class="fc" id="L147"> Class<?> aClass = (Class<?>) propertyType;</span> |
| <span class="pc bpc" id="L148" title="2 of 8 branches missed."> return aClass.isPrimitive() || aClass.isEnum() || WRAPPER_TYPES.contains(aClass) || CharSequence.class.isAssignableFrom(aClass);</span> |
| } else { |
| <span class="fc" id="L150"> return false;</span> |
| } |
| } |
| |
| static void ensureBeanTypeMapExists(Binder binder) { |
| <span class="fc" id="L155"> beanTypeMapBinding(binder).addBinding(TypeLiteral.get(BeanTypeKey.class)).toInstance(new BeanTypeKey(null));</span> |
| <span class="fc" id="L156"> }</span> |
| |
| static <T> void bindBeanType(Binder binder, TypeLiteral<T> typeLiteral, Key<? extends T> key) { |
| <span class="fc" id="L159"> beanTypeMapBinding(binder).addBinding(typeLiteral).toInstance(new BeanTypeKey(key));</span> |
| <span class="fc" id="L160"> }</span> |
| |
| private static MapBinder<TypeLiteral, BeanTypeKey> beanTypeMapBinding(Binder binder) { |
| <span class="fc" id="L163"> return MapBinder.newMapBinder(binder, TypeLiteral.class, BeanTypeKey.class, Names.named(BEAN_TYPE_MAP_NAME));</span> |
| } |
| |
| private static class BeanTypeKey { |
| Key<?> key; |
| |
| <span class="fc" id="L169"> private BeanTypeKey(Key<?> key) {</span> |
| <span class="fc" id="L170"> this.key = key;</span> |
| <span class="fc" id="L171"> }</span> |
| } |
| } |
| </pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.3.201901230119</span></div></body></html> |