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.functions.system;
020
021import java.util.Iterator;
022import java.util.Map;
023import java.util.Map.Entry;
024
025import lucee.commons.date.TimeZoneUtil;
026import lucee.commons.io.res.Resource;
027import lucee.runtime.Component;
028import lucee.runtime.ComponentSpecificAccess;
029import lucee.runtime.Mapping;
030import lucee.runtime.PageContext;
031import lucee.runtime.PageContextImpl;
032import lucee.runtime.config.Config;
033import lucee.runtime.config.ConfigImpl;
034import lucee.runtime.config.ConfigWebUtil;
035import lucee.runtime.db.DataSource;
036import lucee.runtime.exp.PageException;
037import lucee.runtime.i18n.LocaleFactory;
038import lucee.runtime.listener.AppListenerUtil;
039import lucee.runtime.listener.ApplicationContextPro;
040import lucee.runtime.listener.JavaSettings;
041import lucee.runtime.listener.ModernApplicationContext;
042import lucee.runtime.net.s3.Properties;
043import lucee.runtime.op.Caster;
044import lucee.runtime.orm.ORMConfiguration;
045import lucee.runtime.type.Array;
046import lucee.runtime.type.ArrayImpl;
047import lucee.runtime.type.Collection;
048import lucee.runtime.type.Collection.Key;
049import lucee.runtime.type.KeyImpl;
050import lucee.runtime.type.Struct;
051import lucee.runtime.type.StructImpl;
052import lucee.runtime.type.UDF;
053import lucee.runtime.type.scope.Scope;
054import lucee.runtime.type.scope.Undefined;
055import lucee.runtime.type.util.ArrayUtil;
056import lucee.runtime.type.util.KeyConstants;
057import lucee.runtime.type.util.ListUtil;
058
059public class GetApplicationSettings {
060        public static Struct call(PageContext pc) {
061                return call(pc, false);
062        }
063        
064        public static Struct call(PageContext pc, boolean suppressFunctions) {
065                ApplicationContextPro ac = (ApplicationContextPro) pc.getApplicationContext();
066                Component cfc = null;
067                if(ac instanceof ModernApplicationContext)cfc= ((ModernApplicationContext)ac).getComponent();
068                
069                Struct sct=new StructImpl();
070                sct.setEL("applicationTimeout", ac.getApplicationTimeout());
071                sct.setEL("clientManagement", Caster.toBoolean(ac.isSetClientManagement()));
072                sct.setEL("clientStorage", ac.getClientstorage());
073                sct.setEL("sessionStorage", ac.getSessionstorage());
074                sct.setEL("customTagPaths", toArray(ac.getCustomTagMappings()));
075                sct.setEL("loginStorage", AppListenerUtil.translateLoginStorage(ac.getLoginStorage()));
076                sct.setEL(KeyConstants._mappings, toStruct(ac.getMappings()));
077                sct.setEL(KeyConstants._name, ac.getName());
078                sct.setEL("scriptProtect", AppListenerUtil.translateScriptProtect(ac.getScriptProtect()));
079                sct.setEL("secureJson", Caster.toBoolean(ac.getSecureJson()));
080                sct.setEL("typeChecking", Caster.toBoolean(ac.getTypeChecking()));
081                sct.setEL("secureJsonPrefix", ac.getSecureJsonPrefix());
082                sct.setEL("sessionManagement", Caster.toBoolean(ac.isSetSessionManagement()));
083                sct.setEL("sessionTimeout", ac.getSessionTimeout());
084                sct.setEL("clientTimeout", ac.getClientTimeout());
085                sct.setEL("setClientCookies", Caster.toBoolean(ac.isSetClientCookies()));
086                sct.setEL("setDomainCookies", Caster.toBoolean(ac.isSetDomainCookies()));
087                sct.setEL(KeyConstants._name, ac.getName());
088                sct.setEL("localMode", ac.getLocalMode()==Undefined.MODE_LOCAL_OR_ARGUMENTS_ALWAYS?Boolean.TRUE:Boolean.FALSE);
089                sct.setEL(KeyConstants._locale,LocaleFactory.toString(pc.getLocale()));
090                sct.setEL(KeyConstants._timezone,TimeZoneUtil.toString(pc.getTimeZone()));
091                sct.setEL("scopeCascading",ConfigWebUtil.toScopeCascading(ac.getScopeCascading(),null));
092                
093                Struct cs=new StructImpl();
094                cs.setEL("web",((PageContextImpl)pc).getWebCharset().name());
095                cs.setEL("resource",((PageContextImpl)pc).getResourceCharset().name());
096                sct.setEL("charset", cs);
097                
098                
099                sct.setEL("sessionType", ((PageContextImpl) pc).getSessionType()==ConfigImpl.SESSION_TYPE_CFML?"cfml":"j2ee");
100                sct.setEL("serverSideFormValidation", Boolean.FALSE); // TODO impl
101
102                sct.setEL("clientCluster", Caster.toBoolean(ac.getClientCluster()));
103                sct.setEL("sessionCluster", Caster.toBoolean(ac.getSessionCluster()));
104                
105
106                sct.setEL("invokeImplicitAccessor", Caster.toBoolean(ac.getTriggerComponentDataMember()));
107                sct.setEL("triggerDataMember", Caster.toBoolean(ac.getTriggerComponentDataMember()));
108                sct.setEL("sameformfieldsasarray", Caster.toBoolean(ac.getSameFieldAsArray(Scope.SCOPE_FORM)));
109                sct.setEL("sameurlfieldsasarray", Caster.toBoolean(ac.getSameFieldAsArray(Scope.SCOPE_URL)));
110                
111                Object ds = ac.getDefDataSource();
112                if(ds instanceof DataSource) ds=_call((DataSource)ds);
113                else ds=Caster.toString(ds,null);
114                sct.setEL(KeyConstants._datasource, ds);
115                sct.setEL("defaultDatasource", ds);
116                
117                Resource src = ac.getSource();
118                if(src!=null)sct.setEL(KeyConstants._source,src.getAbsolutePath());
119                
120                // orm
121                if(ac.isORMEnabled()){
122                        ORMConfiguration conf = ac.getORMConfiguration();
123                        if(conf!=null)sct.setEL(KeyConstants._orm, conf.toStruct());
124                }
125                // s3
126                Properties props = ac.getS3();
127                if(props!=null) {
128                        sct.setEL(KeyConstants._s3, props.toStruct());
129                }
130                
131                // ws settings
132                {
133                Struct wssettings=new StructImpl();
134                wssettings.put(KeyConstants._type, AppListenerUtil.toWSType(ac.getWSType(),"Axis1"));
135                sct.setEL("wssettings", wssettings);
136                }
137                
138                // datasources
139                DataSource[] sources = ac.getDataSources();
140                if(!ArrayUtil.isEmpty(sources)){
141                        Struct _sources = new StructImpl(),s;
142                        sct.setEL(KeyConstants._datasources, _sources);
143                        for(int i=0;i<sources.length;i++){
144                                _sources.setEL(KeyImpl.init(sources[i].getName()), _call(sources[i]));
145                        }
146                        
147                }
148                
149                // tag
150                Map<Key, Map<Collection.Key, Object>> tags = ac.getTagAttributeDefaultValues();
151                if(tags!=null) {
152                        Struct tag = new StructImpl(),s;
153                        Iterator<Entry<Key, Map<Collection.Key, Object>>> it = tags.entrySet().iterator();
154                        Entry<Collection.Key, Map<Collection.Key, Object>> e;
155                        Iterator<Entry<Collection.Key, Object>> iit;
156                        Entry<Collection.Key, Object> ee;
157                        Struct tmp;
158                        //TagLib lib = ((ConfigImpl)pc.getConfig()).getCoreTagLib();
159                        while(it.hasNext()){
160                                e = it.next();
161                                iit=e.getValue().entrySet().iterator();
162                                tmp=new StructImpl();
163                                while(iit.hasNext()){
164                                        ee = iit.next();
165                                        //lib.getTagByClassName(ee.getKey());
166                                        tmp.setEL(ee.getKey(), ee.getValue());
167                                }
168                                tag.setEL(e.getKey(), tmp);
169                                
170                        }
171                        sct.setEL(KeyConstants._tag, tag);
172                }
173                
174                
175                //cache
176                String func = ac.getDefaultCacheName(Config.CACHE_DEFAULT_FUNCTION);
177                String obj = ac.getDefaultCacheName(Config.CACHE_DEFAULT_OBJECT);
178                String qry = ac.getDefaultCacheName(Config.CACHE_DEFAULT_QUERY);
179                String res = ac.getDefaultCacheName(Config.CACHE_DEFAULT_RESOURCE);
180                String tmp = ac.getDefaultCacheName(Config.CACHE_DEFAULT_TEMPLATE);
181                String inc = ac.getDefaultCacheName(Config.CACHE_DEFAULT_INCLUDE);
182                if(func!=null || obj!=null || qry!=null || res!=null || tmp!=null || inc!=null) {
183                        Struct cache=new StructImpl();
184                        sct.setEL(KeyConstants._cache, cache);
185                        if(func!=null)cache.setEL(KeyConstants._function, func);
186                        if(obj!=null)cache.setEL(KeyConstants._object, obj);
187                        if(qry!=null)cache.setEL(KeyConstants._query, qry);
188                        if(res!=null)cache.setEL(KeyConstants._resource, res);
189                        if(tmp!=null)cache.setEL(KeyConstants._template, tmp);
190                        if(inc!=null)cache.setEL(KeyConstants._include, inc);
191                }
192                
193                // java settings
194                JavaSettings js = ac.getJavaSettings();
195                StructImpl jsSct = new StructImpl();
196                jsSct.put("loadCFMLClassPath",js.loadCFMLClassPath());
197                jsSct.put("reloadOnChange",js.reloadOnChange());
198                jsSct.put("watchInterval",new Double(js.watchInterval()));
199                jsSct.put("watchExtensions",ListUtil.arrayToList(js.watchedExtensions(),","));
200                Resource[] reses = js.getResources();
201                StringBuilder sb=new StringBuilder();
202                for(int i=0;i<reses.length;i++){
203                        if(i>0)sb.append(',');
204                        sb.append(reses[i].getAbsolutePath());
205                }
206                jsSct.put("loadCFMLClassPath",sb.toString());
207                sct.put("javaSettings",jsSct);
208                // REST Settings
209                // MUST
210                
211                if(cfc!=null){
212                        sct.setEL(KeyConstants._component, cfc.getPageSource().getDisplayPath());
213                        
214                        try {
215                                ComponentSpecificAccess cw=ComponentSpecificAccess.toComponentSpecificAccess(Component.ACCESS_PRIVATE, cfc);
216                                Iterator<Key> it = cw.keyIterator();
217                                Collection.Key key;
218                                Object value;
219                        while(it.hasNext()) {
220                            key=it.next();
221                            value=cw.get(key);
222                            if(suppressFunctions && value instanceof UDF) continue;
223                            if(!sct.containsKey(key))sct.setEL(key, value);
224                                }
225                        } 
226                        catch (PageException e) {e.printStackTrace();}
227                }
228                return sct;
229        }
230
231        
232
233        private static Struct _call(DataSource source) {
234                Struct s = new StructImpl();
235                s.setEL(KeyConstants._class, source.getClazz().getName());
236                if(source.getConnectionLimit()>=0)s.setEL(AppListenerUtil.CONNECTION_LIMIT, Caster.toDouble(source.getConnectionLimit()));
237                if(source.getConnectionTimeout()!=1)s.setEL(AppListenerUtil.CONNECTION_TIMEOUT, Caster.toDouble(source.getConnectionTimeout()));
238                s.setEL(AppListenerUtil.CONNECTION_STRING, source.getDsnTranslated());
239                if(source.getMetaCacheTimeout() != 60000)s.setEL(AppListenerUtil.META_CACHE_TIMEOUT, Caster.toDouble(source.getMetaCacheTimeout()));
240                s.setEL(KeyConstants._username, source.getUsername());
241                s.setEL(KeyConstants._password, source.getPassword());
242                if(source.getTimeZone()!=null)s.setEL(AppListenerUtil.TIMEZONE, source.getTimeZone().getID());
243                if(source.isBlob())s.setEL(AppListenerUtil.BLOB, source.isBlob());
244                if(source.isClob())s.setEL(AppListenerUtil.CLOB, source.isClob());
245                if(source.isReadOnly())s.setEL(AppListenerUtil.READ_ONLY, source.isReadOnly());
246                if(source.isStorage())s.setEL(AppListenerUtil.STORAGE, source.isStorage());
247                return s;
248        }
249
250        private static Array toArray(Mapping[] mappings) {
251                Array arr=new ArrayImpl();
252                if(mappings!=null)for(int i=0;i<mappings.length;i++){
253                        arr.appendEL(mappings[i].getStrPhysical());
254                }
255                return arr;
256        }
257
258        private static Struct toStruct(Mapping[] mappings) {
259                Struct sct=new StructImpl();
260                if(mappings!=null)for(int i=0;i<mappings.length;i++){
261                        sct.setEL(KeyImpl.init(mappings[i].getVirtual()), mappings[i].getStrPhysical());
262                }
263                return sct;
264        }
265}