001    /**
002     * Copyright (c) 2000-2010 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.memory;
016    
017    import com.liferay.portal.kernel.util.GetterUtil;
018    import com.liferay.portal.kernel.util.PropsKeys;
019    import com.liferay.portal.kernel.util.PropsUtil;
020    
021    import java.lang.ref.Reference;
022    import java.lang.ref.ReferenceQueue;
023    import java.lang.ref.WeakReference;
024    
025    import java.util.Map;
026    import java.util.concurrent.ConcurrentHashMap;
027    
028    /**
029     * @author Shuyang Zhou
030     */
031    public class FinalizeManager {
032    
033            public static final boolean THREAD_ENABLED = GetterUtil.getBoolean(
034                    PropsUtil.get(PropsKeys.FINALIZE_MANAGER_THREAD_ENABLED));
035    
036            public static <T> Reference<T> register(
037                    T realReference, FinalizeAction finalizeAction) {
038    
039                    Reference<T> reference = new WeakReference<T>(
040                            realReference, _referenceQueue);
041    
042                    _referenceActionMap.put(reference, finalizeAction);
043    
044                    if (!THREAD_ENABLED) {
045                            _pollingCleanup();
046                    }
047    
048                    return reference;
049            }
050    
051            private static void _pollingCleanup() {
052                    Reference<? extends Object> reference = null;
053    
054                    while ((reference = _referenceQueue.poll()) != null) {
055                            FinalizeAction finalizeAction = _referenceActionMap.remove(
056                                    reference);
057    
058                            finalizeAction.finalize();
059                    }
060            }
061    
062            private static Map<Reference<?>, FinalizeAction> _referenceActionMap =
063                    new ConcurrentHashMap<Reference<?>, FinalizeAction>();
064            private static ReferenceQueue<Object> _referenceQueue =
065                    new ReferenceQueue<Object>();
066    
067            private static class FinalizeThread extends Thread {
068    
069                    public FinalizeThread(String name) {
070                            super(name);
071                    }
072    
073                    public void run() {
074                            while (true) {
075                                    try {
076                                            Reference<? extends Object> reference =
077                                                    _referenceQueue.remove();
078    
079                                            FinalizeAction finalizeAction =
080                                                    _referenceActionMap.remove(reference);
081    
082                                            finalizeAction.finalize();
083                                    }
084                                    catch (InterruptedException ie) {
085                                    }
086                            }
087                    }
088            }
089    
090            static {
091                    if (THREAD_ENABLED) {
092                            Thread thread = new FinalizeThread("Finalize Thread");
093    
094                            thread.setDaemon(true);
095    
096                            thread.start();
097                    }
098            }
099    
100    }