blob: 392fa7791c9d50b72fd5799b8f1a5e71e0da042c [file] [log] [blame]
<?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 :: Support :: Guice</a> &gt; <a href="index.source.html" class="el_package">org.apache.shiro.guice</a> &gt; <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
* &quot;License&quot;); 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
* &quot;AS IS&quot; 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.PropertyUtils;
import org.apache.shiro.SecurityUtils;
/**
* TypeListener that injects setter methods on Shiro objects.
*/
<span class="fc" id="L52">class BeanTypeListener implements TypeListener {</span>
<span class="fc" id="L53"> public static final Package SHIRO_GUICE_PACKAGE = ShiroModule.class.getPackage();</span>
<span class="fc" id="L54"> public static final Package SHIRO_PACKAGE = SecurityUtils.class.getPackage();</span>
<span class="fc" id="L56"> private static Matcher&lt;Class&gt; shiroMatcher = Matchers.inSubpackage(SHIRO_PACKAGE.getName());</span>
<span class="fc" id="L57"> private static Matcher&lt;Class&gt; shiroGuiceMatcher = Matchers.inSubpackage(SHIRO_GUICE_PACKAGE.getName());</span>
<span class="fc" id="L59"> private static Matcher&lt;Class&gt; classMatcher = ShiroMatchers.ANY_PACKAGE.and(shiroMatcher.and(Matchers.not(shiroGuiceMatcher)));</span>
<span class="fc" id="L61"> public static final Matcher&lt;TypeLiteral&gt; MATCHER = ShiroMatchers.typeLiteral(classMatcher);</span>
private static final String BEAN_TYPE_MAP_NAME = &quot;__SHIRO_BEAN_TYPES__&quot;;
<span class="fc" id="L64"> static final Key&lt;?&gt; MAP_KEY = Key.get(Types.mapOf(TypeLiteral.class, BeanTypeKey.class), Names.named(BEAN_TYPE_MAP_NAME));</span>
<span class="fc" id="L66"> private static final Set&lt;Class&lt;?&gt;&gt; WRAPPER_TYPES = new HashSet&lt;Class&lt;?&gt;&gt;(Arrays.asList(</span>
Byte.class,
Boolean.class,
Character.class,
Double.class,
Float.class,
Integer.class,
Long.class,
Short.class,
Void.class));
public &lt;I&gt; void hear(TypeLiteral&lt;I&gt; type, final TypeEncounter&lt;I&gt; encounter) {
<span class="fc" id="L78"> PropertyDescriptor propertyDescriptors[] = PropertyUtils.getPropertyDescriptors(type.getRawType());</span>
<span class="fc" id="L79"> final Map&lt;PropertyDescriptor, Key&lt;?&gt;&gt; propertyDependencies = new HashMap&lt;PropertyDescriptor, Key&lt;?&gt;&gt;(propertyDescriptors.length);</span>
<span class="fc" id="L80"> final Provider&lt;Injector&gt; injectorProvider = encounter.getProvider(Injector.class);</span>
<span class="fc bfc" id="L81" title="All 2 branches covered."> for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {</span>
<span class="pc bpc" id="L82" title="1 of 4 branches missed."> if (propertyDescriptor.getWriteMethod() != null &amp;&amp; Modifier.isPublic(propertyDescriptor.getWriteMethod().getModifiers())) {</span>
<span class="fc" id="L83"> Type propertyType = propertyDescriptor.getWriteMethod().getGenericParameterTypes()[0];</span>
<span class="fc" id="L84"> propertyDependencies.put(propertyDescriptor, createDependencyKey(propertyDescriptor, propertyType));</span>
}
}
<span class="fc" id="L87"> encounter.register(new MembersInjector&lt;I&gt;() {</span>
public void injectMembers(I instance) {
<span class="fc bfc" id="L89" title="All 2 branches covered."> for (Map.Entry&lt;PropertyDescriptor, Key&lt;?&gt;&gt; dependency : propertyDependencies.entrySet()) {</span>
try {
<span class="fc" id="L91"> final Injector injector = injectorProvider.get();</span>
<span class="fc" id="L93"> Object value = injector.getInstance(getMappedKey(injector, dependency.getValue()));</span>
<span class="fc" id="L94"> dependency.getKey().getWriteMethod().invoke(instance, value);</span>
<span class="fc" id="L96"> } 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="L99"> } catch (InvocationTargetException e) {</span>
<span class="nc" id="L100"> throw new RuntimeException(&quot;Couldn't set property &quot; + dependency.getKey().getDisplayName(), e);</span>
<span class="nc" id="L101"> } catch (IllegalAccessException e) {</span>
<span class="nc" id="L102"> throw new RuntimeException(&quot;We shouldn't have ever reached this point, we don't try to inject to non-accessible methods.&quot;, e);</span>
<span class="fc" id="L103"> }</span>
<span class="fc" id="L104"> }</span>
<span class="fc" id="L106"> }</span>
});
<span class="fc" id="L108"> }</span>
private static Key&lt;?&gt; getMappedKey(Injector injector, Key&lt;?&gt; key) {
<span class="fc" id="L111"> Map&lt;TypeLiteral, BeanTypeKey&gt; beanTypeMap = getBeanTypeMap(injector);</span>
<span class="fc bfc" id="L112" title="All 4 branches covered."> if(key.getAnnotation() == null &amp;&amp; beanTypeMap.containsKey(key.getTypeLiteral())) {</span>
<span class="fc" id="L113"> return beanTypeMap.get(key.getTypeLiteral()).key;</span>
} else {
<span class="fc" id="L115"> return key;</span>
}
}
@SuppressWarnings({&quot;unchecked&quot;})
private static Map&lt;TypeLiteral, BeanTypeKey&gt; getBeanTypeMap(Injector injector) {
<span class="fc" id="L121"> return (Map&lt;TypeLiteral, BeanTypeKey&gt;) injector.getInstance(MAP_KEY);</span>
}
private static Key&lt;?&gt; createDependencyKey(PropertyDescriptor propertyDescriptor, Type propertyType) {
<span class="fc bfc" id="L125" title="All 2 branches covered."> if(requiresName(propertyType)) {</span>
<span class="fc" id="L126"> return Key.get(propertyType, Names.named(&quot;shiro.&quot; + propertyDescriptor.getName()));</span>
} else {
<span class="fc" id="L128"> return Key.get(propertyType);</span>
}
}
private static boolean requiresName(Type propertyType) {
<span class="fc bfc" id="L133" title="All 2 branches covered."> if (propertyType instanceof Class) {</span>
<span class="fc" id="L134"> Class&lt;?&gt; aClass = (Class&lt;?&gt;) propertyType;</span>
<span class="pc bpc" id="L135" 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="L137"> return false;</span>
}
}
static void ensureBeanTypeMapExists(Binder binder) {
<span class="fc" id="L142"> beanTypeMapBinding(binder).addBinding(TypeLiteral.get(BeanTypeKey.class)).toInstance(new BeanTypeKey(null));</span>
<span class="fc" id="L143"> }</span>
static &lt;T&gt; void bindBeanType(Binder binder, TypeLiteral&lt;T&gt; typeLiteral, Key&lt;? extends T&gt; key) {
<span class="fc" id="L146"> beanTypeMapBinding(binder).addBinding(typeLiteral).toInstance(new BeanTypeKey(key));</span>
<span class="fc" id="L147"> }</span>
private static MapBinder&lt;TypeLiteral, BeanTypeKey&gt; beanTypeMapBinding(Binder binder) {
<span class="fc" id="L150"> return MapBinder.newMapBinder(binder, TypeLiteral.class, BeanTypeKey.class, Names.named(BEAN_TYPE_MAP_NAME));</span>
}
private static class BeanTypeKey {
Key&lt;?&gt; key;
<span class="fc" id="L156"> private BeanTypeKey(Key&lt;?&gt; key) {</span>
<span class="fc" id="L157"> this.key = key;</span>
<span class="fc" id="L158"> }</span>
}
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.eclemma.org/jacoco">JaCoCo</a> 0.7.7.201606060606</span></div></body></html>