KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > deployment > AbstractBuilderCollection


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.deployment;
18
19 import java.util.Collection JavaDoc;
20 import java.util.Collections JavaDoc;
21 import java.util.Iterator JavaDoc;
22
23 import javax.xml.namespace.QName JavaDoc;
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 /**
32  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
33  */

34 public abstract class AbstractBuilderCollection {
35     protected final Collection JavaDoc builders;
36     protected final QName JavaDoc basePlanElementName;
37     protected QNameSet specQNames = QNameSet.EMPTY;
38     protected QNameSet planQNames = QNameSet.EMPTY;
39
40     protected AbstractBuilderCollection(Collection JavaDoc builders, final QName JavaDoc 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 JavaDoc 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 JavaDoc iterator = this.builders.iterator(); iterator.hasNext();) {
61             Object JavaDoc builder = iterator.next();
62             addBuilder(builder);
63         }
64     }
65
66
67     protected void addBuilder(Object JavaDoc builder) {
68         QNameSet builderSpecQNames = ((AbstractNamespaceBuilder) builder).getSpecQNameSet();
69         QNameSet builderPlanQNames = ((AbstractNamespaceBuilder) builder).getPlanQNameSet();
70         if (builderSpecQNames == null) {
71             throw new IllegalStateException JavaDoc("Builder " + builder + " is missing spec qnames");
72         }
73         if (builderPlanQNames == null) {
74             throw new IllegalStateException JavaDoc("Builder " + builder + " is missing plan qnames");
75         }
76         if (!specQNames.isDisjoint(builderSpecQNames) && !planQNames.isDisjoint(builderPlanQNames)) {
77             throw new IllegalArgumentException JavaDoc("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 JavaDoc e) {
83             throw (IllegalArgumentException JavaDoc) new IllegalArgumentException JavaDoc("could not merge qnamesets for builder " + builder).initCause(e);
84
85         }
86         //really?
87
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