1 22 package org.jboss.kernel.plugins.deployment.xml; 23 24 import java.util.ArrayList ; 25 import java.util.HashSet ; 26 import java.util.List ; 27 import java.util.Set ; 28 29 import javax.xml.namespace.NamespaceContext ; 30 import javax.xml.namespace.QName ; 31 32 import org.jboss.beans.metadata.plugins.*; 33 import org.jboss.beans.metadata.plugins.factory.GenericBeanFactoryMetaData; 34 import org.jboss.beans.metadata.spi.BeanMetaDataFactory; 35 import org.jboss.beans.metadata.spi.DemandMetaData; 36 import org.jboss.beans.metadata.spi.DependencyMetaData; 37 import org.jboss.beans.metadata.spi.MetaDataVisitorNode; 38 import org.jboss.beans.metadata.spi.ParameterMetaData; 39 import org.jboss.beans.metadata.spi.PropertyMetaData; 40 import org.jboss.beans.metadata.spi.SupplyMetaData; 41 import org.jboss.beans.metadata.spi.ValueMetaData; 42 import org.jboss.beans.metadata.plugins.InjectionType; 43 import org.jboss.dependency.spi.ControllerMode; 44 import org.jboss.dependency.spi.ControllerState; 45 import org.jboss.kernel.plugins.deployment.AbstractKernelDeployment; 46 import org.jboss.logging.Logger; 47 import org.jboss.xb.binding.sunday.unmarshalling.CharactersHandler; 48 import org.jboss.xb.binding.sunday.unmarshalling.DefaultElementHandler; 49 import org.jboss.xb.binding.sunday.unmarshalling.DefaultElementInterceptor; 50 import org.jboss.xb.binding.sunday.unmarshalling.ElementBinding; 51 import org.jboss.xb.binding.sunday.unmarshalling.SchemaBinding; 52 import org.jboss.xb.binding.sunday.unmarshalling.TypeBinding; 53 import org.xml.sax.Attributes ; 54 55 62 public class BeanSchemaBinding 63 { 64 65 private static final Logger log = Logger.getLogger(BeanSchemaBinding.class); 66 67 68 private static final String BEAN_DEPLOYER_NS = "urn:jboss:bean-deployer"; 69 70 71 private static final QName deploymentTypeQName = new QName (BEAN_DEPLOYER_NS, "deploymentType"); 72 73 74 private static final QName beanTypeQName = new QName (BEAN_DEPLOYER_NS, "beanType"); 75 76 77 private static final QName beanQName = new QName (BEAN_DEPLOYER_NS, "bean"); 78 79 80 private static final QName beanFactoryTypeQName = new QName (BEAN_DEPLOYER_NS, "beanfactoryType"); 81 82 83 private static final QName beanFactoryQName = new QName (BEAN_DEPLOYER_NS, "beanfactory"); 84 85 86 private static final QName classloaderTypeQName = new QName (BEAN_DEPLOYER_NS, "classloaderType"); 87 88 89 private static final QName classloaderQName = new QName (BEAN_DEPLOYER_NS, "classloader"); 90 91 92 private static final QName constructorTypeQName = new QName (BEAN_DEPLOYER_NS, "constructorType"); 93 94 95 private static final QName constructorQName = new QName (BEAN_DEPLOYER_NS, "constructor"); 96 97 98 private static final QName factoryQName = new QName (BEAN_DEPLOYER_NS, "factory"); 99 100 101 private static final QName parameterTypeQName = new QName (BEAN_DEPLOYER_NS, "parameterType"); 102 103 104 private static final QName parameterQName = new QName (BEAN_DEPLOYER_NS, "parameter"); 105 106 107 private static final QName lifecycleTypeQName = new QName (BEAN_DEPLOYER_NS, "lifecycleType"); 108 109 110 private static final QName createQName = new QName (BEAN_DEPLOYER_NS, "create"); 111 112 113 private static final QName startQName = new QName (BEAN_DEPLOYER_NS, "start"); 114 115 116 private static final QName stopQName = new QName (BEAN_DEPLOYER_NS, "stop"); 117 118 119 private static final QName destroyQName = new QName (BEAN_DEPLOYER_NS, "destroy"); 120 121 122 private static final QName propertyTypeQName = new QName (BEAN_DEPLOYER_NS, "propertyType"); 123 124 125 private static final QName propertyQName = new QName (BEAN_DEPLOYER_NS, "property"); 126 127 128 private static final QName dependsTypeQName = new QName (BEAN_DEPLOYER_NS, "dependsType"); 129 130 131 private static final QName dependsQName = new QName (BEAN_DEPLOYER_NS, "depends"); 132 133 134 private static final QName demandTypeQName = new QName (BEAN_DEPLOYER_NS, "demandType"); 135 136 137 private static final QName demandQName = new QName (BEAN_DEPLOYER_NS, "demand"); 138 139 140 private static final QName supplyTypeQName = new QName (BEAN_DEPLOYER_NS, "supplyType"); 141 142 143 private static final QName supplyQName = new QName (BEAN_DEPLOYER_NS, "supply"); 144 145 146 private static final QName dependencyTypeQName = new QName (BEAN_DEPLOYER_NS, "dependencyType"); 147 148 149 private static final QName injectionTypeQName = new QName (BEAN_DEPLOYER_NS, "injectionType"); 150 151 152 private static final QName injectQName = new QName (BEAN_DEPLOYER_NS, "inject"); 153 154 155 private static final QName plainValueTypeQName = new QName (BEAN_DEPLOYER_NS, "plainValueType"); 156 157 158 private static final QName valueTypeQName = new QName (BEAN_DEPLOYER_NS, "valueType"); 159 160 161 private static final QName valueQName = new QName (BEAN_DEPLOYER_NS, "value"); 162 163 164 private static final QName nullQName = new QName (BEAN_DEPLOYER_NS, "null"); 165 166 167 private static final QName collectionTypeQName = new QName (BEAN_DEPLOYER_NS, "collectionType"); 168 169 170 private static final QName collectionQName = new QName (BEAN_DEPLOYER_NS, "collection"); 171 172 173 private static final QName listTypeQName = new QName (BEAN_DEPLOYER_NS, "listType"); 174 175 176 private static final QName listQName = new QName (BEAN_DEPLOYER_NS, "list"); 177 178 179 private static final QName setTypeQName = new QName (BEAN_DEPLOYER_NS, "setType"); 180 181 182 private static final QName setQName = new QName (BEAN_DEPLOYER_NS, "set"); 183 184 185 private static final QName arrayTypeQName = new QName (BEAN_DEPLOYER_NS, "arrayType"); 186 187 188 private static final QName arrayQName = new QName (BEAN_DEPLOYER_NS, "array"); 189 190 191 private static final QName mapTypeQName = new QName (BEAN_DEPLOYER_NS, "mapType"); 192 193 194 private static final QName mapQName = new QName (BEAN_DEPLOYER_NS, "map"); 195 196 197 private static final QName entryTypeQName = new QName (BEAN_DEPLOYER_NS, "entryType"); 198 199 200 private static final QName entryQName = new QName (BEAN_DEPLOYER_NS, "entry"); 201 202 203 private static final QName keyQName = new QName (BEAN_DEPLOYER_NS, "key"); 204 205 206 private static SchemaBinding schemaBinding; 207 208 209 public static ValueMetaDataElementInterceptor VALUES = new ValueMetaDataElementInterceptor(); 210 211 212 public static NullValueElementInterceptor NULLVALUES = new NullValueElementInterceptor(); 213 214 219 protected synchronized static void init(SchemaBinding schemaBinding) 220 { 221 log.warn("You should use the 2.0 version of the Microcontainer xml. xmlns='urn:jboss:bean-deployer:2.0'"); 223 224 BeanSchemaBinding.schemaBinding = schemaBinding; 225 226 TypeBinding deploymentType = schemaBinding.getType(deploymentTypeQName); 228 deploymentType.setHandler(new DefaultElementHandler() 229 { 230 public Object startElement(Object parent, QName name, ElementBinding element) 231 { 232 return new AbstractKernelDeployment(); 233 } 234 }); 235 236 deploymentType.pushInterceptor(classloaderQName, new DefaultElementInterceptor() 238 { 239 public void add(Object parent, Object child, QName name) 240 { 241 AbstractKernelDeployment deployment = (AbstractKernelDeployment) parent; 242 AbstractClassLoaderMetaData classloader = (AbstractClassLoaderMetaData) child; 243 deployment.setClassLoader(classloader); 244 List <BeanMetaDataFactory> beans = deployment.getBeanFactories(); 246 if (beans == null) 247 { 248 beans = new ArrayList <BeanMetaDataFactory>(); 249 deployment.setBeanFactories(beans); 250 } 251 beans.add(classloader); 252 } 253 }); 254 255 deploymentType.pushInterceptor(beanQName, new DefaultElementInterceptor() 257 { 258 public void add(Object parent, Object child, QName name) 259 { 260 AbstractKernelDeployment deployment = (AbstractKernelDeployment) parent; 261 AbstractBeanMetaData bean = (AbstractBeanMetaData) child; 262 List <BeanMetaDataFactory> beans = deployment.getBeanFactories(); 263 if (beans == null) 264 { 265 beans = new ArrayList <BeanMetaDataFactory>(); 266 deployment.setBeanFactories(beans); 267 } 268 beans.add(bean); 269 } 270 }); 271 272 deploymentType.pushInterceptor(beanFactoryQName, new DefaultElementInterceptor() 274 { 275 public void add(Object parent, Object child, QName name) 276 { 277 AbstractKernelDeployment deployment = (AbstractKernelDeployment) parent; 278 AbstractBeanMetaData bean = (AbstractBeanMetaData) child; 279 List <BeanMetaDataFactory> beans = deployment.getBeanFactories(); 280 if (beans == null) 281 { 282 beans = new ArrayList <BeanMetaDataFactory>(); 283 deployment.setBeanFactories(beans); 284 } 285 beans.add(bean); 286 } 287 }); 288 289 TypeBinding beanType = schemaBinding.getType(beanTypeQName); 291 292 315 beanType.setHandler(new DefaultElementHandler() 316 { 317 318 public Object startElement(Object parent, QName name, ElementBinding element) 319 { 320 return new AbstractBeanMetaData(); 321 } 322 323 public void attributes(Object o, QName elementName, ElementBinding element, Attributes attrs, NamespaceContext nsCtx) 324 { 325 AbstractBeanMetaData bean = (AbstractBeanMetaData) o; 326 for (int i = 0; i < attrs.getLength(); ++i) 327 { 328 String localName = attrs.getLocalName(i); 329 if ("name".equals(localName)) 330 bean.setName(attrs.getValue(i)); 331 else if ("class".equals(localName)) 332 bean.setBean(attrs.getValue(i)); 333 else if ("mode".equals(localName)) 334 bean.setMode(new ControllerMode(attrs.getValue(i))); 335 } 336 } 337 }); 338 339 TypeBinding beanFactoryType = schemaBinding.getType(beanFactoryTypeQName); 341 beanFactoryType.setHandler(new DefaultElementHandler() 342 { 343 public Object startElement(Object parent, QName name, ElementBinding element) 344 { 345 return new GenericBeanFactoryMetaData(); 346 } 347 348 public void attributes(Object o, QName elementName, ElementBinding element, Attributes attrs, NamespaceContext nsCtx) 349 { 350 GenericBeanFactoryMetaData bean = (GenericBeanFactoryMetaData) o; 351 for (int i = 0; i < attrs.getLength(); ++i) 352 { 353 String localName = attrs.getLocalName(i); 354 if ("name".equals(localName)) 355 bean.setName(attrs.getValue(i)); 356 else if ("class".equals(localName)) 357 bean.setBeanClass(attrs.getValue(i)); 358 else if ("mode".equals(localName)) 359 bean.setMode(new ControllerMode(attrs.getValue(i))); 360 } 361 } 362 }); 363 364 beanType.pushInterceptor(classloaderQName, new DefaultElementInterceptor() 366 { 367 public void add(Object parent, Object child, QName name) 368 { 369 AbstractBeanMetaData bean = (AbstractBeanMetaData) parent; 370 AbstractClassLoaderMetaData classloader = (AbstractClassLoaderMetaData) child; 371 bean.setClassLoader(classloader); 372 } 373 }); 374 375 beanFactoryType.pushInterceptor(classloaderQName, new DefaultElementInterceptor() 377 { 378 public void add(Object parent, Object child, QName name) 379 { 380 GenericBeanFactoryMetaData bean = (GenericBeanFactoryMetaData) parent; 381 AbstractClassLoaderMetaData classloader = (AbstractClassLoaderMetaData) child; 382 bean.setClassLoader(classloader); 383 } 384 }); 385 386 beanType.pushInterceptor(constructorQName, new DefaultElementInterceptor() 388 { 389 public void add(Object parent, Object child, QName name) 390 { 391 AbstractBeanMetaData bean = (AbstractBeanMetaData) parent; 392 AbstractConstructorMetaData constructor = (AbstractConstructorMetaData) child; 393 bean.setConstructor(constructor); 394 } 395 }); 396 397 beanFactoryType.pushInterceptor(constructorQName, new DefaultElementInterceptor() 399 { 400 public void add(Object parent, Object child, QName name) 401 { 402 GenericBeanFactoryMetaData bean = (GenericBeanFactoryMetaData) parent; 403 AbstractConstructorMetaData constructor = (AbstractConstructorMetaData) child; 404 bean.setBeanConstructor(constructor); 405 } 406 }); 407 408 TypeBinding classloaderType = schemaBinding.getType(classloaderTypeQName); 410 configureValueBindings(classloaderType); 411 classloaderType.setHandler(new DefaultElementHandler() 412 { 413 public Object startElement(Object parent, QName name, ElementBinding element) 414 { 415 return new AbstractClassLoaderMetaData(); 416 } 417 418 public void attributes(Object o, QName elementName, ElementBinding element, Attributes attrs, NamespaceContext nsCtx) 419 { 420 } 421 }); 422 423 TypeBinding constructorType = schemaBinding.getType(constructorTypeQName); 425 constructorType.setHandler(new DefaultElementHandler() 426 { 427 public Object startElement(Object parent, QName name, ElementBinding element) 428 { 429 return new AbstractConstructorMetaData(); 430 } 431 432 public void attributes(Object o, QName elementName, ElementBinding element, Attributes attrs, NamespaceContext nsCtx) 433 { 434 AbstractConstructorMetaData constructor = (AbstractConstructorMetaData) o; 435 for (int i = 0; i < attrs.getLength(); ++i) 436 { 437 String localName = attrs.getLocalName(i); 438 if ("factoryClass".equals(localName)) 439 constructor.setFactoryClass(attrs.getValue(i)); 440 else if ("factoryMethod".equals(localName)) 441 constructor.setFactoryMethod(attrs.getValue(i)); 442 } 443 } 444 }); 445 446 constructorType.pushInterceptor(factoryQName, new DefaultElementInterceptor() 448 { 449 public void add(Object parent, Object child, QName name) 450 { 451 AbstractConstructorMetaData constructor = (AbstractConstructorMetaData) parent; 452 AbstractDependencyValueMetaData factory = (AbstractDependencyValueMetaData) child; 453 constructor.setFactory(factory); 454 } 455 }); 456 457 constructorType.pushInterceptor(parameterQName, new DefaultElementInterceptor() 459 { 460 public void add(Object parent, Object child, QName name) 461 { 462 AbstractConstructorMetaData constructor = (AbstractConstructorMetaData) parent; 463 AbstractParameterMetaData parameter = (AbstractParameterMetaData) child; 464 List <ParameterMetaData> parameters = constructor.getParameters(); 465 if (parameters == null) 466 { 467 parameters = new ArrayList <ParameterMetaData>(); 468 constructor.setParameters(parameters); 469 } 470 parameter.setIndex(parameters.size()); 471 parameters.add(parameter); 472 } 473 }); 474 475 TypeBinding parameterType = schemaBinding.getType(parameterTypeQName); 477 configureValueBindings(parameterType); 478 parameterType.setHandler(new DefaultElementHandler() 479 { 480 public Object startElement(Object parent, QName name, ElementBinding element) 481 { 482 return new AbstractParameterMetaData(); 483 } 484 485 public void attributes(Object o, QName elementName, ElementBinding element, Attributes attrs, NamespaceContext nsCtx) 486 { 487 AbstractParameterMetaData parameter = (AbstractParameterMetaData) o; 488 for (int i = 0; i < attrs.getLength(); ++i) 489 { 490 String localName = attrs.getLocalName(i); 491 if ("class".equals(localName)) 492 parameter.setType(attrs.getValue(i)); 493 } 494 } 495 }); 496 497 parameterType.setSimpleType(new CharactersHandler() 499 { 500 public Object unmarshal(QName qName, TypeBinding typeBinding, NamespaceContext nsCtx, org.jboss.xb.binding.metadata.ValueMetaData valueMetaData, String value) 501 { 502 return new StringValueMetaData(value); 503 } 504 505 public void setValue(QName qName, ElementBinding element, Object owner, Object value) 506 { 507 AbstractParameterMetaData parameter = (AbstractParameterMetaData) owner; 508 parameter.setValue((StringValueMetaData) value); 509 } 510 }); 511 512 beanType.pushInterceptor(createQName, new DefaultElementInterceptor() 514 { 515 public void add(Object parent, Object child, QName name) 516 { 517 AbstractBeanMetaData bean = (AbstractBeanMetaData) parent; 518 AbstractLifecycleMetaData lifecycle = (AbstractLifecycleMetaData) child; 519 lifecycle.setType("create"); 520 bean.setCreate(lifecycle); 521 } 522 }); 523 524 beanFactoryType.pushInterceptor(createQName, new DefaultElementInterceptor() 526 { 527 public void add(Object parent, Object child, QName name) 528 { 529 GenericBeanFactoryMetaData bean = (GenericBeanFactoryMetaData) parent; 530 AbstractLifecycleMetaData lifecycle = (AbstractLifecycleMetaData) child; 531 lifecycle.setType("create"); 532 bean.setBeanCreate(lifecycle); 533 } 534 }); 535 536 beanType.pushInterceptor(startQName, new DefaultElementInterceptor() 538 { 539 public void add(Object parent, Object child, QName name) 540 { 541 AbstractBeanMetaData bean = (AbstractBeanMetaData) parent; 542 AbstractLifecycleMetaData lifecycle = (AbstractLifecycleMetaData) child; 543 lifecycle.setType("start"); 544 bean.setStart(lifecycle); 545 } 546 }); 547 548 beanFactoryType.pushInterceptor(startQName, new DefaultElementInterceptor() 550 { 551 public void add(Object parent, Object child, QName name) 552 { 553 GenericBeanFactoryMetaData bean = (GenericBeanFactoryMetaData) parent; 554 AbstractLifecycleMetaData lifecycle = (AbstractLifecycleMetaData) child; 555 lifecycle.setType("start"); 556 bean.setBeanStart(lifecycle); 557 } 558 }); 559 560 beanType.pushInterceptor(stopQName, new DefaultElementInterceptor() 562 { 563 public void add(Object parent, Object child, QName name) 564 { 565 AbstractBeanMetaData bean = (AbstractBeanMetaData) parent; 566 AbstractLifecycleMetaData lifecycle = (AbstractLifecycleMetaData) child; 567 lifecycle.setType("stop"); 568 bean.setStop(lifecycle); 569 } 570 }); 571 572 beanType.pushInterceptor(destroyQName, new DefaultElementInterceptor() 574 { 575 public void add(Object parent, Object child, QName name) 576 { 577 AbstractBeanMetaData bean = (AbstractBeanMetaData) parent; 578 AbstractLifecycleMetaData lifecycle = (AbstractLifecycleMetaData) child; 579 lifecycle.setType("destroy"); 580 bean.setDestroy(lifecycle); 581 } 582 }); 583 584 TypeBinding lifecycleType = schemaBinding.getType(lifecycleTypeQName); 586 lifecycleType.setHandler(new DefaultElementHandler() 587 { 588 public Object startElement(Object parent, QName name, ElementBinding element) 589 { 590 return new AbstractLifecycleMetaData(); 591 } 592 593 public void attributes(Object o, QName elementName, ElementBinding element, Attributes attrs, NamespaceContext nsCtx) 594 { 595 AbstractLifecycleMetaData lifecycle = (AbstractLifecycleMetaData) o; 596 for (int i = 0; i < attrs.getLength(); ++i) 597 { 598 String localName = attrs.getLocalName(i); 599 if ("method".equals(localName)) 600 lifecycle.setMethodName(attrs.getValue(i)); 601 } 602 } 603 }); 604 605 lifecycleType.pushInterceptor(parameterQName, new DefaultElementInterceptor() 607 { 608 public void add(Object parent, Object child, QName name) 609 { 610 AbstractLifecycleMetaData lifecycle = (AbstractLifecycleMetaData) parent; 611 AbstractParameterMetaData parameter = (AbstractParameterMetaData) child; 612 List <ParameterMetaData> parameters = lifecycle.getParameters(); 613 if (parameters == null) 614 { 615 parameters = new ArrayList <ParameterMetaData>(); 616 lifecycle.setParameters(parameters); 617 } 618 parameter.setIndex(parameters.size()); 619 parameters.add(parameter); 620 } 621 }); 622 623 beanType.pushInterceptor(propertyQName, new DefaultElementInterceptor() 625 { 626 public void add(Object parent, Object child, QName name) 627 { 628 AbstractBeanMetaData bean = (AbstractBeanMetaData) parent; 629 AbstractPropertyMetaData property = (AbstractPropertyMetaData) child; 630 Set <PropertyMetaData> properties = bean.getProperties(); 631 if (properties == null) 632 { 633 properties = new HashSet <PropertyMetaData>(); 634 bean.setProperties(properties); 635 } 636 properties.add(property); 637 } 638 }); 639 640 beanFactoryType.pushInterceptor(propertyQName, new DefaultElementInterceptor() 642 { 643 public void add(Object parent, Object child, QName name) 644 { 645 GenericBeanFactoryMetaData bean = (GenericBeanFactoryMetaData) parent; 646 AbstractPropertyMetaData property = (AbstractPropertyMetaData) child; 647 bean.addBeanProperty(property); 648 } 649 }); 650 651 beanType.pushInterceptor(dependsQName, new DefaultElementInterceptor() 653 { 654 public void add(Object parent, Object child, QName name) 655 { 656 AbstractBeanMetaData bean = (AbstractBeanMetaData) parent; 657 AbstractDependencyMetaData dependency = (AbstractDependencyMetaData) child; 658 Set <DependencyMetaData> depends = bean.getDepends(); 659 if (depends == null) 660 { 661 depends = new HashSet <DependencyMetaData>(); 662 bean.setDepends(depends); 663 } 664 depends.add(dependency); 665 } 666 }); 667 668 beanFactoryType.pushInterceptor(dependsQName, new DefaultElementInterceptor() 670 { 671 public void add(Object parent, Object child, QName name) 672 { 673 GenericBeanFactoryMetaData bean = (GenericBeanFactoryMetaData) parent; 674 AbstractDependencyMetaData dependency = (AbstractDependencyMetaData) child; 675 Set <DependencyMetaData> depends = bean.getDepends(); 676 if (depends == null) 677 { 678 depends = new HashSet <DependencyMetaData>(); 679 bean.setDepends(depends); 680 } 681 depends.add(dependency); 682 } 683 }); 684 685 beanType.pushInterceptor(demandQName, new DefaultElementInterceptor() 687 { 688 public void add(Object parent, Object child, QName name) 689 { 690 AbstractBeanMetaData bean = (AbstractBeanMetaData) parent; 691 AbstractDemandMetaData demand = (AbstractDemandMetaData) child; 692 Set <DemandMetaData> demands = bean.getDemands(); 693 if (demands == null) 694 { 695 demands = new HashSet <DemandMetaData>(); 696 bean.setDemands(demands); 697 } 698 demands.add(demand); 699 } 700 }); 701 702 beanFactoryType.pushInterceptor(demandQName, new DefaultElementInterceptor() 704 { 705 public void add(Object parent, Object child, QName name) 706 { 707 AbstractBeanMetaData bean = (AbstractBeanMetaData) parent; 708 AbstractDemandMetaData demand = (AbstractDemandMetaData) child; 709 Set <DemandMetaData> demands = bean.getDemands(); 710 if (demands == null) 711 { 712 demands = new HashSet <DemandMetaData>(); 713 bean.setDemands(demands); 714 } 715 demands.add(demand); 716 } 717 }); 718 719 beanType.pushInterceptor(supplyQName, new DefaultElementInterceptor() 721 { 722 public void add(Object parent, Object child, QName name) 723 { 724 AbstractBeanMetaData bean = (AbstractBeanMetaData) parent; 725 AbstractSupplyMetaData supply = (AbstractSupplyMetaData) child; 726 Set <SupplyMetaData> supplies = bean.getSupplies(); 727 if (supplies == null) 728 { 729 supplies = new HashSet <SupplyMetaData>(); 730 bean.setSupplies(supplies); 731 } 732 supplies.add(supply); 733 } 734 }); 735 736 beanFactoryType.pushInterceptor(supplyQName, new DefaultElementInterceptor() 738 { 739 public void add(Object parent, Object child, QName name) 740 { 741 AbstractBeanMetaData bean = (AbstractBeanMetaData) parent; 742 AbstractSupplyMetaData supply = (AbstractSupplyMetaData) child; 743 Set <SupplyMetaData> supplies = bean.getSupplies(); 744 if (supplies == null) 745 { 746 supplies = new HashSet <SupplyMetaData>(); 747 bean.setSupplies(supplies); 748 } 749 supplies.add(supply); 750 } 751 }); 752 753 TypeBinding propertyType = schemaBinding.getType(propertyTypeQName); 755 configureValueBindings(propertyType); 756 propertyType.setHandler(new DefaultElementHandler() 757 { 758 public Object startElement(Object parent, QName name, ElementBinding element) 759 { 760 return new AbstractPropertyMetaData(); 761 } 762 763 public void attributes(Object o, QName elementName, ElementBinding element, Attributes attrs, NamespaceContext nsCtx) 764 { 765 AbstractPropertyMetaData property = (AbstractPropertyMetaData) o; 766 for (int i = 0; i < attrs.getLength(); ++i) 767 { 768 String localName = attrs.getLocalName(i); 769 if ("name".equals(localName)) 770 property.setName(attrs.getValue(i)); 771 else if ("class".equals(localName)) 772 { 773 StringValueMetaData svmd = new StringValueMetaData(); 774 svmd.setType(attrs.getValue(i)); 775 property.setValue(svmd); 776 } 777 } 778 } 779 780 public Object endElement(Object o, QName qName, ElementBinding element) 781 { 782 AbstractPropertyMetaData x = (AbstractPropertyMetaData) o; 783 String name = x.getName(); 784 if (name == null || name.trim().length() == 0) 785 throw new IllegalArgumentException ("Null or empty property name."); 786 return o; 787 } 788 }); 789 790 propertyType.setSimpleType(new CharactersHandler() 792 { 793 public Object unmarshal(QName qName, TypeBinding typeBinding, NamespaceContext nsCtx, org.jboss.xb.binding.metadata.ValueMetaData valueMetaData, String value) 794 { 795 return new StringValueMetaData(value); 796 } 797 798 public void setValue(QName qName, ElementBinding element, Object owner, Object value) 799 { 800 AbstractPropertyMetaData property = (AbstractPropertyMetaData) owner; 801 StringValueMetaData svmd = (StringValueMetaData) value; 802 ValueMetaData vmd = property.getValue(); 803 if (vmd != null && vmd instanceof StringValueMetaData) 804 { 805 StringValueMetaData previous = (StringValueMetaData) vmd; 806 String type = previous.getType(); 807 if (type != null) 808 svmd.setType(type); 809 } 810 property.setValue(svmd); 811 } 812 }); 813 814 TypeBinding dependsType = schemaBinding.getType(dependsTypeQName); 816 dependsType.setHandler(new DefaultElementHandler() 817 { 818 public Object startElement(Object parent, QName name, ElementBinding element) 819 { 820 return new AbstractDependencyMetaData(); 821 } 822 823 public void attributes(Object o, QName elementName, ElementBinding element, Attributes attrs, NamespaceContext nsCtx) 824 { 825 } 826 827 public Object endElement(Object o, QName qName, ElementBinding element) 828 { 829 AbstractDependencyMetaData x = (AbstractDependencyMetaData) o; 830 String name = (String ) x.getDependency(); 831 if (name == null || name.trim().length() == 0) 832 throw new IllegalArgumentException ("Null or empty dependency."); 833 return o; 834 } 835 }); 836 837 dependsType.setSimpleType(new CharactersHandler() 839 { 840 public Object unmarshal(QName qName, TypeBinding typeBinding, NamespaceContext nsCtx, org.jboss.xb.binding.metadata.ValueMetaData valueMetaData, String value) 841 { 842 return value; 843 } 844 845 public void setValue(QName qname, ElementBinding element, Object owner, Object value) 846 { 847 AbstractDependencyMetaData depends = (AbstractDependencyMetaData) owner; 848 depends.setDependency(value); 849 } 850 }); 851 852 TypeBinding demandType = schemaBinding.getType(demandTypeQName); 854 demandType.setHandler(new DefaultElementHandler() 855 { 856 public Object startElement(Object parent, QName name, ElementBinding element) 857 { 858 return new AbstractDemandMetaData(); 859 } 860 861 public void attributes(Object o, QName elementName, ElementBinding element, Attributes attrs, NamespaceContext nsCtx) 862 { 863 AbstractDemandMetaData demand = (AbstractDemandMetaData) o; 864 for (int i = 0; i < attrs.getLength(); ++i) 865 { 866 String localName = attrs.getLocalName(i); 867 if ("state".equals(localName)) 868 demand.setWhenRequired(new ControllerState(attrs.getValue(i))); 869 } 870 } 871 872 public Object endElement(Object o, QName qName, ElementBinding element) 873 { 874 AbstractDemandMetaData x = (AbstractDemandMetaData) o; 875 String name = (String ) x.getDemand(); 876 if (name == null || name.trim().length() == 0) 877 throw new IllegalArgumentException ("Null or empty demand."); 878 return o; 879 } 880 }); 881 882 demandType.setSimpleType(new CharactersHandler() 884 { 885 public Object unmarshal(QName qName, TypeBinding typeBinding, NamespaceContext nsCtx, org.jboss.xb.binding.metadata.ValueMetaData valueMetaData, String value) 886 { 887 return value; 888 } 889 890 public void setValue(QName qname, ElementBinding element, Object owner, Object value) 891 { 892 AbstractDemandMetaData demand = (AbstractDemandMetaData) owner; 893 demand.setDemand(value); 894 } 895 }); 896 897 TypeBinding supplyType = schemaBinding.getType(supplyTypeQName); 899 supplyType.setHandler(new DefaultElementHandler() 900 { 901 public Object startElement(Object parent, QName name, ElementBinding element) 902 { 903 return new AbstractSupplyMetaData(); 904 } 905 906 public Object endElement(Object o, QName qName, ElementBinding element) 907 { 908 AbstractSupplyMetaData x = (AbstractSupplyMetaData) o; 909 String name = (String ) x.getSupply(); 910 if (name == null || name.trim().length() == 0) 911 throw new IllegalArgumentException ("Null or empty supply."); 912 return o; 913 } 914 }); 915 916 supplyType.setSimpleType(new CharactersHandler() 918 { 919 public Object unmarshal(QName qName, TypeBinding typeBinding, NamespaceContext nsCtx, org.jboss.xb.binding.metadata.ValueMetaData valueMetaData, String value) 920 { 921 return value; 922 } 923 924 public void setValue(QName qName, ElementBinding element, Object owner, Object value) 925 { 926 AbstractSupplyMetaData supply = (AbstractSupplyMetaData) owner; 927 supply.setSupply(value); 928 } 929 }); 930 931 TypeBinding dependencyType = schemaBinding.getType(dependencyTypeQName); 933 dependencyType.setHandler(new DefaultElementHandler() 934 { 935 public Object startElement(Object parent, QName name, ElementBinding element) 936 { 937 return new AbstractDependencyValueMetaData(); 938 } 939 940 public void attributes(Object o, QName elementName, ElementBinding element, Attributes attrs, NamespaceContext nsCtx) 941 { 942 AbstractDependencyValueMetaData dependency = (AbstractDependencyValueMetaData) o; 943 for (int i = 0; i < attrs.getLength(); ++i) 944 { 945 String localName = attrs.getLocalName(i); 946 if ("bean".equals(localName)) 947 dependency.setValue(attrs.getValue(i)); 948 else if ("property".equals(localName)) 949 dependency.setProperty(attrs.getValue(i)); 950 else if ("state".equals(localName)) 951 dependency.setDependentState(new ControllerState(attrs.getValue(i))); 952 else if ("whenRequired".equals(localName)) 953 dependency.setWhenRequiredState(new ControllerState(attrs.getValue(i))); 954 } 955 } 956 957 public Object endElement(Object o, QName qName, ElementBinding element) 958 { 959 AbstractDependencyValueMetaData x = (AbstractDependencyValueMetaData) o; 960 String name = (String ) x.getUnderlyingValue(); 961 if (name == null || name.trim().length() == 0) 962 throw new IllegalArgumentException ("Null or empty bean in injection/factory."); 963 return o; 964 } 965 }); 966 967 TypeBinding injectionType = schemaBinding.getType(injectionTypeQName); 969 injectionType.setHandler(new DefaultElementHandler() 970 { 971 public Object startElement(Object parent, QName name, ElementBinding element) 972 { 973 AbstractInjectionValueMetaData vmd = new AbstractInjectionValueMetaData(); 974 if (parent instanceof AbstractPropertyMetaData) 975 { 976 AbstractPropertyMetaData x = (AbstractPropertyMetaData) parent; 977 vmd.setPropertyMetaData(x); 978 } 979 return vmd; 980 } 981 982 public void attributes(Object o, QName elementName, ElementBinding element, Attributes attrs, NamespaceContext nsCtx) 983 { 984 AbstractInjectionValueMetaData injection = (AbstractInjectionValueMetaData) o; 985 for (int i = 0; i < attrs.getLength(); ++i) 986 { 987 String localName = attrs.getLocalName(i); 988 if ("bean".equals(localName)) 989 injection.setValue(attrs.getValue(i)); 990 else if ("property".equals(localName)) 991 injection.setProperty(attrs.getValue(i)); 992 else if ("state".equals(localName)) 993 injection.setDependentState(new ControllerState(attrs.getValue(i))); 994 else if ("whenRequired".equals(localName)) 995 injection.setWhenRequiredState(new ControllerState(attrs.getValue(i))); 996 else if ("type".equals(localName)) 997 injection.setInjectionType(new InjectionType(attrs.getValue(i))); 998 } 999 } 1000 1001 }); 1002 1003 TypeBinding plainValueType = schemaBinding.getType(plainValueTypeQName); 1005 plainValueType.setHandler(new DefaultElementHandler() 1006 { 1007 public Object startElement(Object parent, QName name, ElementBinding element) 1008 { 1009 return new StringValueMetaData(); 1010 } 1011 1012 public void attributes(Object o, QName elementName, ElementBinding element, Attributes attrs, NamespaceContext nsCtx) 1013 { 1014 StringValueMetaData value = (StringValueMetaData) o; 1015 for (int i = 0; i < attrs.getLength(); ++i) 1016 { 1017 String localName = attrs.getLocalName(i); 1018 if ("class".equals(localName)) 1019 value.setType(attrs.getValue(i)); 1020 } 1021 } 1022 }); 1023 1024 plainValueType.setSimpleType(new CharactersHandler() 1026 { 1027 public Object unmarshal(QName qName, TypeBinding typeBinding, NamespaceContext nsCtx, org.jboss.xb.binding.metadata.ValueMetaData valueMetaData, String value) 1028 { 1029 return value; 1030 } 1031 1032 public void setValue(QName qName, ElementBinding element, Object owner, Object value) 1033 { 1034 StringValueMetaData valueMetaData = (StringValueMetaData) owner; 1035 valueMetaData.setValue(value); 1036 } 1037 }); 1038 1039 TypeBinding valueType = schemaBinding.getType(valueTypeQName); 1041 configureValueBindings(valueType); 1042 valueType.setHandler(new DefaultElementHandler() 1043 { 1044 public Object startElement(Object parent, QName name, ElementBinding element) 1045 { 1046 return new AbstractValueMetaData(new StringValueMetaData()); 1047 } 1048 1049 public void attributes(Object o, QName elementName, ElementBinding element, Attributes attrs, NamespaceContext nsCtx) 1050 { 1051 AbstractValueMetaData value = (AbstractValueMetaData) o; 1052 StringValueMetaData string = (StringValueMetaData) value.getValue(); 1053 for (int i = 0; i < attrs.getLength(); ++i) 1054 { 1055 String localName = attrs.getLocalName(i); 1056 if ("class".equals(localName)) 1057 string.setType(attrs.getValue(i)); 1058 } 1059 } 1060 }); 1061 1062 valueType.setSimpleType(new CharactersHandler() 1064 { 1065 public Object unmarshal(QName qName, TypeBinding typeBinding, NamespaceContext nsCtx, org.jboss.xb.binding.metadata.ValueMetaData valueMetaData, String value) 1066 { 1067 return value; 1068 } 1069 1070 public void setValue(QName qName, ElementBinding element, Object owner, Object value) 1071 { 1072 AbstractValueMetaData valueMetaData = (AbstractValueMetaData) owner; 1073 StringValueMetaData string = (StringValueMetaData) valueMetaData.getValue(); 1074 string.setValue(value); 1075 } 1076 }); 1077 1078 configureCollection(collectionTypeQName); 1080 1081 configureCollection(listTypeQName); 1083 1084 configureCollection(setTypeQName); 1086 1087 configureCollection(arrayTypeQName); 1089 1090 TypeBinding mapType = schemaBinding.getType(mapTypeQName); 1092 mapType.setHandler(new DefaultElementHandler() 1093 { 1094 public Object startElement(Object parent, QName name, ElementBinding element) 1095 { 1096 return new AbstractMapMetaData(); 1097 } 1098 1099 public void attributes(Object o, QName elementName, ElementBinding element, Attributes attrs, NamespaceContext nsCtx) 1100 { 1101 AbstractMapMetaData collection = (AbstractMapMetaData) o; 1102 for (int i = 0; i < attrs.getLength(); ++i) 1103 { 1104 String localName = attrs.getLocalName(i); 1105 if ("class".equals(localName)) 1106 collection.setType(attrs.getValue(i)); 1107 else if ("keyClass".equals(localName)) 1108 collection.setKeyType(attrs.getValue(i)); 1109 else if ("valueClass".equals(localName)) 1110 collection.setValueType(attrs.getValue(i)); 1111 } 1112 } 1113 }); 1114 1115 mapType.pushInterceptor(entryQName, new DefaultElementInterceptor() 1117 { 1118 public void add(Object parent, Object child, QName name) 1119 { 1120 AbstractMapMetaData map = (AbstractMapMetaData) parent; 1121 MapEntry entry = (MapEntry) child; 1122 AbstractValueMetaData entryKey = (AbstractValueMetaData) entry.key; 1123 if (entryKey == null) 1124 throw new IllegalArgumentException ("No key in map entry"); 1125 AbstractValueMetaData entryValue = (AbstractValueMetaData) entry.value; 1126 if (entryValue == null) 1127 throw new IllegalArgumentException ("No value in map entry"); 1128 map.put((MetaDataVisitorNode) entryKey.getValue(), (MetaDataVisitorNode) entryValue.getValue()); 1129 } 1130 }); 1131 1132 TypeBinding entryType = schemaBinding.getType(entryTypeQName); 1134 entryType.setHandler(new DefaultElementHandler() 1135 { 1136 public Object startElement(Object parent, QName name, ElementBinding element) 1137 { 1138 return new MapEntry(); 1139 } 1140 }); 1141 1142 entryType.pushInterceptor(keyQName, new DefaultElementInterceptor() 1144 { 1145 public void add(Object parent, Object child, QName name) 1146 { 1147 MapEntry entry = (MapEntry) parent; 1148 entry.key = (ValueMetaData) child; 1149 } 1150 }); 1151 1152 entryType.pushInterceptor(valueQName, new DefaultElementInterceptor() 1154 { 1155 public void add(Object parent, Object child, QName name) 1156 { 1157 MapEntry entry = (MapEntry) parent; 1158 entry.value = (ValueMetaData) child; 1159 } 1160 }); 1161 } 1162 1163 1166 private static void configureCollection(QName qname) 1167 { 1168 TypeBinding collectionType = schemaBinding.getType(qname); 1169 collectionType.setHandler(new DefaultElementHandler() 1170 { 1171 public Object startElement(Object parent, QName name, ElementBinding element) 1172 { 1173 if (collectionQName.equals(name)) 1174 return new AbstractCollectionMetaData(); 1175 else if (listQName.equals(name)) 1176 return new AbstractListMetaData(); 1177 else if (setQName.equals(name)) 1178 return new AbstractSetMetaData(); 1179 else if (arrayQName.equals(name)) 1180 return new AbstractArrayMetaData(); 1181 else 1182 throw new IllegalArgumentException ("Unknown collection qname=" + name); 1183 } 1184 1185 public void attributes(Object o, QName elementName, ElementBinding element, Attributes attrs, NamespaceContext nsCtx) 1186 { 1187 AbstractCollectionMetaData collection = (AbstractCollectionMetaData) o; 1188 for (int i = 0; i < attrs.getLength(); ++i) 1189 { 1190 String localName = attrs.getLocalName(i); 1191 if ("class".equals(localName)) 1192 collection.setType(attrs.getValue(i)); 1193 else if ("elementClass".equals(localName)) 1194 collection.setElementType(attrs.getValue(i)); 1195 } 1196 } 1197 }); 1198 1199 configureValueBindings(collectionType); 1200 } 1201 1202 private static void configureValueBindings(TypeBinding typeBinding) 1203 { 1204 typeBinding.pushInterceptor(valueQName, VALUES); 1206 1207 typeBinding.pushInterceptor(injectQName, VALUES); 1209 1210 typeBinding.pushInterceptor(collectionQName, VALUES); 1212 1213 typeBinding.pushInterceptor(listQName, VALUES); 1215 1216 typeBinding.pushInterceptor(setQName, VALUES); 1218 1219 typeBinding.pushInterceptor(arrayQName, VALUES); 1221 1222 typeBinding.pushInterceptor(mapQName, VALUES); 1224 1225 typeBinding.pushInterceptor(nullQName, NULLVALUES); 1227 } 1228 1229 private static class NullValueElementInterceptor extends DefaultElementInterceptor 1230 { 1231 public void add(Object parent, Object child, QName name) 1232 { 1233 if (parent instanceof AbstractCollectionMetaData) 1234 { 1235 AbstractCollectionMetaData collection = (AbstractCollectionMetaData) parent; 1236 collection.add(new AbstractValueMetaData()); 1237 } 1238 else if (parent instanceof AbstractParameterMetaData) 1239 { 1240 AbstractParameterMetaData valueMetaData = (AbstractParameterMetaData) parent; 1241 valueMetaData.setValue(new AbstractValueMetaData()); 1242 } 1243 else if (parent instanceof AbstractPropertyMetaData) 1244 { 1245 AbstractPropertyMetaData valueMetaData = (AbstractPropertyMetaData) parent; 1246 valueMetaData.setValue(new AbstractValueMetaData()); 1247 } 1248 else if (parent instanceof AbstractClassLoaderMetaData) 1249 { 1250 AbstractClassLoaderMetaData valueMetaData = (AbstractClassLoaderMetaData) parent; 1251 valueMetaData.setClassLoader(new AbstractValueMetaData()); 1252 } 1253 else 1254 { 1255 AbstractValueMetaData valueMetaData = (AbstractValueMetaData) parent; 1256 valueMetaData.setValue(new AbstractValueMetaData()); 1257 } 1258 } 1259 } 1260 1261 private static class ValueMetaDataElementInterceptor extends DefaultElementInterceptor 1262 { 1263 public void add(Object parent, Object child, QName name) 1264 { 1265 if (parent instanceof AbstractCollectionMetaData) 1266 { 1267 AbstractCollectionMetaData collection = (AbstractCollectionMetaData) parent; 1268 ValueMetaData value = (ValueMetaData) child; 1269 collection.add(value); 1270 } 1271 else if (parent instanceof AbstractParameterMetaData) 1272 { 1273 AbstractParameterMetaData valueMetaData = (AbstractParameterMetaData) parent; 1274 ValueMetaData value = (ValueMetaData) child; 1275 valueMetaData.setValue(value); 1276 } 1277 else if (parent instanceof AbstractPropertyMetaData) 1278 { 1279 AbstractPropertyMetaData valueMetaData = (AbstractPropertyMetaData) parent; 1280 ValueMetaData value = (ValueMetaData) child; 1281 valueMetaData.setValue(value); 1282 } 1283 else if (parent instanceof AbstractClassLoaderMetaData) 1284 { 1285 AbstractClassLoaderMetaData valueMetaData = (AbstractClassLoaderMetaData) parent; 1286 ValueMetaData value = (ValueMetaData) child; 1287 valueMetaData.setClassLoader(value); 1288 } 1289 else 1290 { 1291 AbstractValueMetaData valueMetaData = (AbstractValueMetaData) parent; 1292 ValueMetaData value = (ValueMetaData) child; 1293 valueMetaData.setValue(value); 1294 } 1295 } 1296 } 1297 1298 private static class MapEntry 1299 { 1300 public Object key; 1301 public Object value; 1302 } 1303} 1304 | Popular Tags |