1 //****************************************************************************** 2 // 3 // File: ResourceCache.java 4 // Package: edu.rit.pj.cluster 5 // Unit: Class edu.rit.pj.cluster.ResourceCache 6 // 7 // This Java source file is copyright (C) 2006 by Alan Kaminsky. All rights 8 // reserved. For further information, contact the author, Alan Kaminsky, at 9 // ark@cs.rit.edu. 10 // 11 // This Java source file is part of the Parallel Java Library ("PJ"). PJ is free 12 // software; you can redistribute it and/or modify it under the terms of the GNU 13 // General Public License as published by the Free Software Foundation; either 14 // version 3 of the License, or (at your option) any later version. 15 // 16 // PJ is distributed in the hope that it will be useful, but WITHOUT ANY 17 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 18 // A PARTICULAR PURPOSE. See the GNU General Public License for more details. 19 // 20 // Linking this library statically or dynamically with other modules is making a 21 // combined work based on this library. Thus, the terms and conditions of the GNU 22 // General Public License cover the whole combination. 23 // 24 // As a special exception, the copyright holders of this library give you 25 // permission to link this library with independent modules to produce an 26 // executable, regardless of the license terms of these independent modules, and 27 // to copy and distribute the resulting executable under terms of your choice, 28 // provided that you also meet, for each linked independent module, the terms 29 // and conditions of the license of that module. An independent module is a module 30 // which is not derived from or based on this library. If you modify this library, 31 // you may extend this exception to your version of the library, but you are not 32 // obligated to do so. If you do not wish to do so, delete this exception 33 // statement from your version. 34 // 35 // A copy of the GNU General Public License is provided in the file gpl.txt. You 36 // may also obtain a copy of the GNU General Public License on the World Wide 37 // Web at http://www.gnu.org/licenses/gpl.html. 38 // 39 //****************************************************************************** 40 package edu.rit.pj.cluster; 41 42 import java.util.HashMap; 43 import java.util.Map; 44 45 /** 46 * Class ResourceCache provides a cache of resources, indexed by resource name. 47 * A resource is a piece of content (sequence of bytes) obtained from a class 48 * loader. 49 * <P> 50 * <I>Note:</I> Class ResourceCache is multiple thread safe. 51 * 52 * @author Alan Kaminsky 53 * @version 26-Oct-2006 54 */ 55 public class ResourceCache { 56 57 // Hidden data members. 58 private Map<String, ResourceInfo> myMap 59 = new HashMap<String, ResourceInfo>(); 60 61 // Hidden helper classes. 62 private static class ResourceInfo { 63 64 public byte[] content; 65 66 public ResourceInfo(byte[] content) { 67 this.content = content; 68 } 69 } 70 71 // Exported constructors. 72 /** 73 * Construct a new resource cache. 74 */ 75 public ResourceCache() { 76 } 77 78 // Exported operations. 79 /** 80 * Determine if this resource cache contains resource information for the 81 * given resource name. If the answer is yes, the resource content may or 82 * may not have been found. 83 * 84 * @param name Resource name. 85 * @return True if this resource cache contains resource information for 86 * <code>name</code>, false otherwise. 87 */ 88 public synchronized boolean contains(String name) { 89 ResourceInfo info = myMap.get(name); 90 return info != null; 91 } 92 93 /** 94 * Determine if this resource cache contains the resource content for the 95 * given resource name. If the answer is yes, the resource content was 96 * found, otherwise the resource content was not found or no resource 97 * information is available. 98 * 99 * @param name Resource name. 100 * @return True if this resource cache contains the resource content for 101 * <code>name</code>, false otherwise. 102 */ 103 public synchronized boolean containsContent(String name) { 104 ResourceInfo info = myMap.get(name); 105 return info == null ? false : info.content != null; 106 } 107 108 /** 109 * Obtain the resource content for the given resource name from this 110 * resource cache (blocking). This method will block if necessary until this 111 * resource cache contains the content for <code>name</code> or until this 112 * resource cache knows the content was not found. 113 * 114 * @param name Resource name. 115 * @return Resource content, or null if not found. 116 * @exception InterruptedException Thrown if the calling thread is 117 * interrupted while blocked in this method. 118 * @throws java.lang.InterruptedException if any. 119 */ 120 public synchronized byte[] get(String name) 121 throws InterruptedException { 122 ResourceInfo info = myMap.get(name); 123 while (info == null) { 124 wait(); 125 info = myMap.get(name); 126 } 127 return info.content; 128 } 129 130 /** 131 * Obtain the resource content for the given resource name from this 132 * resource cache (non-blocking). This method will return null if the 133 * resource content was not found or no resource information is available. 134 * 135 * @param name Resource name. 136 * @return Resource content, or null if not found or no information is 137 * available. 138 */ 139 public synchronized byte[] getNoWait(String name) { 140 ResourceInfo info = myMap.get(name); 141 return info == null ? null : info.content; 142 } 143 144 /** 145 * Store the given resource content under the given resource name in this 146 * resource cache. Any existing content for <code>name</code> is overwritten. 147 * <P> 148 * <I>Note:</I> The resource cache assumes that the contents of 149 * <code>content</code> are not changed after <code>put()</code> is called. 150 * 151 * @param name Resource name. 152 * @param content Resource content, or null if not found. 153 */ 154 public synchronized void put(String name, 155 byte[] content) { 156 myMap.put(name, new ResourceInfo(content)); 157 notifyAll(); 158 } 159 160 /** 161 * Remove the resource content for the given resource name from this 162 * resource cache. If there is no content for <code>name</code>, the 163 * <code>remove()</code> method does nothing. 164 * 165 * @param name Resource name. 166 */ 167 public synchronized void remove(String name) { 168 myMap.remove(name); 169 } 170 171 }