001    /**
002     * Copyright (c) 2000-2012 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                            _referenceEntries.add(new ReferenceEntry(object, field));
037                    }
038                    catch (Exception e) {
039                            _log.error(
040                                    "Failed the get field " + fieldName + " for class " + clazz, e);
041                    }
042            }
043    
044            public static void registerReference(Class<?> clazz, String fieldName) {
045                    registerReference(clazz, null, fieldName);
046            }
047    
048            public static void registerReference(Field field) {
049                    _referenceEntries.add(new ReferenceEntry(field));
050            }
051    
052            public static void registerReference(Object object, Field field) {
053                    _referenceEntries.add(new ReferenceEntry(object, field));
054            }
055    
056            public static void releaseReferences() {
057                    for (ReferenceEntry referenceEntry : _referenceEntries) {
058                            try {
059                                    referenceEntry.setValue(null);
060                            }
061                            catch (Exception e) {
062                                    _log.error(
063                                            "Failed to release reference for " + referenceEntry, e);
064                            }
065                    }
066    
067                    _referenceEntries.clear();
068            }
069    
070            private static Log _log = LogFactoryUtil.getLog(ReferenceRegistry.class);
071    
072            private static Set<ReferenceEntry> _referenceEntries =
073                    new ConcurrentHashSet<ReferenceEntry>();
074    
075    }