下面列出了java.util.ResourceBundle#clearCache ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void saveBundle(final String bundleName, final Locale locale, Map<String, String> localizedValues) {
Preconditions.checkArgument(!StringUtils.isBlank(bundleName), "bundleName must not be blank");
Preconditions.checkNotNull(locale, "locale must not be null");
if(localizedValues.isEmpty()) {
return;
}
localizedValues = new HashMap<String,String>(localizedValues);
final BatchStatement batch = new BatchStatement();
localizedValues.forEach((k,v) -> {
batch.add(new BoundStatement(saveBundleStmt).bind(bundleName, sanitizeLocale(locale), k, v));
});
try(Timer.Context ctxt = saveBundleTimer.time()) {
session.execute(batch);
}
// make sure the cache is flushed for the bundle
// TODO: this works for this node, but we have no way of notifying the cluster to flush the cache
ResourceBundle.clearCache();
}
@Test
void testKeyCount() {
ResourceBundle defaultBundle = Utils.getResourceBundle(null);
HashMap<String, ArrayList<String>> dictionary = new HashMap<>();
for (String lang : Utils.getSupportedLanguages()) {
ResourceBundle.clearCache();
if (lang.equals(DEFAULT_LANG))
continue;
ResourceBundle selectedLang = Utils.getResourceBundle(lang);
for (final Enumeration<String> keys = defaultBundle.getKeys(); keys.hasMoreElements();) {
String element = keys.nextElement();
if (selectedLang.containsKey(element)
&& !selectedLang.getString(element).equals(defaultBundle.getString(element))) {
if (dictionary.get(lang) == null)
dictionary.put(lang, new ArrayList<>());
dictionary.get(lang).add(element);
}
}
}
dictionary.keySet().forEach(d -> {
logger.warn(String.format("Keys missing in %s", d));
dictionary.get(d).forEach(v -> logger.warn(v));
logger.warn("\n");
});
}
public CommandResponse doSITE_RELOAD(CommandRequest request) {
try {
GlobalContext.getGlobalContext().getSectionManager().reload();
GlobalContext.getGlobalContext().reloadFtpConfig();
GlobalContext.getGlobalContext().getSlaveSelectionManager().reload();
// Send event to every plugins
GlobalContext.getEventService().publish(new ReloadEvent());
} catch (IOException e) {
logger.log(Level.FATAL, "Error reloading config", e);
return new CommandResponse(200, e.getMessage());
}
// Clear base system classloader also
ResourceBundle.clearCache(ClassLoader.getSystemClassLoader());
return StandardCommandManager.genericResponse("RESPONSE_200_COMMAND_OK");
}
public ResourceBundle getBundle(String filename, boolean clearCache) {
ResourceBundle rb = null;
try {
if (clearCache)
ResourceBundle.clearCache();
rb = ResourceBundle.getBundle(filename, locale, new I18NControl(ENCODING));
} catch (Exception e) {
EngineLogger.error("ERROR LOADING BUNDLE: " + filename);
}
return rb;
}
public static void clearResourceBundleCache() throws Exception {
WebUtil.getServiceLocator().getToolsService().clearResourceBundleCache();
//https://stackoverflow.com/questions/4325164/how-to-reload-resource-bundle-in-web-application
ResourceBundle.clearCache(Thread.currentThread().getContextClassLoader());
Iterator<ApplicationResourceBundle> it = ApplicationAssociate.getCurrentInstance().getResourceBundles().values().iterator();
while (it.hasNext()) {
ApplicationResourceBundle appBundle = it.next();
Map<Locale, ResourceBundle> resources = CommonUtil.getDeclaredFieldValue(appBundle, "resources");
resources.clear();
}
}
private static void loadBundle(String filename) {
descriptionsBundle = ResourceBundle.getBundle(filename, Locale.getDefault());
for (String key : descriptionsBundle.keySet()) {
descriptions.put(key, descriptionsBundle.getString(key));
}
ResourceBundle.clearCache();
descriptionsBundle = null;
}
public static Version getProgVersion() {
String TOKEN_VERSION = "VERSION";
try {
ResourceBundle.clearCache();
ResourceBundle rb = ResourceBundle.getBundle(RBVERSION);
if (rb.containsKey(TOKEN_VERSION)) {
return new Version(rb.getString(TOKEN_VERSION));
}
} catch (Exception e) {
Log.errorLog(134679898, e);
}
return new Version("");
}
/**
* Reset the caches
*
* @since 5.1
*/
public static void resetCache( )
{
ResourceBundle.clearCache( );
if ( _overrideLoader != null )
{
ResourceBundle.clearCache( _overrideLoader );
}
_resourceBundleCache.clear( );
}
/**
* Get the message in the file for the respective language.
* @param string message.
* @return translated message.
*/
public String t(String string) {
Locale locale = new Locale(language, country);
ResourceBundle.clearCache();
ResourceBundle translation = ResourceBundle.getBundle("i18n/Tech_Gallery", locale);
return translation.getString(string);
}
@Deprecated
public static String getBuildNr() {
String TOKEN_VERSION = "VERSION";
try {
ResourceBundle.clearCache();
ResourceBundle rb = ResourceBundle.getBundle(RBVERSION);
if (rb.containsKey(TOKEN_VERSION)) {
return new Version(rb.getString(TOKEN_VERSION)).toString();
}
} catch (Exception e) {
Log.errorLog(134679898, e);
}
return new Version("").toString();
}
/**
* Clear the complete resource bundle cache using the specified class loader!
*
* @param aClassLoader
* The class loader to be used. May not be <code>null</code>.
*/
public static void clearCache (@Nonnull final ClassLoader aClassLoader)
{
ResourceBundle.clearCache (aClassLoader);
if (LOGGER.isDebugEnabled ())
LOGGER.debug ("Cache was cleared: " + ResourceBundle.class.getName () + "; classloader=" + aClassLoader);
}
@After
public void tearDown() {
ResourceBundle.clearCache();
}
/**
* Sets the locale of the bundle.
*
* @param locale the locale to set.
*/
public void setLocale(final Locale locale) {
Locale.setDefault(locale);
ResourceBundle.clearCache();
fResourceBundle = ResourceBundle.getBundle(BUNDLE_NAME, locale);
}
public static void clearResourceBundleCache() {
if (classLoader != null) {
ResourceBundle.clearCache(classLoader);
}
}
/**
* Return content of testWatch.properties. It should be resolved to target/classes until
* resources-watch/testWatch.properties is modified, then it should return the new value.
*/
protected String getHelloResourceWatch() {
ResourceBundle.clearCache();
return ResourceBundle.getBundle("testWatch").getString("hello");
}
/**
* Releases any cached resources which were managed by this class from the {@link ResourceBundle}.
*/
public void clearCache() {
ResourceBundle.clearCache(this.resourceClassLoader);
}
public static void clearCache() {
ResourceBundle.clearCache();
}
/**
* Return basic resource, it should be handled from target/extra directory.
*/
protected String getHelloResource() {
ResourceBundle.clearCache();
return ResourceBundle.getBundle("test").getString("hello");
}
/**
* Return content of testWatch.properties. It should be resolved to target/classes until
* resources-watch/testWatch.properties is modified, then it should return the new value.
*/
protected String getHelloResourceWatch() {
ResourceBundle.clearCache();
return ResourceBundle.getBundle("testWatch").getString("hello");
}
/**
* Return basic resource, it should be handled from target/extra directory.
*/
protected String getHelloResource() {
ResourceBundle.clearCache();
return ResourceBundle.getBundle("test").getString("hello");
}