1 2 3 /* 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 5 * 6 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. 7 * 8 * Portions Copyright Apache Software Foundation. 9 * 10 * The contents of this file are subject to the terms of either the GNU 11 * General Public License Version 2 only ("GPL") or the Common Development 12 * and Distribution License("CDDL") (collectively, the "License"). You 13 * may not use this file except in compliance with the License. You can obtain 14 * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html 15 * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific 16 * language governing permissions and limitations under the License. 17 * 18 * When distributing the software, include this License Header Notice in each 19 * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt. 20 * Sun designates this particular file as subject to the "Classpath" exception 21 * as provided by Sun in the GPL Version 2 section of the License file that 22 * accompanied this code. If applicable, add the following below the License 23 * Header, with the fields enclosed by brackets [] replaced by your own 24 * identifying information: "Portions Copyrighted [year] 25 * [name of copyright owner]" 26 * 27 * Contributor(s): 28 * 29 * If you wish your version of this file to be governed by only the CDDL or 30 * only the GPL Version 2, indicate your decision by adding "[Contributor] 31 * elects to include this software in this distribution under the [CDDL or GPL 32 * Version 2] license." If you don't indicate a single choice of license, a 33 * recipient has the option to distribute your version of this file under 34 * either the CDDL, the GPL Version 2 or to extend the choice of license to 35 * its licensees as provided above. However, if you add GPL Version 2 code 36 * and therefore, elected the GPL Version 2 license, then the option applies 37 * only if the new code is made subject to such option by the copyright 38 * holder. 39 */ 40 package org.apache.tomcat.util.threads; 41 42 import java.io.IOException; 43 import java.util.Enumeration; 44 import java.util.Hashtable; 45 import java.util.Vector; 46 import org.apache.tomcat.util.buf; 47 48 /** 49 * Expire unused objects. 50 * 51 */ 52 public final class Expirer implements ThreadPoolRunnable 53 { 54 55 private static com.sun.org.apache.commons.logging.Log log= 56 com.sun.org.apache.commons.logging.LogFactory.getLog(Expirer.class ); 57 58 // We can use Event/Listener, but this is probably simpler 59 // and more efficient 60 public static interface ExpireCallback { 61 public void expired( TimeStamp o ); 62 } 63 64 private int checkInterval = 60; 65 private Reaper reaper; 66 ExpireCallback expireCallback; 67 68 public Expirer() { 69 } 70 71 // ------------------------------------------------------------- Properties 72 public int getCheckInterval() { 73 return checkInterval; 74 } 75 76 public void setCheckInterval(int checkInterval) { 77 this.checkInterval = checkInterval; 78 } 79 80 public void setExpireCallback( ExpireCallback cb ) { 81 expireCallback=cb; 82 } 83 84 // -------------------- Managed objects -------------------- 85 static final int INITIAL_SIZE=8; 86 TimeStamp managedObjs[]=new TimeStamp[INITIAL_SIZE]; 87 TimeStamp checkedObjs[]=new TimeStamp[INITIAL_SIZE]; 88 int managedLen=managedObjs.length; 89 int managedCount=0; 90 91 public void addManagedObject( TimeStamp ts ) { 92 synchronized( managedObjs ) { 93 if( managedCount >= managedLen ) { 94 // What happens if expire is on the way ? Nothing, 95 // expire will do it's job on the old array ( GC magic ) 96 // and the expired object will be marked as such 97 // Same thing would happen ( and did ) with Hashtable 98 TimeStamp newA[]=new TimeStamp[ 2 * managedLen ]; 99 System.arraycopy( managedObjs, 0, newA, 0, managedLen); 100 managedObjs = newA; 101 managedLen = 2 * managedLen; 102 } 103 managedObjs[managedCount]=ts; 104 managedCount++; 105 } 106 } 107 108 public void removeManagedObject( TimeStamp ts ) { 109 for( int i=0; i< managedCount; i++ ) { 110 if( ts == managedObjs[i] ) { 111 synchronized( managedObjs ) { 112 managedObjs[ i ] = managedObjs[managedCount-1]; 113 managedCount--; 114 } 115 return; 116 } 117 } 118 } 119 120 // --------------------------------------------------------- Public Methods 121 122 public void start() { 123 // Start the background reaper thread 124 if( reaper==null) { 125 reaper=new Reaper("Expirer"); 126 reaper.addCallback( this, checkInterval * 1000 ); 127 } 128 129 reaper.startReaper(); 130 } 131 132 public void stop() { 133 reaper.stopReaper(); 134 } 135 136 137 // -------------------------------------------------------- Private Methods 138 139 // ThreadPoolRunnable impl 140 141 public Object[] getInitData() { 142 return null; 143 } 144 145 public void runIt( Object td[] ) { 146 long timeNow = System.currentTimeMillis(); 147 if( dL > 2 ) debug( "Checking " + timeNow ); 148 int checkedCount; 149 synchronized( managedObjs ) { 150 checkedCount=managedCount; 151 if(checkedObjs.length < checkedCount) 152 checkedObjs = new TimeStamp[managedLen]; 153 System.arraycopy( managedObjs, 0, checkedObjs, 0, checkedCount); 154 } 155 for( int i=0; i< checkedCount; i++ ) { 156 TimeStamp ts=checkedObjs[i]; 157 checkedObjs[i] = null; 158 159 if (ts==null || !ts.isValid()) 160 continue; 161 162 long maxInactiveInterval = ts.getMaxInactiveInterval(); 163 if( dL > 3 ) debug( "TS: " + maxInactiveInterval + " " + 164 ts.getLastAccessedTime()); 165 if (maxInactiveInterval < 0) 166 continue; 167 168 long timeIdle = timeNow - ts.getLastAccessedTime(); 169 170 if (timeIdle >= maxInactiveInterval) { 171 if( expireCallback != null ) { 172 if( dL > 0 ) 173 debug( ts + " " + timeIdle + " " + 174 maxInactiveInterval ); 175 expireCallback.expired( ts ); 176 } 177 } 178 } 179 } 180 181 private static final int dL=0; 182 private void debug( String s ) { 183 if (log.isDebugEnabled()) 184 log.debug("Expirer: " + s ); 185 } 186 }