1 16 package org.apache.myfaces.application; 17 18 import org.apache.myfaces.application.jsp.JspStateManagerImpl; 19 import org.apache.myfaces.application.jsp.JspViewHandlerImpl; 20 import org.apache.myfaces.el.MethodBindingImpl; 21 import org.apache.myfaces.el.PropertyResolverImpl; 22 import org.apache.myfaces.el.ValueBindingImpl; 23 import org.apache.myfaces.el.VariableResolverImpl; 24 import org.apache.myfaces.util.BiLevelCacheMap; 25 import org.apache.myfaces.util.ClassUtils; 26 import org.apache.myfaces.config.impl.digester.elements.Property; 27 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 import org.apache.commons.beanutils.BeanUtils; 31 32 import javax.faces.FacesException; 33 import javax.faces.application.Application; 34 import javax.faces.application.NavigationHandler; 35 import javax.faces.application.StateManager; 36 import javax.faces.application.ViewHandler; 37 import javax.faces.component.UIComponent; 38 import javax.faces.context.FacesContext; 39 import javax.faces.convert.Converter; 40 import javax.faces.el.*; 41 import javax.faces.event.ActionListener; 42 import javax.faces.validator.Validator; 43 import java.util.*; 44 45 46 79 public class ApplicationImpl 80 extends Application 81 { 82 private static final Log log = LogFactory.getLog(ApplicationImpl.class); 83 84 86 private final Map _valueBindingCache = 87 new BiLevelCacheMap(90) 88 { 89 protected Object newInstance(Object key) 90 { 91 return new ValueBindingImpl(ApplicationImpl.this, (String ) key); 92 } 93 }; 94 95 private Collection _supportedLocales = Collections.EMPTY_SET; 96 private Locale _defaultLocale = Locale.getDefault(); 97 private String _messageBundle; 98 99 private ViewHandler _viewHandler; 100 private NavigationHandler _navigationHandler; 101 private VariableResolver _variableResolver; 102 private PropertyResolver _propertyResolver; 103 private ActionListener _actionListener; 104 private String _defaultRenderKitId; 105 private StateManager _stateManager; 106 107 private final Map _converterIdToClassMap = Collections.synchronizedMap(new HashMap()); 109 private final Map _converterClassToClassMap = Collections.synchronizedMap(new HashMap()); 110 private final Map _converterClassNameToConfigurationMap = Collections.synchronizedMap(new HashMap()); 111 private final Map _componentClassMap = Collections.synchronizedMap(new HashMap()); 112 private final Map _validatorClassMap = Collections.synchronizedMap(new HashMap()); 113 114 115 117 public ApplicationImpl() 118 { 119 _viewHandler = new JspViewHandlerImpl(); 122 _navigationHandler = new NavigationHandlerImpl(); 123 _variableResolver = new VariableResolverImpl(); 124 _propertyResolver = new PropertyResolverImpl(); 125 _actionListener = new ActionListenerImpl(); 126 _defaultRenderKitId = "HTML_BASIC"; 127 _stateManager = new JspStateManagerImpl(); 128 if (log.isTraceEnabled()) log.trace("New Application instance created"); 129 } 130 131 133 public void setActionListener(ActionListener actionListener) 134 { 135 if (actionListener == null) 136 { 137 log.error("setting actionListener to null is not allowed"); 138 throw new NullPointerException ("setting actionListener to null is not allowed"); 139 } 140 _actionListener = actionListener; 141 if (log.isTraceEnabled()) log.trace("set actionListener = " + actionListener.getClass().getName()); 142 } 143 144 public ActionListener getActionListener() 145 { 146 return _actionListener; 147 } 148 149 public Iterator getComponentTypes() 150 { 151 return _componentClassMap.keySet().iterator(); 152 } 153 154 public Iterator getConverterIds() 155 { 156 return _converterIdToClassMap.keySet().iterator(); 157 } 158 159 public Iterator getConverterTypes() 160 { 161 return _converterClassToClassMap.keySet().iterator(); 162 } 163 164 public void setDefaultLocale(Locale locale) 165 { 166 if (locale == null) 167 { 168 log.error("setting locale to null is not allowed"); 169 throw new NullPointerException ("setting locale to null is not allowed"); 170 } 171 _defaultLocale = locale; 172 if (log.isTraceEnabled()) log.trace("set defaultLocale = " + locale.getCountry() + " " + locale.getLanguage()); 173 } 174 175 public Locale getDefaultLocale() 176 { 177 return _defaultLocale; 178 } 179 180 public void setMessageBundle(String messageBundle) 181 { 182 if (messageBundle == null) 183 { 184 log.error("setting messageBundle to null is not allowed"); 185 throw new NullPointerException ("setting messageBundle to null is not allowed"); 186 } 187 _messageBundle = messageBundle; 188 if (log.isTraceEnabled()) log.trace("set MessageBundle = " + messageBundle); 189 } 190 191 public String getMessageBundle() 192 { 193 return _messageBundle; 194 } 195 196 public void setNavigationHandler(NavigationHandler navigationHandler) 197 { 198 if (navigationHandler == null) 199 { 200 log.error("setting navigationHandler to null is not allowed"); 201 throw new NullPointerException ("setting navigationHandler to null is not allowed"); 202 } 203 _navigationHandler = navigationHandler; 204 if (log.isTraceEnabled()) log.trace("set NavigationHandler = " + navigationHandler.getClass().getName()); 205 } 206 207 public NavigationHandler getNavigationHandler() 208 { 209 return _navigationHandler; 210 } 211 212 public void setPropertyResolver(PropertyResolver propertyResolver) 213 { 214 if (propertyResolver == null) 215 { 216 log.error("setting propertyResolver to null is not allowed"); 217 throw new NullPointerException ("setting propertyResolver to null is not allowed"); 218 } 219 _propertyResolver = propertyResolver; 220 if (log.isTraceEnabled()) log.trace("set PropertyResolver = " + propertyResolver.getClass().getName()); 221 } 222 223 public PropertyResolver getPropertyResolver() 224 { 225 return _propertyResolver; 226 } 227 228 public void setSupportedLocales(Collection locales) 229 { 230 if (locales == null) 231 { 232 log.error("setting supportedLocales to null is not allowed"); 233 throw new NullPointerException ("setting supportedLocales to null is not allowed"); 234 } 235 _supportedLocales = locales; 236 if (log.isTraceEnabled()) log.trace("set SupportedLocales"); 237 } 238 239 public Iterator getSupportedLocales() 240 { 241 return _supportedLocales.iterator(); 242 } 243 244 public Iterator getValidatorIds() 245 { 246 return _validatorClassMap.keySet().iterator(); 247 } 248 249 public void setVariableResolver(VariableResolver variableResolver) 250 { 251 if (variableResolver == null) 252 { 253 log.error("setting variableResolver to null is not allowed"); 254 throw new NullPointerException ("setting variableResolver to null is not allowed"); 255 } 256 _variableResolver = variableResolver; 257 if (log.isTraceEnabled()) log.trace("set VariableResolver = " + variableResolver.getClass().getName()); 258 } 259 260 public VariableResolver getVariableResolver() 261 { 262 return _variableResolver; 263 } 264 265 public void setViewHandler(ViewHandler viewHandler) 266 { 267 if (viewHandler == null) 268 { 269 log.error("setting viewHandler to null is not allowed"); 270 throw new NullPointerException ("setting viewHandler to null is not allowed"); 271 } 272 _viewHandler = viewHandler; 273 if (log.isTraceEnabled()) log.trace("set ViewHandler = " + viewHandler.getClass().getName()); 274 } 275 276 public ViewHandler getViewHandler() 277 { 278 return _viewHandler; 279 } 280 281 public void addComponent(String componentType, String componentClassName) 282 { 283 if ((componentType == null) || (componentType.length() == 0)) 284 { 285 log.error("addComponent: componentType = null is not allowed"); 286 throw new NullPointerException ("addComponent: componentType = null ist not allowed"); 287 } 288 if ((componentClassName == null) || (componentClassName.length() == 0)) 289 { 290 log.error("addComponent: component = null is not allowed"); 291 throw new NullPointerException ("addComponent: component = null is not allowed"); 292 } 293 294 try 295 { 296 _componentClassMap.put(componentType, ClassUtils.simpleClassForName(componentClassName)); 297 if (log.isTraceEnabled()) log.trace("add Component class = " + componentClassName + 298 " for type = " + componentType); 299 } 300 catch (Exception e) 301 { 302 log.error("Component class " + componentClassName + " not found", e); 303 } 304 } 305 306 public void addConverter(String converterId, String converterClass) 307 { 308 if ((converterId == null) || (converterId.length() == 0)) 309 { 310 log.error("addConverter: converterId = null is not allowed"); 311 throw new NullPointerException ("addConverter: converterId = null ist not allowed"); 312 } 313 if ((converterClass == null) || (converterClass.length() == 0)) 314 { 315 log.error("addConverter: converterClass = null is not allowed"); 316 throw new NullPointerException ("addConverter: converterClass = null ist not allowed"); 317 } 318 319 320 try 321 { 322 _converterIdToClassMap.put(converterId, ClassUtils.simpleClassForName(converterClass)); 323 if (log.isTraceEnabled()) log.trace("add Converter id = " + converterId + 324 " converterClass = " + converterClass); 325 } 326 catch (Exception e) 327 { 328 log.error("Converter class " + converterClass + " not found", e); 329 } 330 } 331 332 public void addConverter(Class targetClass, String converterClass) 333 { 334 if ((targetClass == null)) 335 { 336 log.error("addConverter: targetClass = null is not allowed"); 337 throw new NullPointerException ("addConverter: targetClass = null ist not allowed"); 338 } 339 if ((converterClass == null) || (converterClass.length() == 0)) 340 { 341 log.error("addConverter: converterClass = null is not allowed"); 342 throw new NullPointerException ("addConverter: converterClass = null ist not allowed"); 343 } 344 345 try 346 { 347 _converterClassToClassMap.put(targetClass, ClassUtils.simpleClassForName(converterClass)); 348 if (log.isTraceEnabled()) log.trace("add Converter for class = " + targetClass + 349 " converterClass = " + converterClass); 350 } 351 catch (Exception e) 352 { 353 log.error("Converter class " + converterClass + " not found", e); 354 } 355 } 356 357 public void addConverterConfiguration(String converterClassName, 358 org.apache.myfaces.config.impl.digester.elements.Converter configuration) 359 { 360 if ((converterClassName == null) || (converterClassName.length() == 0)) 361 { 362 log.error("addConverterConfiguration: converterClassName = null is not allowed"); 363 throw new NullPointerException ("addConverterConfiguration: converterClassName = null ist not allowed"); 364 } 365 if ((configuration == null)) 366 { 367 log.error("addConverterConfiguration: configuration = null is not allowed"); 368 throw new NullPointerException ("addConverterConfiguration: configuration = null ist not allowed"); 369 } 370 371 _converterClassNameToConfigurationMap.put(converterClassName, configuration); 372 } 373 374 public void addValidator(String validatorId, String validatorClass) 375 { 376 if ((validatorId == null) || (validatorId.length() == 0)) 377 { 378 log.error("addValidator: validatorId = null is not allowed"); 379 throw new NullPointerException ("addValidator: validatorId = null ist not allowed"); 380 } 381 if ((validatorClass == null) || (validatorClass.length() == 0)) 382 { 383 log.error("addValidator: validatorClass = null is not allowed"); 384 throw new NullPointerException ("addValidator: validatorClass = null ist not allowed"); 385 } 386 387 try 388 { 389 _validatorClassMap.put(validatorId, ClassUtils.simpleClassForName(validatorClass)); 390 if (log.isTraceEnabled()) log.trace("add Validator id = " + validatorId + 391 " class = " + validatorClass); 392 } 393 catch (Exception e) 394 { 395 log.error("Validator class " + validatorClass + " not found", e); 396 } 397 } 398 399 public UIComponent createComponent(String componentType) 400 throws FacesException 401 { 402 if ((componentType == null) || (componentType.length() == 0)) 403 { 404 log.error("createComponent: componentType = null is not allowed"); 405 throw new NullPointerException ("createComponent: componentType = null is not allowed"); 406 } 407 408 Class componentClass; 409 synchronized (_componentClassMap) 410 { 411 componentClass = (Class ) _componentClassMap.get(componentType); 412 } 413 if (componentClass == null) 414 { 415 log.error("Undefined component type " + componentType); 416 throw new FacesException("Undefined component type " + componentType); 417 } 418 419 try 420 { 421 return (UIComponent) componentClass.newInstance(); 422 } 423 catch (Exception e) 424 { 425 log.error("Could not instantiate component componentType = " + componentType, e); 426 throw new FacesException("Could not instantiate component componentType = " + componentType, e); 427 } 428 } 429 430 public UIComponent createComponent(ValueBinding valueBinding, 431 FacesContext facesContext, 432 String componentType) 433 throws FacesException 434 { 435 if ((valueBinding == null)) 436 { 437 log.error("createComponent: valueBinding = null is not allowed"); 438 throw new NullPointerException ("createComponent: valueBinding = null ist not allowed"); 439 } 440 if ((facesContext == null)) 441 { 442 log.error("createComponent: facesContext = null is not allowed"); 443 throw new NullPointerException ("createComponent: facesContext = null ist not allowed"); 444 } 445 if ((componentType == null) || (componentType.length() == 0)) 446 { 447 log.error("createComponent: componentType = null is not allowed"); 448 throw new NullPointerException ("createComponent: componentType = null ist not allowed"); 449 } 450 451 Object obj = valueBinding.getValue(facesContext); 452 if (obj instanceof UIComponent) 453 { 454 return (UIComponent) obj; 455 } 456 else 457 { 458 UIComponent component = createComponent(componentType); 459 valueBinding.setValue(facesContext, component); 460 return component; 461 } 462 } 463 464 public Converter createConverter(String converterId) 465 { 466 if ((converterId == null) || (converterId.length() == 0)) 467 { 468 log.error("createConverter: converterId = null is not allowed"); 469 throw new NullPointerException ("createConverter: converterId = null ist not allowed"); 470 } 471 472 Class converterClass = (Class ) _converterIdToClassMap.get(converterId); 473 474 try 475 { 476 return (Converter) converterClass.newInstance(); 477 } 478 catch (Exception e) 479 { 480 log.error("Could not instantiate converter " + converterClass, e); 481 throw new FacesException("Could not instantiate converter: " + converterClass, e); 482 } 483 } 484 485 486 public Converter createConverter(Class targetClass) 487 { 488 if (targetClass == null) 489 { 490 log.error("createConverter: targetClass = null is not allowed"); 491 throw new NullPointerException ("createConverter: targetClass = null ist not allowed"); 492 } 493 494 Converter converter = internalCreateConverter(targetClass); 495 496 return converter; 497 } 498 499 500 private Converter internalCreateConverter(Class targetClass) 501 { 502 Class converterClass = (Class )_converterClassToClassMap.get(targetClass); 504 505 if (converterClass == null) 508 { 509 Class interfaces[] = targetClass.getInterfaces(); 510 if (interfaces != null) 511 { 512 for (int i = 0, len = interfaces.length; i < len; i++) 513 { 514 Converter converter = internalCreateConverter(interfaces[i]); 516 if (converter != null) 517 { 518 return converter; 519 } 520 } 521 } 522 } 523 524 if (converterClass != null) 525 { 526 try 527 { 528 Converter converter = (Converter) converterClass.newInstance(); 529 530 org.apache.myfaces.config.impl.digester.elements.Converter converterConfig = 531 (org.apache.myfaces.config.impl.digester.elements.Converter) 532 _converterClassNameToConfigurationMap.get(converterClass.getName()); 533 534 Iterator it = converterConfig.getProperties(); 535 536 while (it.hasNext()) 537 { 538 Property property = (Property) it.next(); 539 540 try 541 { 542 BeanUtils.setProperty(converter,property.getPropertyName(),property.getDefaultValue()); 543 } 544 catch(Throwable th) 545 { 546 log.error("Initializing converter : "+converterClass.getName()+" with property : "+ 547 property.getPropertyName()+" and value : "+property.getDefaultValue()+" failed."); 548 } 549 } 550 551 return converter; 552 } 553 catch (Exception e) 554 { 555 log.error("Could not instantiate converter " + converterClass, e); 556 throw new FacesException("Could not instantiate converter: " + converterClass, e); 557 } 558 } 559 560 if (targetClass == Long.TYPE) 562 { 563 return internalCreateConverter(Long .class); 564 } else if (targetClass == Boolean.TYPE) 565 { 566 return internalCreateConverter(Boolean .class); 567 } else if (targetClass == Double.TYPE) 568 { 569 return internalCreateConverter(Double .class); 570 } else if (targetClass == Byte.TYPE) 571 { 572 return internalCreateConverter(Byte .class); 573 } else if (targetClass == Short.TYPE) 574 { 575 return internalCreateConverter(Short .class); 576 } else if (targetClass == Integer.TYPE) 577 { 578 return internalCreateConverter(Integer .class); 579 } else if (targetClass == Float.TYPE) 580 { 581 return internalCreateConverter(Float .class); 582 } else if (targetClass == Character.TYPE) 583 { 584 return internalCreateConverter(Character .class); 585 } 586 587 588 Class superClazz = targetClass.getSuperclass(); 591 if (superClazz != null) 592 { 593 return internalCreateConverter(superClazz); 594 } 595 else 596 { 597 return null; 598 } 599 600 } 601 602 603 public synchronized MethodBinding createMethodBinding(String reference, Class [] params) 604 throws ReferenceSyntaxException 605 { 606 if ((reference == null) || (reference.length() == 0)) 607 { 608 log.error("createMethodBinding: reference = null is not allowed"); 609 throw new NullPointerException ("createMethodBinding: reference = null ist not allowed"); 610 } 611 612 return new MethodBindingImpl(this, reference, params); 617 } 618 619 public Validator createValidator(String validatorId) throws FacesException 620 { 621 if ((validatorId == null) || (validatorId.length() == 0)) 622 { 623 log.error("createValidator: validatorId = null is not allowed"); 624 throw new NullPointerException ("createValidator: validatorId = null ist not allowed"); 625 } 626 627 Class validatorClass = (Class ) _validatorClassMap.get(validatorId); 628 if (validatorClass == null) 629 { 630 String message = "Unknown converter id '" + validatorId + "'."; 631 log.error(message); 632 throw new FacesException(message); 633 } 634 635 try 636 { 637 return (Validator) validatorClass.newInstance(); 638 } 639 catch (Exception e) 640 { 641 log.error("Could not instantiate converter " + validatorClass, e); 642 throw new FacesException("Could not instantiate converter: " + validatorClass, e); 643 } 644 } 645 646 public ValueBinding createValueBinding(String reference) throws ReferenceSyntaxException 647 { 648 if ((reference == null) || (reference.length() == 0)) 649 { 650 log.error("createValueBinding: reference = null is not allowed"); 651 throw new NullPointerException ("createValueBinding: reference = null is not allowed"); 652 } 653 return (ValueBinding) _valueBindingCache.get(reference); 654 } 655 656 657 public String getDefaultRenderKitId() 658 { 659 return _defaultRenderKitId; 660 } 661 662 public void setDefaultRenderKitId(String defaultRenderKitId) 663 { 664 _defaultRenderKitId = defaultRenderKitId; 665 } 666 667 public StateManager getStateManager() 668 { 669 return _stateManager; 670 } 671 672 public void setStateManager(StateManager stateManager) 673 { 674 _stateManager = stateManager; 675 } 676 } 677 | Popular Tags |