1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.shiro.guice; |
20 | |
|
21 | |
import com.google.common.collect.Sets; |
22 | |
import com.google.inject.Key; |
23 | |
import com.google.inject.PrivateModule; |
24 | |
import com.google.inject.TypeLiteral; |
25 | |
import com.google.inject.binder.AnnotatedBindingBuilder; |
26 | |
import com.google.inject.binder.LinkedBindingBuilder; |
27 | |
import com.google.inject.multibindings.Multibinder; |
28 | |
import com.google.inject.util.Types; |
29 | |
import org.apache.shiro.config.ConfigurationException; |
30 | |
import org.apache.shiro.env.Environment; |
31 | |
import org.apache.shiro.mgt.DefaultSecurityManager; |
32 | |
import org.apache.shiro.mgt.SecurityManager; |
33 | |
import org.apache.shiro.realm.Realm; |
34 | |
import org.apache.shiro.session.mgt.DefaultSessionManager; |
35 | |
import org.apache.shiro.session.mgt.SessionManager; |
36 | |
import org.apache.shiro.util.Destroyable; |
37 | |
|
38 | |
import javax.annotation.PreDestroy; |
39 | |
import java.util.Collection; |
40 | |
import java.util.Set; |
41 | |
import java.util.WeakHashMap; |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | 11 | public abstract class ShiroModule extends PrivateModule implements Destroyable { |
50 | |
|
51 | 11 | private Set<Destroyable> destroyables = Sets.newSetFromMap(new WeakHashMap<Destroyable, Boolean>()); |
52 | |
|
53 | |
public void configure() { |
54 | |
|
55 | 11 | bindSecurityManager(bind(SecurityManager.class)); |
56 | 11 | bindSessionManager(bind(SessionManager.class)); |
57 | 11 | bindEnvironment(bind(Environment.class)); |
58 | 11 | bindListener(BeanTypeListener.MATCHER, new BeanTypeListener()); |
59 | 11 | final DestroyableInjectionListener.DestroyableRegistry registry = new DestroyableInjectionListener.DestroyableRegistry() { |
60 | |
public void add(Destroyable destroyable) { |
61 | 20 | ShiroModule.this.add(destroyable); |
62 | 20 | } |
63 | |
|
64 | |
@PreDestroy |
65 | |
public void destroy() throws Exception { |
66 | 0 | ShiroModule.this.destroy(); |
67 | 0 | } |
68 | |
}; |
69 | 11 | bindListener(LifecycleTypeListener.MATCHER, new LifecycleTypeListener(registry)); |
70 | |
|
71 | 11 | expose(SecurityManager.class); |
72 | |
|
73 | 11 | configureShiro(); |
74 | 11 | bind(realmCollectionKey()) |
75 | |
.to(realmSetKey()); |
76 | |
|
77 | 11 | bind(DestroyableInjectionListener.DestroyableRegistry.class).toInstance(registry); |
78 | 11 | BeanTypeListener.ensureBeanTypeMapExists(binder()); |
79 | 11 | } |
80 | |
|
81 | |
@SuppressWarnings({"unchecked"}) |
82 | |
private Key<Set<Realm>> realmSetKey() { |
83 | 11 | return (Key<Set<Realm>>) Key.get(TypeLiteral.get(Types.setOf(Realm.class))); |
84 | |
} |
85 | |
|
86 | |
@SuppressWarnings({"unchecked"}) |
87 | |
private Key<Collection<Realm>> realmCollectionKey() { |
88 | 11 | return (Key<Collection<Realm>>) Key.get(Types.newParameterizedType(Collection.class, Realm.class)); |
89 | |
} |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
protected abstract void configureShiro(); |
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
protected final LinkedBindingBuilder<Realm> bindRealm() { |
103 | 11 | Multibinder<Realm> multibinder = Multibinder.newSetBinder(binder(), Realm.class); |
104 | 11 | return multibinder.addBinding(); |
105 | |
} |
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
protected void bindSecurityManager(AnnotatedBindingBuilder<? super SecurityManager> bind) { |
115 | |
try { |
116 | 5 | bind.toConstructor(DefaultSecurityManager.class.getConstructor(Collection.class)).asEagerSingleton(); |
117 | 0 | } catch (NoSuchMethodException e) { |
118 | 0 | throw new ConfigurationException("This really shouldn't happen. Either something has changed in Shiro, or there's a bug in " + ShiroModule.class.getSimpleName(), e); |
119 | 5 | } |
120 | 5 | } |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
protected void bindSessionManager(AnnotatedBindingBuilder<SessionManager> bind) { |
130 | 5 | bind.to(DefaultSessionManager.class).asEagerSingleton(); |
131 | 5 | } |
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
protected void bindEnvironment(AnnotatedBindingBuilder<Environment> bind) { |
141 | 5 | bind.to(GuiceEnvironment.class).asEagerSingleton(); |
142 | 5 | } |
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
protected final <T> void bindBeanType(TypeLiteral<T> typeLiteral, Key<? extends T> key) { |
151 | 5 | BeanTypeListener.bindBeanType(binder(), typeLiteral, key); |
152 | 5 | } |
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
public final void destroy() throws Exception { |
161 | 1 | for (Destroyable destroyable : destroyables) { |
162 | 2 | destroyable.destroy(); |
163 | |
} |
164 | 1 | } |
165 | |
|
166 | |
public void add(Destroyable destroyable) { |
167 | 20 | this.destroyables.add(destroyable); |
168 | 20 | } |
169 | |
} |