View Javadoc

1   /**
2    * This file Copyright (c) 2003-2014 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   * @author Philipp Bracher
67   * @version $Revision$ ($Author$)
68   */
69  public abstract class AbstractContext implements Context, Serializable {
70      private static final Logger log = LoggerFactory.getLogger(AbstractContext.class);
71  
72      @Override
73      public User getUser() {
74          return Security.getSystemUser();
75      }
76  
77      @Override
78      public Subject getSubject() {
79          return Security.getSystemSubject();
80      }
81  
82      /**
83       * The locale for this context.
84       */
85      protected Locale locale;
86  
87      private AttributeStrategy attributeStrategy;
88  
89      private JCRSessionStrategy repositoryStrategy;
90  
91      public AttributeStrategy getAttributeStrategy() {
92          return attributeStrategy;
93      }
94  
95      public void setAttributeStrategy(AttributeStrategy strategy) {
96          this.attributeStrategy = strategy;
97      }
98  
99      public JCRSessionStrategy getRepositoryStrategy() {
100         return repositoryStrategy;
101     }
102 
103     public void setRepositoryStrategy(JCRSessionStrategy strategy) {
104         this.repositoryStrategy = strategy;
105     }
106 
107     @Override
108     public Object getAttribute(String name, int scope) {
109         return getAttributeStrategy().getAttribute(name, scope);
110     }
111 
112     @Override
113     public Map<String, Object> getAttributes(int scope) {
114         return getAttributeStrategy().getAttributes(scope);
115     }
116 
117     @Override
118     public void removeAttribute(String name, int scope) {
119         getAttributeStrategy().removeAttribute(name, scope);
120     }
121 
122     @Override
123     public void setAttribute(String name, Object value, int scope) {
124         getAttributeStrategy().setAttribute(name, value, scope);
125     }
126 
127     @Override
128     public AccessManager getAccessManager(String workspace) {
129         return PermissionUtil.getAccessManager(workspace, getSubject());
130     }
131 
132     @Override
133     public Session getJCRSession(String workspaceName) throws LoginException, RepositoryException {
134         return getRepositoryStrategy().getSession(workspaceName);
135     }
136 
137     /**
138      * Get the attribute value.
139      * @param name to which value is associated to
140      * @return attribute value
141      */
142     @Override
143     public Object getAttribute(String name) {
144         Object value = this.getAttribute(name, Context.LOCAL_SCOPE);
145         if (null == value) {
146             value = this.getAttribute(name, Context.SESSION_SCOPE);
147         }
148         if (null == value) {
149             value = this.getAttribute(name, Context.APPLICATION_SCOPE);
150         }
151         return value;
152     }
153 
154     /**
155      * Merge the scopes maps.
156      */
157     @Override
158     public Map<String, Object> getAttributes() {
159         final Map<String, Object> map = new HashMap<String, Object>();
160         map.putAll(this.getAttributes(Context.LOCAL_SCOPE));
161         map.putAll(this.getAttributes(Context.SESSION_SCOPE));
162         map.putAll(this.getAttributes(Context.APPLICATION_SCOPE));
163         return map;
164     }
165 
166     /**
167      * If not yet set try to get the locale of the user. Else use the locale of the system context.
168      * @see Context#getLocale()
169      */
170     @Override
171     public Locale getLocale() {
172         if (locale == null) {
173             final SystemContext sysctx = MgnlContext.getSystemContext();
174             if (this == sysctx) {
175                 // do not fall in the endless loop
176                 return null;
177             }
178             locale = sysctx.getLocale();
179         }
180         return locale;
181     }
182 
183     @Override
184     public void setLocale(Locale locale) {
185         this.locale = locale;
186     }
187 
188     /**
189      * TODO: This duplicates methods from MessagesManager : remove either.
190      */
191     @Override
192     public Messages getMessages() {
193         return getMessages(MessagesManager.DEFAULT_BASENAME);
194     }
195 
196     /**
197      * TODO: This duplicates methods from MessagesManager : remove either.
198      */
199     @Override
200     public Messages getMessages(String basename) {
201         return MessagesManager.getMessages(basename, getLocale());
202     }
203 
204     @Override
205     public HierarchyManager getHierarchyManager(String workspaceName) {
206         try {
207             return HierarchyManagerUtil.asHierarchyManager(getJCRSession(workspaceName));
208         } catch (RepositoryException e) {
209             throw new RuntimeRepositoryException(e);
210         }
211     }
212 
213     @Override
214     public QueryManager getQueryManager(String workspaceName) {
215         return this.getHierarchyManager(workspaceName).getQueryManager();
216     }
217 
218     // ------ Map interface methods -------
219     /**
220      * {@inheritDoc}
221      */
222     @Override
223     public Object get(Object key) {
224         return this.getAttribute(key.toString());
225     }
226 
227     /**
228      * {@inheritDoc}
229      */
230     @Override
231     public Object put(Object key, Object value) {
232         this.setAttribute(key.toString(), value, Context.LOCAL_SCOPE);
233         return value;
234     }
235 
236     /**
237      * {@inheritDoc}
238      */
239     @Override
240     public void clear() {
241         for (String key : this.getAttributes().keySet()) {
242             this.removeAttribute(key, Context.LOCAL_SCOPE);
243         }
244         throw new UnsupportedOperationException("you can not clear a magnolia context");
245     }
246 
247     /**
248      * This implementation is very slow!
249      */
250     @Override
251     public boolean containsValue(Object value) {
252         return this.getAttributes().containsValue(value);
253     }
254 
255     /**
256      * {@inheritDoc}
257      */
258     @Override
259     public Set<Entry<String, Object>> entrySet() {
260         return this.getAttributes().entrySet();
261     }
262 
263     /**
264      * {@inheritDoc}
265      */
266     @Override
267     public boolean isEmpty() {
268         return this.getAttributes().isEmpty();
269     }
270 
271     /**
272      * {@inheritDoc}
273      */
274     @Override
275     public Set<String> keySet() {
276         return this.getAttributes().keySet();
277     }
278 
279     /**
280      * {@inheritDoc}
281      */
282     @Override
283     public void putAll(Map map) {
284         for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) {
285             Entry entry = (Entry) iter.next();
286             this.setAttribute(entry.getKey().toString(), entry.getValue(), Context.LOCAL_SCOPE);
287         }
288     }
289 
290     /**
291      * {@inheritDoc}
292      */
293     @Override
294     public Object remove(Object key) {
295         Object obj = this.getAttribute(key.toString());
296         this.removeAttribute(key.toString(), Context.LOCAL_SCOPE);
297         return obj;
298     }
299 
300     /**
301      * {@inheritDoc}
302      */
303     @Override
304     public Collection<Object> values() {
305         return this.getAttributes().values();
306     }
307 
308     /**
309      * {@inheritDoc}
310      */
311     @Override
312     public boolean containsKey(Object arg0) {
313         return this.getAttributes().containsKey(arg0);
314     }
315 
316     /**
317      * {@inheritDoc}
318      */
319     @Override
320     public int size() {
321         return this.getAttributes().size();
322     }
323 
324     /**
325      * {@inheritDoc}
326      */
327     @Override
328     public void release() {
329         getRepositoryStrategy().release();
330     }
331 }