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.listener; 020 021import java.util.HashMap; 022import java.util.Iterator; 023import java.util.Map; 024import java.util.Map.Entry; 025 026import lucee.commons.lang.StringUtil; 027import lucee.runtime.config.Config; 028import lucee.runtime.config.ConfigImpl; 029import lucee.runtime.config.ConfigWeb; 030import lucee.runtime.db.DataSource; 031import lucee.runtime.exp.ApplicationException; 032import lucee.runtime.op.Caster; 033import lucee.runtime.type.Collection; 034import lucee.runtime.type.Collection.Key; 035import lucee.runtime.type.KeyImpl; 036import lucee.runtime.type.Struct; 037import lucee.runtime.type.util.ArrayUtil; 038import lucee.transformer.library.tag.TagLib; 039import lucee.transformer.library.tag.TagLibTag; 040import lucee.transformer.library.tag.TagLibTagAttr; 041 042public abstract class ApplicationContextSupport implements ApplicationContextPro { 043 044 private static final long serialVersionUID = 1384678713928757744L; 045 046 protected int idletimeout=1800; 047 protected String cookiedomain; 048 protected String applicationtoken; 049 050 private Map<Collection.Key,Map<Collection.Key,Object>> tagDefaultAttributeValues=null; 051 052 protected ConfigWeb config; 053 054 public ApplicationContextSupport(ConfigWeb config) { 055 this.config=config; 056 tagDefaultAttributeValues=((ConfigImpl)config).getTagDefaultAttributeValues(); 057 } 058 059 060 protected void _duplicate(ApplicationContextSupport other) { 061 idletimeout=other.idletimeout; 062 cookiedomain=other.cookiedomain; 063 applicationtoken=other.applicationtoken; 064 if(other.tagDefaultAttributeValues!=null) { 065 tagDefaultAttributeValues=new HashMap<Collection.Key, Map<Collection.Key,Object>>(); 066 Iterator<Entry<Collection.Key, Map<Collection.Key, Object>>> it = other.tagDefaultAttributeValues.entrySet().iterator(); 067 Entry<Collection.Key, Map<Collection.Key, Object>> e; 068 Iterator<Entry<Collection.Key, Object>> iit; 069 Entry<Collection.Key, Object> ee; 070 Map<Collection.Key, Object> map; 071 while(it.hasNext()){ 072 e = it.next(); 073 iit=e.getValue().entrySet().iterator(); 074 map=new HashMap<Collection.Key, Object>(); 075 while(iit.hasNext()){ 076 ee = iit.next(); 077 map.put(ee.getKey(), ee.getValue()); 078 } 079 tagDefaultAttributeValues.put(e.getKey(), map); 080 } 081 } 082 } 083 084 @Override 085 public void setSecuritySettings(String applicationtoken, String cookiedomain, int idletimeout) { 086 this.applicationtoken=applicationtoken; 087 this.cookiedomain=cookiedomain; 088 this.idletimeout=idletimeout; 089 090 } 091 092 @Override 093 public String getSecurityApplicationToken() { 094 if(StringUtil.isEmpty(applicationtoken,true)) return getName(); 095 return applicationtoken; 096 } 097 098 @Override 099 public String getSecurityCookieDomain() { 100 if(StringUtil.isEmpty(applicationtoken,true)) return null; 101 return cookiedomain; 102 } 103 104 @Override 105 public int getSecurityIdleTimeout() { 106 if(idletimeout<1) return 1800; 107 return idletimeout; 108 } 109 110 111 112 @Override 113 public DataSource getDataSource(String dataSourceName, DataSource defaultValue) { 114 dataSourceName=dataSourceName.trim(); 115 DataSource[] sources = getDataSources(); 116 if(!ArrayUtil.isEmpty(sources)) { 117 for(int i=0;i<sources.length;i++){ 118 if(sources[i].getName().equalsIgnoreCase(dataSourceName)) 119 return sources[i]; 120 } 121 } 122 return defaultValue; 123 } 124 125 @Override 126 public DataSource getDataSource(String dataSourceName) throws ApplicationException { 127 DataSource source = getDataSource(dataSourceName,null); 128 if(source==null) 129 throw new ApplicationException("there is no datasource with name ["+dataSourceName+"]"); 130 return source; 131 } 132 133 @Override 134 public Map<Collection.Key, Map<Collection.Key, Object>> getTagAttributeDefaultValues() { 135 return tagDefaultAttributeValues; 136 } 137 138 @Override 139 public Map<Collection.Key, Object> getTagAttributeDefaultValues(String fullname) { 140 if(tagDefaultAttributeValues==null) return null; 141 return tagDefaultAttributeValues.get(KeyImpl.init(fullname)); 142 } 143 144 145 @Override 146 public void setTagAttributeDefaultValues(Struct sct) { 147 if(tagDefaultAttributeValues==null) 148 tagDefaultAttributeValues=new HashMap<Collection.Key, Map<Collection.Key,Object>>(); 149 initTagDefaultAttributeValues(config, tagDefaultAttributeValues, sct); 150 } 151 152 153 public static void initTagDefaultAttributeValues(Config config,Map<Collection.Key, Map<Collection.Key, Object>> tagDefaultAttributeValues, Struct sct) { 154 if(sct.size()==0) return; 155 ConfigImpl ci = ((ConfigImpl)config); 156 157 // first check the core lib without namespace 158 TagLib lib = ci.getCoreTagLib(); 159 _initTagDefaultAttributeValues(config, lib, tagDefaultAttributeValues, sct,false); 160 if(sct.size()==0) return; 161 162 // then all the other libs including the namespace 163 TagLib[] tlds = ci.getTLDs(); 164 for(int i=0;i<tlds.length;i++){ 165 _initTagDefaultAttributeValues(config, tlds[i], tagDefaultAttributeValues, sct,true); 166 if(sct.size()==0) return; 167 } 168 } 169 170 private static void _initTagDefaultAttributeValues(Config config,TagLib lib, 171 Map<Collection.Key, Map<Collection.Key, Object>> tagDefaultAttributeValues, Struct sct, boolean checkNameSpace) { 172 if(sct==null) return; 173 Iterator<Entry<Key, Object>> it = sct.entryIterator(); 174 // loop tags 175 Struct attrs; 176 TagLibTag tag; 177 Iterator<Entry<Key, Object>> iit; 178 Entry<Key, Object> e; 179 Map<Collection.Key,Object> map; 180 TagLibTagAttr attr; 181 String name; 182 while(it.hasNext()){ 183 e = it.next(); 184 attrs=Caster.toStruct(e.getValue(),null); 185 if(attrs!=null){ 186 tag=null; 187 if(checkNameSpace) { 188 name=e.getKey().getLowerString(); 189 if(StringUtil.startsWithIgnoreCase(name, lib.getNameSpaceAndSeparator())) { 190 name=name.substring(lib.getNameSpaceAndSeparator().length()); 191 tag = lib.getTag(name); 192 } 193 } 194 else 195 tag = lib.getTag(e.getKey().getLowerString()); 196 197 if(tag!=null) { 198 sct.removeEL(e.getKey()); 199 map=new HashMap<Collection.Key, Object>(); 200 iit = attrs.entryIterator(); 201 while(iit.hasNext()){ 202 e = iit.next(); 203 map.put(KeyImpl.init(e.getKey().getLowerString()),e.getValue()); 204 } 205 tagDefaultAttributeValues.put(KeyImpl.init(tag.getFullName()), map); 206 } 207 } 208 } 209 } 210 211}