View Javadoc
1   /**
2    * This file Copyright (c) 2003-2015 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.context;
35  
36  import info.magnolia.cms.core.HierarchyManager;
37  import info.magnolia.cms.core.search.QueryManager;
38  import info.magnolia.cms.i18n.Messages;
39  import info.magnolia.cms.i18n.MessagesManager;
40  import info.magnolia.cms.security.AccessManager;
41  import info.magnolia.cms.security.PermissionUtil;
42  import info.magnolia.cms.security.Security;
43  import info.magnolia.cms.security.User;
44  import info.magnolia.cms.util.HierarchyManagerUtil;
45  import info.magnolia.jcr.RuntimeRepositoryException;
46  
47  import java.io.Serializable;
48  import java.util.Collection;
49  import java.util.HashMap;
50  import java.util.Iterator;
51  import java.util.Locale;
52  import java.util.Map;
53  import java.util.Set;
54  
55  import javax.jcr.LoginException;
56  import javax.jcr.RepositoryException;
57  import javax.jcr.Session;
58  import javax.security.auth.Subject;
59  
60  import org.slf4j.Logger;
61  import org.slf4j.LoggerFactory;
62  
63  
64  /**
65   * Default implementation of the Context interface.
66   */
67  public abstract class AbstractContext implements Context, Serializable {
68      private static final Logger log = LoggerFactory.getLogger(AbstractContext.class);
69  
70      @Override
71      public User getUser() {
72          return Security.getSystemUser();
73      }
74  
75      @Override
76      public Subject getSubject() {
77          return Security.getSystemSubject();
78      }
79  
80      /**
81       * The locale for this context.
82       */
83      protected Locale locale;
84  
85      private AttributeStrategy attributeStrategy;
86  
87      private JCRSessionStrategy repositoryStrategy;
88  
89      public AttributeStrategy getAttributeStrategy() {
90          return attributeStrategy;
91      }
92  
93      public void setAttributeStrategy(AttributeStrategy strategy) {
94          this.attributeStrategy = strategy;
95      }
96  
97      public JCRSessionStrategy getRepositoryStrategy() {
98          return repositoryStrategy;
99      }
100 
101     public void setRepositoryStrategy(JCRSessionStrategy strategy) {
102         this.repositoryStrategy = strategy;
103     }
104 
105     @Override
106     public Object getAttribute(String name, int scope) {
107         return getAttributeStrategy().getAttribute(name, scope);
108     }
109 
110     @Override
111     public Map<String, Object> getAttributes(int scope) {
112         return getAttributeStrategy().getAttributes(scope);
113     }
114 
115     @Override
116     public void removeAttribute(String name, int scope) {
117         getAttributeStrategy().removeAttribute(name, scope);
118     }
119 
120     @Override
121     public void setAttribute(String name, Object value, int scope) {
122         getAttributeStrategy().setAttribute(name, value, scope);
123     }
124 
125     @Override
126     public AccessManager getAccessManager(String workspace) {
127         return PermissionUtil.getAccessManager(workspace, getSubject());
128     }
129 
130     @Override
131     public Session getJCRSession(String workspaceName) throws LoginException, RepositoryException {
132         return getRepositoryStrategy().getSession(workspaceName);
133     }
134 
135     /**
136      * Get the attribute value.
137      * @param name to which value is associated to
138      * @return attribute value
139      */
140     @Override
141     public Object getAttribute(String name) {
142         Object value = this.getAttribute(name, Context.LOCAL_SCOPE);
143         if (null == value) {
144             value = this.getAttribute(name, Context.SESSION_SCOPE);
145         }
146         if (null == value) {
147             value = this.getAttribute(name, Context.APPLICATION_SCOPE);
148         }
149         return value;
150     }
151 
152     /**
153      * Merge the scopes maps.
154      */
155     @Override
156     public Map<String, Object> getAttributes() {
157         final Map<String, Object> map = new HashMap<String, Object>();
158         map.putAll(this.getAttributes(Context.LOCAL_SCOPE));
159         map.putAll(this.getAttributes(Context.SESSION_SCOPE));
160         map.putAll(this.getAttributes(Context.APPLICATION_SCOPE));
161         return map;
162     }
163 
164     /**
165      * If not yet set try to get the locale of the user. Else use the locale of the system context.
166      * @see Context#getLocale()
167      */
168     @Override
169     public Locale getLocale() {
170         if (locale == null) {
171             final SystemContext sysctx = MgnlContext.getSystemContext();
172             if (this == sysctx) {
173                 // do not fall in the endless loop
174                 return null;
175             }
176             locale = sysctx.getLocale();
177         }
178         return locale;
179     }
180 
181     @Override
182     public void setLocale(Locale locale) {
183         this.locale = locale;
184     }
185 
186     /**
187      * TODO: This duplicates methods from MessagesManager : remove either.
188      */
189     @Override
190     public Messages getMessages() {
191         return getMessages(MessagesManager.DEFAULT_BASENAME);
192     }
193 
194     /**
195      * TODO: This duplicates methods from MessagesManager : remove either.
196      */
197     @Override
198     public Messages getMessages(String basename) {
199         return MessagesManager.getMessages(basename, getLocale());
200     }
201 
202     @Override
203     public HierarchyManager getHierarchyManager(String workspaceName) {
204         try {
205             return HierarchyManagerUtil.asHierarchyManager(getJCRSession(workspaceName));
206         } catch (RepositoryException e) {
207             throw new RuntimeRepositoryException(e);
208         }
209     }
210 
211     @Override
212     public QueryManager getQueryManager(String workspaceName) {
213         return this.getHierarchyManager(workspaceName).getQueryManager();
214     }
215 
216     // ------ Map interface methods -------
217     /**
218      * {@inheritDoc}
219      */
220     @Override
221     public Object get(Object key) {
222         return this.getAttribute(key.toString());
223     }
224 
225     /**
226      * {@inheritDoc}
227      */
228     @Override
229     public Object put(Object key, Object value) {
230         this.setAttribute(key.toString(), value, Context.LOCAL_SCOPE);
231         return value;
232     }
233 
234     /**
235      * {@inheritDoc}
236      */
237     @Override
238     public void clear() {
239         for (String key : this.getAttributes().keySet()) {
240             this.removeAttribute(key, Context.LOCAL_SCOPE);
241         }
242         throw new UnsupportedOperationException("you can not clear a magnolia context");
243     }
244 
245     /**
246      * This implementation is very slow!
247      */
248     @Override
249     public boolean containsValue(Object value) {
250         return this.getAttributes().containsValue(value);
251     }
252 
253     /**
254      * {@inheritDoc}
255      */
256     @Override
257     public Set<Entry<String, Object>> entrySet() {
258         return this.getAttributes().entrySet();
259     }
260 
261     /**
262      * {@inheritDoc}
263      */
264     @Override
265     public boolean isEmpty() {
266         return this.getAttributes().isEmpty();
267     }
268 
269     /**
270      * {@inheritDoc}
271      */
272     @Override
273     public Set<String> keySet() {
274         return this.getAttributes().keySet();
275     }
276 
277     /**
278      * {@inheritDoc}
279      */
280     @Override
281     public void putAll(Map map) {
282         for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) {
283             Entry entry = (Entry) iter.next();
284             this.setAttribute(entry.getKey().toString(), entry.getValue(), Context.LOCAL_SCOPE);
285         }
286     }
287 
288     /**
289      * {@inheritDoc}
290      */
291     @Override
292     public Object remove(Object key) {
293         Object obj = this.getAttribute(key.toString());
294         this.removeAttribute(key.toString(), Context.LOCAL_SCOPE);
295         return obj;
296     }
297 
298     /**
299      * {@inheritDoc}
300      */
301     @Override
302     public Collection<Object> values() {
303         return this.getAttributes().values();
304     }
305 
306     /**
307      * {@inheritDoc}
308      */
309     @Override
310     public boolean containsKey(Object arg0) {
311         return this.getAttributes().containsKey(arg0);
312     }
313 
314     /**
315      * {@inheritDoc}
316      */
317     @Override
318     public int size() {
319         return this.getAttributes().size();
320     }
321 
322     /**
323      * {@inheritDoc}
324      */
325     @Override
326     public void release() {
327         getRepositoryStrategy().release();
328     }
329 }