Fall back to slower transliterator (#543)

pull/544/head
Michael Barry 2023-03-31 06:33:12 -04:00 zatwierdzone przez GitHub
rodzic e77aaa2816
commit 72ea82cd50
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
1 zmienionych plików z 14 dodań i 2 usunięć

Wyświetl plik

@ -1,7 +1,11 @@
package com.onthegomap.planetiler.util;
import com.ibm.icu.text.Transliterator;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.concurrent.atomic.AtomicBoolean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* A {@link com.ibm.icu.text.Transliterator} that does not share any static data with other thread local
@ -12,6 +16,8 @@ import java.lang.reflect.Method;
* used across different threads in order to transliterate without contention.
*/
public class ThreadLocalTransliterator {
private static final Logger LOGGER = LoggerFactory.getLogger(ThreadLocalTransliterator.class);
private static final AtomicBoolean loggedOnce = new AtomicBoolean(false);
private final ClassLoader classLoader = DuplicateClassLoader.duplicateClassesWithPrefix("com.ibm.icu");
/**
@ -31,8 +37,14 @@ public class ThreadLocalTransliterator {
throw new IllegalStateException(e);
}
};
} catch (ClassNotFoundException | InvocationTargetException | NoSuchMethodException | IllegalAccessException e) {
throw new IllegalStateException(e);
} catch (ClassNotFoundException | InvocationTargetException | NoSuchMethodException | IllegalAccessException |
ExceptionInInitializerError e) {
if (!loggedOnce.get() && loggedOnce.compareAndSet(false, true)) {
LOGGER.warn("Could not get thread-local transliterator, falling back to slower shared instance: {}",
e.toString());
}
Transliterator t = Transliterator.getInstance(id);
return t::transliterate;
}
}