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                            ReferenceEntry referenceEntry = _pacl.getReferenceEntry(
035                                    clazz, object, fieldName);
036    
037                            _referenceEntries.add(referenceEntry);
038                    }
039                    catch (SecurityException se) {
040                            if (_log.isWarnEnabled()) {
041                                    _log.warn(
042                                            "Not allowed to get field " + fieldName + " for " + clazz);
043                            }
044                    }
045                    catch (Exception e) {
046                            _log.error("Unable to get field " + fieldName + " for " + clazz);
047                    }
048            }
049    
050            public static void registerReference(Class<?> clazz, String fieldName) {
051                    registerReference(clazz, null, fieldName);
052            }
053    
054            public static void registerReference(Field field) {
055                    ReferenceEntry referenceEntry = new ReferenceEntry(field);
056    
057                    _referenceEntries.add(referenceEntry);
058            }
059    
060            public static void registerReference(Object object, Field field) {
061                    ReferenceEntry referenceEntry = new ReferenceEntry(object, field);
062    
063                    _referenceEntries.add(referenceEntry);
064            }
065    
066            public static void releaseReferences() {
067                    for (ReferenceEntry referenceEntry : _referenceEntries) {
068                            try {
069                                    referenceEntry.setValue(null);
070                            }
071                            catch (Exception e) {
072                                    _log.error(
073                                            "Failed to release reference for " + referenceEntry, e);
074                            }
075                    }
076    
077                    _referenceEntries.clear();
078            }
079    
080            public static interface PACL {
081    
082                    public ReferenceEntry getReferenceEntry(
083                                    Class<?> clazz, Object object, String fieldName)
084                            throws NoSuchFieldException, SecurityException;
085    
086            }
087    
088            private static Log _log = LogFactoryUtil.getLog(ReferenceRegistry.class);
089    
090            private static PACL _pacl = new NoPACL();
091            private static Set<ReferenceEntry> _referenceEntries =
092                    new ConcurrentHashSet<ReferenceEntry>();
093    
094            private static class NoPACL implements PACL {
095    
096                    @Override
097                    public ReferenceEntry getReferenceEntry(
098                                    Class<?> clazz, Object object, String fieldName)
099                            throws NoSuchFieldException, SecurityException {
100    
101                            Field field = clazz.getDeclaredField(fieldName);
102    
103                            return new ReferenceEntry(object, field);
104                    }
105    
106            }
107    
108    }