001 package railo.runtime.config; 002 003 import java.net.URL; 004 import java.util.Enumeration; 005 import java.util.Map; 006 007 import javax.servlet.ServletConfig; 008 import javax.servlet.ServletContext; 009 010 import org.apache.commons.collections.map.ReferenceMap; 011 012 import railo.commons.io.SystemUtil; 013 import railo.commons.io.log.Log; 014 import railo.commons.io.log.LogAndSource; 015 import railo.commons.io.log.LogAndSourceImpl; 016 import railo.commons.io.log.LogConsole; 017 import railo.commons.io.res.Resource; 018 import railo.commons.io.res.ResourceProvider; 019 import railo.commons.io.res.ResourcesImpl; 020 import railo.commons.lang.StringUtil; 021 import railo.commons.lock.KeyLock; 022 import railo.runtime.CFMLFactoryImpl; 023 import railo.runtime.Mapping; 024 import railo.runtime.MappingImpl; 025 import railo.runtime.Page; 026 import railo.runtime.PageContext; 027 import railo.runtime.PageSourceImpl; 028 import railo.runtime.cfx.CFXTagPool; 029 import railo.runtime.compiler.CFMLCompilerImpl; 030 import railo.runtime.engine.CFMLEngineImpl; 031 import railo.runtime.exp.ExpressionException; 032 import railo.runtime.exp.PageException; 033 import railo.runtime.exp.PageRuntimeException; 034 import railo.runtime.exp.SecurityException; 035 import railo.runtime.gateway.GatewayEngineImpl; 036 import railo.runtime.gateway.GatewayEntry; 037 import railo.runtime.lock.LockManager; 038 import railo.runtime.lock.LockManagerImpl; 039 import railo.runtime.monitor.RequestMonitor; 040 import railo.runtime.security.SecurityManager; 041 import railo.runtime.security.SecurityManagerImpl; 042 import railo.runtime.tag.TagHandlerPool; 043 044 /** 045 * Web Context 046 */ 047 public final class ConfigWebImpl extends ConfigImpl implements ServletConfig, ConfigWeb { 048 049 private ServletConfig config; 050 private ConfigServerImpl configServer; 051 private SecurityManager securityManager; 052 private static final LockManager lockManager= LockManagerImpl.getInstance(false); 053 private Resource rootDir; 054 private CFMLCompilerImpl compiler=new CFMLCompilerImpl(); 055 private Page baseComponentPage; 056 private MappingImpl serverTagMapping; 057 private MappingImpl serverFunctionMapping; 058 private KeyLock<String> contextLock; 059 private GatewayEngineImpl gatewayEngine; 060 private LogAndSource gatewayLogger=null;//new LogAndSourceImpl(LogConsole.getInstance(Log.LEVEL_INFO),""); 061 062 //private File deployDirectory; 063 064 /** 065 * constructor of the class 066 * @param configServer 067 * @param config 068 * @param configDir 069 * @param configFile 070 * @param cloneServer 071 */ 072 protected ConfigWebImpl(CFMLFactoryImpl factory,ConfigServerImpl configServer, ServletConfig config, Resource configDir, Resource configFile) { 073 super(factory,configDir, configFile,configServer.getTLDs(),configServer.getFLDs()); 074 this.configServer=configServer; 075 this.config=config; 076 factory.setConfig(this); 077 ResourceProvider frp = ResourcesImpl.getFileResourceProvider(); 078 079 this.rootDir=frp.getResource(config.getServletContext().getRealPath("/")); 080 081 082 // Fix for tomcat 083 if(this.rootDir.getName().equals(".") || this.rootDir.getName().equals("..")) 084 this.rootDir=this.rootDir.getParentResource(); 085 } 086 087 public void reset() { 088 super.reset(); 089 tagHandlerPool.reset(); 090 contextLock=null; 091 } 092 093 /* * 094 * Constructor of the class, used for configserver dummy instance 095 * @param factory 096 * @param configServer 097 * @param configx 098 * @param configDir 099 * @param configFile 100 * / 101 protected ConfigWebImpl(CFMLFactoryImpl factory,ConfigServerImpl configServer, Resource configDir, Resource configFile,Resource rootDir) { 102 super(factory,configDir, configFile,configServer.getTLDs(),configServer.getFLDs()); 103 this.configServer=configServer; 104 factory.setConfig(this); 105 106 this.rootDir=rootDir; 107 108 // Fix for tomcat 109 if(this.rootDir.getName().equals(".") || this.rootDir.getName().equals("..")) 110 this.rootDir=this.rootDir.getParentResource(); 111 }*/ 112 113 114 115 /** 116 * @see javax.servlet.ServletConfig#getServletName() 117 */ 118 public String getServletName() { 119 return config.getServletName(); 120 } 121 122 /** 123 * @see javax.servlet.ServletConfig#getServletContext() 124 */ 125 public ServletContext getServletContext() { 126 return config.getServletContext(); 127 } 128 129 /** 130 * @see javax.servlet.ServletConfig#getInitParameter(java.lang.String) 131 */ 132 public String getInitParameter(String name) { 133 return config.getInitParameter(name); 134 } 135 136 /** 137 * @see javax.servlet.ServletConfig#getInitParameterNames() 138 */ 139 public Enumeration getInitParameterNames() { 140 return config.getInitParameterNames(); 141 } 142 143 /** 144 * @see railo.runtime.config.ConfigImpl#getConfigServerImpl() 145 */ 146 protected ConfigServerImpl getConfigServerImpl() { 147 return configServer; 148 } 149 150 151 public ConfigServer getConfigServer() { 152 throw new PageRuntimeException(new SecurityException("access on server config without password denied")); 153 //return configServer; 154 } 155 156 /** 157 * @see railo.runtime.config.ConfigImpl#getConfigServer(java.lang.String) 158 */ 159 public ConfigServer getConfigServer(String password) throws ExpressionException { 160 if(!configServer.hasPassword()) 161 throw new ExpressionException("Cannot access, no password is defined"); 162 if(!configServer.getPassword().equalsIgnoreCase(password)) 163 throw new ExpressionException("No access, password is invalid"); 164 return configServer; 165 } 166 167 // FUTURE 168 public String getServerId() { 169 return configServer.getId(); 170 } 171 172 public String getServerSecurityKey() { 173 return configServer.getSecurityKey(); 174 } 175 176 public Resource getServerConfigDir() { 177 return configServer.getConfigDir(); 178 } 179 180 181 /** 182 * @return Returns the accessor. 183 */ 184 public SecurityManager getSecurityManager() { 185 return securityManager; 186 } 187 188 /** 189 * @param securityManager The accessor to set. 190 */ 191 protected void setSecurityManager(SecurityManager securityManager) { 192 ((SecurityManagerImpl)securityManager).setRootDirectory(getRootDirectory()); 193 this.securityManager = securityManager; 194 } 195 196 /** 197 * @throws SecurityException 198 * @see railo.runtime.config.ConfigImpl#getCFXTagPool() 199 */ 200 public CFXTagPool getCFXTagPool() throws SecurityException { 201 if(securityManager.getAccess(SecurityManager.TYPE_CFX_USAGE)==SecurityManager.VALUE_YES) return super.getCFXTagPool(); 202 throw new SecurityException("no access to cfx functionality", "disabled by security settings"); 203 } 204 205 /** 206 * @return Returns the rootDir. 207 */ 208 public Resource getRootDirectory() { 209 return rootDir; 210 } 211 212 /** 213 * @see railo.runtime.config.Config#getUpdateType() 214 */ 215 public String getUpdateType() { 216 return configServer.getUpdateType(); 217 } 218 219 /** 220 * @see railo.runtime.config.Config#getUpdateLocation() 221 */ 222 public URL getUpdateLocation() { 223 return configServer.getUpdateLocation(); 224 } 225 226 /** 227 * @see railo.runtime.config.ConfigWeb#getLockManager() 228 */ 229 public LockManager getLockManager() { 230 return lockManager; 231 } 232 233 /** 234 * @return the compiler 235 */ 236 public CFMLCompilerImpl getCompiler() { 237 return compiler; 238 } 239 240 public Page getBaseComponentPage(PageContext pc) throws PageException { 241 if(baseComponentPage==null) { 242 baseComponentPage=((PageSourceImpl)getBaseComponentPageSource(pc)).loadPage(pc,this); 243 244 } 245 return baseComponentPage; 246 } 247 public void resetBaseComponentPage() { 248 baseComponentPage=null; 249 } 250 251 252 253 254 255 public Mapping getServerTagMapping() { 256 if(serverTagMapping==null){ 257 serverTagMapping=getConfigServerImpl().tagMapping.cloneReadOnly(this); 258 } 259 return serverTagMapping; 260 } 261 public Mapping getServerFunctionMapping() { 262 if(serverFunctionMapping==null){ 263 serverFunctionMapping=getConfigServerImpl().functionMapping.cloneReadOnly(this); 264 } 265 return serverFunctionMapping; 266 } 267 private Map applicationMappings=new ReferenceMap(); 268 private TagHandlerPool tagHandlerPool=new TagHandlerPool(); 269 public Mapping getApplicationMapping(String virtual, String physical) { 270 String key=virtual.toLowerCase()+physical.toLowerCase(); 271 Mapping m=(Mapping) applicationMappings.get(key); 272 if(m==null){ 273 m=new MappingImpl(this, 274 virtual, 275 physical, 276 null,false,true,false,false,false,true,false 277 ); 278 applicationMappings.put(key, m); 279 } 280 return m; 281 } 282 283 public String getLabel() { 284 String hash=getHash(); 285 String label=hash; 286 Map<String, String> labels = configServer.getLabels(); 287 if(labels!=null) { 288 String l = labels.get(hash); 289 if(!StringUtil.isEmpty(l)) { 290 label=l; 291 } 292 } 293 return label; 294 } 295 296 public String getHash() { 297 return SystemUtil.hash(getServletContext()); 298 } 299 300 public KeyLock<String> getContextLock() { 301 if(contextLock==null) { 302 contextLock=new KeyLock<String>(); 303 } 304 return contextLock; 305 } 306 307 308 protected void setGatewayEntries(Map<String, GatewayEntry> gatewayEntries) { 309 try { 310 getGatewayEngine().addEntries(this,gatewayEntries); 311 } catch (Exception e) { 312 e.printStackTrace(); 313 } 314 } 315 public GatewayEngineImpl getGatewayEngine() { 316 if(gatewayEngine==null){ 317 gatewayEngine=new GatewayEngineImpl(this); 318 } 319 return gatewayEngine; 320 } 321 public void setGatewayEngine(GatewayEngineImpl gatewayEngine) { 322 this.gatewayEngine=gatewayEngine; 323 } 324 325 /** 326 * @see railo.runtime.config.Config#getMailLogger() 327 */ 328 public LogAndSource getGatewayLogger() { 329 if(gatewayLogger==null)gatewayLogger=new LogAndSourceImpl(LogConsole.getInstance(this,Log.LEVEL_ERROR),""); 330 return gatewayLogger; 331 } 332 333 334 public void setGatewayLogger(LogAndSource gatewayLogger) { 335 this.gatewayLogger=gatewayLogger; 336 } 337 /* * 338 * this is a config web that reflect the configServer, this allows to run cfml code on server level 339 * @param gatewayEngine 340 * @return 341 * @throws PageException 342 * / 343 public ConfigWeb createGatewayConfig(GatewayEngineImpl gatewayEngine) { 344 QueryCacheSupport cqc = QueryCacheSupport.getInstance(this); 345 CFMLEngineImpl engine = getConfigServerImpl().getCFMLEngineImpl(); 346 CFMLFactoryImpl factory = new CFMLFactoryImpl(engine,cqc); 347 348 ServletContextDummy sContext = new ServletContextDummy( 349 this, 350 getRootDirectory(), 351 new StructImpl(), 352 new StructImpl(), 353 1,1); 354 ServletConfigDummy sConfig = new ServletConfigDummy(sContext,"CFMLServlet"); 355 ConfigWebImpl cwi = new ConfigWebImpl( 356 factory, 357 getConfigServerImpl(), 358 sConfig, 359 getConfigDir(), 360 getConfigFile(),true); 361 cqc.setConfigWeb(cwi); 362 try { 363 ConfigWebFactory.createContextFiles(getConfigDir(),sConfig); 364 ConfigWebFactory.load(getConfigServerImpl(), cwi, ConfigWebFactory.loadDocument(getConfigFile()),true); 365 ConfigWebFactory.createContextFilesPost(getConfigDir(),cwi,sConfig,true); 366 } 367 catch (Exception e) { 368 e.printStackTrace(); 369 } 370 371 cwi.setGatewayEngine(gatewayEngine); 372 373 //cwi.setGatewayMapping(new MappingImpl(cwi,"/",gatewayEngine.getCFCDirectory().getAbsolutePath(),null,false,true,false,false,false)); 374 return cwi; 375 }*/ 376 377 public TagHandlerPool getTagHandlerPool() { 378 return tagHandlerPool; 379 } 380 381 @Override 382 public int getLoginDelay() { 383 return configServer.getLoginDelay(); 384 } 385 386 @Override 387 public boolean getLoginCaptcha() { 388 return configServer.getLoginCaptcha(); 389 } 390 391 public boolean allowRequestTimeout() { 392 return configServer.getCFMLEngineImpl().allowRequestTimeout(); 393 } 394 395 public Resource getConfigServerDir() { 396 return configServer.getConfigDir(); 397 } 398 399 @Override 400 public boolean isMonitoringEnabled() { 401 return configServer.isMonitoringEnabled(); 402 } 403 404 @Override 405 public RequestMonitor[] getRequestMonitors() { 406 return configServer.getRequestMonitors(); 407 } 408 409 }