This package contains the classes that can be used to support a hot cache, as presented in Chapter 14. E.3.1 com.mediamania.hotcache.AbstractCache1 package com.mediamania.hotcache; 2 3 import java.util.Map; 4 import java.util.HashMap; 5 6 import com.mediamania.prototype.PrototypeQueries; 7 import com.mediamania.MediaManiaApp; 8 import com.mediamania.prototype.Movie; 9 10 public abstract class AbstractCache extends MediaManiaApp 11 implements com.mediamania.hotcache.CacheAccess { 12 13 protected Map cache = new HashMap( ); 14 15 /** Creates a new instance of AbstractCache. The AbstractCache is the 16 * base class for MasterCache and SlaveCache. 17 */ 18 protected AbstractCache( ) { 19 } 20 21 /** Get the Movie by title. If the movie is not in the cache, put it in. 22 * @param title the title of the movie 23 * @return the movie instance 24 */ 25 public Movie getMovieByTitle(String title) { 26 Movie movie = (Movie) cache.get(title); 27 if (movie == null) { 28 movie = PrototypeQueries.getMovie (pm, title); 29 if (movie != null) { 30 cache.put (title, movie); 31 } 32 } 33 return movie; 34 } 35 } E.3.2 com.mediamania.hotcache.AbstractDriver1 package com.mediamania.hotcache; 2 3 import java.io.InputStream; 4 import java.io.InputStreamReader; 5 import java.io.IOException; 6 import java.io.Reader; 7 import java.io.BufferedReader; 8 9 import java.util.StringTokenizer; 10 11 import java.net.URL; 12 import java.net.MalformedURLException; 13 14 import com.mediamania.Utilities; 15 16 import com.mediamania.prototype.Movie; 17 18 public class AbstractDriver { 19 protected BufferedReader requestReader; 20 protected BufferedReader updateReader; 21 protected CacheAccess cache; 22 protected int timeoutMillis; 23 protected AbstractDriver(String updateURL, String requestURL, 24 String timeout) { 25 updateReader = openReader(updateURL); 26 requestReader = openReader(requestURL); 27 timeoutMillis = Integer.parseInt(timeout); 28 } 29 30 protected BufferedReader openReader (String urlName) { 31 try { 32 URL url = new URL(urlName); 33 InputStream is = url.openStream( ); 34 Reader r = new InputStreamReader(is); 35 return new BufferedReader(r); 36 } catch (Exception ex) { 37 return null; 38 } 39 } 40 41 protected void serviceReaders( ) { 42 boolean done = false; 43 boolean lastTime = false; 44 try { 45 while (!done) { 46 if (updateReader.ready( )) { 47 handleUpdate( ); 48 done = false; 49 lastTime = false; 50 } else if (requestReader.ready( )) { 51 handleRequest( ); 52 done = false; 53 lastTime = false; 54 } else { 55 try { 56 Thread.sleep (timeoutMillis); 57 if (lastTime) done = true; 58 lastTime = true; 59 } catch (InterruptedException ex) { 60 done = true; 61 } 62 } 63 } 64 } catch (Exception ex) { 65 return; 66 } 67 } 68 69 protected void handleRequest( ) throws IOException { 70 String request = requestReader.readLine( ); 71 Movie movie = cache.getMovieByTitle(request); 72 System.out.println("Movie: " + movie.getTitle( )); 73 } 74 75 protected void handleUpdate( ) throws IOException { 76 String update = updateReader.readLine( ); 77 StringTokenizer tokenizer = new StringTokenizer(update, ";"); 78 String movieName = tokenizer.nextToken( ); 79 String webSite = tokenizer.nextToken( ); 80 cache.updateWebSite (movieName, webSite); 81 } 82 } E.3.3 com.mediamania.hotcache.CacheAccess1 package com.mediamania.hotcache; 2 3 import com.mediamania.prototype.Movie; 4 5 /** Manage a cache of persistent Movie instances. 6 */ 7 public interface CacheAccess { 8 9 /** Get the Movie by title. 10 * @param title the title of the movie 11 * @return the movie instance 12 */ 13 Movie getMovieByTitle (String title); 14 15 /** Update the Movie website. 16 * @param title the title of the movie 17 * @param website the new website for the movie 18 */ 19 void updateWebSite (String title, String website); 20 } E.3.4 com.mediamania.hotcache.MasterCache1 package com.mediamania.hotcache; 2 3 import java.util.Map; 4 import java.util.HashMap; 5 6 import com.mediamania.prototype.PrototypeQueries; 7 import com.mediamania.prototype.Movie; 8 9 public class MasterCache extends AbstractCache 10 implements com.mediamania.hotcache.CacheAccess { 11 12 /** Creates a new instance of MasterCache. The MasterCache performs 13 * updates of the database and manages a cache of Movie. 14 */ 15 public MasterCache( ) { 16 } 17 18 /** Update the Movie website. 19 * @param title the title of the movie 20 * @param website the new website for the movie 21 */ 22 public void updateWebSite(String title, String website) { 23 Movie movie = getMovieByTitle (title); 24 if (movie != null) { 25 tx.begin( ); 26 movie.setWebSite (website); 27 tx.commit( ); 28 } 29 } 30 31 public void execute( ) { 32 } 33 34 protected static Map getPropertyOverrides( ) 35 { 36 Map overrides = new HashMap( ); 37 overrides.put ("javax.jdo.options.NontransactionalRead", "true"); 38 overrides.put ("javax.jdo.options.RetainValues", "true"); 39 return overrides; 40 } 41 } E.3.5 com.mediamania.hotcache.MasterDriver1 package com.mediamania.hotcache; 2 3 public class MasterDriver extends AbstractDriver { 4 protected MasterDriver(String updateURL, String requestURL, 5 String timeout) { 6 super(updateURL, requestURL, timeout); 7 cache = new MasterCache( ); 8 } 9 10 public static void main(String[] args) { 11 MasterDriver master = new MasterDriver( 12 args[0], args[1], args[2]); 13 master.serviceReaders( ); 14 } 15 } E.3.6 com.mediamania.hotcache.SlaveCache1 package com.mediamania.hotcache; 2 3 import java.util.Map; 4 import java.util.HashMap; 5 6 import com.mediamania.prototype.Movie; 7 8 public class SlaveCache extends AbstractCache 9 implements com.mediamania.hotcache.CacheAccess { 10 11 /** Creates a new instance of SlaveCache. The SlaveCache performs 12 * lookups of the database and manages a cache of Movie. 13 */ 14 public SlaveCache( ) { 15 } 16 17 /** Update the Movie website in the cache, only if it is already there. 18 * The datastore will be updated by the MasterCache. 19 * @param title the title of the movie 20 * @param website the new website for the movie 21 */ 22 public void updateWebSite(String title, String website) { 23 Movie movie = (Movie)cache.get (title); 24 if (movie == null) 25 return; 26 movie.setWebSite (website); 27 } 28 29 public void execute( ) { 30 } 31 32 protected static Map getPropertyOverrides( ) 33 { 34 Map overrides = new HashMap( ); 35 overrides.put ("javax.jdo.options.NontransactionalRead", "true"); 36 overrides.put ("javax.jdo.options.NontransactionalWrite", "true"); 37 return overrides; 38 } 39 } E.3.7 com.mediamania.hotcache.SlaveDriver1 package com.mediamania.hotcache; 2 3 public class SlaveDriver extends AbstractDriver { 4 protected SlaveDriver(String updateURL, String requestURL, 5 String timeout) { 6 super(updateURL, requestURL, timeout); 7 cache = new SlaveCache( ); 8 } 9 10 public static void main(String[] args) { 11 SlaveDriver slave = new SlaveDriver( 12 args[0], args[1], args[2]); 13 slave.serviceReaders( ); 14 } 15 } |