1 13 package info.magnolia.context; 14 15 import info.magnolia.cms.beans.config.ContentRepository; 16 import info.magnolia.cms.core.HierarchyManager; 17 import info.magnolia.cms.core.search.QueryManager; 18 import info.magnolia.cms.i18n.Messages; 19 import info.magnolia.cms.i18n.MessagesManager; 20 import info.magnolia.cms.security.AccessManager; 21 import info.magnolia.cms.security.Security; 22 import info.magnolia.cms.security.User; 23 24 import java.util.Collection ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.Locale ; 28 import java.util.Map ; 29 import java.util.Set ; 30 31 import org.slf4j.Logger; 32 import org.slf4j.LoggerFactory; 33 34 35 40 public abstract class AbstractContext implements Context { 41 42 45 private static Logger log = LoggerFactory.getLogger(AbstractContext.class); 46 47 50 protected User user; 51 52 55 private Locale locale; 56 57 62 public Object getAttribute(String name) { 63 Object value = this.getAttribute(name, Context.LOCAL_SCOPE); 64 if (null == value) { 65 value = this.getAttribute(name, Context.SESSION_SCOPE); 66 } 67 if (null == value) { 68 value = this.getAttribute(name, Context.APPLICATION_SCOPE); 69 } 70 return value; 71 } 72 73 76 public Map getAttributes() { 77 Map map = new HashMap (); 78 map.putAll(this.getAttributes(Context.LOCAL_SCOPE)); 79 map.putAll(this.getAttributes(Context.SESSION_SCOPE)); 80 map.putAll(this.getAttributes(Context.APPLICATION_SCOPE)); 81 return map; 82 } 83 84 88 public void setUser(User user) { 89 this.user = user; 90 setLocale(new Locale (user.getLanguage())); 91 } 92 93 98 public User getUser() { 99 if (this.user == null) { 100 log.debug("JAAS Subject is null, returning Anonymous user"); 101 this.user = Security.getUserManager().getAnonymousUser(); 102 } 103 return this.user; 104 } 105 106 110 public Locale getLocale() { 111 if (locale == null) { 112 User user = this.getUser(); 113 if (user != null) { 114 locale = new Locale (user.getLanguage()); 115 } 116 if (locale == null) { 117 locale = MgnlContext.getSystemContext().getLocale(); 118 } 119 } 120 121 return locale; 122 } 123 124 public void setLocale(Locale locale) { 125 this.locale = locale; 126 } 127 128 public Messages getMessages() { 129 return getMessages(MessagesManager.DEFAULT_BASENAME); 130 } 131 132 public Messages getMessages(String basename) { 133 return MessagesManager.getMessages(basename, getLocale()); 134 } 135 136 public HierarchyManager getHierarchyManager(String repositoryId) { 137 return this.getHierarchyManager(repositoryId, ContentRepository.getDefaultWorkspace(repositoryId)); 138 } 139 140 public AccessManager getAccessManager(String repositoryId) { 141 return this.getAccessManager(repositoryId, ContentRepository.getDefaultWorkspace(repositoryId)); 142 } 143 144 public QueryManager getQueryManager(String repositoryId) { 145 return this.getQueryManager(repositoryId, ContentRepository.getDefaultWorkspace(repositoryId)); 146 } 147 148 151 public Object get(Object key) { 152 return this.getAttribute(key.toString()); 153 } 154 155 158 public Object put(Object key, Object value) { 159 this.setAttribute(key.toString(), value, Context.LOCAL_SCOPE); 160 return value; 161 } 162 163 166 public void clear() { 167 for (Iterator iter = this.getAttributes().keySet().iterator(); iter.hasNext();) { 168 String key = (String ) iter.next(); 169 this.removeAttribute(key, Context.LOCAL_SCOPE); 170 171 } 172 throw new UnsupportedOperationException ("you can not clear a magnolia context"); 173 } 174 175 178 public boolean containsValue(Object value) { 179 return this.getAttributes().containsValue(value); 180 } 181 182 185 public Set entrySet() { 186 return this.getAttributes().entrySet(); 187 } 188 189 192 public boolean isEmpty() { 193 return this.getAttributes().isEmpty(); 194 } 195 196 199 public Set keySet() { 200 return this.getAttributes().keySet(); 201 } 202 203 206 public void putAll(Map map) { 207 for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) { 208 Entry entry = (Entry) iter.next(); 209 this.setAttribute(entry.getKey().toString(), entry.getValue(), Context.LOCAL_SCOPE); 210 } 211 } 212 213 216 public Object remove(Object key) { 217 Object obj = this.getAttribute(key.toString()); 218 this.removeAttribute(key.toString(), Context.LOCAL_SCOPE); 219 return obj; 220 } 221 222 225 public Collection values() { 226 return this.getAttributes().values(); 227 } 228 229 232 public boolean containsKey(Object arg0) { 233 return this.getAttributes().containsKey(arg0); 234 } 235 236 239 public int size() { 240 return this.getAttributes().size(); 241 } 242 243 } 244 | Popular Tags |