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.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                    @Override
091                    public UnsyncPrintWriter onBorrow(
092                            UnsyncPrintWriter unsyncPrintWriter, Writer writer) {
093    
094                            unsyncPrintWriter.reset(writer);
095    
096                            return unsyncPrintWriter;
097                    }
098    
099                    @Override
100                    public UnsyncPrintWriter onCreate(Writer writer) {
101                            return new UnsyncPrintWriter(writer);
102                    }
103    
104                    @Override
105                    public void onReturn(UnsyncPrintWriter unsyncPrintWriter) {
106                            unsyncPrintWriter.reset(null);
107                    }
108    
109            }
110    
111    }