View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional information regarding
4    * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
5    * "License"); you may not use this file except in compliance with the License. You may obtain a
6    * copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software distributed under the License
11   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing permissions and limitations under
13   * the License.
14   */
15  package org.apache.felix.bundleplugin;
16  
17  
18  import java.util.HashMap;
19  import java.util.HashSet;
20  import java.util.Iterator;
21  import java.util.Map;
22  import java.util.Set;
23  
24  import org.apache.maven.artifact.Artifact;
25  
26  
27  /**
28   * Information result of the bundling process
29   *
30   * @author <a href="mailto:carlos@apache.org">Carlos Sanchez</a>
31   * @version $Id$
32   */
33  public class BundleInfo
34  {
35      /**
36       * {@link Map} &lt; {@link String}, {@link Set} &lt; {@link Artifact} > >
37       * Used to check for duplicated exports. Key is package name and value list of artifacts where it's exported.
38       */
39      private Map<String, Set<Artifact>> m_exportedPackages = new HashMap<String, Set<Artifact>>();
40  
41  
42      public void addExportedPackage( String packageName, Artifact artifact )
43      {
44          Set<Artifact> artifacts = getExportedPackages().get( packageName );
45          if ( artifacts == null )
46          {
47              artifacts = new HashSet<Artifact>();
48              m_exportedPackages.put( packageName, artifacts );
49          }
50          artifacts.add( artifact );
51      }
52  
53  
54      protected Map<String, Set<Artifact>> getExportedPackages()
55      {
56          return m_exportedPackages;
57      }
58  
59  
60      /**
61       * Get a list of packages that are exported in more than one bundle.
62       * Key is package name and value list of artifacts where it's exported.
63       * @return {@link Map} &lt; {@link String}, {@link Set} &lt; {@link Artifact} &gt; &gt;
64       */
65      public Map<String, Set<Artifact>> getDuplicatedExports()
66      {
67          Map<String, Set<Artifact>> duplicatedExports = new HashMap<String, Set<Artifact>>();
68  
69          for ( Iterator<Map.Entry<String, Set<Artifact>>> it = getExportedPackages().entrySet().iterator(); it.hasNext(); )
70          {
71              Map.Entry<String, Set<Artifact>> entry = it.next();
72              Set<Artifact> artifacts = entry.getValue();
73              if ( artifacts.size() > 1 )
74              {
75                  /* remove warnings caused by different versions of same artifact */
76                  Set<String> artifactKeys = new HashSet<String>();
77  
78                  String packageName = entry.getKey();
79                  for ( Iterator<Artifact> it2 = artifacts.iterator(); it2.hasNext(); )
80                  {
81                      Artifact artifact = it2.next();
82                      artifactKeys.add( artifact.getGroupId() + "." + artifact.getArtifactId() );
83                  }
84  
85                  if ( artifactKeys.size() > 1 )
86                  {
87                      duplicatedExports.put( packageName, artifacts );
88                  }
89              }
90          }
91  
92          return duplicatedExports;
93      }
94  
95  
96      public void merge( BundleInfo bundleInfo )
97      {
98          for ( Iterator<Map.Entry<String, Set<Artifact>>> it = bundleInfo.getExportedPackages().entrySet().iterator(); it.hasNext(); )
99          {
100             Map.Entry<String, Set<Artifact>> entry = it.next();
101             String packageName = entry.getKey();
102             Set<Artifact> artifacts = entry.getValue();
103 
104             Set<Artifact> artifactsWithPackage = getExportedPackages().get( packageName );
105             if ( artifactsWithPackage == null )
106             {
107                 artifactsWithPackage = new HashSet<Artifact>();
108                 getExportedPackages().put( packageName, artifactsWithPackage );
109             }
110             artifactsWithPackage.addAll( artifacts );
111         }
112     }
113 }