001    package railo.runtime.engine;
002    
003    import java.util.Locale;
004    import java.util.TimeZone;
005    
006    import railo.runtime.PageContext;
007    import railo.runtime.config.Config;
008    
009    /**
010     * class to handle thread local PageContext, 
011     * do use pagecontext in classes that have no method argument pagecontext
012     */
013    public final class ThreadLocalPageContext {
014    
015            private static final Locale DEFAULT_LOCALE = Locale.getDefault();
016            private static final TimeZone DEFAULT_TIMEZONE = TimeZone.getDefault();
017            private static ThreadLocal<PageContext> pcThreadLocal=new ThreadLocal<PageContext>();
018    
019            /**
020             * register a pagecontext for he current thread
021             * @param pc PageContext to register
022             */
023            public static void register(PageContext pc) {
024                    pcThreadLocal.set(pc);
025            }
026    
027            /**
028             * returns pagecontext registered for the current thread
029             * @return pagecontext for the current thread or null 
030             * if no pagecontext is regisred for the current thread
031             */
032            public static PageContext get() {//print.dumpStack();
033                    return pcThreadLocal.get();
034            }
035            
036            public static Config getConfig() {
037                    PageContext pc = get();
038                    if(pc!=null) return pc.getConfig();
039                    return ThreadLocalConfig.get();
040                    
041            }
042    
043            /**
044             * release the pagecontext for the current thread
045             */
046            public static void release() {
047                    pcThreadLocal.set(null);
048            }
049    
050            public static Config getConfig(PageContext pc) {
051                    if(pc==null)return getConfig();
052                return pc.getConfig();
053            }
054    
055            public static Config getConfig(Config config) {
056                    if(config==null)return getConfig();
057                return config;
058            }
059            
060            public static TimeZone getTimeZone(PageContext pc) {
061                    // pc
062                    pc = get(pc);
063                    if(pc!=null){
064                            if(pc.getTimeZone()!=null)return  pc.getTimeZone();
065                            return DEFAULT_TIMEZONE;
066                    }
067                    
068                    // config
069                    Config config = getConfig((Config)null);
070                    if(config!=null && config.getTimeZone()!=null) {
071                            return config.getTimeZone();
072                    }
073                return DEFAULT_TIMEZONE;
074            }
075            
076            public static Locale getLocale(PageContext pc) {
077                    // pc
078                    pc = get(pc);
079                    if(pc!=null){
080                            if(pc.getLocale()!=null)return  pc.getLocale();
081                            return DEFAULT_LOCALE;
082                    }
083                    
084                    // config
085                    Config config = getConfig((Config)null);
086                    if(config!=null && config.getLocale()!=null) {
087                            return config.getLocale();
088                    }
089                return DEFAULT_LOCALE;
090            }
091            
092            public static TimeZone getTimeZone(Config config) {
093                    PageContext pc = get();
094                    if(pc!=null && pc.getTimeZone()!=null)
095                            return  pc.getTimeZone();
096                            
097                    config=getConfig(config);
098                    if(config!=null && config.getTimeZone()!=null) {
099                            return config.getTimeZone();
100                    }
101                return DEFAULT_TIMEZONE;
102            }
103            
104            public static TimeZone getTimeZone(TimeZone timezone) {
105                    if(timezone!=null) return timezone;
106                return getTimeZone((PageContext)null);
107            }
108            
109            public static TimeZone getTimeZone() {
110                    return getTimeZone((PageContext)null);
111            }
112    
113            public static PageContext get(PageContext pc) {
114                    if(pc==null)return get();
115                return pc;
116            }
117    
118    }