1 22 package org.jboss.kernel.plugins.config; 23 24 import java.util.Collections ; 25 import java.util.HashSet ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.Set ; 29 30 import org.jboss.beans.info.spi.BeanInfo; 31 import org.jboss.beans.info.spi.PropertyInfo; 32 import org.jboss.beans.metadata.spi.BeanMetaData; 33 import org.jboss.beans.metadata.spi.ClassLoaderMetaData; 34 import org.jboss.beans.metadata.spi.ConstructorMetaData; 35 import org.jboss.beans.metadata.spi.ParameterMetaData; 36 import org.jboss.beans.metadata.spi.PropertyMetaData; 37 import org.jboss.beans.metadata.spi.ValueMetaData; 38 import org.jboss.joinpoint.plugins.Config; 39 import org.jboss.joinpoint.spi.ConstructorJoinpoint; 40 import org.jboss.joinpoint.spi.Joinpoint; 41 import org.jboss.joinpoint.spi.JoinpointException; 42 import org.jboss.joinpoint.spi.JoinpointFactory; 43 import org.jboss.joinpoint.spi.MethodJoinpoint; 44 import org.jboss.joinpoint.spi.TargettedJoinpoint; 45 import org.jboss.kernel.spi.config.KernelConfig; 46 import org.jboss.reflect.spi.ConstructorInfo; 47 import org.jboss.reflect.spi.MethodInfo; 48 import org.jboss.reflect.spi.TypeInfo; 49 50 56 public class Configurator extends Config 57 { 58 67 public static Object instantiateAndConfigure(KernelConfig config, BeanInfo info, BeanMetaData metaData) throws Throwable 68 { 69 Object result = instantiate(config, info, metaData); 70 if (metaData != null) 71 configure(result, info, metaData); 72 return result; 73 } 74 75 84 public static Object instantiate(KernelConfig config, BeanInfo info, BeanMetaData metaData) throws Throwable 85 { 86 boolean trace = log.isTraceEnabled(); 87 if (trace) 88 log.trace("Instantiating info=" + info + " metaData=" + metaData); 89 90 ConstructorMetaData constructor = null; 91 if (metaData != null) 92 constructor = metaData.getConstructor(); 93 Joinpoint joinPoint = getConstructorJoinPoint(config, info, constructor, metaData); 94 return joinPoint.dispatch(); 95 } 96 97 107 public static Joinpoint getConstructorJoinPoint(KernelConfig config, BeanInfo info, ConstructorMetaData metaData, BeanMetaData beanMetaData) 108 throws Throwable 109 { 110 boolean trace = log.isTraceEnabled(); 111 112 if (trace) 113 log.trace("Get constructor joinpoint info=" + info + " constructor=" + metaData); 114 115 if (config == null) 116 throw new IllegalArgumentException ("Null config"); 117 118 if (metaData != null) 119 { 120 ClassLoader cl = getClassLoader(beanMetaData); 121 122 ValueMetaData vmd = metaData.getValue(); 123 if (vmd != null) 124 { 125 TypeInfo typeInfo = null; 126 if (info != null) 127 typeInfo = info.getClassInfo(); 128 return new ValueJoinpoint(vmd, typeInfo, cl); 129 } 130 131 vmd = metaData.getFactory(); 132 if (vmd != null) 133 { 134 Object factory = vmd.getValue(null, cl); 136 137 List parameters = metaData.getParameters(); 139 140 BeanInfo factoryInfo = config.getBeanInfo(factory.getClass()); 142 143 MethodJoinpoint joinPoint = findMethod(trace, factoryInfo, cl, metaData.getFactoryMethod(), parameters, false, true); 145 joinPoint.setTarget(factory); 146 MethodInfo minfo = joinPoint.getMethodInfo(); 147 148 if (minfo != null) 150 { 151 TypeInfo[] pinfos = minfo.getParameterTypes(); 152 Object [] params = getParameters(trace, cl, pinfos, parameters); 153 joinPoint.setArguments(params); 154 } 155 return joinPoint; 156 } 157 158 String factoryClassName = metaData.getFactoryClass(); 159 if (factoryClassName != null) 160 { 161 List parameters = metaData.getParameters(); 163 164 BeanInfo factoryInfo = config.getBeanInfo(factoryClassName, cl); 165 166 MethodJoinpoint joinPoint = findMethod(trace, factoryInfo, cl, metaData.getFactoryMethod(), parameters, true, true); 168 MethodInfo minfo = joinPoint.getMethodInfo(); 169 170 if (minfo != null) 172 { 173 TypeInfo[] pinfos = minfo.getParameterTypes(); 174 Object [] params = getParameters(trace, cl, pinfos, parameters); 175 joinPoint.setArguments(params); 176 } 177 return joinPoint; 178 } 179 180 ConstructorJoinpoint joinPoint = findConstructor(trace, info, metaData, beanMetaData); 182 ConstructorInfo cinfo = joinPoint.getConstructorInfo(); 183 184 if (cinfo != null) 186 { 187 TypeInfo[] pinfos = cinfo.getParameterTypes(); 188 Object [] params = getParameters(trace, cl, pinfos, metaData.getParameters()); 189 joinPoint.setArguments(params); 190 } 191 return joinPoint; 192 } 193 194 return findConstructor(trace, info, metaData, beanMetaData); 196 } 197 198 208 public static ConstructorJoinpoint findConstructor(boolean trace, BeanInfo info, ConstructorMetaData metaData, BeanMetaData beanMetaData) throws Exception 209 { 210 ConstructorInfo cinfo = resolveConstructor(trace, info, metaData); 211 JoinpointFactory jpf = info.getJoinpointFactory(); 212 return jpf.getConstructorJoinpoint(cinfo); 213 } 214 215 223 public static ConstructorInfo resolveConstructor(boolean trace, BeanInfo info, ConstructorMetaData metaData) 224 { 225 if (info == null) 226 throw new IllegalArgumentException ("Null bean info"); 227 228 List params = Collections.EMPTY_LIST; 229 if (metaData != null && metaData.getParameters() != null) 230 params = metaData.getParameters(); 231 String [] paramTypes = new String [params.size()]; 232 if (params.isEmpty() == false) 233 { 234 int x = 0; 235 for (Iterator i = params.iterator(); i.hasNext();) 236 { 237 ParameterMetaData pdata = (ParameterMetaData) i.next(); 238 paramTypes[x++] = pdata.getType(); 239 } 240 } 241 return findConstructorInfo(info.getClassInfo(), paramTypes); 242 } 243 244 252 public static void configure(Object object, BeanInfo info, BeanMetaData metaData) throws Throwable 253 { 254 boolean trace = log.isTraceEnabled(); 255 256 if (object == null) 257 throw new IllegalArgumentException ("Null object"); 258 if (info == null) 259 throw new IllegalArgumentException ("Null bean info"); 260 if (metaData == null) 261 throw new IllegalArgumentException ("Null bean metadata"); 262 263 Set properties = metaData.getProperties(); 264 if (properties != null && properties.isEmpty() == false) 265 { 266 ClassLoader cl = getClassLoader(metaData); 267 268 for (Iterator i = metaData.getProperties().iterator(); i.hasNext();) 269 { 270 PropertyMetaData property = (PropertyMetaData) i.next(); 271 configure(trace, object, info, cl, property); 272 } 273 } 274 } 275 276 285 public static void configure(Object object, BeanInfo info, ClassLoader cl, PropertyMetaData metaData) throws Throwable 286 { 287 boolean trace = log.isTraceEnabled(); 288 configure(trace, object, info, cl, metaData); 289 } 290 291 301 public static void configure(boolean trace, Object object, BeanInfo info, ClassLoader cl, PropertyMetaData metaData) throws Throwable 302 { 303 PropertyInfo ainfo = resolveProperty(trace, info, metaData.getName()); 304 configure(trace, object, ainfo, cl, metaData); 305 } 306 307 316 public static void configure(Object object, PropertyInfo info, ClassLoader cl, PropertyMetaData metaData) throws Throwable 317 { 318 boolean trace = log.isTraceEnabled(); 319 configure(trace, object, info, cl, metaData); 320 } 321 322 332 public static void configure(boolean trace, Object object, PropertyInfo info, ClassLoader cl, PropertyMetaData metaData) throws Throwable 333 { 334 if (trace) 335 log.trace("Configuring info=" + info + " metaData=" + metaData); 336 337 TargettedJoinpoint joinPoint = getPropertySetterJoinPoint(trace, info, cl, metaData.getValue()); 338 joinPoint.setTarget(object); 339 340 if (trace) 341 log.trace("Setting property " + joinPoint); 342 343 joinPoint.dispatch(); 344 } 345 346 354 public static TargettedJoinpoint getPropertyGetterJoinPoint(BeanInfo info, String property) throws Throwable 355 { 356 boolean trace = log.isTraceEnabled(); 357 PropertyInfo ainfo = resolveProperty(trace, info, property); 358 return getPropertyGetterJoinPoint(trace, ainfo); 359 } 360 361 369 public static TargettedJoinpoint getPropertyGetterJoinPoint(boolean trace, PropertyInfo info) throws Throwable 370 { 371 if (trace) 372 log.trace("Get property setter join point info=" + info); 373 374 if (info == null) 375 throw new IllegalArgumentException ("Null property info"); 376 377 JoinpointFactory jpf = info.getBeanInfo().getJoinpointFactory(); 378 MethodInfo minfo = info.getGetter(); 379 return getMethodJoinpoint(null, jpf, minfo.getName(), null, null); 380 } 381 382 390 public static Set <TargettedJoinpoint> getPropertySetterJoinPoints(BeanInfo info, BeanMetaData metaData) throws Throwable 391 { 392 boolean trace = log.isTraceEnabled(); 393 394 if (info == null) 395 throw new IllegalArgumentException ("Null bean info"); 396 if (metaData == null) 397 throw new IllegalArgumentException ("Null bean metadata"); 398 399 Set <TargettedJoinpoint> result = new HashSet <TargettedJoinpoint>(); 400 Set <PropertyMetaData> propertys = metaData.getProperties(); 401 if (propertys != null && propertys.isEmpty() == false) 402 { 403 ClassLoader cl = getClassLoader(metaData); 404 405 for (Iterator i = metaData.getProperties().iterator(); i.hasNext();) 406 { 407 PropertyMetaData property = (PropertyMetaData) i.next(); 408 TargettedJoinpoint joinPoint = getPropertySetterJoinPoint(trace, info, cl, property); 409 result.add(joinPoint); 410 } 411 } 412 413 return result; 414 } 415 416 425 public static TargettedJoinpoint getPropertySetterJoinPoint(BeanInfo info, ClassLoader cl, PropertyMetaData metaData) throws Throwable 426 { 427 boolean trace = log.isTraceEnabled(); 428 return getPropertySetterJoinPoint(trace, info, cl, metaData); 429 } 430 431 441 public static TargettedJoinpoint getPropertySetterJoinPoint(boolean trace, BeanInfo info, ClassLoader cl, PropertyMetaData metaData) throws Throwable 442 { 443 PropertyInfo ainfo = resolveProperty(trace, info, metaData.getName()); 444 return getPropertySetterJoinPoint(trace, ainfo, cl, metaData.getValue()); 445 } 446 447 456 public static TargettedJoinpoint getPropertySetterJoinPoint(PropertyInfo info, ClassLoader cl, PropertyMetaData metaData) throws Throwable 457 { 458 boolean trace = log.isTraceEnabled(); 459 return getPropertySetterJoinPoint(trace, info, cl, metaData.getValue()); 460 } 461 462 472 public static TargettedJoinpoint getPropertySetterJoinPoint(BeanInfo info, String property, ClassLoader cl, ValueMetaData vmd) throws Throwable 473 { 474 boolean trace = log.isTraceEnabled(); 475 PropertyInfo ainfo = resolveProperty(trace, info, property); 476 return getPropertySetterJoinPoint(trace, ainfo, cl, vmd); 477 } 478 479 489 public static TargettedJoinpoint getPropertySetterJoinPoint(boolean trace, PropertyInfo info, ClassLoader cl, ValueMetaData metaData) throws Throwable 490 { 491 if (trace) 492 log.trace("Get property setter join point info=" + info + " metaData=" + metaData); 493 494 if (info == null) 495 throw new IllegalArgumentException ("Null property info"); 496 if (metaData == null) 497 throw new IllegalArgumentException ("Null value metadata"); 498 499 TypeInfo type = info.getType(); 500 Object value = metaData.getValue(type, cl); 501 JoinpointFactory jpf = info.getBeanInfo().getJoinpointFactory(); 502 MethodInfo minfo = info.getSetter(); 503 if (minfo == null) 504 throw new IllegalArgumentException ("No setter configured for property: " + info); 505 String [] parameterTypes = getParameterTypes(trace, minfo.getParameterTypes()); 506 return getMethodJoinpoint(null, jpf, minfo.getName(), parameterTypes, new Object [] { value }); 507 } 508 509 517 public static void unconfigure(Object object, BeanInfo info, BeanMetaData metaData) throws Throwable 518 { 519 if (object == null) 520 throw new IllegalArgumentException ("Null object"); 521 if (info == null) 522 throw new IllegalArgumentException ("Null bean info"); 523 if (metaData == null) 524 throw new IllegalArgumentException ("Null bean metadata"); 525 526 Set propertys = metaData.getProperties(); 527 if (propertys != null && propertys.isEmpty() == false) 528 { 529 for (Iterator i = metaData.getProperties().iterator(); i.hasNext();) 530 { 531 PropertyMetaData property = (PropertyMetaData) i.next(); 532 unconfigure(object, info, property); 533 } 534 } 535 } 536 537 545 public static void unconfigure(Object object, BeanInfo info, PropertyMetaData metaData) throws Throwable 546 { 547 boolean trace = log.isTraceEnabled(); 548 PropertyInfo ainfo = resolveProperty(trace, info, metaData.getName()); 549 unconfigure(trace, object, ainfo, metaData); 550 } 551 552 561 public static void unconfigure(boolean trace, Object object, PropertyInfo info, PropertyMetaData metaData) throws Throwable 562 { 563 if (trace) 564 log.trace("Unconfiguring info=" + info + " metaData=" + metaData); 565 566 TargettedJoinpoint joinPoint = getPropertyNullerJoinPoint(info, metaData); 567 joinPoint.setTarget(object); 568 569 if (trace) 570 log.trace("Unsetting property " + joinPoint); 571 572 joinPoint.dispatch(); 573 } 574 575 583 public static Set <TargettedJoinpoint> getPropertyNullerJoinPoints(BeanInfo info, BeanMetaData metaData) throws Throwable 584 { 585 if (info == null) 586 throw new IllegalArgumentException ("Null bean info"); 587 if (metaData == null) 588 throw new IllegalArgumentException ("Null bean metadata"); 589 590 Set <TargettedJoinpoint> result = new HashSet <TargettedJoinpoint>(); 591 Set <PropertyMetaData> propertys = metaData.getProperties(); 592 if (propertys != null && propertys.isEmpty() == false) 593 { 594 for (Iterator i = metaData.getProperties().iterator(); i.hasNext();) 595 { 596 PropertyMetaData property = (PropertyMetaData) i.next(); 597 TargettedJoinpoint joinPoint = getPropertyNullerJoinPoint(info, property); 598 result.add(joinPoint); 599 } 600 } 601 return result; 602 } 603 604 612 public static TargettedJoinpoint getPropertyNullerJoinPoint(BeanInfo info, PropertyMetaData metaData) throws Throwable 613 { 614 boolean trace = log.isTraceEnabled(); 615 PropertyInfo ainfo = resolveProperty(trace, info, metaData.getName()); 616 return getPropertyNullerJoinPoint(ainfo, metaData); 617 } 618 619 627 public static TargettedJoinpoint getPropertyNullerJoinPoint(PropertyInfo info, PropertyMetaData metaData) throws Throwable 628 { 629 boolean trace = log.isTraceEnabled(); 630 if (trace) 631 log.trace("Get property nuller join point info=" + info + " metaData=" + metaData); 632 633 if (info == null) 634 throw new IllegalArgumentException ("Null property info"); 635 636 JoinpointFactory jpf = info.getBeanInfo().getJoinpointFactory(); 637 MethodInfo minfo = info.getSetter(); 638 String [] parameterTypes = getParameterTypes(trace, minfo.getParameterTypes()); 639 return getMethodJoinpoint(null, jpf, minfo.getName(), parameterTypes, new Object [] { null }); 640 } 641 642 651 public static PropertyInfo resolveProperty(boolean trace, BeanInfo info, String name) throws Throwable 652 { 653 if (info == null) 654 throw new IllegalArgumentException ("Null bean info"); 655 if (name == null) 656 throw new IllegalArgumentException ("Null name"); 657 658 Set properties = info.getProperties(); 659 if (properties != null && properties.size() > 0) 660 { 661 for (Iterator i = properties.iterator(); i.hasNext();) 662 { 663 PropertyInfo ainfo = (PropertyInfo) i.next(); 664 if (name.equals(ainfo.getName())) 665 return ainfo; 666 } 667 } 668 669 throw new JoinpointException("Property " + name + " not found for " + info); 670 } 671 672 684 public static MethodJoinpoint findMethod(BeanInfo info, ClassLoader cl, String name, List parameters, boolean isStatic, boolean isPublic) throws Throwable 685 { 686 boolean trace = log.isTraceEnabled(); 687 return findMethod(trace, info, cl, name, parameters, isStatic, isPublic); 688 } 689 690 703 public static MethodJoinpoint findMethod(boolean trace, BeanInfo info, ClassLoader cl, String name, List parameters, boolean isStatic, boolean isPublic) throws Throwable 704 { 705 if (info == null) 706 throw new IllegalArgumentException ("Null bean info"); 707 if (name == null) 708 throw new IllegalArgumentException ("Null name"); 709 710 String [] paramTypes = getParameterTypes(trace, parameters); 711 MethodInfo minfo = findMethodInfo(info.getClassInfo(), name, paramTypes, isStatic, isPublic); 712 JoinpointFactory jpf = info.getJoinpointFactory(); 713 MethodJoinpoint joinPoint = jpf.getMethodJoinpoint(minfo); 714 715 if (minfo != null) 717 { 718 TypeInfo[] pinfos = minfo.getParameterTypes(); 719 Object [] params = getParameters(trace, cl, pinfos, parameters); 720 joinPoint.setArguments(params); 721 } 722 723 return joinPoint; 724 } 725 726 734 public static String [] getParameterTypes(boolean trace, List parameters) throws Throwable 735 { 736 if (parameters == null) 737 return null; 738 739 String [] paramTypes = new String [parameters.size()]; 740 int x = 0; 741 for (Iterator i = parameters.iterator(); i.hasNext();) 742 { 743 ParameterMetaData pmd = (ParameterMetaData) i.next(); 744 paramTypes[x++] = pmd.getType(); 745 } 746 return paramTypes; 747 } 748 749 757 public static String [] getParameterTypes(boolean trace, TypeInfo[] parameters) throws Throwable 758 { 759 if (parameters == null) 760 return null; 761 762 String [] paramTypes = new String [parameters.length]; 763 int x = 0; 764 for (int i = 0; i < parameters.length; ++i) 765 paramTypes[x++] = parameters[i].getName(); 766 return paramTypes; 767 } 768 769 779 public static Object [] getParameters(boolean trace, ClassLoader cl, TypeInfo[] pinfos, List parameters) throws Throwable 780 { 781 if (parameters == null) 782 return null; 783 784 Object [] params = new Object [parameters.size()]; 785 int x = 0; 786 for (Iterator i = parameters.iterator(); i.hasNext();) 787 { 788 ParameterMetaData pdata = (ParameterMetaData) i.next(); 789 ValueMetaData vmd = pdata.getValue(); 790 params[x] = vmd.getValue(pinfos[x], cl); 791 x++; 792 } 793 return params; 794 } 795 796 803 public static ClassLoader getClassLoader(BeanMetaData metaData) throws Throwable 804 { 805 ClassLoaderMetaData clmd = null; 806 if (metaData != null) 807 clmd = metaData.getClassLoader(); 808 return getClassLoader(clmd); 809 } 810 811 818 public static ClassLoader getClassLoader(ClassLoaderMetaData metaData) throws Throwable 819 { 820 ClassLoader tcl = Thread.currentThread().getContextClassLoader(); 821 ClassLoader cl = null; 822 if (metaData != null) 823 { 824 ValueMetaData clVMD = metaData.getClassLoader(); 825 if (clVMD != null) 826 { 827 Object object = clVMD.getValue(null, tcl); 828 if (object != null && object instanceof ClassLoader == false) 829 throw new IllegalArgumentException ("Configured object is not a classloader " + metaData); 830 cl = (ClassLoader ) object; 831 } 832 } 833 if (cl == null) 834 cl = tcl; 835 return cl; 836 } 837 838 841 private static class ValueJoinpoint implements Joinpoint 842 { 843 844 private ValueMetaData vmd; 845 846 847 private TypeInfo info; 848 849 850 private ClassLoader cl; 851 852 859 public ValueJoinpoint(ValueMetaData vmd, TypeInfo info, ClassLoader cl) 860 { 861 this.vmd = vmd; 862 this.info = info; 863 this.cl = cl; 864 } 865 866 public Object dispatch() throws Throwable 867 { 868 return vmd.getValue(info, cl); 869 } 870 871 public String toHumanReadableString() 872 { 873 return vmd.toShortString(); 874 } 875 876 public Object clone() 877 { 878 try 879 { 880 return super.clone(); 881 } 882 catch (CloneNotSupportedException e) 883 { 884 throw new Error (e); 885 } 886 } 887 888 } 889 } 890 | Popular Tags |