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