001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    }