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 if (in == null)
68 {
69 LOG.warn("Unable to find properties for package export: " + exportFilePath);
70 return ImmutableMap.of();
71 }
72
73 props.load(in);
74 }
75 catch (IOException e)
76 {
77 LOG.warn("Problem occurred while processing package export: " + exportFilePath, e);
78 return ImmutableMap.of();
79 }
80 finally
81 {
82 IOUtils.closeQuietly(in);
83 }
84
85
86
87 return Maps.transformValues(Maps.fromProperties(props), CONVERT_VERSION);
88 }
89
90
91
92
93 static void copyUnlessExist(final Map<String, String> dest, final Map<String, String> src)
94 {
95 dest.putAll(Maps.filterKeys(src, new Predicate<String>()
96 {
97 public boolean apply(String key)
98 {
99 return !dest.containsKey(key);
100 }
101 }));
102 }
103
104
105
106
107 static Map<String, String> toMap(Iterable<ExportPackage> exportPackages)
108 {
109 Map<String, String> output = new HashMap<String, String>();
110 for (ExportPackage pkg : exportPackages)
111 {
112 String version = pkg.getVersion() == null ? EMPTY_OSGI_VERSION : pkg.getVersion();
113 output.put(pkg.getPackageName(), version);
114 }
115 return ImmutableMap.copyOf(output);
116 }
117 }