001    /**
002     * Copyright (c) 2000-2011 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                            _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    }