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.type.scope;
020
021import java.io.File;
022
023import lucee.commons.io.SystemUtil;
024import lucee.commons.lang.ClassUtil;
025import lucee.commons.lang.ExceptionUtil;
026import lucee.commons.lang.StringUtil;
027import lucee.runtime.Info;
028import lucee.runtime.PageContext;
029import lucee.runtime.engine.ThreadLocalPageContext;
030import lucee.runtime.exp.ExpressionException;
031import lucee.runtime.exp.PageException;
032import lucee.runtime.i18n.LocaleFactory;
033import lucee.runtime.instrumentation.InstrumentationUtil;
034import lucee.runtime.op.Caster;
035import lucee.runtime.type.Collection;
036import lucee.runtime.type.KeyImpl;
037import lucee.runtime.type.ReadOnlyStruct;
038import lucee.runtime.type.dt.DateTimeImpl;
039import lucee.runtime.type.util.KeyConstants;
040
041
042/**
043 * Server Scope
044 */
045public final class ServerImpl extends ScopeSupport implements Server,SharedScope {
046
047        private static final DateTimeImpl expired=new DateTimeImpl(2145913200000L,false);
048
049        private static final Key PRODUCT_NAME = KeyImpl.intern("productname");
050        private static final Key PRODUCT_LEVEL = KeyImpl.intern("productlevel");
051    private static final Key PRODUCT_VERSION = KeyImpl.intern("productversion");
052    private static final Key SERIAL_NUMBER = KeyImpl.intern("serialnumber");
053    private static final Key APP_SERVER =KeyConstants._appserver;
054    private static final Key EXPIRATION = KeyImpl.intern("expiration");
055    private static final Key INSTALL_KIT = KeyImpl.intern("installkit");
056    private static final Key ROOT_DIR = KeyImpl.intern("rootdir");
057    private static final Key SUPPORTED_LOCALES = KeyImpl.intern("supportedlocales");
058    private static final Key  COLDFUSION= KeyConstants._coldfusion;
059    private static final Key  SERVLET= KeyConstants._servlet;
060    private static final Key  ARCH= KeyImpl.intern("arch");
061    private static final Key  MAC_ADDRESS= KeyImpl.intern("macAddress");
062    private static final Key  ARCH_MODEL= KeyImpl.intern("archModel");
063//  private static final Key  JAVA_AGENT_PATH = KeyImpl.intern("javaAgentPath");
064    private static final Key  JAVA_EXECUTION_PATH = KeyImpl.intern("executionPath");
065    private static final Key  JAVA_AGENT_SUPPORTED = KeyImpl.intern("javaAgentSupported");
066    private static final Key  LOADER_VERSION= KeyImpl.intern("loaderVersion");
067    private static final Key  LOADER_PATH = KeyImpl.intern("loaderPath");
068    private static final Key  VERSION= KeyConstants._version;
069    private static final Key  ADDITIONAL_INFORMATION= KeyImpl.intern("additionalinformation");
070    private static final Key BUILD_NUMBER = KeyImpl.intern("buildnumber");
071    private static final Key OS = KeyConstants._os;
072    private static final Key STATE = KeyConstants._state;
073    private static final Key RELEASE_DATE = KeyImpl.intern("release-date");
074    private static final Key LUCEE = KeyConstants._lucee;
075    private static final Key FILE = KeyConstants._file;
076    private static final Key SEPARATOR = KeyConstants._separator;
077    private static final Key VENDOR = KeyImpl.intern("vendor");
078    private static final Key FREE_MEMORY = KeyImpl.intern("freeMemory");
079    private static final Key MAX_MEMORY = KeyImpl.intern("maxMemory");
080    private static final Key TOTAL_MEMORY = KeyImpl.intern("totalMemory");
081    private static final Key JAVA = KeyConstants._java;
082        private static final Key VERSION_NAME = KeyImpl.intern("versionName");
083        private static final Key VERSION_NAME_EXPLANATION = KeyImpl.intern("versionNameExplanation");
084        private static String jep;
085    
086        /**
087         * constructor of the server scope
088         * @param pc
089         */
090        public ServerImpl(PageContext pc) {
091                super(true,"server",SCOPE_SERVER);
092                reload(pc);
093
094        }
095        
096        @Override
097        public Object get(Key key, Object defaultValue) {
098                if(key.equalsIgnoreCase(KeyConstants._railo))
099                        return super.get(KeyConstants._lucee, defaultValue);
100                return super.get(key, defaultValue);
101        }
102
103        @Override
104        public Object g(Key key, Object defaultValue) {
105                if(key.equalsIgnoreCase(KeyConstants._railo))
106                        return super.g(KeyConstants._lucee, defaultValue);
107                return super.g(key, defaultValue);
108        }
109
110        @Override
111        public Object g(Key key) throws PageException {
112                if(key.equalsIgnoreCase(KeyConstants._railo))
113                        return super.g(KeyConstants._lucee);
114                return super.g(key);
115        }
116
117        @Override
118        public Object get(Key key) throws PageException {
119                if(key.equalsIgnoreCase(KeyConstants._railo))
120                        return super.get(KeyConstants._lucee);
121                return super.get(key);
122        }
123        
124        
125
126        @Override
127        public void reload() {  
128                reload(ThreadLocalPageContext.get());
129        }
130        
131        public void reload(PageContext pc) {            
132            
133            ReadOnlyStruct coldfusion=new ReadOnlyStruct();
134                        coldfusion.setEL(PRODUCT_LEVEL,Info.getLevel());
135                        //coldfusion.setEL(PRODUCT_CONTEXT_COUNT,"inf");
136                        coldfusion.setEL(PRODUCT_VERSION,"10,0,0,0");
137                        //coldfusion.setEL(PRODUCT_VERSION,"8,0,0,1");
138                        coldfusion.setEL(SERIAL_NUMBER,"0");
139                        coldfusion.setEL(PRODUCT_NAME,"Lucee");
140                        
141                        // TODO scope server missing values
142                        coldfusion.setEL(APP_SERVER,"");// Jrun
143                        coldfusion.setEL(EXPIRATION,expired);// 
144                        coldfusion.setEL(INSTALL_KIT,"");// 
145                        
146                        String rootdir="";
147                        try{
148                                rootdir=ThreadLocalPageContext.getConfig(pc).getRootDirectory().getAbsolutePath();
149                        }
150                        catch(Throwable t){
151                                ExceptionUtil.rethrowIfNecessary(t);
152                        }
153                        coldfusion.setEL(ROOT_DIR,rootdir);// 
154
155                        
156                        coldfusion.setEL(SUPPORTED_LOCALES,LocaleFactory.getLocaleList());// 
157                        
158                        
159                        coldfusion.setReadOnly(true);
160                super.setEL (COLDFUSION,coldfusion);
161                
162                ReadOnlyStruct os=new ReadOnlyStruct();
163                        os.setEL(KeyConstants._name,System.getProperty("os.name") );
164                        os.setEL(ARCH,System.getProperty("os.arch") );
165                        os.setEL(MAC_ADDRESS,SystemUtil.getMacAddress());
166                        int arch=SystemUtil.getOSArch();
167                        if(arch!=SystemUtil.ARCH_UNKNOW)os.setEL(ARCH_MODEL,new Double(arch) );
168                        os.setEL(VERSION,System.getProperty("os.version") );
169                        os.setEL(ADDITIONAL_INFORMATION,"");
170                        os.setEL(BUILD_NUMBER,"");
171
172                        
173                        os.setReadOnly(true);
174                super.setEL (OS,os);
175                
176                ReadOnlyStruct lucee=new ReadOnlyStruct();
177                        lucee.setEL(VERSION,Info.getVersionAsString());
178                        lucee.setEL(VERSION_NAME,Info.getVersionName());
179                        lucee.setEL(VERSION_NAME_EXPLANATION,Info.getVersionNameExplanation());
180                        lucee.setEL(STATE,Info.getStateAsString());
181                        lucee.setEL(RELEASE_DATE,Info.getRealeaseDate());
182                        lucee.setEL(LOADER_VERSION,Caster.toDouble(SystemUtil.getLoaderVersion()));
183                        lucee.setEL(LOADER_PATH, ClassUtil.getSourcePathForClass("lucee.loader.servlet.CFMLServlet", ""));
184
185                        lucee.setReadOnly(true);
186                super.setEL (LUCEE,lucee);
187
188                ReadOnlyStruct separator=new ReadOnlyStruct();
189                        separator.setEL(KeyConstants._path,System.getProperty("path.separator"));
190                        separator.setEL(FILE,System.getProperty("file.separator"));
191                        separator.setEL(KeyConstants._line,System.getProperty("line.separator"));
192                        separator.setReadOnly(true);
193                super.setEL (SEPARATOR,separator);
194                        
195                ReadOnlyStruct java=new ReadOnlyStruct();
196                        java.setEL(VERSION,System.getProperty("java.version"));
197                        java.setEL(VENDOR,System.getProperty("java.vendor"));
198                        arch=SystemUtil.getJREArch();
199                        if(arch!=SystemUtil.ARCH_UNKNOW)java.setEL(ARCH_MODEL,new Double(arch) );
200                        Runtime rt = Runtime.getRuntime();
201                        java.setEL(FREE_MEMORY,new Double(rt.freeMemory()));
202                        java.setEL(TOTAL_MEMORY,new Double(rt.totalMemory()));
203                        java.setEL(MAX_MEMORY,new Double(rt.maxMemory()));
204                        java.setEL(JAVA_AGENT_SUPPORTED,Caster.toBoolean(InstrumentationUtil.isSupported()));
205                        
206                        //if(jap==null) jap=JavaUtil.getSourcePathForClass("lucee.runtime.instrumentation.Agent");
207                        //java.setEL(JAVA_AGENT_PATH, jap);
208                        
209                        if(jep==null) {
210                                String temp = System.getProperty( "user.dir", "" );
211                                if ( !StringUtil.isEmpty(temp) && !temp.endsWith( File.separator ) )
212                                        temp = temp + File.separator;
213                                jep=temp;
214                        }
215                        java.setEL( JAVA_EXECUTION_PATH, jep );
216
217                        java.setReadOnly(true);
218                        super.setEL (JAVA,java);
219                
220
221                        ReadOnlyStruct servlet=new ReadOnlyStruct();
222                        String name="";
223                        try{
224                                name=pc.getServletContext().getServerInfo();
225                        }
226                        catch(Throwable t){
227                                ExceptionUtil.rethrowIfNecessary(t);
228                        }
229                        servlet.setEL(KeyConstants._name,name);
230                        servlet.setReadOnly(true);
231                        
232                        
233                        super.setEL (SERVLET,servlet);
234            
235        }
236
237        @Override
238        public Object set(Collection.Key key, Object value) throws PageException {
239                if(isReadOnlyKey(key))
240                        throw new ExpressionException("you can't rewrite key ["+key+"] from server scope, key is readonly");
241                return super.set (key, value);
242        }
243
244
245        @Override
246        public Object setEL(Collection.Key key, Object value) {
247                if(!isReadOnlyKey(key))return super.setEL (key, value);
248                return value;
249        }
250        
251        /**
252         * returns if the key is a readonly key
253         * @param key key to check
254         * @return is readonly 
255         */
256        private boolean isReadOnlyKey(Collection.Key key) {
257                
258                return (key.equals(JAVA) || 
259                                key.equals(SEPARATOR) || 
260                                key.equals(OS) || 
261                                key.equals(COLDFUSION) || 
262                                key.equals(LUCEE));
263        }
264
265        public void touchBeforeRequest(PageContext pc) {
266                // do nothing
267        }
268
269        public void touchAfterRequest(PageContext pc) {
270                // do nothing
271        }
272}