View Javadoc

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