1 17 package org.apache.geronimo.deployment; 18 19 import java.util.Collection ; 20 import java.util.Collections ; 21 import java.util.Iterator ; 22 23 import javax.xml.namespace.QName ; 24 25 import org.apache.xmlbeans.QNameSet; 26 import org.apache.geronimo.deployment.xmlbeans.XmlBeansUtil; 27 import org.apache.geronimo.gbean.ReferenceCollection; 28 import org.apache.geronimo.gbean.ReferenceCollectionListener; 29 import org.apache.geronimo.gbean.ReferenceCollectionEvent; 30 31 34 public abstract class AbstractBuilderCollection { 35 protected final Collection builders; 36 protected final QName basePlanElementName; 37 protected QNameSet specQNames = QNameSet.EMPTY; 38 protected QNameSet planQNames = QNameSet.EMPTY; 39 40 protected AbstractBuilderCollection(Collection builders, final QName basePlanElementName) { 41 this.builders = builders == null ? Collections.EMPTY_SET : builders; 42 this.basePlanElementName = basePlanElementName; 43 if (builders instanceof ReferenceCollection) { 44 ((ReferenceCollection) builders).addReferenceCollectionListener(new ReferenceCollectionListener() { 45 46 public void memberAdded(ReferenceCollectionEvent event) { 47 addBuilder(event.getMember()); 48 } 49 50 public void memberRemoved(ReferenceCollectionEvent event) { 51 Object builder = event.getMember(); 52 QNameSet builderSpecQNames = ((AbstractNamespaceBuilder) builder).getSpecQNameSet(); 53 specQNames = specQNames.intersect(builderSpecQNames.inverse()); 54 QNameSet builderPlanQNames = ((AbstractNamespaceBuilder) builder).getPlanQNameSet(); 55 planQNames = planQNames.intersect(builderPlanQNames.inverse()); 56 XmlBeansUtil.unregisterSubstitutionGroupElements(basePlanElementName, builderPlanQNames); 57 } 58 }); 59 } 60 for (Iterator iterator = this.builders.iterator(); iterator.hasNext();) { 61 Object builder = iterator.next(); 62 addBuilder(builder); 63 } 64 } 65 66 67 protected void addBuilder(Object builder) { 68 QNameSet builderSpecQNames = ((AbstractNamespaceBuilder) builder).getSpecQNameSet(); 69 QNameSet builderPlanQNames = ((AbstractNamespaceBuilder) builder).getPlanQNameSet(); 70 if (builderSpecQNames == null) { 71 throw new IllegalStateException ("Builder " + builder + " is missing spec qnames"); 72 } 73 if (builderPlanQNames == null) { 74 throw new IllegalStateException ("Builder " + builder + " is missing plan qnames"); 75 } 76 if (!specQNames.isDisjoint(builderSpecQNames) && !planQNames.isDisjoint(builderPlanQNames)) { 77 throw new IllegalArgumentException ("Duplicate builderSpecQNames in builder set: " + builderSpecQNames + " and duplicate builderPlanQNames in builder set: " + builderPlanQNames); 78 } 79 try { 80 specQNames = specQNames.union(builderSpecQNames); 81 planQNames = planQNames.union(builderPlanQNames); 82 } catch (NullPointerException e) { 83 throw (IllegalArgumentException ) new IllegalArgumentException ("could not merge qnamesets for builder " + builder).initCause(e); 84 85 } 86 XmlBeansUtil.registerSubstitutionGroupElements(basePlanElementName, builderPlanQNames); 88 } 89 90 public QNameSet getSpecQNameSet() { 91 return specQNames; 92 } 93 94 public QNameSet getPlanQNameSet() { 95 return null; 96 } 97 } 98 | Popular Tags |