Changeset 7713


Ignore:
Timestamp:
May 21, 2019, 1:15:36 PM (2 years ago)
Author:
Nicklas Nordborg
Message:

References #2139: Switch to Java 11 (or later)

Replaced the finalize() methods in StaticCache with a cleaner-based approach.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/core/net/sf/basedb/util/StaticCache.java

    r7622 r7713  
    2222package net.sf.basedb.util;
    2323
     24import java.io.Closeable;
    2425import java.io.File;
    2526import java.io.FileFilter;
     
    3637import java.io.OutputStream;
    3738import java.io.Serializable;
     39import java.lang.ref.Cleaner;
    3840import java.lang.ref.WeakReference;
     41import java.lang.ref.Cleaner.Cleanable;
    3942import java.util.List;
    4043import java.util.Map;
     
    8386    org.slf4j.LoggerFactory.getLogger(StaticCache.class);
    8487
     88  private final static Cleaner cleaner = Cleaner.create();
     89 
    8590  /**
    8691    Checks if the given key is a vilid cache entry key.
     
    273278    finally
    274279    {
    275       if (out != null) out.close();
     280      FileUtil.close(out);
    276281    }
    277282    return numBytes;
     
    329334    finally
    330335    {
    331       if (in != null) in.close();
     336      FileUtil.close(in);
    332337    }
    333338    return numBytes;
     
    385390    finally
    386391    {
    387       if (out != null)
    388       {
    389         try
    390         {
    391           out.close();
    392         }
    393         catch (IOException ex)
    394         {}
    395       }
     392      FileUtil.close(out);
    396393    }
    397394    return stored;
     
    460457    finally
    461458    {
    462       if (in != null)
    463       {
    464         try
    465         {
    466           in.close();
    467         }
    468         catch (IOException ex)
    469         {}
    470       }
     459      FileUtil.close(in);
    471460    }
    472461    return (T)object;   
     
    651640    extends FilterOutputStream
    652641  {
    653     private final Throwable calledFrom;
    654     private ReadWriteLock lock;
    655     private String key;
     642    private final State state;
     643    private final Cleanable cleanable;
     644    private final String key;
    656645    private boolean closed;
    657646
     
    659648    {
    660649      super(out);
    661       this.lock = lock;
    662       this.key = lock.getKey();
    663       this.calledFrom = new Throwable();
     650      state = new State(out, lock, false);
     651      state.calledFrom = new Throwable();
     652      cleanable = cleaner.register(this, state);
     653      key = lock.getKey();
    664654    }
    665655
     
    670660      if (closed) return;
    671661      log.debug("Releasing write lock on: " + key);
    672       lock.writeLock().unlock();
     662      state.calledFrom = null; // Set to null to not get a warning in the log file
     663      cleanable.clean();
    673664      closed = true;
    674       lock = null;
    675       key = null;
    676       super.close();
    677     }
    678    
    679     @SuppressWarnings("deprecation")
    680     @Override
    681     protected void finalize()
    682       throws Throwable
    683     {
    684       if (!closed)
    685       {
    686         log.warn("Found unreleased write lock on: " + key, calledFrom);
    687         close();
    688       }
    689       super.finalize();
    690665    }
    691666 
     
    712687  {
    713688 
    714     private final Throwable calledFrom;
    715     private ReadWriteLock lock;
    716     private String key;
     689    private final State state;
     690    private final Cleanable cleanable;
     691    private final String key;
    717692    private boolean closed;
    718693 
     
    720695    {
    721696      super(in);
    722       this.lock = lock;
    723       this.key = lock.getKey();
    724       this.calledFrom = new Throwable();
     697      state = new State(in, lock, true);
     698      state.calledFrom = new Throwable();
     699      cleanable = cleaner.register(this, state);
     700      key = lock.getKey();
    725701    }
    726702 
     
    731707      if (closed) return;
    732708      log.debug("Releasing read lock on: " + key);
    733       lock.readLock().unlock();
     709      state.calledFrom = null; // Set to null to not get a warning in the log file
     710      cleanable.clean();
    734711      closed = true;
    735       lock = null;
    736       key = null;
    737       super.close();
    738     }
    739    
    740     @SuppressWarnings("deprecation")
    741     @Override
    742     protected void finalize()
    743       throws Throwable
    744     {
    745       if (!closed)
    746       {
    747         log.warn("Found unreleased read lock on: " + key, calledFrom);
    748         close();
    749       }
    750       super.finalize();
    751712    }
    752713   
     
    802763 
    803764  /**
     765    Inner class for performing the actual release of locks and closing of streams (in/out).
     766  */
     767  static class State
     768    implements Runnable
     769  {
     770   
     771    final Closeable stream;
     772    final LockEntry lock;
     773    final boolean readLock;
     774    Throwable calledFrom;
     775   
     776    State(Closeable stream, LockEntry lock, boolean readLock)
     777    {
     778      this.stream = stream;
     779      this.lock = lock;
     780      this.readLock = readLock;
     781    }
     782   
     783    @Override
     784    public void run()
     785    {
     786      if (calledFrom != null)
     787      {
     788        log.warn("Found unreleased " + (readLock ? "read" : "write") + " lock on: " + lock.getKey(), calledFrom);
     789      }
     790      if (stream != null) FileUtil.close(stream);
     791      if (readLock)
     792      {
     793        lock.readLock().unlock();
     794      }
     795      else
     796      {
     797        lock.writeLock().unlock();
     798      }
     799    }
     800  }
     801
     802 
     803  /**
    804804    A timer task that clean up the cache when it is executed.
    805805  */
Note: See TracChangeset for help on using the changeset viewer.