1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.aether.util.version;
20
21 import java.util.Collections;
22 import java.util.Map;
23 import java.util.WeakHashMap;
24 import java.util.concurrent.atomic.AtomicLong;
25
26 import org.eclipse.aether.ConfigurationProperties;
27 import org.eclipse.aether.version.InvalidVersionSpecificationException;
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 public class GenericVersionScheme extends VersionSchemeSupport {
55
56
57 private final Map<String, GenericVersion> versionCache = Collections.synchronizedMap(new WeakHashMap<>());
58
59
60 private final AtomicLong cacheHits = new AtomicLong(0);
61 private final AtomicLong cacheMisses = new AtomicLong(0);
62 private final AtomicLong totalRequests = new AtomicLong(0);
63
64
65 private static final AtomicLong GLOBAL_CACHE_HITS = new AtomicLong(0);
66 private static final AtomicLong GLOBAL_CACHE_MISSES = new AtomicLong(0);
67 private static final AtomicLong GLOBAL_TOTAL_REQUESTS = new AtomicLong(0);
68 private static final AtomicLong INSTANCE_COUNT = new AtomicLong(0);
69
70 static {
71
72 if (isStatisticsEnabled()) {
73 Runtime.getRuntime().addShutdownHook(new Thread(GenericVersionScheme::printGlobalStatistics));
74 }
75 }
76
77 public GenericVersionScheme() {
78 INSTANCE_COUNT.incrementAndGet();
79 }
80
81
82
83
84
85 private static boolean isStatisticsEnabled() {
86
87 String sysProp = System.getProperty(ConfigurationProperties.VERSION_SCHEME_CACHE_DEBUG);
88 if (sysProp != null) {
89 return Boolean.parseBoolean(sysProp);
90 }
91
92
93 return ConfigurationProperties.DEFAULT_VERSION_SCHEME_CACHE_DEBUG;
94 }
95
96 @Override
97 public GenericVersion parseVersion(final String version) throws InvalidVersionSpecificationException {
98 totalRequests.incrementAndGet();
99 GLOBAL_TOTAL_REQUESTS.incrementAndGet();
100
101 GenericVersion existing = versionCache.get(version);
102 if (existing != null) {
103 cacheHits.incrementAndGet();
104 GLOBAL_CACHE_HITS.incrementAndGet();
105 return existing;
106 } else {
107 cacheMisses.incrementAndGet();
108 GLOBAL_CACHE_MISSES.incrementAndGet();
109 return versionCache.computeIfAbsent(version, GenericVersion::new);
110 }
111 }
112
113
114
115
116 public String getCacheStatistics() {
117 long hits = cacheHits.get();
118 long misses = cacheMisses.get();
119 long total = totalRequests.get();
120 double hitRate = total > 0 ? (double) hits / total * 100.0 : 0.0;
121
122 return String.format(
123 "GenericVersionScheme Cache Stats: hits=%d, misses=%d, total=%d, hit-rate=%.2f%%, cache-size=%d",
124 hits, misses, total, hitRate, versionCache.size());
125 }
126
127
128
129
130 private static void printGlobalStatistics() {
131 long hits = GLOBAL_CACHE_HITS.get();
132 long misses = GLOBAL_CACHE_MISSES.get();
133 long total = GLOBAL_TOTAL_REQUESTS.get();
134 long instances = INSTANCE_COUNT.get();
135 double hitRate = total > 0 ? (double) hits / total * 100.0 : 0.0;
136
137 System.err.println("=== GenericVersionScheme Global Cache Statistics (WeakHashMap) ===");
138 System.err.println(String.format("Total instances created: %d", instances));
139 System.err.println(String.format("Total requests: %d", total));
140 System.err.println(String.format("Cache hits: %d", hits));
141 System.err.println(String.format("Cache misses: %d", misses));
142 System.err.println(String.format("Hit rate: %.2f%%", hitRate));
143 System.err.println(
144 String.format("Average requests per instance: %.2f", instances > 0 ? (double) total / instances : 0.0));
145 System.err.println("=== End Cache Statistics ===");
146 }
147
148
149
150
151
152
153
154
155
156 public static void main(String... args) {
157 System.out.println(
158 "Display parameters as parsed by Maven Resolver 'generic' scheme (in canonical form and as a list of tokens)"
159 + " and comparison result:");
160 if (args.length == 0) {
161 return;
162 }
163
164 GenericVersionScheme scheme = new GenericVersionScheme();
165 GenericVersion prev = null;
166 int i = 1;
167 for (String version : args) {
168 try {
169 GenericVersion c = scheme.parseVersion(version);
170
171 if (prev != null) {
172 int compare = prev.compareTo(c);
173 System.out.println(
174 " " + prev + ' ' + ((compare == 0) ? "==" : ((compare < 0) ? "<" : ">")) + ' ' + version);
175 }
176
177 System.out.println((i++) + ". " + version + " -> " + c.asString() + "; tokens: " + c.asItems());
178
179 prev = c;
180 } catch (InvalidVersionSpecificationException e) {
181 System.err.println("Invalid version: " + version + " - " + e.getMessage());
182 }
183 }
184 }
185 }