1 15 package org.apache.tapestry; 16 17 import java.io.IOException ; 18 import java.io.InputStream ; 19 import java.text.MessageFormat ; 20 import java.util.ArrayList ; 21 import java.util.Collection ; 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.util.Locale ; 26 import java.util.Map ; 27 import java.util.Properties ; 28 import java.util.ResourceBundle ; 29 import java.util.Set ; 30 31 import org.apache.hivemind.ApplicationRuntimeException; 32 import org.apache.hivemind.HiveMind; 33 import org.apache.hivemind.Location; 34 import org.apache.hivemind.service.ClassFabUtils; 35 import org.apache.tapestry.event.ChangeObserver; 36 import org.apache.tapestry.event.ObservedChangeEvent; 37 import org.apache.tapestry.services.ServiceConstants; 38 import org.apache.tapestry.spec.IComponentSpecification; 39 import org.apache.tapestry.util.StringSplitter; 40 41 48 49 public final class Tapestry 50 { 51 59 60 public final static String ACTION_SERVICE = "action"; 61 62 72 73 public final static String DIRECT_SERVICE = "direct"; 74 75 85 86 public final static String EXTERNAL_SERVICE = "external"; 87 88 94 95 public final static String PAGE_SERVICE = "page"; 96 97 101 102 public final static String HOME_SERVICE = "home"; 103 104 108 109 public static final String RESTART_SERVICE = "restart"; 110 111 114 115 public static final String ASSET_SERVICE = "asset"; 116 117 126 127 public static final String RESET_SERVICE = "reset"; 128 129 136 137 public static final String SERVICE_QUERY_PARAMETER_NAME = ServiceConstants.SERVICE; 138 139 150 151 public static final String PARAMETERS_QUERY_PARAMETER_NAME = ServiceConstants.PARAMETER; 152 153 161 162 public static final String TEMPLATE_EXTENSION_PROPERTY = "org.apache.tapestry.template-extension"; 163 164 169 170 public static final String LINK_COMPONENT_ATTRIBUTE_NAME = "org.apache.tapestry.active-link-component"; 171 172 178 179 public static final String PARAMETER_PROPERTY_NAME_SUFFIX = "Binding"; 180 181 187 188 public static final String REQUEST_DECODER_EXTENSION_NAME = "org.apache.tapestry.request-decoder"; 189 190 198 199 public static final String MULTIPART_DECODER_EXTENSION_NAME = "org.apache.tapestry.multipart-decoder"; 200 201 207 208 public static final String ABSTRACTPAGE_VALIDATE_METHOD_ID = "AbstractPage.validate()"; 209 210 216 217 public static final String ABSTRACTPAGE_DETACH_METHOD_ID = "AbstractPage.detach()"; 218 219 226 227 public static final String SIMPLE_PROPERTY_NAME_PATTERN = "^_?[a-zA-Z]\\w*$"; 228 229 236 237 public static final String MONITOR_FACTORY_EXTENSION_NAME = "org.apache.tapestry.monitor-factory"; 238 239 243 public static final String OGNL_TYPE_CONVERTER = "org.apache.tapestry.ognl-type-converter"; 244 245 248 249 private Tapestry() 250 { 251 } 252 253 256 257 public static final String VERSION = readVersion(); 258 259 264 265 private static ResourceBundle _strings; 266 267 271 272 private static final Map _localeMap = new HashMap (); 273 274 static 275 { 276 Locale [] locales = Locale.getAvailableLocales(); 277 for (int i = 0; i < locales.length; i++) 278 { 279 _localeMap.put(locales[i].toString(), locales[i]); 280 } 281 } 282 283 286 287 private static final ThreadLocal _invokedMethodIds = new ThreadLocal (); 288 289 294 295 public static void copyInformalBindings(IComponent source, IComponent destination) 296 { 297 Collection names = source.getBindingNames(); 298 299 if (names == null) 300 return; 301 302 IComponentSpecification specification = source.getSpecification(); 303 Iterator i = names.iterator(); 304 305 while (i.hasNext()) 306 { 307 String name = (String ) i.next(); 308 309 311 if (specification.getParameter(name) == null) 312 { 313 IBinding binding = source.getBinding(name); 314 315 destination.setBinding(name, binding); 316 } 317 } 318 } 319 320 325 326 public static Locale getLocale(String s) 327 { 328 Locale result = null; 329 330 synchronized (_localeMap) 331 { 332 result = (Locale ) _localeMap.get(s); 333 } 334 335 if (result == null) 336 { 337 StringSplitter splitter = new StringSplitter('_'); 338 String [] terms = splitter.splitToArray(s); 339 340 switch (terms.length) 341 { 342 case 1: 343 344 result = new Locale (terms[0], ""); 345 break; 346 347 case 2: 348 349 result = new Locale (terms[0], terms[1]); 350 break; 351 352 case 3: 353 354 result = new Locale (terms[0], terms[1], terms[2]); 355 break; 356 357 default: 358 359 throw new IllegalArgumentException ("Unable to convert '" + s + "' to a Locale."); 360 } 361 362 synchronized (_localeMap) 363 { 364 _localeMap.put(s, result); 365 } 366 367 } 368 369 return result; 370 371 } 372 373 378 379 public static void close(InputStream stream) 380 { 381 if (stream != null) 382 { 383 try 384 { 385 stream.close(); 386 } 387 catch (IOException ex) 388 { 389 } 391 } 392 } 393 394 400 401 public static String format(String key, Object [] args) 402 { 403 if (_strings == null) 404 _strings = ResourceBundle.getBundle("org.apache.tapestry.TapestryStrings"); 405 406 String pattern = _strings.getString(key); 407 408 if (args == null) 409 return pattern; 410 411 return MessageFormat.format(pattern, args); 412 } 413 414 419 420 public static String getMessage(String key) 421 { 422 return format(key, null); 423 } 424 425 430 431 public static String format(String key, Object arg) 432 { 433 return format(key, new Object [] 434 { arg }); 435 } 436 437 442 443 public static String format(String key, Object arg1, Object arg2) 444 { 445 return format(key, new Object [] 446 { arg1, arg2 }); 447 } 448 449 454 455 public static String format(String key, Object arg1, Object arg2, Object arg3) 456 { 457 return format(key, new Object [] 458 { arg1, arg2, arg3 }); 459 } 460 461 private static final String UNKNOWN_VERSION = "Unknown"; 462 463 466 467 private static final String readVersion() 468 { 469 Properties props = new Properties (); 470 471 try 472 { 473 InputStream in = Tapestry.class.getResourceAsStream("version.properties"); 474 475 if (in == null) 476 return UNKNOWN_VERSION; 477 478 props.load(in); 479 480 in.close(); 481 482 return props.getProperty("project.version", UNKNOWN_VERSION); 483 } 484 catch (IOException ex) 485 { 486 return UNKNOWN_VERSION; 487 } 488 489 } 490 491 496 497 public static int size(Collection c) 498 { 499 if (c == null) 500 return 0; 501 502 return c.size(); 503 } 504 505 510 511 public static int size(Object [] array) 512 { 513 if (array == null) 514 return 0; 515 516 return array.length; 517 } 518 519 524 525 public static boolean isEmpty(Map map) 526 { 527 return map == null || map.isEmpty(); 528 } 529 530 535 536 public static boolean isEmpty(Collection c) 537 { 538 return c == null || c.isEmpty(); 539 } 540 541 552 553 public static Object [] convertMapToArray(Map map) 554 { 555 if (isEmpty(map)) 556 return null; 557 558 Set entries = map.entrySet(); 559 560 Object [] result = new Object [2 * entries.size()]; 561 int x = 0; 562 563 Iterator i = entries.iterator(); 564 while (i.hasNext()) 565 { 566 Map.Entry entry = (Map.Entry ) i.next(); 567 568 result[x++] = entry.getKey(); 569 result[x++] = entry.getValue(); 570 } 571 572 return result; 573 } 574 575 582 583 public static Map convertArrayToMap(Object [] array) 584 { 585 if (array == null || array.length == 0) 586 return null; 587 588 if (array.length % 2 != 0) 589 throw new IllegalArgumentException (getMessage("Tapestry.even-sized-array")); 590 591 Map result = new HashMap (); 592 593 int x = 0; 594 while (x < array.length) 595 { 596 Object key = array[x++]; 597 Object value = array[x++]; 598 599 result.put(key, value); 600 } 601 602 return result; 603 } 604 605 612 613 public static String getClassName(Class subject) 614 { 615 return ClassFabUtils.getJavaClassName(subject); 616 } 617 618 623 624 public static BindingException createNullBindingException(IBinding binding) 625 { 626 return new BindingException(getMessage("null-value-for-binding"), binding); 627 } 628 629 630 631 public static ApplicationRuntimeException createNoSuchComponentException(IComponent component, 632 String id, Location location) 633 { 634 return new ApplicationRuntimeException(format("no-such-component", component 635 .getExtendedId(), id), component, location, null); 636 } 637 638 639 640 public static BindingException createRequiredParameterException(IComponent component, 641 String parameterName) 642 { 643 return new BindingException(format("required-parameter", parameterName, component 644 .getExtendedId()), component, null, component.getBinding(parameterName), null); 645 } 646 647 648 649 public static ApplicationRuntimeException createRenderOnlyPropertyException( 650 IComponent component, String propertyName) 651 { 652 return new ApplicationRuntimeException(format( 653 "render-only-property", 654 propertyName, 655 component.getExtendedId()), component, null, null); 656 } 657 658 664 665 public static void clearMethodInvocations() 666 { 667 _invokedMethodIds.set(null); 668 } 669 670 677 678 public static void addMethodInvocation(Object methodId) 679 { 680 List methodIds = (List ) _invokedMethodIds.get(); 681 682 if (methodIds == null) 683 { 684 methodIds = new ArrayList (); 685 _invokedMethodIds.set(methodIds); 686 } 687 688 methodIds.add(methodId); 689 } 690 691 704 705 public static void checkMethodInvocation(Object methodId, String methodName, Object object) 706 { 707 List methodIds = (List ) _invokedMethodIds.get(); 708 709 if (methodIds != null && methodIds.contains(methodId)) 710 return; 711 712 throw new ApplicationRuntimeException(Tapestry.format( 713 "Tapestry.missing-method-invocation", 714 object.getClass().getName(), 715 methodName)); 716 } 717 718 729 public static void fireObservedChange(IComponent component, String propertyName, Object newValue) 730 { 731 ChangeObserver observer = component.getPage().getChangeObserver(); 732 733 if (observer == null) 734 return; 735 736 ObservedChangeEvent event = new ObservedChangeEvent(component, propertyName, newValue); 737 738 observer.observeChange(event); 739 } 740 741 751 752 public static boolean isBlank(String input) 753 { 754 return HiveMind.isBlank(input); 755 } 756 757 764 765 public static boolean isNonBlank(String input) 766 { 767 return HiveMind.isNonBlank(input); 768 } 769 } | Popular Tags |