1 13 package info.magnolia.context; 14 15 import info.magnolia.cms.beans.runtime.File; 16 import info.magnolia.cms.beans.runtime.MultipartForm; 17 import info.magnolia.cms.core.Content; 18 import info.magnolia.cms.core.HierarchyManager; 19 import info.magnolia.cms.core.search.QueryManager; 20 import info.magnolia.cms.i18n.Messages; 21 import info.magnolia.cms.security.AccessManager; 22 import info.magnolia.cms.security.User; 23 import info.magnolia.cms.util.FactoryUtil; 24 25 import java.util.Locale ; 26 import java.util.Map ; 27 28 import javax.servlet.http.HttpServletRequest ; 29 30 import org.apache.commons.lang.StringUtils; 31 import org.slf4j.Logger; 32 import org.slf4j.LoggerFactory; 33 34 35 45 46 public class MgnlContext { 47 48 51 public static Logger log = LoggerFactory.getLogger(MgnlContext.class); 52 53 56 private static ThreadLocal localContext = new ThreadLocal (); 57 58 61 public MgnlContext() { 62 } 63 64 68 public static User getUser() { 69 return getInstance().getUser(); 70 } 71 72 76 public static void setLocale(Locale locale) { 77 getInstance().setLocale(locale); 78 } 79 80 84 public static Locale getLocale() { 85 return getInstance().getLocale(); 86 } 87 88 public static Messages getMessages() { 89 return getInstance().getMessages(); 90 } 91 92 public static Messages getMessages(String basename) { 93 return getInstance().getMessages(basename); 94 } 95 96 100 public static void setUser(User user) { 101 getInstance().setUser(user); 102 } 103 104 109 public static HierarchyManager getHierarchyManager(String repositoryId) { 110 return getInstance().getHierarchyManager(repositoryId); 111 } 112 113 119 public static HierarchyManager getHierarchyManager(String repositoryId, String workspaceId) { 120 return getInstance().getHierarchyManager(repositoryId, workspaceId); 121 } 122 123 128 public static AccessManager getAccessManager(String repositoryId) { 129 return getInstance().getAccessManager(repositoryId); 130 } 131 132 138 public static AccessManager getAccessManager(String repositoryId, String workspaceId) { 139 return getInstance().getAccessManager(repositoryId, workspaceId); 140 } 141 142 147 public static QueryManager getQueryManager(String repositoryId) { 148 return getInstance().getQueryManager(repositoryId); 149 } 150 151 157 public static QueryManager getQueryManager(String repositoryId, String workspaceId) { 158 return getInstance().getQueryManager(repositoryId, workspaceId); 159 } 160 161 165 public static Content getActivePage() { 166 167 WebContext ctx = getWebContextIfExisting(getInstance()); 168 if (ctx != null) { 169 return ctx.getActivePage(); 170 } 171 return null; 172 } 173 174 178 public static File getFile() { 179 WebContext ctx = getWebContextIfExisting(getInstance()); 180 if (ctx != null) { 181 return ctx.getFile(); 182 } 183 return null; 184 } 185 186 190 public static MultipartForm getPostedForm() { 191 WebContext ctx = getWebContextIfExisting(getInstance()); 192 if (ctx != null) { 193 return ctx.getPostedForm(); 194 } 195 return null; 196 } 197 198 203 public static String getParameter(String name) { 204 WebContext ctx = getWebContextIfExisting(getInstance()); 205 if (ctx != null) { 206 return ctx.getParameter(name); 207 } 208 return null; 209 210 } 211 212 216 public static Map getParameters() { 217 WebContext ctx = getWebContextIfExisting(getInstance()); 218 if (ctx != null) { 219 return ctx.getParameters(); 220 } 221 return null; 222 } 223 224 227 public static String getContextPath() { 228 WebContext ctx = getWebContextIfExisting(getInstance()); 229 if (ctx != null) { 230 return ctx.getContextPath(); 231 } 232 return StringUtils.EMPTY; 233 } 234 235 240 public static void setAttribute(String name, Object value) { 241 getInstance().setAttribute(name, value, Context.LOCAL_SCOPE); 242 } 243 244 250 public static void setAttribute(String name, Object value, int scope) { 251 getInstance().setAttribute(name, value, scope); 252 } 253 254 259 public static Object getAttribute(String name) { 260 return getInstance().getAttribute(name); 261 } 262 263 269 public static Object getAttribute(String name, int scope) { 270 return getInstance().getAttribute(name, scope); 271 } 272 273 277 public static void setInstance(Context context) { 278 localContext.set(context); 279 } 280 281 285 public static Context getInstance() { 286 Context context = (Context) localContext.get(); 287 if (context == null) { 289 log.error("MgnlContext is not initialized, This could happen if the request does not go through magnolia " + 290 "default filters"); 291 log.error("See : MgnlContext.setInstance(Context context)"); 292 throw new IllegalStateException ("MgnlContext is not set for this thread"); 293 } 294 return context; 295 } 296 297 301 public static boolean hasInstance() { 302 return localContext.get() != null; 303 } 304 305 309 public static Context getSystemContext() { 310 return (SystemContext) FactoryUtil.getSingleton(SystemContext.class); 311 } 312 313 317 public static void initAsWebContext(HttpServletRequest request) { 318 WebContext ctx = (WebContext) FactoryUtil.newInstance(WebContext.class); 319 ctx.init(request); 320 setInstance(ctx); 321 } 322 323 328 private static WebContext getWebContextIfExisting(Context ctx) { 329 if (ctx instanceof WebContext) { 330 return (WebContext) ctx; 331 } 332 else if (ctx instanceof ContextDecorator) { 333 return getWebContextIfExisting(((ContextDecorator) ctx).getWrappedContext()); 334 } 335 return null; 336 } 337 } | Popular Tags |