| <?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 :: Jar Bundle</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.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<Class> shiroMatcher = Matchers.inSubpackage(SHIRO_PACKAGE.getName());</span> |
| <span class="fc" id="L57"> private static Matcher<Class> shiroGuiceMatcher = Matchers.inSubpackage(SHIRO_GUICE_PACKAGE.getName());</span> |
| |
| <span class="fc" id="L59"> private static Matcher<Class> classMatcher = ShiroMatchers.ANY_PACKAGE.and(shiroMatcher.and(Matchers.not(shiroGuiceMatcher)));</span> |
| |
| <span class="fc" id="L61"> 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="L64"> static final Key<?> 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<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)); |
| |
| public <I> void hear(TypeLiteral<I> type, final TypeEncounter<I> encounter) { |
| <span class="fc" id="L78"> PropertyDescriptor propertyDescriptors[] = PropertyUtils.getPropertyDescriptors(type.getRawType());</span> |
| <span class="fc" id="L79"> final Map<PropertyDescriptor, Key<?>> propertyDependencies = new HashMap<PropertyDescriptor, Key<?>>(propertyDescriptors.length);</span> |
| <span class="fc" id="L80"> final Provider<Injector> 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 && 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<I>() {</span> |
| public void injectMembers(I instance) { |
| <span class="fc bfc" id="L89" title="All 2 branches covered."> for (Map.Entry<PropertyDescriptor, Key<?>> 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("Couldn't set property " + dependency.getKey().getDisplayName(), e);</span> |
| <span class="nc" id="L101"> } catch (IllegalAccessException e) {</span> |
| <span class="nc" id="L102"> 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="L103"> }</span> |
| <span class="fc" id="L104"> }</span> |
| |
| <span class="fc" id="L106"> }</span> |
| }); |
| <span class="fc" id="L108"> }</span> |
| |
| private static Key<?> getMappedKey(Injector injector, Key<?> key) { |
| <span class="fc" id="L111"> Map<TypeLiteral, BeanTypeKey> beanTypeMap = getBeanTypeMap(injector);</span> |
| <span class="fc bfc" id="L112" title="All 4 branches covered."> if(key.getAnnotation() == null && 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({"unchecked"}) |
| private static Map<TypeLiteral, BeanTypeKey> getBeanTypeMap(Injector injector) { |
| <span class="fc" id="L121"> return (Map<TypeLiteral, BeanTypeKey>) injector.getInstance(MAP_KEY);</span> |
| } |
| |
| private static Key<?> 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("shiro." + 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<?> aClass = (Class<?>) 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 <T> void bindBeanType(Binder binder, TypeLiteral<T> typeLiteral, Key<? extends T> key) { |
| <span class="fc" id="L146"> beanTypeMapBinding(binder).addBinding(typeLiteral).toInstance(new BeanTypeKey(key));</span> |
| <span class="fc" id="L147"> }</span> |
| |
| private static MapBinder<TypeLiteral, BeanTypeKey> 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<?> key; |
| |
| <span class="fc" id="L156"> private BeanTypeKey(Key<?> 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> |