Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / gnu / java / security / util / ExpirableObject.java
blob2d4452015afed5b00de28315bdc6dbc4f4494f04
1 /* ExpirableObject.java -- an object that is automatically destroyed.
2 Copyright (C) 2004, 2006 Free Software Foundation, Inc.
4 This file is a part of GNU Classpath.
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.
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.
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
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.
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. */
39 package gnu.java.security.util;
41 import java.util.Timer;
42 import java.util.TimerTask;
44 import javax.security.auth.DestroyFailedException;
45 import javax.security.auth.Destroyable;
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
50 * this class will automatically be added to a {@link Timer} object that,
51 * once a timeout elapses, will automatically call the {@link
52 * Destroyable#destroy()} method.
54 * <p>Concrete subclasses must implement the {@link doDestroy()} method
55 * instead of {@link Destroyable#destroy()}; the behavior of that method
56 * should match exactly the behavior desired of <code>destroy()</code>.
58 * <p>Note that if a {@link DestroyFailedException} occurs when the timeout
59 * expires, it will not be reported.
61 * @see Destroyable
63 public abstract class ExpirableObject implements Destroyable
66 // Constants and fields.
67 // -------------------------------------------------------------------------
69 /**
70 * The default timeout, used in the default constructor.
72 public static final long DEFAULT_TIMEOUT = 3600000L;
74 /**
75 * The timer that expires instances.
77 private static final Timer EXPIRER = new Timer(true);
79 /**
80 * A reference to the task that will destroy this object when the timeout
81 * expires.
83 private final Destroyer destroyer;
85 // Constructors.
86 // -------------------------------------------------------------------------
88 /**
89 * Create a new expirable object that will expire after one hour.
91 protected ExpirableObject()
93 this(DEFAULT_TIMEOUT);
96 /**
97 * Create a new expirable object that will expire after the specified
98 * timeout.
100 * @param delay The delay before expiration.
101 * @throws IllegalArgumentException If <i>delay</i> is negative, or if
102 * <code>delay + System.currentTimeMillis()</code> is negative.
104 protected ExpirableObject(final long delay)
106 destroyer = new Destroyer(this);
107 EXPIRER.schedule(destroyer, delay);
110 // Instance methods.
111 // -------------------------------------------------------------------------
114 * Destroys this object. This method calls {@link doDestroy}, then, if
115 * no exception is thrown, cancels the task that would destroy this object
116 * when the timeout is reached.
118 * @throws DestroyFailedException If this operation fails.
120 public final void destroy() throws DestroyFailedException
122 doDestroy();
123 destroyer.cancel();
127 * Subclasses must implement this method instead of the {@link
128 * Destroyable#destroy()} method.
130 * @throws DestroyFailedException If this operation fails.
132 protected abstract void doDestroy() throws DestroyFailedException;
134 // Inner classes.
135 // -------------------------------------------------------------------------
138 * The task that destroys the target when the timeout elapses.
140 private final class Destroyer extends TimerTask
143 // Fields.
144 // -----------------------------------------------------------------------
146 private final ExpirableObject target;
148 // Constructor.
149 // -----------------------------------------------------------------------
151 Destroyer(final ExpirableObject target)
153 super();
154 this.target = target;
157 // Instance methods.
158 // -----------------------------------------------------------------------
160 public void run()
164 if (!target.isDestroyed())
165 target.doDestroy();
167 catch (DestroyFailedException dfe)