001/** 002 * 003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 017 * 018 **/ 019package lucee.commons.io.retirement; 020 021import java.util.ArrayList; 022import java.util.List; 023 024import lucee.commons.io.SystemUtil; 025import lucee.commons.lang.ExceptionUtil; 026 027public class RetireOutputStreamFactory { 028 029 static List<RetireOutputStream> list=new ArrayList<RetireOutputStream>(); 030 private static RetireThread thread; 031 032 static void startThread(long timeout) { 033 if(timeout<1000) timeout=1000; 034 if(thread==null || !thread.isAlive()) { 035 thread=new RetireThread(timeout); 036 thread.start(); 037 } 038 else if(thread.sleepTime>timeout) { 039 thread.sleepTime=timeout; 040 SystemUtil.notify(thread); 041 } 042 } 043 044 static class RetireThread extends Thread { 045 046 public long sleepTime; 047 048 public RetireThread(long sleepTime){ 049 this.sleepTime=sleepTime; 050 } 051 052 053 public void run(){ 054 //print.e("start thread"); 055 while(true){ 056 try{ 057 if(list.size()==0) break; 058 SystemUtil.wait(this,sleepTime); 059 //SystemUtil.sleep(sleepTime); 060 RetireOutputStream[] arr = list.toArray(new RetireOutputStream[list.size()]); // not using iterator to avoid ConcurrentModificationException 061 for(int i=0;i<arr.length;i++){ 062 arr[i].retire(); 063 } 064 065 } 066 catch(Throwable t){ 067 ExceptionUtil.rethrowIfNecessary(t); 068 t.printStackTrace(); 069 } 070 } 071 //print.e("stop thread"); 072 thread=null; 073 } 074 } 075}