| <?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>ShiroModule.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 :: Test Coverage</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">ShiroModule.java</span></div><h1>ShiroModule.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.lang.reflect.Method; |
| import java.util.Collection; |
| import java.util.Collections; |
| import java.util.List; |
| import java.util.Set; |
| import java.util.WeakHashMap; |
| |
| import javax.annotation.PreDestroy; |
| |
| import com.google.inject.Provider; |
| import com.google.inject.matcher.Matchers; |
| import com.google.inject.spi.InjectionListener; |
| import com.google.inject.spi.TypeEncounter; |
| import com.google.inject.spi.TypeListener; |
| import org.apache.shiro.config.ConfigurationException; |
| import org.apache.shiro.env.Environment; |
| import org.apache.shiro.event.EventBus; |
| import org.apache.shiro.event.EventBusAware; |
| import org.apache.shiro.event.Subscribe; |
| import org.apache.shiro.event.support.DefaultEventBus; |
| import org.apache.shiro.mgt.DefaultSecurityManager; |
| import org.apache.shiro.mgt.SecurityManager; |
| import org.apache.shiro.realm.Realm; |
| import org.apache.shiro.session.mgt.DefaultSessionManager; |
| import org.apache.shiro.session.mgt.SessionManager; |
| import org.apache.shiro.util.ClassUtils; |
| import org.apache.shiro.util.Destroyable; |
| |
| import com.google.inject.Key; |
| import com.google.inject.PrivateModule; |
| import com.google.inject.TypeLiteral; |
| import com.google.inject.binder.AnnotatedBindingBuilder; |
| import com.google.inject.binder.LinkedBindingBuilder; |
| import com.google.inject.multibindings.Multibinder; |
| import com.google.inject.util.Types; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| |
| /** |
| * Sets up Shiro lifecycles within Guice, enables the injecting of Shiro objects, and binds a default |
| * {@link org.apache.shiro.mgt.SecurityManager} and {@link org.apache.shiro.session.mgt.SessionManager}. At least one realm must be added by using |
| * {@link #bindRealm() bindRealm}. |
| */ |
| <span class="fc" id="L65">public abstract class ShiroModule extends PrivateModule implements Destroyable {</span> |
| |
| <span class="fc" id="L67"> private final Logger log = LoggerFactory.getLogger(ShiroModule.class);</span> |
| |
| <span class="fc" id="L69"> private Set<Destroyable> destroyables = Collections.newSetFromMap(new WeakHashMap<Destroyable, Boolean>());</span> |
| public void configure() { |
| // setup security manager |
| <span class="fc" id="L72"> bindSecurityManager(bind(SecurityManager.class));</span> |
| <span class="fc" id="L73"> bindSessionManager(bind(SessionManager.class));</span> |
| <span class="fc" id="L74"> bindEnvironment(bind(Environment.class));</span> |
| <span class="fc" id="L75"> bindListener(BeanTypeListener.MATCHER, new BeanTypeListener());</span> |
| <span class="fc" id="L76"> bindEventBus(bind(EventBus.class));</span> |
| <span class="fc" id="L77"> bindListener(Matchers.any(), new SubscribedEventTypeListener());</span> |
| <span class="fc" id="L78"> bindListener(Matchers.any(), new EventBusAwareTypeListener());</span> |
| <span class="fc" id="L79"> final DestroyableInjectionListener.DestroyableRegistry registry = new DestroyableInjectionListener.DestroyableRegistry() {</span> |
| public void add(Destroyable destroyable) { |
| <span class="fc" id="L81"> ShiroModule.this.add(destroyable);</span> |
| <span class="fc" id="L82"> }</span> |
| |
| @PreDestroy |
| public void destroy() { |
| <span class="nc" id="L86"> ShiroModule.this.destroy();</span> |
| <span class="nc" id="L87"> }</span> |
| }; |
| <span class="fc" id="L89"> bindListener(LifecycleTypeListener.MATCHER, new LifecycleTypeListener(registry));</span> |
| |
| <span class="fc" id="L91"> expose(SecurityManager.class);</span> |
| <span class="fc" id="L92"> expose(EventBus.class);</span> |
| |
| <span class="fc" id="L94"> configureShiro();</span> |
| <span class="fc" id="L95"> bind(realmCollectionKey())</span> |
| <span class="fc" id="L96"> .to(realmSetKey());</span> |
| |
| <span class="fc" id="L98"> bind(DestroyableInjectionListener.DestroyableRegistry.class).toInstance(registry);</span> |
| <span class="fc" id="L99"> BeanTypeListener.ensureBeanTypeMapExists(binder());</span> |
| <span class="fc" id="L100"> }</span> |
| |
| @SuppressWarnings({"unchecked"}) |
| private Key<Set<Realm>> realmSetKey() { |
| <span class="fc" id="L104"> return (Key<Set<Realm>>) Key.get(TypeLiteral.get(Types.setOf(Realm.class)));</span> |
| } |
| |
| @SuppressWarnings({"unchecked"}) |
| private Key<Collection<Realm>> realmCollectionKey() { |
| <span class="fc" id="L109"> return (Key<Collection<Realm>>) Key.get(Types.newParameterizedType(Collection.class, Realm.class));</span> |
| } |
| |
| /** |
| * Implement this method in order to configure your realms and any other Shiro customization you may need. |
| */ |
| protected abstract void configureShiro(); |
| |
| /** |
| * This is the preferred manner to bind a realm. The {@link org.apache.shiro.mgt.SecurityManager} will be injected with any Realm bound |
| * with this method. |
| * |
| * @return a binding builder for a realm |
| */ |
| protected final LinkedBindingBuilder<Realm> bindRealm() { |
| <span class="fc" id="L124"> Multibinder<Realm> multibinder = Multibinder.newSetBinder(binder(), Realm.class);</span> |
| <span class="fc" id="L125"> return multibinder.addBinding();</span> |
| } |
| |
| /** |
| * Binds the security manager. Override this method in order to provide your own security manager binding. |
| * <p/> |
| * By default, a {@link org.apache.shiro.mgt.DefaultSecurityManager} is bound as an eager singleton. |
| * |
| * @param bind |
| */ |
| protected void bindSecurityManager(AnnotatedBindingBuilder<? super SecurityManager> bind) { |
| try { |
| <span class="fc" id="L137"> bind.toConstructor(DefaultSecurityManager.class.getConstructor(Collection.class)).asEagerSingleton();</span> |
| <span class="nc" id="L138"> } catch (NoSuchMethodException e) {</span> |
| <span class="nc" id="L139"> throw new ConfigurationException("This really shouldn't happen. Either something has changed in Shiro, or there's a bug in " + ShiroModule.class.getSimpleName(), e);</span> |
| <span class="fc" id="L140"> }</span> |
| <span class="fc" id="L141"> }</span> |
| |
| /** |
| * Binds the session manager. Override this method in order to provide your own session manager binding. |
| * <p/> |
| * By default, a {@link org.apache.shiro.session.mgt.DefaultSessionManager} is bound as an eager singleton. |
| * |
| * @param bind |
| */ |
| protected void bindSessionManager(AnnotatedBindingBuilder<SessionManager> bind) { |
| <span class="fc" id="L151"> bind.to(DefaultSessionManager.class).asEagerSingleton();</span> |
| <span class="fc" id="L152"> }</span> |
| |
| /** |
| * Binds the environment. Override this method in order to provide your own environment binding. |
| * <p/> |
| * By default, a {@link GuiceEnvironment} is bound as an eager singleton. |
| * |
| * @param bind |
| */ |
| protected void bindEnvironment(AnnotatedBindingBuilder<Environment> bind) { |
| <span class="fc" id="L162"> bind.to(GuiceEnvironment.class).asEagerSingleton();</span> |
| <span class="fc" id="L163"> }</span> |
| |
| /** |
| * Binds a key to use for injecting setters in shiro classes. |
| * @param typeLiteral the bean property type |
| * @param key the key to use to satisfy the bean property dependency |
| * @param <T> |
| */ |
| protected final <T> void bindBeanType(TypeLiteral<T> typeLiteral, Key<? extends T> key) { |
| <span class="fc" id="L172"> BeanTypeListener.bindBeanType(binder(), typeLiteral, key);</span> |
| <span class="fc" id="L173"> }</span> |
| |
| /** |
| * Binds the EventBus. Override this method in order to provide your own {@link EventBus} binding. |
| * @param bind |
| * @since 1.4 |
| */ |
| protected void bindEventBus(AnnotatedBindingBuilder<EventBus> bind) { |
| <span class="fc" id="L181"> bind.to(DefaultEventBus.class).asEagerSingleton();</span> |
| <span class="fc" id="L182"> }</span> |
| |
| /** |
| * Destroys all beans created within this module that implement {@link org.apache.shiro.util.Destroyable}. Should be called when this |
| * module will no longer be used. |
| * |
| * @throws Exception |
| */ |
| public final void destroy() { |
| <span class="fc bfc" id="L191" title="All 2 branches covered."> for (Destroyable destroyable : destroyables) {</span> |
| try { |
| <span class="fc" id="L193"> destroyable.destroy();</span> |
| } |
| <span class="nc" id="L195"> catch(Exception e) {</span> |
| <span class="nc" id="L196"> log.warn("Error destroying component class: " + destroyable.getClass(), e);</span> |
| <span class="fc" id="L197"> }</span> |
| <span class="fc" id="L198"> }</span> |
| <span class="fc" id="L199"> }</span> |
| |
| public void add(Destroyable destroyable) { |
| <span class="fc" id="L202"> this.destroyables.add(destroyable);</span> |
| <span class="fc" id="L203"> }</span> |
| |
| <span class="fc" id="L205"> private class SubscribedEventTypeListener implements TypeListener {</span> |
| @Override |
| public <I> void hear(TypeLiteral<I> typeLiteral, TypeEncounter<I> typeEncounter) { |
| |
| <span class="fc" id="L209"> final Provider<EventBus> eventBusProvider = typeEncounter.getProvider(EventBus.class);</span> |
| |
| <span class="fc" id="L211"> List<Method> methods = ClassUtils.getAnnotatedMethods(typeLiteral.getRawType(), Subscribe.class);</span> |
| <span class="pc bpc" id="L212" title="1 of 4 branches missed."> if (methods != null && !methods.isEmpty()) {</span> |
| <span class="fc" id="L213"> typeEncounter.register( new InjectionListener<I>() {</span> |
| @Override |
| public void afterInjection(Object o) { |
| <span class="fc" id="L216"> eventBusProvider.get().register(o);</span> |
| <span class="fc" id="L217"> }</span> |
| }); |
| } |
| <span class="fc" id="L220"> }</span> |
| } |
| |
| <span class="fc" id="L223"> private class EventBusAwareTypeListener implements TypeListener {</span> |
| @Override |
| public <I> void hear(TypeLiteral<I> typeLiteral, TypeEncounter<I> typeEncounter) { |
| |
| <span class="fc" id="L227"> final Provider<EventBus> eventBusProvider = typeEncounter.getProvider(EventBus.class);</span> |
| |
| <span class="fc bfc" id="L229" title="All 2 branches covered."> if (EventBusAware.class.isAssignableFrom(typeLiteral.getRawType())) {</span> |
| <span class="fc" id="L230"> typeEncounter.register( new InjectionListener<I>() {</span> |
| @Override |
| public void afterInjection(Object o) { |
| <span class="fc" id="L233"> ((EventBusAware)o).setEventBus(eventBusProvider.get());</span> |
| <span class="fc" id="L234"> }</span> |
| }); |
| } |
| <span class="fc" id="L237"> }</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> |