View Javadoc

1   package com.atlassian.plugin.osgi.factory.transform.model;
2   
3   import java.util.Map;
4   import java.util.HashMap;
5   
6   import com.atlassian.plugin.osgi.util.OsgiHeaderUtil;
7   
8   import com.google.common.base.Function;
9   import com.google.common.collect.ImmutableMap;
10  
11  import static com.google.common.collect.Maps.transformValues;
12  
13  /**
14   * Encapsulates the package exports from the system bundle
15   *
16   * @since 2.2.0
17   */
18  public class SystemExports
19  {
20      private final Map<String, Map<String,String>> exports;
21  
22      public static final SystemExports NONE = new SystemExports("");
23  
24      /**
25       * Constructs an instance by parsing the exports line from the manifest
26       *
27       * @param exportsLine The Export-Package header value
28       */
29      public SystemExports(String exportsLine)
30      {
31          if (exportsLine == null)
32          {
33              exportsLine = "";
34          }
35  
36          this.exports = internAttributeKeys(OsgiHeaderUtil.parseHeader(exportsLine));
37      }
38  
39      /**
40       * OSGi manifest header keys come from a very small set of possible strings, and are ripe for interning. The other
41       * strings in this map are less suitable for interning, and so are left alone.
42       */
43      private static Map<String, Map<String, String>> internAttributeKeys(final Map<String, Map<String, String>> map)
44      {
45          return transformValues(map, new Function<Map<String, String>, Map<String, String>>()
46          {
47              @Override
48              public Map<String, String> apply(final Map<String, String> innerMap)
49              {
50                  return internKeys(innerMap);
51              }
52          });
53      }
54  
55      private static Map<String, String> internKeys(final Map<String, String> innerMap)
56      {
57          // Guava doesn't offer a "transformKeys" method, so we do it the old-fashioned way
58          final ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
59          for (Map.Entry<String, String> entry : innerMap.entrySet())
60          {
61              builder.put(
62                      entry.getKey().intern(),  // this here's the manifest header key
63                      entry.getValue()
64              );
65          }
66  
67          return builder.build();
68      }
69  
70      /**
71       * Constructs a package export, taking into account any attributes on the system export, including the version.
72       * The version is handled special, in that is added as an exact match, i.e. [1.0,1.0].
73       *
74       * @param pkg The java package
75       * @return The full export line to use for a host component import
76       */
77      public String getFullExport(String pkg)
78      {
79          if (exports.containsKey(pkg))
80          {
81              Map<String,String> attrs = new HashMap<String,String>(exports.get(pkg));
82              if (attrs.containsKey("version"))
83              {
84                  final String version = attrs.get("version");
85                  attrs.put("version", "[" + version + "," + version + "]");
86              }
87              return OsgiHeaderUtil.buildHeader(pkg, attrs);
88          }
89          return pkg;
90      }
91  
92      /**
93       * @param pkg The package to check
94       * @return True if the package is being exported, false otherwise
95       */
96      public boolean isExported(String pkg)
97      {
98          return exports.containsKey(pkg);
99      }
100 }