View Javadoc

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