001/*
002 * Copyright (C) 2007 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.base;
018
019import com.google.common.annotations.VisibleForTesting;
020
021import java.io.Closeable;
022import java.io.FileNotFoundException;
023import java.io.IOException;
024import java.lang.ref.PhantomReference;
025import java.lang.ref.Reference;
026import java.lang.ref.ReferenceQueue;
027import java.lang.reflect.Method;
028import java.net.URL;
029import java.net.URLClassLoader;
030import java.util.logging.Level;
031import java.util.logging.Logger;
032
033import javax.annotation.Nullable;
034
035/**
036 * A reference queue with an associated background thread that dequeues references and invokes
037 * {@link FinalizableReference#finalizeReferent()} on them.
038 *
039 * <p>Keep a strong reference to this object until all of the associated referents have been
040 * finalized. If this object is garbage collected earlier, the backing thread will not invoke {@code
041 * finalizeReferent()} on the remaining references.
042 *
043 * <p>As an example of how this is used, imagine you have a class {@code MyServer} that creates a
044 * a {@link java.net.ServerSocket ServerSocket}, and you would like to ensure that the
045 * {@code ServerSocket} is closed even if the {@code MyServer} object is garbage-collected without
046 * calling its {@code close} method. You <em>could</em> use a finalizer to accomplish this, but
047 * that has a number of well-known problems. Here is how you might use this class instead:
048 *
049 * <pre>
050 * public class MyServer implements Closeable {
051 *   private static final FinalizableReferenceQueue frq = new FinalizableReferenceQueue();
052 *   // You might also share this between several objects.
053 *
054 *   private static final Set&lt;Reference&lt;?>> references = Sets.newConcurrentHashSet();
055 *   // This ensures that the FinalizablePhantomReference itself is not garbage-collected.
056 *
057 *   private final ServerSocket serverSocket;
058 *
059 *   private MyServer(...) {
060 *     ...
061 *     this.serverSocket = new ServerSocket(...);
062 *     ...
063 *   }
064 *
065 *   public static MyServer create(...) {
066 *     MyServer myServer = new MyServer(...);
067 *     final ServerSocket serverSocket = myServer.serverSocket;
068 *     Reference&lt;?> reference = new FinalizablePhantomReference&lt;MyServer>(myServer, frq) {
069 *       &#64;Override public void finalizeReferent() {
070 *         references.remove(this):
071 *         if (!serverSocket.isClosed()) {
072 *           ...log a message about how nobody called close()...
073 *           try {
074 *             serverSocket.close();
075 *           } catch (IOException e) {
076 *             ...
077 *           }
078 *         }
079 *       }
080 *     };
081 *     references.add(reference);
082 *     return myServer;
083 *   }
084 *
085 *   &#64;Override public void close() {
086 *     serverSocket.close();
087 *   }
088 * }
089 * </pre>
090 *
091 * @author Bob Lee
092 * @since 2.0
093 */
094public class FinalizableReferenceQueue implements Closeable {
095  /*
096   * The Finalizer thread keeps a phantom reference to this object. When the client (for example, a
097   * map built by MapMaker) no longer has a strong reference to this object, the garbage collector
098   * will reclaim it and enqueue the phantom reference. The enqueued reference will trigger the
099   * Finalizer to stop.
100   *
101   * If this library is loaded in the system class loader, FinalizableReferenceQueue can load
102   * Finalizer directly with no problems.
103   *
104   * If this library is loaded in an application class loader, it's important that Finalizer not
105   * have a strong reference back to the class loader. Otherwise, you could have a graph like this:
106   *
107   * Finalizer Thread runs instance of -> Finalizer.class loaded by -> Application class loader
108   * which loaded -> ReferenceMap.class which has a static -> FinalizableReferenceQueue instance
109   *
110   * Even if no other references to classes from the application class loader remain, the Finalizer
111   * thread keeps an indirect strong reference to the queue in ReferenceMap, which keeps the
112   * Finalizer running, and as a result, the application class loader can never be reclaimed.
113   *
114   * This means that dynamically loaded web applications and OSGi bundles can't be unloaded.
115   *
116   * If the library is loaded in an application class loader, we try to break the cycle by loading
117   * Finalizer in its own independent class loader:
118   *
119   * System class loader -> Application class loader -> ReferenceMap -> FinalizableReferenceQueue
120   * -> etc. -> Decoupled class loader -> Finalizer
121   *
122   * Now, Finalizer no longer keeps an indirect strong reference to the static
123   * FinalizableReferenceQueue field in ReferenceMap. The application class loader can be reclaimed
124   * at which point the Finalizer thread will stop and its decoupled class loader can also be
125   * reclaimed.
126   *
127   * If any of this fails along the way, we fall back to loading Finalizer directly in the
128   * application class loader.
129   */
130
131  private static final Logger logger = Logger.getLogger(FinalizableReferenceQueue.class.getName());
132
133  private static final String FINALIZER_CLASS_NAME = "com.google.common.base.internal.Finalizer";
134
135  /** Reference to Finalizer.startFinalizer(). */
136  private static final Method startFinalizer;
137
138  static {
139    Class<?> finalizer =
140        loadFinalizer(new SystemLoader(), new DecoupledLoader(), new DirectLoader());
141    startFinalizer = getStartFinalizer(finalizer);
142  }
143
144  /**
145   * The actual reference queue that our background thread will poll.
146   */
147  final ReferenceQueue<Object> queue;
148
149  final PhantomReference<Object> frqRef;
150
151  /**
152   * Whether or not the background thread started successfully.
153   */
154  final boolean threadStarted;
155
156  /**
157   * Constructs a new queue.
158   */
159  public FinalizableReferenceQueue() {
160    // We could start the finalizer lazily, but I'd rather it blow up early.
161    queue = new ReferenceQueue<Object>();
162    frqRef = new PhantomReference<Object>(this, queue);
163    boolean threadStarted = false;
164    try {
165      startFinalizer.invoke(null, FinalizableReference.class, queue, frqRef);
166      threadStarted = true;
167    } catch (IllegalAccessException impossible) {
168      throw new AssertionError(impossible); // startFinalizer() is public
169    } catch (Throwable t) {
170      logger.log(
171          Level.INFO,
172          "Failed to start reference finalizer thread."
173              + " Reference cleanup will only occur when new references are created.",
174          t);
175    }
176
177    this.threadStarted = threadStarted;
178  }
179
180  @Override
181  public void close() {
182    frqRef.enqueue();
183    cleanUp();
184  }
185
186  /**
187   * Repeatedly dequeues references from the queue and invokes {@link
188   * FinalizableReference#finalizeReferent()} on them until the queue is empty. This method is a
189   * no-op if the background thread was created successfully.
190   */
191  void cleanUp() {
192    if (threadStarted) {
193      return;
194    }
195
196    Reference<?> reference;
197    while ((reference = queue.poll()) != null) {
198      /*
199       * This is for the benefit of phantom references. Weak and soft references will have already
200       * been cleared by this point.
201       */
202      reference.clear();
203      try {
204        ((FinalizableReference) reference).finalizeReferent();
205      } catch (Throwable t) {
206        logger.log(Level.SEVERE, "Error cleaning up after reference.", t);
207      }
208    }
209  }
210
211  /**
212   * Iterates through the given loaders until it finds one that can load Finalizer.
213   *
214   * @return Finalizer.class
215   */
216  private static Class<?> loadFinalizer(FinalizerLoader... loaders) {
217    for (FinalizerLoader loader : loaders) {
218      Class<?> finalizer = loader.loadFinalizer();
219      if (finalizer != null) {
220        return finalizer;
221      }
222    }
223
224    throw new AssertionError();
225  }
226
227  /**
228   * Loads Finalizer.class.
229   */
230  interface FinalizerLoader {
231
232    /**
233     * Returns Finalizer.class or null if this loader shouldn't or can't load it.
234     *
235     * @throws SecurityException if we don't have the appropriate privileges
236     */
237    @Nullable
238    Class<?> loadFinalizer();
239  }
240
241  /**
242   * Tries to load Finalizer from the system class loader. If Finalizer is in the system class path,
243   * we needn't create a separate loader.
244   */
245  static class SystemLoader implements FinalizerLoader {
246    // This is used by the ClassLoader-leak test in FinalizableReferenceQueueTest to disable
247    // finding Finalizer on the system class path even if it is there.
248    @VisibleForTesting static boolean disabled;
249
250    @Override
251    public Class<?> loadFinalizer() {
252      if (disabled) {
253        return null;
254      }
255      ClassLoader systemLoader;
256      try {
257        systemLoader = ClassLoader.getSystemClassLoader();
258      } catch (SecurityException e) {
259        logger.info("Not allowed to access system class loader.");
260        return null;
261      }
262      if (systemLoader != null) {
263        try {
264          return systemLoader.loadClass(FINALIZER_CLASS_NAME);
265        } catch (ClassNotFoundException e) {
266          // Ignore. Finalizer is simply in a child class loader.
267          return null;
268        }
269      } else {
270        return null;
271      }
272    }
273  }
274
275  /**
276   * Try to load Finalizer in its own class loader. If Finalizer's thread had a direct reference to
277   * our class loader (which could be that of a dynamically loaded web application or OSGi bundle),
278   * it would prevent our class loader from getting garbage collected.
279   */
280  static class DecoupledLoader implements FinalizerLoader {
281    private static final String LOADING_ERROR =
282        "Could not load Finalizer in its own class loader. Loading Finalizer in the current class "
283            + "loader instead. As a result, you will not be able to garbage collect this class "
284            + "loader. To support reclaiming this class loader, either resolve the underlying "
285            + "issue, or move Guava to your system class path.";
286
287    @Override
288    public Class<?> loadFinalizer() {
289      try {
290        /*
291         * We use URLClassLoader because it's the only concrete class loader implementation in the
292         * JDK. If we used our own ClassLoader subclass, Finalizer would indirectly reference this
293         * class loader:
294         *
295         * Finalizer.class -> CustomClassLoader -> CustomClassLoader.class -> This class loader
296         *
297         * System class loader will (and must) be the parent.
298         */
299        ClassLoader finalizerLoader = newLoader(getBaseUrl());
300        return finalizerLoader.loadClass(FINALIZER_CLASS_NAME);
301      } catch (Exception e) {
302        logger.log(Level.WARNING, LOADING_ERROR, e);
303        return null;
304      }
305    }
306
307    /**
308     * Gets URL for base of path containing Finalizer.class.
309     */
310    URL getBaseUrl() throws IOException {
311      // Find URL pointing to Finalizer.class file.
312      String finalizerPath = FINALIZER_CLASS_NAME.replace('.', '/') + ".class";
313      URL finalizerUrl = getClass().getClassLoader().getResource(finalizerPath);
314      if (finalizerUrl == null) {
315        throw new FileNotFoundException(finalizerPath);
316      }
317
318      // Find URL pointing to base of class path.
319      String urlString = finalizerUrl.toString();
320      if (!urlString.endsWith(finalizerPath)) {
321        throw new IOException("Unsupported path style: " + urlString);
322      }
323      urlString = urlString.substring(0, urlString.length() - finalizerPath.length());
324      return new URL(finalizerUrl, urlString);
325    }
326
327    /** Creates a class loader with the given base URL as its classpath. */
328    URLClassLoader newLoader(URL base) {
329      // We use the bootstrap class loader as the parent because Finalizer by design uses
330      // only standard Java classes. That also means that FinalizableReferenceQueueTest
331      // doesn't pick up the wrong version of the Finalizer class.
332      return new URLClassLoader(new URL[] {base}, null);
333    }
334  }
335
336  /**
337   * Loads Finalizer directly using the current class loader. We won't be able to garbage collect
338   * this class loader, but at least the world doesn't end.
339   */
340  static class DirectLoader implements FinalizerLoader {
341    @Override
342    public Class<?> loadFinalizer() {
343      try {
344        return Class.forName(FINALIZER_CLASS_NAME);
345      } catch (ClassNotFoundException e) {
346        throw new AssertionError(e);
347      }
348    }
349  }
350
351  /**
352   * Looks up Finalizer.startFinalizer().
353   */
354  static Method getStartFinalizer(Class<?> finalizer) {
355    try {
356      return finalizer.getMethod(
357          "startFinalizer", Class.class, ReferenceQueue.class, PhantomReference.class);
358    } catch (NoSuchMethodException e) {
359      throw new AssertionError(e);
360    }
361  }
362}