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