Skip to content

Commit

Permalink
Fall back to slower transliterator (#543)
Browse files Browse the repository at this point in the history
  • Loading branch information
msbarry authored Mar 31, 2023
1 parent e77aaa2 commit 72ea82c
Showing 1 changed file with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -12,6 +16,8 @@
* 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");

/**
Expand All @@ -31,8 +37,14 @@ public TransliteratorInstance getInstance(String id) {
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;
}
}

Expand Down

0 comments on commit 72ea82c

Please sign in to comment.