blob: f3397d60e91e1c5793948207456705e691deec7b [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>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 :: Support :: Guice</a> &gt; <a href="index.source.html" class="el_package">org.apache.shiro.guice</a> &gt; <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
* &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.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.name.Names;
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;
/**
* 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="L64">public abstract class ShiroModule extends PrivateModule implements Destroyable {</span>
<span class="fc" id="L66"> private Set&lt;Destroyable&gt; destroyables = Collections.newSetFromMap(new WeakHashMap&lt;Destroyable, Boolean&gt;());</span>
public void configure() {
// setup security manager
<span class="fc" id="L69"> bindSecurityManager(bind(SecurityManager.class));</span>
<span class="fc" id="L70"> bindSessionManager(bind(SessionManager.class));</span>
<span class="fc" id="L71"> bindEnvironment(bind(Environment.class));</span>
<span class="fc" id="L72"> bindListener(BeanTypeListener.MATCHER, new BeanTypeListener());</span>
<span class="fc" id="L73"> bindEventBus(bind(EventBus.class));</span>
<span class="fc" id="L74"> bindListener(Matchers.any(), new SubscribedEventTypeListener());</span>
<span class="fc" id="L75"> bindListener(Matchers.any(), new EventBusAwareTypeListener());</span>
<span class="fc" id="L76"> final DestroyableInjectionListener.DestroyableRegistry registry = new DestroyableInjectionListener.DestroyableRegistry() {</span>
public void add(Destroyable destroyable) {
<span class="fc" id="L78"> ShiroModule.this.add(destroyable);</span>
<span class="fc" id="L79"> }</span>
@PreDestroy
public void destroy() throws Exception {
<span class="nc" id="L83"> ShiroModule.this.destroy();</span>
<span class="nc" id="L84"> }</span>
};
<span class="fc" id="L86"> bindListener(LifecycleTypeListener.MATCHER, new LifecycleTypeListener(registry));</span>
<span class="fc" id="L88"> expose(SecurityManager.class);</span>
<span class="fc" id="L89"> expose(EventBus.class);</span>
<span class="fc" id="L91"> configureShiro();</span>
<span class="fc" id="L92"> bind(realmCollectionKey())</span>
<span class="fc" id="L93"> .to(realmSetKey());</span>
<span class="fc" id="L95"> bind(DestroyableInjectionListener.DestroyableRegistry.class).toInstance(registry);</span>
<span class="fc" id="L96"> BeanTypeListener.ensureBeanTypeMapExists(binder());</span>
<span class="fc" id="L97"> }</span>
@SuppressWarnings({&quot;unchecked&quot;})
private Key&lt;Set&lt;Realm&gt;&gt; realmSetKey() {
<span class="fc" id="L101"> return (Key&lt;Set&lt;Realm&gt;&gt;) Key.get(TypeLiteral.get(Types.setOf(Realm.class)));</span>
}
@SuppressWarnings({&quot;unchecked&quot;})
private Key&lt;Collection&lt;Realm&gt;&gt; realmCollectionKey() {
<span class="fc" id="L106"> return (Key&lt;Collection&lt;Realm&gt;&gt;) 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&lt;Realm&gt; bindRealm() {
<span class="fc" id="L121"> Multibinder&lt;Realm&gt; multibinder = Multibinder.newSetBinder(binder(), Realm.class);</span>
<span class="fc" id="L122"> return multibinder.addBinding();</span>
}
/**
* Binds the security manager. Override this method in order to provide your own security manager binding.
* &lt;p/&gt;
* By default, a {@link org.apache.shiro.mgt.DefaultSecurityManager} is bound as an eager singleton.
*
* @param bind
*/
protected void bindSecurityManager(AnnotatedBindingBuilder&lt;? super SecurityManager&gt; bind) {
try {
<span class="fc" id="L134"> bind.toConstructor(DefaultSecurityManager.class.getConstructor(Collection.class)).asEagerSingleton();</span>
<span class="nc" id="L135"> } catch (NoSuchMethodException e) {</span>
<span class="nc" id="L136"> throw new ConfigurationException(&quot;This really shouldn't happen. Either something has changed in Shiro, or there's a bug in &quot; + ShiroModule.class.getSimpleName(), e);</span>
<span class="fc" id="L137"> }</span>
<span class="fc" id="L138"> }</span>
/**
* Binds the session manager. Override this method in order to provide your own session manager binding.
* &lt;p/&gt;
* By default, a {@link org.apache.shiro.session.mgt.DefaultSessionManager} is bound as an eager singleton.
*
* @param bind
*/
protected void bindSessionManager(AnnotatedBindingBuilder&lt;SessionManager&gt; bind) {
<span class="fc" id="L148"> bind.to(DefaultSessionManager.class).asEagerSingleton();</span>
<span class="fc" id="L149"> }</span>
/**
* Binds the environment. Override this method in order to provide your own environment binding.
* &lt;p/&gt;
* By default, a {@link GuiceEnvironment} is bound as an eager singleton.
*
* @param bind
*/
protected void bindEnvironment(AnnotatedBindingBuilder&lt;Environment&gt; bind) {
<span class="fc" id="L159"> bind.to(GuiceEnvironment.class).asEagerSingleton();</span>
<span class="fc" id="L160"> }</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 &lt;T&gt;
*/
protected final &lt;T&gt; void bindBeanType(TypeLiteral&lt;T&gt; typeLiteral, Key&lt;? extends T&gt; key) {
<span class="fc" id="L169"> BeanTypeListener.bindBeanType(binder(), typeLiteral, key);</span>
<span class="fc" id="L170"> }</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&lt;EventBus&gt; bind) {
<span class="fc" id="L178"> bind.to(DefaultEventBus.class).asEagerSingleton();</span>
<span class="fc" id="L179"> }</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() throws Exception {
<span class="fc bfc" id="L188" title="All 2 branches covered."> for (Destroyable destroyable : destroyables) {</span>
<span class="fc" id="L189"> destroyable.destroy();</span>
<span class="fc" id="L190"> }</span>
<span class="fc" id="L191"> }</span>
public void add(Destroyable destroyable) {
<span class="fc" id="L194"> this.destroyables.add(destroyable);</span>
<span class="fc" id="L195"> }</span>
<span class="fc" id="L197"> private class SubscribedEventTypeListener implements TypeListener {</span>
@Override
public &lt;I&gt; void hear(TypeLiteral&lt;I&gt; typeLiteral, TypeEncounter&lt;I&gt; typeEncounter) {
<span class="fc" id="L201"> final Provider&lt;EventBus&gt; eventBusProvider = typeEncounter.getProvider(EventBus.class);</span>
<span class="fc" id="L203"> List&lt;Method&gt; methods = ClassUtils.getAnnotatedMethods(typeLiteral.getRawType(), Subscribe.class);</span>
<span class="pc bpc" id="L204" title="1 of 4 branches missed."> if (methods != null &amp;&amp; !methods.isEmpty()) {</span>
<span class="fc" id="L205"> typeEncounter.register( new InjectionListener&lt;I&gt;() {</span>
@Override
public void afterInjection(Object o) {
<span class="fc" id="L208"> eventBusProvider.get().register(o);</span>
<span class="fc" id="L209"> }</span>
});
}
<span class="fc" id="L212"> }</span>
}
<span class="fc" id="L215"> private class EventBusAwareTypeListener implements TypeListener {</span>
@Override
public &lt;I&gt; void hear(TypeLiteral&lt;I&gt; typeLiteral, TypeEncounter&lt;I&gt; typeEncounter) {
<span class="fc" id="L219"> final Provider&lt;EventBus&gt; eventBusProvider = typeEncounter.getProvider(EventBus.class);</span>
<span class="fc bfc" id="L221" title="All 2 branches covered."> if (EventBusAware.class.isAssignableFrom(typeLiteral.getRawType())) {</span>
<span class="fc" id="L222"> typeEncounter.register( new InjectionListener&lt;I&gt;() {</span>
@Override
public void afterInjection(Object o) {
<span class="fc" id="L225"> ((EventBusAware)o).setEventBus(eventBusProvider.get());</span>
<span class="fc" id="L226"> }</span>
});
}
<span class="fc" id="L229"> }</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>