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
29
30 private ExportBuilderUtils()
31 {}
32
33
34
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
53
54
55
56
57
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
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
81
82 return Maps.transformValues(Maps.fromProperties(props), CONVERT_VERSION);
83 }
84
85
86
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
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 }