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

com.liferay.portal.kernel.atom.AtomCollectionAdapterRegistryUtil Maven / Gradle / Ivy

Go to download

Contains interfaces for the portal services. Interfaces are only loaded by the global class loader and are shared by all plugins.

There is a newer version: 156.0.0
Show newest version
/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.liferay.portal.kernel.atom;

import aQute.bnd.annotation.ProviderType;

import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.ListUtil;
import com.liferay.registry.Registry;
import com.liferay.registry.RegistryUtil;
import com.liferay.registry.ServiceReference;
import com.liferay.registry.ServiceTracker;
import com.liferay.registry.ServiceTrackerCustomizer;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Igor Spasic
 */
@ProviderType
public class AtomCollectionAdapterRegistryUtil {

	public static AtomCollectionAdapter getAtomCollectionAdapter(
		String collectionName) {

		return _instance._getAtomCollectionAdapter(collectionName);
	}

	public static List> getAtomCollectionAdapters() {
		return _instance._getAtomCollectionAdapters();
	}

	private AtomCollectionAdapterRegistryUtil() {
		Registry registry = RegistryUtil.getRegistry();

		_serviceTracker = registry.trackServices(
			(Class>)(Class)
				AtomCollectionAdapter.class,
			new AtomCollectionAdapterServiceTrackerCustomizer());

		_serviceTracker.open();
	}

	private AtomCollectionAdapter _getAtomCollectionAdapter(
		String collectionName) {

		return _atomCollectionAdapters.get(collectionName);
	}

	private List> _getAtomCollectionAdapters() {
		return ListUtil.fromMapValues(_atomCollectionAdapters);
	}

	private static final Log _log = LogFactoryUtil.getLog(
		AtomCollectionAdapterRegistryUtil.class);

	private static final AtomCollectionAdapterRegistryUtil _instance =
		new AtomCollectionAdapterRegistryUtil();

	private final Map>
		_atomCollectionAdapters = new ConcurrentHashMap<>();
	private final
		ServiceTracker, AtomCollectionAdapter>
			_serviceTracker;

	private class AtomCollectionAdapterServiceTrackerCustomizer
		implements ServiceTrackerCustomizer
			, AtomCollectionAdapter> {

		@Override
		public AtomCollectionAdapter addingService(
			ServiceReference> serviceReference) {

			Registry registry = RegistryUtil.getRegistry();

			AtomCollectionAdapter atomCollectionAdapter =
				registry.getService(serviceReference);

			if (_atomCollectionAdapters.containsKey(
					atomCollectionAdapter.getCollectionName())) {

				if (_log.isWarnEnabled()) {
					_log.warn(
						"Duplicate collection name " +
							atomCollectionAdapter.getCollectionName());
				}

				return null;
			}

			_atomCollectionAdapters.put(
				atomCollectionAdapter.getCollectionName(),
				atomCollectionAdapter);

			return atomCollectionAdapter;
		}

		@Override
		public void modifiedService(
			ServiceReference> serviceReference,
			AtomCollectionAdapter atomCollectionAdapter) {
		}

		@Override
		public void removedService(
			ServiceReference> serviceReference,
			AtomCollectionAdapter atomCollectionAdapter) {

			Registry registry = RegistryUtil.getRegistry();

			registry.ungetService(serviceReference);

			_atomCollectionAdapters.remove(
				atomCollectionAdapter.getCollectionName());
		}

	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy