001    package railo.runtime.type.scope;
002    
003    import railo.commons.io.SystemUtil;
004    import railo.runtime.Info;
005    import railo.runtime.PageContext;
006    import railo.runtime.engine.ThreadLocalPageContext;
007    import railo.runtime.exp.ExpressionException;
008    import railo.runtime.exp.PageException;
009    import railo.runtime.i18n.LocaleFactory;
010    import railo.runtime.type.Collection;
011    import railo.runtime.type.KeyImpl;
012    import railo.runtime.type.ReadOnlyStruct;
013    import railo.runtime.type.SharedScope;
014    import railo.runtime.type.dt.DateTimeImpl;
015    
016    
017    /**
018     * Server Scope
019     */
020    public final class ServerImpl extends ScopeSupport implements Server,SharedScope {
021    
022    
023    
024            private static final DateTimeImpl expired=new DateTimeImpl(2145913200000L,false);
025    
026    
027            private static final Key PRODUCT_NAME = KeyImpl.intern("productname");
028            private static final Key PRODUCT_LEVEL = KeyImpl.intern("productlevel");
029        private static final Key PRODUCT_CONTEXT_COUNT = KeyImpl.intern("productcontextcount");
030        private static final Key PRODUCT_VERSION = KeyImpl.intern("productversion");
031        private static final Key SERIAL_NUMBER = KeyImpl.intern("serialnumber");
032        private static final Key APP_SERVER = KeyImpl.intern("appserver");
033        private static final Key EXPIRATION = KeyImpl.intern("expiration");
034        private static final Key INSTALL_KIT = KeyImpl.intern("installkit");
035        private static final Key ROOT_DIR = KeyImpl.intern("rootdir");
036        private static final Key SUPPORTED_LOCALES = KeyImpl.intern("supportedlocales");
037        private static final Key  COLDFUSION= KeyImpl.intern("coldfusion");
038        private static final Key  SERVLET= KeyImpl.intern("servlet");
039        private static final Key  ARCH= KeyImpl.intern("arch");
040        private static final Key  ARCH_MODEL= KeyImpl.intern("archModel");
041        private static final Key  VERSION= KeyImpl.intern("version");
042        private static final Key  ADDITIONAL_INFORMATION= KeyImpl.intern("additionalinformation");
043        private static final Key BUILD_NUMBER = KeyImpl.intern("buildnumber");
044        private static final Key OS = KeyImpl.intern("os");
045        private static final Key STATE = KeyImpl.intern("state");
046        private static final Key RELEASE_DATE = KeyImpl.intern("release-date");
047        private static final Key RAILO = KeyImpl.intern("railo");
048        private static final Key FILE = KeyImpl.intern("file");
049        private static final Key SEPARATOR = KeyImpl.intern("separator");
050        private static final Key VENDOR = KeyImpl.intern("vendor");
051        private static final Key FREE_MEMORY = KeyImpl.intern("freeMemory");
052        private static final Key MAX_MEMORY = KeyImpl.intern("maxMemory");
053        private static final Key TOTAL_MEMORY = KeyImpl.intern("totalMemory");
054        private static final Key JAVA = KeyImpl.intern("java");
055            private static final Key VERSION_NAME = KeyImpl.intern("versionName");
056            private static final Key VERSION_NAME_EXPLANATION = KeyImpl.intern("versionNameExplanation");
057    
058            /*
059        Supported CFML Application
060        
061        Blog
062        - http://www.blogcfm.org
063        
064        
065        
066        */
067            /**
068             * constructor of the server scope
069             * @param sn
070             */
071            public ServerImpl(PageContext pc) {
072                    super(true,"server",SCOPE_SERVER);
073                    reload(pc);
074    
075            }
076            
077            /**
078         * @see railo.runtime.type.scope.Server#reload(railo.runtime.security.SerialNumber)
079         */
080            public void reload() {  
081                    reload(ThreadLocalPageContext.get());
082            }
083            
084            public void reload(PageContext pc) {            
085                
086                ReadOnlyStruct coldfusion=new ReadOnlyStruct();
087                            coldfusion.setEL(PRODUCT_LEVEL,Info.getLevel());
088                            //coldfusion.setEL(PRODUCT_CONTEXT_COUNT,"inf");
089                            coldfusion.setEL(PRODUCT_VERSION,"9,0,0,1");
090                            //coldfusion.setEL(PRODUCT_VERSION,"8,0,0,1");
091                            coldfusion.setEL(SERIAL_NUMBER,"0");
092                            coldfusion.setEL(PRODUCT_NAME,"Railo");
093                            
094                            // TODO scope server missing values
095                            coldfusion.setEL(APP_SERVER,"");// Jrun
096                            coldfusion.setEL(EXPIRATION,expired);// 
097                            coldfusion.setEL(INSTALL_KIT,"");// 
098                            
099                            String rootdir="";
100                            try{
101                                    rootdir=ThreadLocalPageContext.getConfig(pc).getRootDirectory().getAbsolutePath();
102                            }
103                            catch(Throwable t){}
104                            coldfusion.setEL(ROOT_DIR,rootdir);// 
105                            
106                            
107                            
108                            coldfusion.setEL(SUPPORTED_LOCALES,LocaleFactory.getLocaleList());// 
109                            
110                            
111                            coldfusion.setReadOnly(true);
112                    super.setEL (COLDFUSION,coldfusion);
113                    
114                    ReadOnlyStruct os=new ReadOnlyStruct();
115                            os.setEL(KeyImpl.NAME,System.getProperty("os.name") );
116                            os.setEL(ARCH,System.getProperty("os.arch") );
117                            int arch=SystemUtil.getOSArch();
118                            if(arch!=SystemUtil.ARCH_UNKNOW)os.setEL(ARCH_MODEL,new Double(arch) );
119                            os.setEL(VERSION,System.getProperty("os.version") );
120                            os.setEL(ADDITIONAL_INFORMATION,"");
121                            os.setEL(BUILD_NUMBER,"");
122                            
123                            os.setReadOnly(true);
124                    super.setEL (OS,os);
125                    
126                    ReadOnlyStruct railo=new ReadOnlyStruct();
127                            railo.setEL(VERSION,Info.getVersionAsString());
128                            railo.setEL(VERSION_NAME,Info.getVersionName());
129                            railo.setEL(VERSION_NAME_EXPLANATION,Info.getVersionNameExplanation());
130                            railo.setEL(STATE,Info.getStateAsString());
131                            railo.setEL(RELEASE_DATE,Info.getRealeaseDate());
132                            railo.setReadOnly(true);
133                    super.setEL (RAILO,railo);
134                    
135                    ReadOnlyStruct separator=new ReadOnlyStruct();
136                            separator.setEL(KeyImpl.PATH,System.getProperty("path.separator"));
137                            separator.setEL(FILE,System.getProperty("file.separator"));
138                            separator.setEL(KeyImpl.LINE,System.getProperty("line.separator"));
139                            separator.setReadOnly(true);
140                    super.setEL (SEPARATOR,separator);
141                            
142                    ReadOnlyStruct java=new ReadOnlyStruct();
143                            java.setEL(VERSION,System.getProperty("java.version"));
144                            java.setEL(VENDOR,System.getProperty("java.vendor"));
145                            arch=SystemUtil.getJREArch();
146                            if(arch!=SystemUtil.ARCH_UNKNOW)java.setEL(ARCH_MODEL,new Double(arch) );
147                            Runtime rt = Runtime.getRuntime();
148                            java.setEL(FREE_MEMORY,new Double(rt.freeMemory()));
149                            java.setEL(TOTAL_MEMORY,new Double(rt.totalMemory()));
150                            java.setEL(MAX_MEMORY,new Double(rt.maxMemory()));
151                            java.setReadOnly(true);
152                            super.setEL (JAVA,java);
153                    
154    
155                            ReadOnlyStruct servlet=new ReadOnlyStruct();
156                            String name="";
157                            try{
158                                    name=pc.getServletContext().getServerInfo();
159                            }
160                            catch(Throwable t){}
161                            servlet.setEL(KeyImpl.NAME,name);
162                            servlet.setReadOnly(true);
163                            
164                            
165                            super.setEL (SERVLET,servlet);
166                
167            }
168    
169            /**
170             *
171             * @see railo.runtime.type.StructImpl#set(railo.runtime.type.Collection.Key, java.lang.Object)
172             */
173            public Object set(Collection.Key key, Object value) throws PageException {
174                    if(isReadOnlyKey(key))
175                            throw new ExpressionException("you can't rewrite key ["+key+"] from server scope, key is readonly");
176                    return super.set (key, value);
177            }
178    
179    
180            /**
181             *
182             * @see railo.runtime.type.StructImpl#setEL(railo.runtime.type.Collection.Key, java.lang.Object)
183             */
184            public Object setEL(Collection.Key key, Object value) {
185                    if(!isReadOnlyKey(key))return super.setEL (key, value);
186                    return value;
187            }
188            
189            /**
190             * returns if the key is a readonly key
191             * @param key key to check
192             * @return is readonly 
193             */
194            private boolean isReadOnlyKey(Collection.Key key) {
195                    
196                    return (key.equals(JAVA) || 
197                                    key.equals(SEPARATOR) || 
198                                    key.equals(OS) || 
199                                    key.equals(COLDFUSION) || 
200                                    key.equals(RAILO));
201            }
202    
203            public void touchBeforeRequest(PageContext pc) {
204                    // do nothing
205            }
206    
207            public void touchAfterRequest(PageContext pc) {
208                    // do nothing
209            }
210    }