Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import com.earth2me.essentials.updatecheck.UpdateChecker;
import com.earth2me.essentials.userstorage.ModernUserMap;
import com.earth2me.essentials.utils.FormatUtil;
import com.earth2me.essentials.utils.PasteUtil;
import com.earth2me.essentials.utils.VersionUtil;
import io.papermc.lib.PaperLib;
import net.ess3.api.Economy;
Expand Down Expand Up @@ -587,6 +588,8 @@ public void onDisable() {
getUsers().shutdown();

EssentialsConfiguration.shutdownExecutor();
PasteUtil.shutdownExecutor();
getServer().getScheduler().cancelTasks(this);

HandlerList.unregisterAll(this);
}
Expand Down
28 changes: 26 additions & 2 deletions Essentials/src/main/java/com/earth2me/essentials/I18n.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.logging.Level;
import java.util.regex.Pattern;

public class I18n implements net.ess3.api.II18n {
private static final String MESSAGES = "messages";
private static final Pattern NODOUBLEMARK = Pattern.compile("''");
private static final ExecutorService BUNDLE_LOADER_EXECUTOR = Executors.newFixedThreadPool(2);
private static volatile ExecutorService BUNDLE_LOADER_EXECUTOR = Executors.newFixedThreadPool(2);
private static final ResourceBundle NULL_BUNDLE = new ResourceBundle() {
@SuppressWarnings("NullableProblems")
public Enumeration<String> getKeys() {
Expand Down Expand Up @@ -106,6 +107,29 @@ public void onEnable() {

public void onDisable() {
instance = null;
shutdownExecutor();
}

private static synchronized ExecutorService getExecutor() {
if (BUNDLE_LOADER_EXECUTOR == null || BUNDLE_LOADER_EXECUTOR.isShutdown() || BUNDLE_LOADER_EXECUTOR.isTerminated()) {
BUNDLE_LOADER_EXECUTOR = Executors.newFixedThreadPool(2);
}
return BUNDLE_LOADER_EXECUTOR;
}

private static synchronized void shutdownExecutor() {
final ExecutorService exec = BUNDLE_LOADER_EXECUTOR;
if (exec == null) {
return;
}
exec.shutdown();
try {
if (!exec.awaitTermination(5, TimeUnit.SECONDS)) {
exec.shutdownNow();
}
} catch (final InterruptedException ignored) {
exec.shutdownNow();
}
}

@Override
Expand All @@ -124,7 +148,7 @@ private ResourceBundle getBundle(final Locale locale) {
synchronized (loadingBundles) {
if (!loadingBundles.contains(locale)) {
loadingBundles.add(locale);
BUNDLE_LOADER_EXECUTOR.submit(() -> {
getExecutor().submit(() -> {
blockingLoadBundle(locale);
synchronized (loadingBundles) {
loadingBundles.remove(locale);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.zip.GZIPOutputStream;

public final class PasteUtil {
private static final String PASTE_URL = "https://pastes.dev/";
private static final String PASTE_UPLOAD_URL = "https://api.pastes.dev/post";
private static final ExecutorService PASTE_EXECUTOR_SERVICE = Executors.newSingleThreadExecutor();
private static volatile ExecutorService PASTE_EXECUTOR_SERVICE = Executors.newSingleThreadExecutor();
private static final Gson GSON = new Gson();

private PasteUtil() {
Expand All @@ -35,7 +36,7 @@ private PasteUtil() {
*/
public static CompletableFuture<PasteResult> createPaste(List<PasteFile> pages) {
final CompletableFuture<PasteResult> future = new CompletableFuture<>();
PASTE_EXECUTOR_SERVICE.submit(() -> {
getExecutor().submit(() -> {
try {
final HttpURLConnection connection = (HttpURLConnection) new URL(PASTE_UPLOAD_URL).openConnection();
connection.setRequestMethod("POST");
Expand Down Expand Up @@ -81,6 +82,28 @@ public static CompletableFuture<PasteResult> createPaste(List<PasteFile> pages)
return future;
}

private static synchronized ExecutorService getExecutor() {
if (PASTE_EXECUTOR_SERVICE == null || PASTE_EXECUTOR_SERVICE.isShutdown() || PASTE_EXECUTOR_SERVICE.isTerminated()) {
PASTE_EXECUTOR_SERVICE = Executors.newSingleThreadExecutor();
}
return PASTE_EXECUTOR_SERVICE;
}

public static synchronized void shutdownExecutor() {
final ExecutorService exec = PASTE_EXECUTOR_SERVICE;
if (exec == null) {
return;
}
exec.shutdown();
try {
if (!exec.awaitTermination(5, TimeUnit.SECONDS)) {
exec.shutdownNow();
}
} catch (final InterruptedException ignored) {
exec.shutdownNow();
}
}

public static class PasteFile {
private final String name;
private final String contents;
Expand Down
Loading