All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.liferay.portal.language.override.internal.PLOOverrideResourceBundleManager Maven / Gradle / Ivy

There is a newer version: 1.0.40
Show newest version
/**
 * SPDX-FileCopyrightText: (c) 2023 Liferay, Inc. https://liferay.com
 * SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
 */

package com.liferay.portal.language.override.internal;

import com.liferay.petra.concurrent.DCLSingleton;
import com.liferay.petra.string.StringBundler;
import com.liferay.petra.string.StringPool;
import com.liferay.portal.kernel.language.Language;
import com.liferay.portal.kernel.security.auth.CompanyThreadLocal;
import com.liferay.portal.kernel.service.CompanyLocalService;
import com.liferay.portal.language.LanguageResources;
import com.liferay.portal.language.override.internal.provider.PLOOriginalTranslationThreadLocal;
import com.liferay.portal.language.override.model.PLOEntry;
import com.liferay.portal.language.override.service.PLOEntryLocalService;

import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;

import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

/**
 * @author Regisson Aguiar
 */
@Component(service = PLOOverrideResourceBundleManager.class)
public class PLOOverrideResourceBundleManager {

	public ResourceBundle getOverrideResourceBundle(Locale locale) {
		Map
			overrideResourceBundles =
				_overrideResourceBundlesDCLSingleton.getSingleton(_supplier);

		if (overrideResourceBundles.isEmpty() ||
			PLOOriginalTranslationThreadLocal.isUseOriginalTranslation()) {

			return null;
		}

		return overrideResourceBundles.get(
			_encodeKey(
				CompanyThreadLocal.getCompanyId(),
				_language.getLanguageId(locale)));
	}

	protected static void clearCache() {
		_overrideResourceBundlesDCLSingleton.destroy(null);

		LanguageResources.clearResourceBundles();
	}

	private void _add(
		Map overrideResourceBundles,
		PLOEntry ploEntry) {

		overrideResourceBundles.compute(
			_encodeKey(ploEntry.getCompanyId(), ploEntry.getLanguageId()),
			(key, value) -> {
				if (value == null) {
					value = new OverrideResourceBundle(new HashMap<>());
				}

				value.put(ploEntry.getKey(), ploEntry.getValue());

				return value;
			});
	}

	private Map
		_createOverrideResourceBundles() {

		Map overrideResourceBundles =
			new ConcurrentHashMap<>();

		_companyLocalService.forEachCompanyId(
			companyId -> {
				for (PLOEntry ploEntry :
						_ploEntryLocalService.getPLOEntries(companyId)) {

					_add(overrideResourceBundles, ploEntry);
				}
			});

		return overrideResourceBundles;
	}

	private String _encodeKey(long companyId, String languageId) {
		return StringBundler.concat(companyId, StringPool.POUND, languageId);
	}

	private static final DCLSingleton>
		_overrideResourceBundlesDCLSingleton = new DCLSingleton<>();

	@Reference
	private CompanyLocalService _companyLocalService;

	@Reference
	private Language _language;

	@Reference
	private PLOEntryLocalService _ploEntryLocalService;

	private final Supplier> _supplier =
		this::_createOverrideResourceBundles;

	private static class OverrideResourceBundle extends ResourceBundle {

		@Override
		public Enumeration getKeys() {
			return Collections.enumeration(_overrideMap.keySet());
		}

		public boolean isEmpty() {
			return _overrideMap.isEmpty();
		}

		public void put(String key, String value) {
			_overrideMap.put(key, value);
		}

		public void remove(String key) {
			_overrideMap.remove(key);
		}

		@Override
		protected Object handleGetObject(String key) {
			return _overrideMap.get(key);
		}

		@Override
		protected Set handleKeySet() {
			return _overrideMap.keySet();
		}

		private OverrideResourceBundle(Map overrideMap) {
			_overrideMap = overrideMap;
		}

		private final Map _overrideMap;

	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy