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.io.OutputStreamWriter;
018    import com.liferay.portal.kernel.io.unsync.UnsyncPrintWriter;
019    import com.liferay.portal.kernel.memory.PoolAction;
020    import com.liferay.portal.kernel.memory.SoftReferencePool;
021    
022    import java.io.OutputStream;
023    import java.io.Writer;
024    
025    import java.util.ArrayList;
026    import java.util.List;
027    
028    /**
029     * @author Shuyang Zhou
030     */
031    public class UnsyncPrintWriterPool {
032    
033            public static UnsyncPrintWriter borrow(OutputStream outputStream) {
034                    return borrow(new OutputStreamWriter(outputStream));
035            }
036    
037            public static UnsyncPrintWriter borrow(Writer writer) {
038                    if (!isEnabled()) {
039                            return new UnsyncPrintWriter(writer);
040                    }
041    
042                    UnsyncPrintWriter unsyncPrintWriter =
043                            _unsyncPrintWriterSoftReferencePool.borrowObject(writer);
044    
045                    List<UnsyncPrintWriter> unsyncPrintWriters =
046                            _borrowedUnsyncPrintWritersThreadLocal.get();
047    
048                    unsyncPrintWriters.add(unsyncPrintWriter);
049    
050                    return unsyncPrintWriter;
051            }
052    
053            public static void cleanUp() {
054                    List<UnsyncPrintWriter> unsyncPrintWriters =
055                            _borrowedUnsyncPrintWritersThreadLocal.get();
056    
057                    for (UnsyncPrintWriter unsyncPrintWriter : unsyncPrintWriters) {
058                            _unsyncPrintWriterSoftReferencePool.returnObject(unsyncPrintWriter);
059                    }
060    
061                    unsyncPrintWriters.clear();
062            }
063    
064            public static boolean isEnabled() {
065                    return _enabledThreadLocal.get();
066            }
067    
068            public static void setEnabled(boolean enabled) {
069                    _enabledThreadLocal.set(enabled);
070            }
071    
072            private static ThreadLocal<List<UnsyncPrintWriter>>
073                    _borrowedUnsyncPrintWritersThreadLocal =
074                            new AutoResetThreadLocal<List<UnsyncPrintWriter>>(
075                                    UnsyncPrintWriterPool.class.getName() +
076                                            "._borrowedUnsyncPrintWritersThreadLocal",
077                                    new ArrayList<UnsyncPrintWriter>());
078            private static ThreadLocal<Boolean> _enabledThreadLocal =
079                    new AutoResetThreadLocal<Boolean>(
080                            UnsyncPrintWriterPool.class.getName() + "._enabledThreadLocal",
081                            false);
082            private static SoftReferencePool<UnsyncPrintWriter, Writer>
083                    _unsyncPrintWriterSoftReferencePool =
084                            new SoftReferencePool<UnsyncPrintWriter, Writer>(
085                                    new UnsyncPrintWriterPoolAction(), 8192);
086    
087            private static class UnsyncPrintWriterPoolAction
088                    implements PoolAction<UnsyncPrintWriter, Writer> {
089    
090                    public UnsyncPrintWriter onBorrow(
091                            UnsyncPrintWriter unsyncPrintWriter, Writer writer) {
092    
093                            unsyncPrintWriter.reset(writer);
094    
095                            return unsyncPrintWriter;
096                    }
097    
098                    public UnsyncPrintWriter onCreate(Writer writer) {
099                            return new UnsyncPrintWriter(writer);
100                    }
101    
102                    public void onReturn(UnsyncPrintWriter unsyncPrintWriter) {
103                            unsyncPrintWriter.reset(null);
104                    }
105    
106            }
107    
108    }