| <?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 :: 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">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.util.Collection; |
| import java.util.Collections; |
| import java.util.Set; |
| import java.util.WeakHashMap; |
| |
| import javax.annotation.PreDestroy; |
| |
| import org.apache.shiro.config.ConfigurationException; |
| import org.apache.shiro.env.Environment; |
| 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.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="L51">public abstract class ShiroModule extends PrivateModule implements Destroyable {</span> |
| |
| <span class="fc" id="L53"> private Set<Destroyable> destroyables = Collections.newSetFromMap(new WeakHashMap<Destroyable, Boolean>());</span> |
| public void configure() { |
| // setup security manager |
| <span class="fc" id="L56"> bindSecurityManager(bind(SecurityManager.class));</span> |
| <span class="fc" id="L57"> bindSessionManager(bind(SessionManager.class));</span> |
| <span class="fc" id="L58"> bindEnvironment(bind(Environment.class));</span> |
| <span class="fc" id="L59"> bindListener(BeanTypeListener.MATCHER, new BeanTypeListener());</span> |
| <span class="fc" id="L60"> final DestroyableInjectionListener.DestroyableRegistry registry = new DestroyableInjectionListener.DestroyableRegistry() {</span> |
| public void add(Destroyable destroyable) { |
| <span class="fc" id="L62"> ShiroModule.this.add(destroyable);</span> |
| <span class="fc" id="L63"> }</span> |
| |
| @PreDestroy |
| public void destroy() throws Exception { |
| <span class="nc" id="L67"> ShiroModule.this.destroy();</span> |
| <span class="nc" id="L68"> }</span> |
| }; |
| <span class="fc" id="L70"> bindListener(LifecycleTypeListener.MATCHER, new LifecycleTypeListener(registry));</span> |
| |
| <span class="fc" id="L72"> expose(SecurityManager.class);</span> |
| |
| <span class="fc" id="L74"> configureShiro();</span> |
| <span class="fc" id="L75"> bind(realmCollectionKey())</span> |
| <span class="fc" id="L76"> .to(realmSetKey());</span> |
| |
| <span class="fc" id="L78"> bind(DestroyableInjectionListener.DestroyableRegistry.class).toInstance(registry);</span> |
| <span class="fc" id="L79"> BeanTypeListener.ensureBeanTypeMapExists(binder());</span> |
| <span class="fc" id="L80"> }</span> |
| |
| @SuppressWarnings({"unchecked"}) |
| private Key<Set<Realm>> realmSetKey() { |
| <span class="fc" id="L84"> return (Key<Set<Realm>>) Key.get(TypeLiteral.get(Types.setOf(Realm.class)));</span> |
| } |
| |
| @SuppressWarnings({"unchecked"}) |
| private Key<Collection<Realm>> realmCollectionKey() { |
| <span class="fc" id="L89"> 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="L104"> Multibinder<Realm> multibinder = Multibinder.newSetBinder(binder(), Realm.class);</span> |
| <span class="fc" id="L105"> 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="L117"> bind.toConstructor(DefaultSecurityManager.class.getConstructor(Collection.class)).asEagerSingleton();</span> |
| <span class="nc" id="L118"> } catch (NoSuchMethodException e) {</span> |
| <span class="nc" id="L119"> 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="L120"> }</span> |
| <span class="fc" id="L121"> }</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="L131"> bind.to(DefaultSessionManager.class).asEagerSingleton();</span> |
| <span class="fc" id="L132"> }</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="L142"> bind.to(GuiceEnvironment.class).asEagerSingleton();</span> |
| <span class="fc" id="L143"> }</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="L152"> BeanTypeListener.bindBeanType(binder(), typeLiteral, key);</span> |
| <span class="fc" id="L153"> }</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="L162" title="All 2 branches covered."> for (Destroyable destroyable : destroyables) {</span> |
| <span class="fc" id="L163"> destroyable.destroy();</span> |
| <span class="fc" id="L164"> }</span> |
| <span class="fc" id="L165"> }</span> |
| |
| public void add(Destroyable destroyable) { |
| <span class="fc" id="L168"> this.destroyables.add(destroyable);</span> |
| <span class="fc" id="L169"> }</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> |