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