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

com.liferay.portal.kernel.search.OpenSearchRegistryUtil Maven / Gradle / Ivy

There is a newer version: 7.4.3.112-ga112
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.search;

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.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Eudaldo Alonso
 */
public class OpenSearchRegistryUtil {

	public static OpenSearch getOpenSearch(Class clazz) {
		return _instance._openSearchInstances.get(clazz.getName());
	}

	public static OpenSearch getOpenSearch(String className) {
		return _instance._openSearchInstances.get(className);
	}

	public static List getOpenSearchInstances() {
		List openSearchInstances = new ArrayList<>(
			_instance._openSearchInstances.values());

		return Collections.unmodifiableList(openSearchInstances);
	}

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

		_serviceTracker = registry.trackServices(
			OpenSearch.class, new OpenSearchServiceTrackerCustomizer());

		_serviceTracker.open();
	}

	private static final OpenSearchRegistryUtil _instance =
		new OpenSearchRegistryUtil();

	private final Map _openSearchInstances =
		new ConcurrentHashMap<>();
	private final ServiceTracker _serviceTracker;

	private class OpenSearchServiceTrackerCustomizer
		implements ServiceTrackerCustomizer {

		@Override
		public OpenSearch addingService(
			ServiceReference serviceReference) {

			Registry registry = RegistryUtil.getRegistry();

			OpenSearch openSearch = registry.getService(serviceReference);

			_openSearchInstances.put(openSearch.getClassName(), openSearch);

			return openSearch;
		}

		@Override
		public void modifiedService(
			ServiceReference serviceReference,
			OpenSearch openSearch) {
		}

		@Override
		public void removedService(
			ServiceReference serviceReference,
			OpenSearch openSearch) {

			Registry registry = RegistryUtil.getRegistry();

			registry.ungetService(serviceReference);

			_openSearchInstances.remove(openSearch.getClassName());
		}

	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy