View Javadoc

1   package com.atlassian.plugin.osgi.container.felix;
2   
3   import com.atlassian.plugin.util.ClassLoaderUtils;
4   import com.google.common.base.Function;
5   import com.google.common.base.Predicate;
6   import com.google.common.collect.ImmutableMap;
7   import com.google.common.collect.Maps;
8   import org.apache.commons.io.IOUtils;
9   import org.osgi.framework.Version;
10  import org.slf4j.Logger;
11  import org.slf4j.LoggerFactory;
12  import org.twdata.pkgscanner.DefaultOsgiVersionConverter;
13  import org.twdata.pkgscanner.ExportPackage;
14  
15  import java.io.IOException;
16  import java.io.InputStream;
17  import java.util.HashMap;
18  import java.util.Map;
19  import java.util.Properties;
20  
21  final class ExportBuilderUtils
22  {
23      private static Logger LOG = LoggerFactory.getLogger(ExportBuilderUtils.class);
24      private static final DefaultOsgiVersionConverter converter = new DefaultOsgiVersionConverter();
25      private static final String EMPTY_OSGI_VERSION = Version.emptyVersion.toString();
26  
27      /**
28       * Not for instantiation.
29       */
30      private ExportBuilderUtils()
31      {}
32  
33      /**
34       * Convert version string into OSGi format.
35       */
36      private static final Function<String, String> CONVERT_VERSION = new Function<String, String>()
37      {
38          public String apply(String from)
39          {
40              if (from != null && (from.trim().length() > 0))
41              {
42                  return converter.getVersion(from);
43              }
44              else
45              {
46                  return EMPTY_OSGI_VERSION;
47              }
48          }
49      };
50  
51      /**
52       * Reads export file and return a map of package->version.
53       * Returned versions are in OSGi format which can be 0.0.0 if not specified in the file.
54       *
55       * @param exportFilePath the file path, never null.
56       *
57       * @return map of package->version, never null.
58       */
59      static Map<String, String> parseExportFile(String exportFilePath)
60      {
61          Properties props = new Properties();
62          InputStream in = null;
63  
64          try
65          {
66              in = ClassLoaderUtils.getResourceAsStream(exportFilePath, ExportBuilderUtils.class);
67              // this should automatically get rid of comment lines.
68              props.load(in);
69          }
70          catch (IOException e)
71          {
72              LOG.warn("Problem occurred while processing package export:" + exportFilePath, e);
73              return ImmutableMap.of();
74          }
75          finally
76          {
77              IOUtils.closeQuietly(in);
78          }
79  
80          // convert version strings to osgi format and return the resultant map.
81          // this Maps.transformValues returns a view backed by immutable map in this case so itself is already immutable.
82          return Maps.transformValues(Maps.fromProperties(props), CONVERT_VERSION);
83      }
84  
85      /**
86       * Copies all the entries from src into dest for the keys that don't already exist in dest.
87       */
88      static void copyUnlessExist(final Map<String, String> dest, final Map<String, String> src)
89      {
90          dest.putAll(Maps.filterKeys(src, new Predicate<String>()
91          {
92              public boolean apply(String key)
93              {
94                  return !dest.containsKey(key);
95              }
96          }));
97      }
98  
99      /**
100      * Converts collection of ExportPackage into map of packageName->version.
101      */
102     static Map<String, String> toMap(Iterable<ExportPackage> exportPackages)
103     {
104         Map<String, String> output = new HashMap<String, String>();
105         for (ExportPackage pkg : exportPackages)
106         {
107             String version = pkg.getVersion() == null ? EMPTY_OSGI_VERSION : pkg.getVersion();
108             output.put(pkg.getPackageName(), version);
109         }
110         return ImmutableMap.copyOf(output);
111     }
112 }