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.context;
35
36 import info.magnolia.cms.i18n.MessagesManager;
37
38 import java.lang.ref.WeakReference;
39 import java.util.Locale;
40
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44
45
46
47
48 public abstract class AbstractSystemContext extends AbstractContext implements SystemContext {
49
50 private static final Logger log = LoggerFactory.getLogger(AbstractSystemContext.class);
51
52 protected static ThreadLocal<JCRSessionStrategy> repositoryStrategyThreadLocal = new ThreadLocal<JCRSessionStrategy>();
53
54 protected static ThreadLocal<WeakReference<Context>> originalContextThreadLocal = new ThreadLocal<>();
55
56
57
58
59 public AbstractSystemContext() {
60 setAttributeStrategy(new MapAttributeStrategy());
61 }
62
63 @Override
64 public void setAttribute(String name, Object value, int scope) {
65 if (scope == Context.LOCAL_SCOPE || scope == Context.SESSION_SCOPE) {
66 log.warn("you should not set an attribute in the system context in request or session scope. You are setting {}={}", name, value);
67 }
68 super.setAttribute(name, value, scope);
69 }
70
71 public void removeAttribute(String name, Object value, int scope) {
72 if (scope == Context.LOCAL_SCOPE || scope == Context.SESSION_SCOPE) {
73 log.warn("you should not manipulate an attribute in the system context in request or session scope. You are setting {}={}", name, value);
74 }
75 super.removeAttribute(name, scope);
76 }
77
78
79 @Override
80 public Locale getLocale() {
81 return MessagesManager.getInstance().getDefaultLocale();
82 }
83
84 @Override
85 public Context getOriginalContext() {
86 WeakReference<Context> ref = originalContextThreadLocal.get();
87 return ref == null ? null : ref.get();
88 }
89
90 public void setOriginalContext(Context ctx) {
91 if (ctx == null) {
92 originalContextThreadLocal.set(null);
93 } else {
94 originalContextThreadLocal.set(new WeakReference<Context>(ctx));
95 }
96 }
97 }