1 /**
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one or more
4 * contributor license agreements. See the NOTICE file distributed with
5 * this work for additional information regarding copyright ownership.
6 * The ASF licenses this file to You under the Apache License, Version 2.0
7 * (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 package org.apache.geronimo.j2ee.deployment;
20
21 import java.util.Collection;
22 import java.util.Iterator;
23 import java.util.Collections;
24 import java.util.Map;
25
26 import javax.xml.namespace.QName;
27
28 import org.apache.geronimo.common.DeploymentException;
29 import org.apache.geronimo.deployment.xmlbeans.XmlBeansUtil;
30 import org.apache.geronimo.gbean.ReferenceCollection;
31 import org.apache.geronimo.gbean.ReferenceCollectionEvent;
32 import org.apache.geronimo.gbean.ReferenceCollectionListener;
33 import org.apache.geronimo.kernel.config.Configuration;
34 import org.apache.geronimo.kernel.repository.Environment;
35 import org.apache.xmlbeans.QNameSet;
36 import org.apache.xmlbeans.XmlObject;
37
38 /**
39 * @version $Rev: 470597 $ $Date: 2006-11-02 15:30:55 -0800 (Thu, 02 Nov 2006) $
40 */
41 public class NamingBuilderCollection implements NamingBuilder {
42
43 private final Collection builders;
44 private final QName basePlanElementName;
45 private QNameSet specQNames = QNameSet.EMPTY;
46 private QNameSet planQNames = QNameSet.EMPTY;
47
48 public NamingBuilderCollection(Collection builders, final QName basePlanElementName) {
49 this.builders = builders == null ? Collections.EMPTY_SET : builders;
50 this.basePlanElementName = basePlanElementName;
51 if (builders instanceof ReferenceCollection) {
52 ((ReferenceCollection) builders).addReferenceCollectionListener(new ReferenceCollectionListener() {
53
54 public void memberAdded(ReferenceCollectionEvent event) {
55 addBuilder(event.getMember());
56 }
57
58 public void memberRemoved(ReferenceCollectionEvent event) {
59 Object builder = event.getMember();
60 QNameSet builderSpecQNames = ((NamingBuilder) builder).getSpecQNameSet();
61 specQNames = specQNames.intersect(builderSpecQNames.inverse());
62 QNameSet builderPlanQNames = ((NamingBuilder) builder).getPlanQNameSet();
63 planQNames = planQNames.intersect(builderPlanQNames.inverse());
64 XmlBeansUtil.unregisterSubstitutionGroupElements(basePlanElementName, builderPlanQNames);
65 }
66 });
67 }
68 for (Iterator iterator = this.builders.iterator(); iterator.hasNext();) {
69 Object builder = iterator.next();
70 addBuilder(builder);
71 }
72 }
73
74 private void addBuilder(Object builder) {
75 QNameSet builderSpecQNames = ((NamingBuilder) builder).getSpecQNameSet();
76 QNameSet builderPlanQNames = ((NamingBuilder) builder).getPlanQNameSet();
77 if (builderSpecQNames == null) {
78 throw new IllegalStateException("Builder " + builder + " is missing spec qnames");
79 }
80 if (builderPlanQNames == null) {
81 throw new IllegalStateException("Builder " + builder + " is missing plan qnames");
82 }
83 if (!specQNames.isDisjoint(builderSpecQNames) && !planQNames.isDisjoint(builderPlanQNames)) {
84 throw new IllegalArgumentException("Duplicate builderSpecQNames in builder set: " + builderSpecQNames + " and duplicate builderPlanQNames in builder set: " + builderPlanQNames);
85 }
86 try {
87 specQNames = specQNames.union(builderSpecQNames);
88 planQNames = planQNames.union(builderPlanQNames);
89 } catch (NullPointerException e) {
90 throw (IllegalArgumentException) new IllegalArgumentException("could not merge qnamesets for builder " + builder).initCause(e);
91
92 }
93
94 XmlBeansUtil.registerSubstitutionGroupElements(basePlanElementName, builderPlanQNames);
95 }
96
97 public void buildEnvironment(XmlObject specDD, XmlObject plan, Environment environment) throws DeploymentException {
98 for (Iterator iterator = builders.iterator(); iterator.hasNext();) {
99 NamingBuilder namingBuilder = (NamingBuilder) iterator.next();
100 namingBuilder.buildEnvironment(specDD, plan, environment);
101 }
102 }
103
104 public void initContext(XmlObject specDD, XmlObject plan, Configuration localConfiguration, Configuration remoteConfiguration, Module module) throws DeploymentException {
105 for (Iterator iterator = builders.iterator(); iterator.hasNext();) {
106 NamingBuilder namingBuilder = (NamingBuilder) iterator.next();
107 namingBuilder.initContext(specDD, plan, localConfiguration, remoteConfiguration, module);
108 }
109 }
110
111 public void buildNaming(XmlObject specDD, XmlObject plan, Configuration localContext, Configuration remoteContext, Module module, Map componentContext) throws DeploymentException {
112 for (Iterator iterator = builders.iterator(); iterator.hasNext();) {
113 NamingBuilder namingBuilder = (NamingBuilder) iterator.next();
114 namingBuilder.buildNaming(specDD, plan, localContext, remoteContext, module, componentContext);
115 }
116 }
117
118 public QNameSet getSpecQNameSet() {
119 return specQNames;
120 }
121
122 public QNameSet getPlanQNameSet() {
123 return null;
124 }
125 }