001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import com.liferay.portal.kernel.concurrent.ConcurrentHashSet;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    
021    import java.lang.reflect.Field;
022    
023    import java.util.Set;
024    
025    /**
026     * @author Shuyang Zhou
027     */
028    public class ReferenceRegistry {
029    
030            public static void registerReference(
031                    Class<?> clazz, Object object, String fieldName) {
032    
033                    try {
034                            Field field = clazz.getDeclaredField(fieldName);
035    
036                            ReferenceEntry referenceEntry = new ReferenceEntry(object, field);
037    
038                            _referenceEntries.add(referenceEntry);
039                    }
040                    catch (SecurityException se) {
041                            if (_log.isWarnEnabled()) {
042                                    _log.warn(
043                                            "Not allowed to get field " + fieldName + " for " + clazz);
044                            }
045                    }
046                    catch (Exception e) {
047                            _log.error("Unable to get field " + fieldName + " for " + clazz);
048                    }
049            }
050    
051            public static void registerReference(Class<?> clazz, String fieldName) {
052                    registerReference(clazz, null, fieldName);
053            }
054    
055            public static void registerReference(Field field) {
056                    ReferenceEntry referenceEntry = new ReferenceEntry(field);
057    
058                    _referenceEntries.add(referenceEntry);
059            }
060    
061            public static void registerReference(Object object, Field field) {
062                    ReferenceEntry referenceEntry = new ReferenceEntry(object, field);
063    
064                    _referenceEntries.add(referenceEntry);
065            }
066    
067            public static void releaseReferences() {
068                    for (ReferenceEntry referenceEntry : _referenceEntries) {
069                            try {
070                                    referenceEntry.setValue(null);
071                            }
072                            catch (Exception e) {
073                                    _log.error(
074                                            "Failed to release reference for " + referenceEntry, e);
075                            }
076                    }
077    
078                    _referenceEntries.clear();
079            }
080    
081            private static Log _log = LogFactoryUtil.getLog(ReferenceRegistry.class);
082    
083            private static Set<ReferenceEntry> _referenceEntries =
084                    new ConcurrentHashSet<ReferenceEntry>();
085    
086    }