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(
038                    OutputStream outputStream, String charsetName) {
039    
040                    return borrow(new OutputStreamWriter(outputStream, charsetName));
041            }
042    
043            public static UnsyncPrintWriter borrow(Writer writer) {
044                    if (!isEnabled()) {
045                            return new UnsyncPrintWriter(writer);
046                    }
047    
048                    UnsyncPrintWriter unsyncPrintWriter =
049                            _unsyncPrintWriterSoftReferencePool.borrowObject(writer);
050    
051                    List<UnsyncPrintWriter> unsyncPrintWriters =
052                            _borrowedUnsyncPrintWritersThreadLocal.get();
053    
054                    unsyncPrintWriters.add(unsyncPrintWriter);
055    
056                    return unsyncPrintWriter;
057            }
058    
059            public static void cleanUp() {
060                    List<UnsyncPrintWriter> unsyncPrintWriters =
061                            _borrowedUnsyncPrintWritersThreadLocal.get();
062    
063                    for (UnsyncPrintWriter unsyncPrintWriter : unsyncPrintWriters) {
064                            _unsyncPrintWriterSoftReferencePool.returnObject(unsyncPrintWriter);
065                    }
066    
067                    unsyncPrintWriters.clear();
068            }
069    
070            public static boolean isEnabled() {
071                    return _enabledThreadLocal.get();
072            }
073    
074            public static void setEnabled(boolean enabled) {
075                    _enabledThreadLocal.set(enabled);
076            }
077    
078            private static ThreadLocal<List<UnsyncPrintWriter>>
079                    _borrowedUnsyncPrintWritersThreadLocal =
080                            new AutoResetThreadLocal<List<UnsyncPrintWriter>>(
081                                    UnsyncPrintWriterPool.class.getName() +
082                                            "._borrowedUnsyncPrintWritersThreadLocal",
083                                    new ArrayList<UnsyncPrintWriter>());
084            private static ThreadLocal<Boolean> _enabledThreadLocal =
085                    new AutoResetThreadLocal<Boolean>(
086                            UnsyncPrintWriterPool.class.getName() + "._enabledThreadLocal",
087                            false);
088            private static SoftReferencePool<UnsyncPrintWriter, Writer>
089                    _unsyncPrintWriterSoftReferencePool =
090                            new SoftReferencePool<UnsyncPrintWriter, Writer>(
091                                    new UnsyncPrintWriterPoolAction(), 8192);
092    
093            private static class UnsyncPrintWriterPoolAction
094                    implements PoolAction<UnsyncPrintWriter, Writer> {
095    
096                    @Override
097                    public UnsyncPrintWriter onBorrow(
098                            UnsyncPrintWriter unsyncPrintWriter, Writer writer) {
099    
100                            unsyncPrintWriter.reset(writer);
101    
102                            return unsyncPrintWriter;
103                    }
104    
105                    @Override
106                    public UnsyncPrintWriter onCreate(Writer writer) {
107                            return new UnsyncPrintWriter(writer);
108                    }
109    
110                    @Override
111                    public void onReturn(UnsyncPrintWriter unsyncPrintWriter) {
112                            unsyncPrintWriter.reset(null);
113                    }
114    
115            }
116    
117    }