1   package com.atlassian.plugin.osgi.factory.transform.model;
2   
3   import aQute.lib.header.OSGiHeader;
4   
5   import java.util.Map;
6   import java.util.Collections;
7   import java.util.HashMap;
8   
9   import org.apache.commons.lang.Validate;
10  import com.atlassian.plugin.osgi.util.OsgiHeaderUtil;
11  
12  /**
13   * Encapsulates the package exports from the system bundle
14   *
15   * @since 2.2.0
16   */
17  public class SystemExports
18  {
19      private final Map<String, Map<String,String>> exports;
20  
21      public static final SystemExports NONE = new SystemExports("");
22  
23      /**
24       * Constructs an instance by parsing the exports line from the manifest
25       *
26       * @param exportsLine The Export-Package header value
27       */
28      public SystemExports(String exportsLine)
29      {
30          if (exportsLine == null)
31          {
32              exportsLine = "";
33          }
34          this.exports = Collections.unmodifiableMap(OsgiHeaderUtil.parseHeader(exportsLine));
35      }
36  
37      /**
38       * Constructs a package export, taking into account any attributes on the system export, including the version.
39       * The version is handled special, in that is added as an exact match, i.e. [1.0,1.0].
40       *
41       * @param pkg The java package
42       * @return The full export line to use for a host component import
43       */
44      public String getFullExport(String pkg)
45      {
46          if (exports.containsKey(pkg))
47          {
48              Map<String,String> attrs = new HashMap<String,String>(exports.get(pkg));
49              if (attrs.containsKey("version"))
50              {
51                  final String version = attrs.get("version");
52                  attrs.put("version", "[" + version + "," + version + "]");
53              }
54              return OsgiHeaderUtil.buildHeader(pkg, attrs);
55          }
56          return pkg;
57      }
58  
59      /**
60       * @param pkg The package to check
61       * @return True if the package is being exported, false otherwise
62       */
63      public boolean isExported(String pkg)
64      {
65          return exports.containsKey(pkg);
66      }
67  }