View Javadoc

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.deployment;
20  
21  import java.util.Set;
22  import java.util.HashSet;
23  import java.util.Iterator;
24  import java.util.Collection;
25  import java.util.List;
26  import java.util.Collections;
27  
28  import org.apache.geronimo.gbean.ReferenceCollection;
29  import org.apache.geronimo.gbean.ReferenceCollectionListener;
30  import org.apache.geronimo.gbean.ReferenceCollectionEvent;
31  import org.apache.geronimo.common.DeploymentException;
32  import org.apache.geronimo.kernel.repository.Environment;
33  import org.apache.xmlbeans.XmlObject;
34  
35  /**
36   * @version $Rev: 470597 $ $Date: 2006-11-02 15:30:55 -0800 (Thu, 02 Nov 2006) $
37   */
38  public class NamespaceDrivenBuilderCollection {
39  
40      private final Collection builders;
41      private final Set namespaces = new HashSet();
42  
43      public NamespaceDrivenBuilderCollection(Collection builders) {
44          this.builders = builders == null? Collections.EMPTY_SET: builders;
45          if (builders instanceof ReferenceCollection) {
46              ((ReferenceCollection)builders).addReferenceCollectionListener(new ReferenceCollectionListener() {
47  
48                  public void memberAdded(ReferenceCollectionEvent event) {
49                      addBuilder(event.getMember());
50                  }
51  
52                  public void memberRemoved(ReferenceCollectionEvent event) {
53                      Object builder = event.getMember();
54                      String namespace = ((NamespaceDrivenBuilder)builder).getNamespace();
55                      namespaces.remove(namespace);
56                  }
57              });
58          }
59          for (Iterator iterator = this.builders.iterator(); iterator.hasNext();) {
60              Object builder = iterator.next();
61              addBuilder(builder);
62          }
63      }
64  
65      private void addBuilder(Object builder) {
66          String namespace = ((NamespaceDrivenBuilder)builder).getNamespace();
67          if (namespaces.contains(namespace)) {
68              throw new IllegalArgumentException("Duplicate namespace in builder set: " + namespace);
69          }
70          namespaces.add(namespace);                                                                   
71      }
72  
73      public void buildEnvironment(XmlObject container, Environment environment) throws DeploymentException {
74          for (Iterator iterator = builders.iterator(); iterator.hasNext();) {
75              NamespaceDrivenBuilder builder = (NamespaceDrivenBuilder) iterator.next();
76              builder.buildEnvironment(container, environment);
77          }
78      }
79  
80      public void build(XmlObject container, DeploymentContext applicationContext, DeploymentContext moduleContext) throws DeploymentException {
81          for (Iterator iterator = builders.iterator(); iterator.hasNext();) {
82              NamespaceDrivenBuilder builder = (NamespaceDrivenBuilder) iterator.next();
83              builder.build(container, applicationContext, moduleContext);
84          }
85      }
86  
87      public void addLoaders(List loaderList) {
88      }
89  }