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

com.liferay.portal.kernel.util.ReferenceRegistry 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.util;

import com.liferay.portal.kernel.concurrent.ConcurrentHashSet;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;

import java.lang.reflect.Field;

import java.util.Set;

/**
 * @author Shuyang Zhou
 */
public class ReferenceRegistry {

	public static void registerReference(
		Class clazz, Object object, String fieldName) {

		try {
			ReferenceEntry referenceEntry = _pacl.getReferenceEntry(
				clazz, object, fieldName);

			_referenceEntries.add(referenceEntry);
		}
		catch (SecurityException se) {
			if (_log.isWarnEnabled()) {
				_log.warn(
					"Not allowed to get field " + fieldName + " for " + clazz);
			}
		}
		catch (Exception e) {
			_log.error("Unable to get field " + fieldName + " for " + clazz);
		}
	}

	public static void registerReference(Class clazz, String fieldName) {
		registerReference(clazz, null, fieldName);
	}

	public static void registerReference(Field field) {
		ReferenceEntry referenceEntry = new ReferenceEntry(field);

		_referenceEntries.add(referenceEntry);
	}

	public static void registerReference(Object object, Field field) {
		ReferenceEntry referenceEntry = new ReferenceEntry(object, field);

		_referenceEntries.add(referenceEntry);
	}

	public static void releaseReferences() {
		for (ReferenceEntry referenceEntry : _referenceEntries) {
			try {
				referenceEntry.setValue(null);
			}
			catch (Exception e) {
				_log.error(
					"Failed to release reference for " + referenceEntry, e);
			}
		}

		_referenceEntries.clear();
	}

	public interface PACL {

		public ReferenceEntry getReferenceEntry(
				Class clazz, Object object, String fieldName)
			throws NoSuchFieldException, SecurityException;

	}

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

	private static final PACL _pacl = new NoPACL();
	private static final Set _referenceEntries =
		new ConcurrentHashSet<>();

	private static class NoPACL implements PACL {

		@Override
		public ReferenceEntry getReferenceEntry(
				Class clazz, Object object, String fieldName)
			throws NoSuchFieldException, SecurityException {

			Field field = clazz.getDeclaredField(fieldName);

			return new ReferenceEntry(object, field);
		}

	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy