Skip to content
This repository was archived by the owner on Apr 22, 2025. It is now read-only.

Commit 5ba8487

Browse files
committed
Maven LibraryLoader Boost
1 parent c02915f commit 5ba8487

File tree

1 file changed

+207
-0
lines changed

1 file changed

+207
-0
lines changed
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2+
From: 404Setup <153366651+404Setup@users.noreply.github.com>
3+
Date: Thu, 12 Sep 2024 18:42:37 +0800
4+
Subject: [PATCH] LibraryLoader Boost
5+
6+
7+
diff --git a/src/main/java/io/papermc/paper/plugin/loader/library/impl/MavenLibraryResolver.java b/src/main/java/io/papermc/paper/plugin/loader/library/impl/MavenLibraryResolver.java
8+
index 70f352630de71f575d1aea5a3126da19a94791ab..66f29766eee1d35aa1b9bd5743dc6ea540e99a9f 100644
9+
--- a/src/main/java/io/papermc/paper/plugin/loader/library/impl/MavenLibraryResolver.java
10+
+++ b/src/main/java/io/papermc/paper/plugin/loader/library/impl/MavenLibraryResolver.java
11+
@@ -105,7 +105,7 @@ public class MavenLibraryResolver implements ClassPathLibrary {
12+
* dependencies from
13+
*/
14+
public void addRepository(@NotNull RemoteRepository remoteRepository) {
15+
- this.repositories.add(remoteRepository);
16+
+ this.repositories.add(one.tranic.vine.maven.Maven.get(remoteRepository));
17+
}
18+
19+
/**
20+
diff --git a/src/main/java/one/tranic/vine/maven/Maven.java b/src/main/java/one/tranic/vine/maven/Maven.java
21+
new file mode 100644
22+
index 0000000000000000000000000000000000000000..c0a3629f8bc3916fc07396f502473c6e822acbf5
23+
--- /dev/null
24+
+++ b/src/main/java/one/tranic/vine/maven/Maven.java
25+
@@ -0,0 +1,155 @@
26+
+package one.tranic.vine.maven;
27+
+
28+
+import it.unimi.dsi.fastutil.objects.Object2ReferenceArrayMap;
29+
+import it.unimi.dsi.fastutil.objects.ObjectArrayList;
30+
+import org.eclipse.aether.repository.RemoteRepository;
31+
+import org.slf4j.LoggerFactory;
32+
+
33+
+import java.io.IOException;
34+
+import java.net.HttpURLConnection;
35+
+import java.net.URI;
36+
+import java.net.URL;
37+
+import java.util.List;
38+
+import java.util.Map;
39+
+import java.util.Objects;
40+
+import java.util.concurrent.*;
41+
+
42+
+public class Maven {
43+
+ private static final Map<String, String> mirrors = new Object2ReferenceArrayMap<>();
44+
+ private static final org.slf4j.Logger logger = LoggerFactory.getLogger("VineMavenBoost");
45+
+ private static final String central = "https://repo.maven.apache.org/maven2";
46+
+ private static final URI centralUri = URI.create(central);
47+
+ private static String maven = "";
48+
+
49+
+ public static boolean isCentral(String str) {
50+
+ try {
51+
+ return Objects.equals(URI.create(str).getHost(), centralUri.getHost());
52+
+ } catch (Exception e) {
53+
+ return false;
54+
+ }
55+
+ }
56+
+
57+
+ public static boolean isCentral(RemoteRepository remoteRepository) {
58+
+ return isCentral(remoteRepository.getUrl());
59+
+ }
60+
+
61+
+ public static RemoteRepository get() {
62+
+ if (maven.isEmpty()) {
63+
+ ping();
64+
+ }
65+
+ return new RemoteRepository.Builder("central", "default", maven).build();
66+
+ }
67+
+
68+
+ public static RemoteRepository get(RemoteRepository remoteRepository) {
69+
+ if (isCentral(remoteRepository)) {
70+
+ return new RemoteRepository.Builder("central", "default", maven).build();
71+
+ }
72+
+ return remoteRepository;
73+
+ }
74+
+
75+
+ public static void ping() {
76+
+ mirrors();
77+
+ String s = System.getProperty("Maven.select");
78+
+ if (s != null) {
79+
+ String p = mirrors.get(s);
80+
+ if (p != null) {
81+
+ maven = p;
82+
+ logger.info("The mirror {} ({}) has been selected", s, p);
83+
+ return;
84+
+ }
85+
+ }
86+
+ selectMirror();
87+
+ }
88+
+
89+
+ public static String replace(String str) {
90+
+ return Objects.equals(str, central) ? maven : str;
91+
+ }
92+
+
93+
+ private static void mirrors() {
94+
+ if (!mirrors.isEmpty()) return;
95+
+ mirrors.put("central", "https://repo.maven.apache.org/maven2");
96+
+ mirrors.put("google-asia", "https://maven-central-asia.storage-download.googleapis.com/maven2/");
97+
+ mirrors.put("google-eu", "https://maven-central-eu.storage-download.googleapis.com/maven2/");
98+
+ mirrors.put("google-us", "https://maven-central.storage-download.googleapis.com/maven2/");
99+
+ String r = System.getProperty("Maven.central");
100+
+ if (r != null && !r.isEmpty()) {
101+
+ try {
102+
+ new URI(r);
103+
+ } catch (Exception e) {
104+
+ return;
105+
+ }
106+
+ mirrors.put("user-custom-mirror", r);
107+
+ }
108+
+ }
109+
+
110+
+ private static void selectMirror() {
111+
+ ExecutorService executor = Executors.newCachedThreadPool(Thread.ofVirtual().factory());
112+
+ List<Future<MirrorResult>> futures = new ObjectArrayList<>();
113+
+
114+
+ for (Map.Entry<String, String> entry : mirrors.entrySet()) {
115+
+ futures.add(executor.submit(() -> testMirror(entry.getKey(), entry.getValue())));
116+
+ }
117+
+
118+
+ long bestTime = Long.MAX_VALUE;
119+
+ String bestMirror = central;
120+
+
121+
+ if (futures.isEmpty()) {
122+
+ executor.shutdown();
123+
+ return;
124+
+ }
125+
+
126+
+ for (Future<MirrorResult> future : futures) {
127+
+ try {
128+
+ MirrorResult result = future.get(4, TimeUnit.SECONDS);
129+
+ if (result.time < bestTime) {
130+
+ bestTime = result.time;
131+
+ bestMirror = result.url;
132+
+ }
133+
+ } catch (TimeoutException | InterruptedException | ExecutionException e) {
134+
+ logger.warn("Error testing mirror: {}", e.getMessage());
135+
+ }
136+
+ }
137+
+
138+
+ maven = bestMirror;
139+
+ logger.info("The fastest mirror is selected: {} ({} ms)", bestMirror, bestTime);
140+
+
141+
+ executor.shutdown();
142+
+ }
143+
+
144+
+ private static MirrorResult testMirror(String name, String url) {
145+
+ long start = System.currentTimeMillis();
146+
+ HttpURLConnection connection = null;
147+
+ try {
148+
+ connection = (HttpURLConnection) new URL(url).openConnection();
149+
+ connection.setRequestMethod("GET");
150+
+ connection.setConnectTimeout(3000);
151+
+ connection.setReadTimeout(3000);
152+
+ connection.connect();
153+
+ int responseCode = connection.getResponseCode();
154+
+ if (responseCode == 200 || responseCode == 404 || responseCode == 302 || responseCode == 301) {
155+
+ long time = System.currentTimeMillis() - start;
156+
+ logger.info("Mirror {} responded in {} ms", name, time);
157+
+ return new MirrorResult(url, time);
158+
+ } else {
159+
+ logger.warn("Mirror {} failed with response code: {}", name, responseCode);
160+
+ }
161+
+ } catch (IOException e) {
162+
+ logger.warn("Mirror {} failed to connect: {}", name, e.getMessage());
163+
+ } finally {
164+
+ if (connection != null) {
165+
+ connection.disconnect();
166+
+ }
167+
+ }
168+
+ return new MirrorResult(url, Long.MAX_VALUE);
169+
+ }
170+
+
171+
+ private static class MirrorResult {
172+
+ String url;
173+
+ long time;
174+
+
175+
+ MirrorResult(String url, long time) {
176+
+ this.url = url;
177+
+ this.time = time;
178+
+ }
179+
+ }
180+
+}
181+
diff --git a/src/main/java/org/bukkit/plugin/java/LibraryLoader.java b/src/main/java/org/bukkit/plugin/java/LibraryLoader.java
182+
index 97f580fccd06a8db5f592a53c8b95a7a6159adac..d161a13e2918ea22ff60d9a6363ebd1f54b443d2 100644
183+
--- a/src/main/java/org/bukkit/plugin/java/LibraryLoader.java
184+
+++ b/src/main/java/org/bukkit/plugin/java/LibraryLoader.java
185+
@@ -6,10 +6,12 @@ import java.net.MalformedURLException;
186+
import java.net.URL;
187+
import java.net.URLClassLoader;
188+
import java.util.ArrayList;
189+
-import java.util.Arrays;
190+
+import java.util.Collections;
191+
import java.util.List;
192+
import java.util.logging.Level;
193+
import java.util.logging.Logger;
194+
+
195+
+import one.tranic.vine.maven.Maven;
196+
import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
197+
import org.bukkit.plugin.PluginDescriptionFile;
198+
import org.eclipse.aether.DefaultRepositorySystemSession;
199+
@@ -79,7 +81,7 @@ public class LibraryLoader
200+
session.setSystemProperties( System.getProperties() );
201+
session.setReadOnly();
202+
203+
- this.repositories = repository.newResolutionRepositories( session, Arrays.asList( new RemoteRepository.Builder( "central", "default", "https://repo.maven.apache.org/maven2" ).build() ) );
204+
+ this.repositories = repository.newResolutionRepositories( session, Collections.singletonList(Maven.get()));
205+
}
206+
207+
@Nullable

0 commit comments

Comments
 (0)