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: 32667 $ ($Author: gjoseph $)
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 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     /**
196      * Map implemenation
197      */
198     public Object get(Object key) {
199         return this.getAttribute(key.toString());
200     }
201 
202     /**
203      * Map implementation
204      */
205     public Object put(Object key, Object value) {
206         this.setAttribute(key.toString(), value, Context.LOCAL_SCOPE);
207         return value;
208     }
209 
210     /**
211      * Map implementation
212      */
213     public void clear() {
214         for (String key : this.getAttributes().keySet()) {
215             this.removeAttribute(key, Context.LOCAL_SCOPE);
216         }
217         throw new UnsupportedOperationException("you can not clear a magnolia context");
218     }
219 
220     /**
221      * Map implementation. This implementation is very slow
222      */
223     public boolean containsValue(Object value) {
224         return this.getAttributes().containsValue(value);
225     }
226 
227     /**
228      * Map implementation
229      */
230     public Set<Entry<String, Object>> entrySet() {
231         return this.getAttributes().entrySet();
232     }
233 
234     /**
235      * Map implementation
236      */
237     public boolean isEmpty() {
238         return this.getAttributes().isEmpty();
239     }
240 
241     /**
242      * Map implementation
243      */
244     public Set<String> keySet() {
245         return this.getAttributes().keySet();
246     }
247 
248     /**
249      * Map implementation
250      */
251     public void putAll(Map map) {
252         for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) {
253             Entry entry = (Entry) iter.next();
254             this.setAttribute(entry.getKey().toString(), entry.getValue(), Context.LOCAL_SCOPE);
255         }
256     }
257 
258     /**
259      * Map implementation
260      */
261     public Object remove(Object key) {
262         Object obj = this.getAttribute(key.toString());
263         this.removeAttribute(key.toString(), Context.LOCAL_SCOPE);
264         return obj;
265     }
266 
267     /**
268      * Map implementation
269      */
270     public Collection<Object> values() {
271         return this.getAttributes().values();
272     }
273 
274     /**
275      * Map implementation
276      */
277     public boolean containsKey(Object arg0) {
278         return this.getAttributes().containsKey(arg0);
279     }
280 
281     /**
282      * Map implementation
283      */
284     public int size() {
285         return this.getAttributes().size();
286     }
287 
288     /**
289      * {@inheritDoc}
290      */
291     public void release() {
292         getRepositoryStrategy().release();
293     }
294 }