Frames | No Frames |
1: /* ExpirableObject.java -- an object that is automatically destroyed. 2: Copyright (C) 2004, 2006 Free Software Foundation, Inc. 3: 4: This file is a part of GNU Classpath. 5: 6: GNU Classpath is free software; you can redistribute it and/or modify 7: it under the terms of the GNU General Public License as published by 8: the Free Software Foundation; either version 2 of the License, or (at 9: your option) any later version. 10: 11: GNU Classpath is distributed in the hope that it will be useful, but 12: WITHOUT ANY WARRANTY; without even the implied warranty of 13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14: General Public License for more details. 15: 16: You should have received a copy of the GNU General Public License 17: along with GNU Classpath; if not, write to the Free Software 18: Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 19: USA 20: 21: Linking this library statically or dynamically with other modules is 22: making a combined work based on this library. Thus, the terms and 23: conditions of the GNU General Public License cover the whole 24: combination. 25: 26: As a special exception, the copyright holders of this library give you 27: permission to link this library with independent modules to produce an 28: executable, regardless of the license terms of these independent 29: modules, and to copy and distribute the resulting executable under 30: terms of your choice, provided that you also meet, for each linked 31: independent module, the terms and conditions of the license of that 32: module. An independent module is a module which is not derived from 33: or based on this library. If you modify this library, you may extend 34: this exception to your version of the library, but you are not 35: obligated to do so. If you do not wish to do so, delete this 36: exception statement from your version. */ 37: 38: 39: package gnu.java.security.util; 40: 41: import java.util.Timer; 42: import java.util.TimerTask; 43: 44: import javax.security.auth.DestroyFailedException; 45: import javax.security.auth.Destroyable; 46: 47: /** 48: * The base class for objects with sensitive data that are automatically 49: * destroyed after a timeout elapses. On creation, an object that extends this 50: * class will automatically be added to a {@link Timer} object that, once a 51: * timeout elapses, will automatically call the {@link Destroyable#destroy()} 52: * method. 53: * <p> 54: * Concrete subclasses must implement the {@link #doDestroy()} method instead of 55: * {@link Destroyable#destroy()}; the behavior of that method should match 56: * exactly the behavior desired of <code>destroy()</code>. 57: * <p> 58: * Note that if a {@link DestroyFailedException} occurs when the timeout 59: * expires, it will not be reported. 60: * 61: * @see Destroyable 62: */ 63: public abstract class ExpirableObject 64: implements Destroyable 65: { 66: /** 67: * The default timeout, used in the default constructor. 68: */ 69: public static final long DEFAULT_TIMEOUT = 3600000L; 70: 71: /** 72: * The timer that expires instances. 73: */ 74: private static final Timer EXPIRER = new Timer(true); 75: 76: /** 77: * A reference to the task that will destroy this object when the timeout 78: * expires. 79: */ 80: private final Destroyer destroyer; 81: 82: /** 83: * Create a new expirable object that will expire after one hour. 84: */ 85: protected ExpirableObject() 86: { 87: this(DEFAULT_TIMEOUT); 88: } 89: 90: /** 91: * Create a new expirable object that will expire after the specified timeout. 92: * 93: * @param delay The delay before expiration. 94: * @throws IllegalArgumentException If <i>delay</i> is negative, or if 95: * <code>delay + System.currentTimeMillis()</code> is negative. 96: */ 97: protected ExpirableObject(final long delay) 98: { 99: destroyer = new Destroyer(this); 100: EXPIRER.schedule(destroyer, delay); 101: } 102: 103: /** 104: * Destroys this object. This method calls {@link #doDestroy}, then, if no 105: * exception is thrown, cancels the task that would destroy this object when 106: * the timeout is reached. 107: * 108: * @throws DestroyFailedException If this operation fails. 109: */ 110: public final void destroy() throws DestroyFailedException 111: { 112: doDestroy(); 113: destroyer.cancel(); 114: } 115: 116: /** 117: * Subclasses must implement this method instead of the {@link 118: * Destroyable#destroy()} method. 119: * 120: * @throws DestroyFailedException If this operation fails. 121: */ 122: protected abstract void doDestroy() throws DestroyFailedException; 123: 124: /** 125: * The task that destroys the target when the timeout elapses. 126: */ 127: private final class Destroyer 128: extends TimerTask 129: { 130: private final ExpirableObject target; 131: 132: Destroyer(final ExpirableObject target) 133: { 134: super(); 135: this.target = target; 136: } 137: 138: public void run() 139: { 140: try 141: { 142: if (! target.isDestroyed()) 143: target.doDestroy(); 144: } 145: catch (DestroyFailedException dfe) 146: { 147: } 148: } 149: } 150: }