aboutsummaryrefslogtreecommitdiff
blob: 9d915281f154897bb4392338a349daa1a217469d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package org.gentoo.java.ebuilder.maven;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.gentoo.java.ebuilder.Config;
import org.gentoo.java.ebuilder.portage.CacheItem;
import org.gentoo.java.ebuilder.portage.MavenVersion;
import org.gentoo.java.ebuilder.portage.PortageParser;

/**
 * Cache for resolving maven artifacts into portage ebuilds.
 *
 * @author fordfrog
 */
public class MavenCache {

    /**
     * Cache containing map of group ids, artifact ids and corresponding cache
     * items.
     */
    private final Map<String, Map<String, List<CacheItem>>> cache
            = new HashMap<>(200);

    /**
     * Searches for system dependency using maven group id, artifact id and
     * version. First version that is the same or greater than specified version
     * is returned. In case there is no such version, the highest version from
     * the available ebuilds is returned.
     *
     * @param groupId    maven group id
     * @param artifactId maven artifact id
     * @param version    maven version
     *
     * @return dependency string or null
     */
    public String getDependency(final String groupId, final String artifactId,
            final String version) {
        final Map<String, List<CacheItem>> artifactIds = cache.get(groupId);

        if (artifactIds == null) {
            return "!!!groupId-not-found!!!";
        }

        final List<CacheItem> versions = artifactIds.get(artifactId);

        if (versions == null) {
            return "!!!artifactId-not-found!!!";
        }

        final MavenVersion mavenVersion = new MavenVersion(version);
        CacheItem cacheItem = null;

        for (final CacheItem curCacheItem : versions) {
            if (curCacheItem.getParsedMavenVersion().compareTo(mavenVersion)
                    <= 0) {
                cacheItem = curCacheItem;

                break;
            }
        }

        if (cacheItem == null) {
            cacheItem = versions.get(versions.size() - 1);
        }

        final StringBuilder sbDependency = new StringBuilder(50);
        sbDependency.append(">=");
        sbDependency.append(cacheItem.getCategory());
        sbDependency.append('/');
        sbDependency.append(cacheItem.getPkg());
        sbDependency.append('-');
        sbDependency.append(stripExtraFromVersion(cacheItem.getVersion()));

        if (cacheItem.getUseFlag() != null) {
            sbDependency.append('[');
            sbDependency.append(cacheItem.getUseFlag());
            sbDependency.append(']');
        }

        sbDependency.append(':');
        sbDependency.append(cacheItem.getSlot());

        return sbDependency.toString();
    }

    /**
     * Loads cache from specified path.
     *
     * @param config application configuration
     */
    public void loadCache(final Config config) {
        config.getStdoutWriter().print("Reading in maven cache...");

        cache.clear();

        try (final BufferedReader reader = new BufferedReader(new FileReader(
                config.getCacheFile().toFile()))) {
            String line = reader.readLine();

            if (!PortageParser.CACHE_VERSION.equals(line)) {
                config.getErrorWriter().println("ERROR: Unsupported version of "
                        + "cache. Please refresh the cache using command line "
                        + "switch --refresh-cache.");
                Runtime.getRuntime().exit(1);
            }

            line = reader.readLine();

            while (line != null) {
                if (!line.isEmpty()) {
                    addCacheItem(new CacheItem(line));
                }

                line = reader.readLine();
            }
        } catch (final IOException ex) {
            throw new RuntimeException("Failed to load cache", ex);
        }

        for (final Map<String, List<CacheItem>> artifactIds : cache.values()) {
            for (final List<CacheItem> versions : artifactIds.values()) {
                versions.sort((final CacheItem o1, final CacheItem o2) -> {
                    return o1.getParsedMavenVersion().compareTo(
                            o2.getParsedMavenVersion());
                });
            }
        }

        config.getStdoutWriter().println("done");
    }

    /**
     * Adds cache item to the cache if it contains maven id.
     *
     * @param cacheItem cache item
     */
    private void addCacheItem(final CacheItem cacheItem) {
        if (cacheItem.getGroupId() == null) {
            return;
        }

        Map<String, List<CacheItem>> artifactIds
                = cache.get(cacheItem.getGroupId());

        if (artifactIds == null) {
            artifactIds = new HashMap<>(100);
            cache.put(cacheItem.getGroupId(), artifactIds);
        }

        List<CacheItem> versions = artifactIds.get(cacheItem.getArtifactId());

        if (versions == null) {
            versions = new ArrayList<>(10);
            artifactIds.put(cacheItem.getArtifactId(), versions);
        }

        versions.add(cacheItem);
    }

    /**
     * Strips all -r* from the version string.
     *
     * @param version version string
     *
     * @return stripped version string
     */
    private String stripExtraFromVersion(final String version) {
        return version.replaceAll("-r\\d+", "");
    }
}