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.Collection;
19  import java.util.HashMap;
20  import java.util.HashSet;
21  import java.util.Iterator;
22  import java.util.Map;
23  import java.util.Set;
24  
25  import org.apache.maven.artifact.Artifact;
26  
27  
28  /**
29   * Information result of the bundling process 
30   * 
31   * @author <a href="mailto:carlos@apache.org">Carlos Sanchez</a>
32   * @version $Id: BundleInfo.html 1143000 2011-07-05 11:50:48Z mcculls $
33   */
34  public class BundleInfo
35  {
36      /**
37       * {@link Map} &lt; {@link String}, {@link Set} &lt; {@link Artifact} > >
38       * Used to check for duplicated exports. Key is package name and value list of artifacts where it's exported.
39       */
40      private Map m_exportedPackages = new HashMap();
41  
42  
43      public void addExportedPackage( String packageName, Artifact artifact )
44      {
45          Set artifacts = ( Set ) getExportedPackages().get( packageName );
46          if ( artifacts == null )
47          {
48              artifacts = new HashSet();
49              m_exportedPackages.put( packageName, artifacts );
50          }
51          artifacts.add( artifact );
52      }
53  
54  
55      protected Map getExportedPackages()
56      {
57          return m_exportedPackages;
58      }
59  
60  
61      /**
62       * Get a list of packages that are exported in more than one bundle.
63       * Key is package name and value list of artifacts where it's exported.
64       * @return {@link Map} &lt; {@link String}, {@link Set} &lt; {@link Artifact} > >
65       */
66      public Map getDuplicatedExports()
67      {
68          Map duplicatedExports = new HashMap();
69  
70          for ( Iterator it = getExportedPackages().entrySet().iterator(); it.hasNext(); )
71          {
72              Map.Entry entry = ( Map.Entry ) it.next();
73              Set artifacts = ( Set ) entry.getValue();
74              if ( artifacts.size() > 1 )
75              {
76                  /* remove warnings caused by different versions of same artifact */
77                  Set artifactKeys = new HashSet();
78  
79                  String packageName = ( String ) entry.getKey();
80                  for ( Iterator it2 = artifacts.iterator(); it2.hasNext(); )
81                  {
82                      Artifact artifact = ( Artifact ) it2.next();
83                      artifactKeys.add( artifact.getGroupId() + "." + artifact.getArtifactId() );
84                  }
85  
86                  if ( artifactKeys.size() > 1 )
87                  {
88                      duplicatedExports.put( packageName, artifacts );
89                  }
90              }
91          }
92  
93          return duplicatedExports;
94      }
95  
96  
97      public void merge( BundleInfo bundleInfo )
98      {
99          for ( Iterator it = bundleInfo.getExportedPackages().entrySet().iterator(); it.hasNext(); )
100         {
101             Map.Entry entry = ( Map.Entry ) it.next();
102             String packageName = ( String ) entry.getKey();
103             Collection artifacts = ( Collection ) entry.getValue();
104 
105             Collection artifactsWithPackage = ( Collection ) getExportedPackages().get( packageName );
106             if ( artifactsWithPackage == null )
107             {
108                 artifactsWithPackage = new HashSet();
109                 getExportedPackages().put( packageName, artifactsWithPackage );
110             }
111             artifactsWithPackage.addAll( artifacts );
112         }
113     }
114 }