1 15 package org.apache.tapestry.services.impl; 16 17 import java.util.HashMap ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 import java.util.Locale ; 21 import java.util.Map ; 22 23 import org.apache.hivemind.ApplicationRuntimeException; 24 import org.apache.hivemind.ClassResolver; 25 import org.apache.hivemind.ErrorLog; 26 import org.apache.hivemind.Resource; 27 import org.apache.hivemind.service.ThreadLocale; 28 import org.apache.hivemind.util.Defense; 29 import org.apache.tapestry.asset.AssetFactory; 30 import org.apache.tapestry.coerce.ValueConverter; 31 import org.apache.tapestry.describe.HTMLDescriber; 32 import org.apache.tapestry.engine.IPageSource; 33 import org.apache.tapestry.engine.IPropertySource; 34 import org.apache.tapestry.engine.IScriptSource; 35 import org.apache.tapestry.engine.ISpecificationSource; 36 import org.apache.tapestry.engine.state.ApplicationStateManager; 37 import org.apache.tapestry.error.ExceptionPresenter; 38 import org.apache.tapestry.error.RequestExceptionReporter; 39 import org.apache.tapestry.error.StaleLinkExceptionPresenter; 40 import org.apache.tapestry.error.StaleSessionExceptionPresenter; 41 import org.apache.tapestry.listener.ListenerInvoker; 42 import org.apache.tapestry.listener.ListenerMapSource; 43 import org.apache.tapestry.markup.MarkupWriterSource; 44 import org.apache.tapestry.services.ComponentMessagesSource; 45 import org.apache.tapestry.services.ComponentPropertySource; 46 import org.apache.tapestry.services.CookieSource; 47 import org.apache.tapestry.services.DataSqueezer; 48 import org.apache.tapestry.services.Infrastructure; 49 import org.apache.tapestry.services.LinkFactory; 50 import org.apache.tapestry.services.ObjectPool; 51 import org.apache.tapestry.services.RequestCycleFactory; 52 import org.apache.tapestry.services.ResetEventCoordinator; 53 import org.apache.tapestry.services.ResponseRenderer; 54 import org.apache.tapestry.services.ServiceMap; 55 import org.apache.tapestry.services.TemplateSource; 56 import org.apache.tapestry.spec.IApplicationSpecification; 57 import org.apache.tapestry.web.WebContext; 58 import org.apache.tapestry.web.WebContextResource; 59 import org.apache.tapestry.web.WebRequest; 60 import org.apache.tapestry.web.WebResponse; 61 62 68 public class InfrastructureImpl implements Infrastructure 69 { 70 73 private List _normalContributions; 74 75 78 private List _overrideContributions; 79 80 private Map _properties = new HashMap (); 81 82 private boolean _initialized; 83 84 private String _mode; 85 86 private ErrorLog _errorLog; 87 88 private ClassResolver _classResolver; 89 90 private ThreadLocale _threadLocale; 91 92 public void setLocale(Locale locale) 93 { 94 _threadLocale.setLocale(locale); 95 } 96 97 public String getApplicationId() 98 { 99 return (String ) getProperty("applicationId"); 100 } 101 102 public IPropertySource getApplicationPropertySource() 103 { 104 return (IPropertySource) getProperty("applicationPropertySource"); 105 } 106 107 public IApplicationSpecification getApplicationSpecification() 108 { 109 return (IApplicationSpecification) getProperty("applicationSpecification"); 110 } 111 112 public ApplicationStateManager getApplicationStateManager() 113 { 114 return (ApplicationStateManager) getProperty("applicationStateManager"); 115 } 116 117 public ClassResolver getClassResolver() 118 { 119 return _classResolver; 120 } 121 122 public ComponentMessagesSource getComponentMessagesSource() 123 { 124 return (ComponentMessagesSource) getProperty("componentMessagesSource"); 125 } 126 127 public ComponentPropertySource getComponentPropertySource() 128 { 129 return (ComponentPropertySource) getProperty("componentPropertySource"); 130 } 131 132 public String getContextPath() 133 { 134 return getRequest().getContextPath(); 135 } 136 137 public Resource getContextRoot() 138 { 139 WebContext context = (WebContext) getProperty("context"); 140 141 return new WebContextResource(context, "/"); 142 } 143 144 public DataSqueezer getDataSqueezer() 145 { 146 return (DataSqueezer) getProperty("dataSqueezer"); 147 } 148 149 public IPropertySource getGlobalPropertySource() 150 { 151 return (IPropertySource) getProperty("globalPropertySource"); 152 } 153 154 public LinkFactory getLinkFactory() 155 { 156 return (LinkFactory) getProperty("linkFactory"); 157 } 158 159 public ObjectPool getObjectPool() 160 { 161 return (ObjectPool) getProperty("objectPool"); 162 } 163 164 public IPageSource getPageSource() 165 { 166 return (IPageSource) getProperty("pageSource"); 167 } 168 169 public WebRequest getRequest() 170 { 171 return (WebRequest) getProperty("request"); 172 } 173 174 public RequestCycleFactory getRequestCycleFactory() 175 { 176 return (RequestCycleFactory) getProperty("requestCycleFactory"); 177 } 178 179 public RequestExceptionReporter getRequestExceptionReporter() 180 { 181 return (RequestExceptionReporter) getProperty("requestExceptionReporter"); 182 } 183 184 public ResetEventCoordinator getResetEventCoordinator() 185 { 186 return (ResetEventCoordinator) getProperty("resetEventCoordinator"); 187 } 188 189 public WebResponse getResponse() 190 { 191 return (WebResponse) getProperty("response"); 192 } 193 194 public ResponseRenderer getResponseRenderer() 195 { 196 return (ResponseRenderer) getProperty("responseRenderer"); 197 } 198 199 public IScriptSource getScriptSource() 200 { 201 return (IScriptSource) getProperty("scriptSource"); 202 } 203 204 public ServiceMap getServiceMap() 205 { 206 return (ServiceMap) getProperty("serviceMap"); 207 } 208 209 public ISpecificationSource getSpecificationSource() 210 { 211 return (ISpecificationSource) getProperty("specificationSource"); 212 } 213 214 public TemplateSource getTemplateSource() 215 { 216 return (TemplateSource) getProperty("templateSource"); 217 } 218 219 public String getOutputEncoding() 220 { 221 return getApplicationPropertySource().getPropertyValue( 222 "org.apache.tapestry.output-encoding"); 223 } 224 225 public MarkupWriterSource getMarkupWriterSource() 226 { 227 return (MarkupWriterSource) getProperty("markupWriterSource"); 228 } 229 230 public HTMLDescriber getHTMLDescriber() 231 { 232 return (HTMLDescriber) getProperty("HTMLDescriber"); 233 } 234 235 public ExceptionPresenter getExceptionPresenter() 236 { 237 return (ExceptionPresenter) getProperty("exceptionPresenter"); 238 } 239 240 public ListenerMapSource getListenerMapSource() 241 { 242 return (ListenerMapSource) getProperty("listenerMapSource"); 243 } 244 245 public StaleSessionExceptionPresenter getStaleSessionExceptionPresenter() 246 { 247 return (StaleSessionExceptionPresenter) getProperty("staleSessionExceptionPresenter"); 248 } 249 250 public StaleLinkExceptionPresenter getStaleLinkExceptionPresenter() 251 { 252 return (StaleLinkExceptionPresenter) getProperty("staleLinkExceptionPresenter"); 253 } 254 255 public ValueConverter getValueConverter() 256 { 257 return (ValueConverter) getProperty("valueConverter"); 258 } 259 260 public ListenerInvoker getListenerInvoker() 261 { 262 return (ListenerInvoker) getProperty("listenerInvoker"); 263 } 264 265 public AssetFactory getAssetFactory() 266 { 267 return (AssetFactory) getProperty("assetFactory"); 268 } 269 270 public CookieSource getCookieSource() 271 { 272 return (CookieSource) getProperty("cookieSource"); 273 } 274 275 public Object getProperty(String propertyName) 276 { 277 Defense.notNull(propertyName, "propertyName"); 278 279 if (!_initialized) 280 throw new IllegalStateException (ImplMessages.infrastructureNotInitialized()); 281 282 Object result = _properties.get(propertyName); 283 284 if (result == null) 285 throw new ApplicationRuntimeException(ImplMessages 286 .missingInfrastructureProperty(propertyName)); 287 288 return result; 289 } 290 291 public synchronized void initialize(String mode) 292 { 293 Defense.notNull(mode, "mode"); 294 295 if (_initialized) 296 throw new IllegalStateException (ImplMessages.infrastructureAlreadyInitialized( 297 mode, 298 _mode)); 299 300 Map normalByMode = buildMapFromContributions(_normalContributions, mode); 301 Map normal = buildMapFromContributions(_normalContributions, null); 302 Map overrideByMode = buildMapFromContributions(_overrideContributions, mode); 303 Map override = buildMapFromContributions(_overrideContributions, null); 304 305 addToProperties(overrideByMode); 306 addToProperties(override); 307 addToProperties(normalByMode); 308 addToProperties(normal); 309 310 _mode = mode; 311 _initialized = true; 312 } 313 314 private Map buildMapFromContributions(List contributions, String mode) 315 { 316 Map result = new HashMap (); 317 318 Iterator i = contributions.iterator(); 319 while (i.hasNext()) 320 { 321 InfrastructureContribution ic = (InfrastructureContribution) i.next(); 322 323 if (!ic.matchesMode(mode)) 324 continue; 325 326 String propertyName = ic.getProperty(); 327 328 InfrastructureContribution existing = (InfrastructureContribution) result 329 .get(propertyName); 330 331 if (existing != null) 332 { 333 _errorLog.error(ImplMessages.duplicateInfrastructureContribution(ic, existing 334 .getLocation()), ic.getLocation(), null); 335 continue; 336 } 337 338 result.put(propertyName, ic); 339 } 340 341 return result; 342 } 343 344 352 353 private void addToProperties(Map map) 354 { 355 Iterator i = map.values().iterator(); 356 while (i.hasNext()) 357 { 358 InfrastructureContribution ic = (InfrastructureContribution) i.next(); 359 String propertyName = ic.getProperty(); 360 361 if (_properties.containsKey(propertyName)) 362 continue; 363 364 _properties.put(propertyName, ic.getObject()); 365 } 366 } 367 368 public void setClassResolver(ClassResolver classResolver) 369 { 370 _classResolver = classResolver; 371 } 372 373 public void setThreadLocale(ThreadLocale threadLocale) 374 { 375 _threadLocale = threadLocale; 376 } 377 378 public void setNormalContributions(List normalContributions) 379 { 380 _normalContributions = normalContributions; 381 } 382 383 public void setOverrideContributions(List overrideContributions) 384 { 385 _overrideContributions = overrideContributions; 386 } 387 388 public void setErrorLog(ErrorLog errorLog) 389 { 390 _errorLog = errorLog; 391 } 392 } | Popular Tags |