001/** 002 * 003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 017 * 018 **/ 019package lucee.runtime.engine; 020 021import java.util.Locale; 022import java.util.TimeZone; 023 024import lucee.runtime.PageContext; 025import lucee.runtime.config.Config; 026 027/** 028 * class to handle thread local PageContext, 029 * do use pagecontext in classes that have no method argument pagecontext 030 */ 031public final class ThreadLocalPageContext { 032 033 private static final Locale DEFAULT_LOCALE = Locale.getDefault(); 034 private static final TimeZone DEFAULT_TIMEZONE = TimeZone.getDefault(); 035 private static ThreadLocal<PageContext> pcThreadLocal=new ThreadLocal<PageContext>(); 036 037 /** 038 * register a pagecontext for he current thread 039 * @param pc PageContext to register 040 */ 041 public static void register(PageContext pc) { 042 pcThreadLocal.set(pc); 043 } 044 045 /** 046 * returns pagecontext registered for the current thread 047 * @return pagecontext for the current thread or null 048 * if no pagecontext is regisred for the current thread 049 */ 050 public static PageContext get() {//print.dumpStack(); 051 return pcThreadLocal.get(); 052 } 053 054 public static Config getConfig() { 055 PageContext pc = get(); 056 if(pc!=null) return pc.getConfig(); 057 return ThreadLocalConfig.get(); 058 059 } 060 061 /** 062 * release the pagecontext for the current thread 063 */ 064 public static void release() { 065 pcThreadLocal.set(null); 066 } 067 068 public static Config getConfig(PageContext pc) { 069 if(pc==null)return getConfig(); 070 return pc.getConfig(); 071 } 072 073 public static Config getConfig(Config config) { 074 if(config==null)return getConfig(); 075 return config; 076 } 077 078 public static TimeZone getTimeZone(PageContext pc) { 079 // pc 080 pc = get(pc); 081 if(pc!=null){ 082 if(pc.getTimeZone()!=null)return pc.getTimeZone(); 083 return DEFAULT_TIMEZONE; 084 } 085 086 // config 087 Config config = getConfig((Config)null); 088 if(config!=null && config.getTimeZone()!=null) { 089 return config.getTimeZone(); 090 } 091 return DEFAULT_TIMEZONE; 092 } 093 094 public static Locale getLocale(PageContext pc) { 095 // pc 096 pc = get(pc); 097 if(pc!=null){ 098 if(pc.getLocale()!=null)return pc.getLocale(); 099 return DEFAULT_LOCALE; 100 } 101 102 // config 103 Config config = getConfig((Config)null); 104 if(config!=null && config.getLocale()!=null) { 105 return config.getLocale(); 106 } 107 return DEFAULT_LOCALE; 108 } 109 110 public static TimeZone getTimeZone(Config config) { 111 PageContext pc = get(); 112 if(pc!=null && pc.getTimeZone()!=null) 113 return pc.getTimeZone(); 114 115 config=getConfig(config); 116 if(config!=null && config.getTimeZone()!=null) { 117 return config.getTimeZone(); 118 } 119 return DEFAULT_TIMEZONE; 120 } 121 122 public static TimeZone getTimeZone(TimeZone timezone) { 123 if(timezone!=null) return timezone; 124 return getTimeZone((PageContext)null); 125 } 126 127 public static TimeZone getTimeZone() { 128 return getTimeZone((PageContext)null); 129 } 130 131 public static PageContext get(PageContext pc) { 132 if(pc==null)return get(); 133 return pc; 134 } 135 136}