1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.objectfactory.guice;
35
36 import javax.servlet.http.HttpServletRequest;
37 import javax.servlet.http.HttpServletResponse;
38 import javax.servlet.http.HttpSession;
39
40 import com.google.inject.Provider;
41 import info.magnolia.cms.core.AggregationState;
42 import info.magnolia.cms.core.Channel;
43 import info.magnolia.context.Context;
44 import info.magnolia.context.MgnlContext;
45 import info.magnolia.context.WebContext;
46 import info.magnolia.objectfactory.annotation.LazySingleton;
47 import info.magnolia.objectfactory.annotation.LocalScoped;
48 import info.magnolia.objectfactory.annotation.SessionScoped;
49
50
51
52
53
54
55 public class GuiceContextAndScopesConfigurer extends AbstractGuiceComponentConfigurer {
56
57 @Override
58 protected void configure() {
59
60
61 bind(Context.class).toProvider(new Provider<Context>() {
62 @Override
63 public Context get() {
64 return MgnlContext.getInstance();
65 }
66 });
67 bind(WebContext.class).toProvider(new Provider<WebContext>() {
68 @Override
69 public WebContext get() {
70 return MgnlContext.getWebContext();
71 }
72 }).in(LocalScoped.class);
73 bind(AggregationState.class).toProvider(new Provider<AggregationState>() {
74 @Override
75 public AggregationState get() {
76 return MgnlContext.getAggregationState();
77 }
78 }).in(LocalScoped.class);
79 bind(Channel.class).toProvider(new Provider<Channel>() {
80 @Override
81 public Channel get() {
82 return MgnlContext.getAggregationState().getChannel();
83 }
84 }).in(LocalScoped.class);
85 bind(HttpSession.class).toProvider(new Provider<HttpSession>() {
86 @Override
87 public HttpSession get() {
88 return MgnlContext.getWebContext().getRequest().getSession();
89 }
90 }).in(SessionScoped.class);
91 bind(HttpServletRequest.class).toProvider(new Provider<HttpServletRequest>() {
92 @Override
93 public HttpServletRequest get() {
94 return MgnlContext.getWebContext().getRequest();
95 }
96 }).in(LocalScoped.class);
97 bind(HttpServletResponse.class).toProvider(new Provider<HttpServletResponse>() {
98 @Override
99 public HttpServletResponse get() {
100 return MgnlContext.getWebContext().getResponse();
101 }
102 }).in(LocalScoped.class);
103
104
105 bindScope(LocalScoped.class, MagnoliaScopes.LOCAL);
106 bindScope(SessionScoped.class, MagnoliaScopes.SESSION);
107 bindScope(LazySingleton.class, MagnoliaScopes.LAZY_SINGLETON);
108 }
109 }