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