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