001 package com.croftsoft.core.util.cache;
002
003 import java.io.*;
004 import java.lang.ref.*;
005 import java.util.*;
006
007 import com.croftsoft.core.util.id.Id;
008
009 /*********************************************************************
010 * A Cache implementation that dumps its content when memory is low.
011 *
012 * <P>
013 *
014 * Backed by a WeakCache. Note that the content will not be dumped if
015 * its Id remains strongly reachable.
016 *
017 * <P>
018 *
019 * @see
020 * WeakCache
021 * @see
022 * java.lang.ref.SoftReference
023 *
024 * @version
025 * 1999-04-20
026 * @author
027 * <a href="https://www.croftsoft.com/">David Wallace Croft</a>
028 *********************************************************************/
029
030 public class SoftCache implements Cache
031 //////////////////////////////////////////////////////////////////////
032 //////////////////////////////////////////////////////////////////////
033 {
034
035 private WeakCache weakCache = new WeakCache ( );
036 private ReferenceQueue referenceQueue = new ReferenceQueue ( );
037 private Set softSet = new HashSet ( );
038
039 //////////////////////////////////////////////////////////////////////
040 //////////////////////////////////////////////////////////////////////
041
042 public Id validate ( Id id, ContentAccessor contentAccessor )
043 throws IOException
044 //////////////////////////////////////////////////////////////////////
045 {
046 clearQueue ( );
047
048 if ( isAvailable ( id ) ) return id;
049
050 InputStream inputStream = contentAccessor.getInputStream ( );
051
052 if ( inputStream == null ) return null;
053
054 return store ( inputStream );
055 }
056
057 /*
058 public Id store ( InputStream in, Id id ) throws IOException
059 //////////////////////////////////////////////////////////////////////
060 {
061 clearQueue ( );
062
063 id = weakCache.store ( in, id );
064
065 softSet.add ( new SoftReference ( id, referenceQueue ) );
066
067 return id;
068 }
069 */
070
071 public Id store ( InputStream in ) throws IOException
072 //////////////////////////////////////////////////////////////////////
073 {
074 clearQueue ( );
075
076 Id id = weakCache.store ( in );
077
078 softSet.add ( new SoftReference ( id, referenceQueue ) );
079
080 return id;
081 }
082
083 public InputStream retrieve ( Id id ) throws IOException
084 //////////////////////////////////////////////////////////////////////
085 {
086 clearQueue ( );
087
088 return weakCache.retrieve ( id );
089 }
090
091 public boolean isAvailable ( Id id )
092 //////////////////////////////////////////////////////////////////////
093 {
094 clearQueue ( );
095
096 return weakCache.isAvailable ( id );
097 }
098
099 //////////////////////////////////////////////////////////////////////
100 //////////////////////////////////////////////////////////////////////
101
102 public void clearQueue ( )
103 //////////////////////////////////////////////////////////////////////
104 {
105 Reference reference = null;
106 while ( ( reference = referenceQueue.poll ( ) ) != null )
107 {
108 softSet.remove ( reference );
109 }
110 }
111
112 //////////////////////////////////////////////////////////////////////
113 //////////////////////////////////////////////////////////////////////
114 }