1 /* Open Source Java Caching Service 2 * Copyright (C) 2002 Frank Karlstrøm 3 * This library is free software; you can redistribute it and/or 4 * modify it under the terms of the GNU Lesser General Public 5 * License as published by the Free Software Foundation; either 6 * version 2.1 of the License, or (at your option) any later version. 7 * 8 * This library is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 * Lesser General Public License for more details. 12 * 13 * You should have received a copy of the GNU Lesser General Public 14 * License along with this library; if not, write to the Free Software 15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 16 * 17 * The author can be contacted by email: fjankk@users.sourceforge.net 18 */ 19 package javax.util.jcache; 20 21 import java.net.InetAddress; 22 import java.util.Enumeration; 23 import java.util.Vector; 24 import org.fjank.jcache.CacheImpl; 25 import org.fjank.jcache.DefaultCacheLogger; 26 27 28 /** 29 * An administrative interface to the cache. 30 * Invalid parameters to the methods of this class will be silently ignored. 31 * @deprecated removed with no replacement. 32 * @author Frank Karlstrøm 33 */ 34 public class CacheAttributes implements Cloneable { 35 private CacheImpl cache; 36 37 public void registerCache(CacheImpl cache) { 38 this.cache=cache; 39 } 40 /**a boolean indication wether thihs object is local or distributed. 41 */ 42 private boolean local = false; 43 44 45 /** 46 *the default log-file name. 47 */ 48 private String defaultLogFileName = "jcache.log"; 49 50 /** 51 * the default clean interval. 52 */ 53 private int cleanInterval = 30; 54 55 /** 56 * the default size of the disk cache. 57 */ 58 private int diskSize = 10; 59 60 /** 61 * the default path the the disk file(s) 62 */ 63 private String diskPath = null; 64 65 /** 66 * the default maximum number of objects in the cache. 67 */ 68 private int maxObjects = 5000; 69 70 /** 71 *the default maximum size of the in-memory cache. 72 */ 73 private int maxSize = 1; 74 75 /** 76 * the default CacheLogger. 77 */ 78 private CacheLogger logger = new DefaultCacheLogger(); 79 80 /** 81 * Default constructor. 82 */ 83 private CacheAttributes() { 84 } 85 /** 86 * sets the attribute to indicate the cache is local. Invalidations and 87 * updates will not be propagated to other caches in the system. 88 */ 89 public void setLocal() { 90 local = true; 91 } 92 93 /** 94 * is used to set the attribute to determine the maximum number of objects 95 * allowed in the memory cache. If the max number of objects or the cache 96 * size is set, the default for one not set is ignored. if both are set, 97 * both are used to determine the capacity of the cache, i.e. object will 98 * be reomved from the cache if either limit is reached. 99 * 100 * @param size the maximum number of objects in the cache. 101 */ 102 public void setMaxObjects(int size) { 103 this.maxObjects = size; 104 } 105 106 /** 107 * sets the attribute to indicate the maximum size of the memory cache. If 108 * the max number of objects in the cache are set, the default for the one 109 * not set is ignored. if both are set, both are used to determine the 110 * capacity of the cache, i.e. object will be reomved from the cache if 111 * either limit is reached. 112 * 113 * @param size the maximum size of the memory cache in megabytes. 114 */ 115 public void setMemoryCacheSize(int size) { 116 this.maxSize = size; 117 } 118 119 /** 120 * sets the attribute to indicate the maximum size of the disk cache. 121 * 122 * @param size the maximum size of the disk cache in megabytes. 123 */ 124 public void setDiskCacheSize(int size) { 125 this.diskSize = size; 126 127 } 128 129 /** 130 * sets the attribute indicating the root location for the disk cache. 131 * 132 * @param path the path to the root location for the disk cache. 133 */ 134 public void setDiskPath(String path) { 135 this.diskPath = path; 136 } 137 138 /** 139 * sets the logger object wich will be used to log cache messages. 140 * 141 * @param logger the logger wich will log cache messages. 142 */ 143 public void setLogger(CacheLogger logger) { 144 this.logger = logger; 145 } 146 147 /** 148 * sets the log file name for the DefaultCacheLogger. If the default logger 149 * is being used (a new CacheLogger has not been implemented and set), all 150 * cache log messages will be written to the file "jcache.log" wich is 151 * created in the directory where the server process is started up. This 152 * method changes the location and name of the file to be used. 153 * 154 * @param pDefaultLogFileName is a full path name for the log file. 155 */ 156 public void setDefaultLogFileName(String pDefaultLogFileName) { 157 this.defaultLogFileName = pDefaultLogFileName; 158 } 159 160 /** 161 * sets the attribute indicating how often the cacha should be checked for 162 * objects invalidated by "time to live" or "idle time" attributes. 163 * 164 * @param seconds how many seconds between each check. 165 */ 166 public void setCleanInterval(int seconds) { 167 this.cleanInterval = seconds; 168 } 169 170 /** 171 * is used to specify the network address and port to be used by the cache 172 * messaging system. At least one address is required by the cache to 173 * allow discovery when a process using the cache is first brought online. 174 * If no address is specified, localhost with a default port is used. If 175 * the system of caches is across multiple nodes, it is best to have an 176 * address specified for each node to protect against unavailable nodes. 177 * 178 * The implementation of this method will do nothing, as the caches 179 * auto-discover themselves. As long as a cache is marked as distributed, 180 * it will automatically register itselves with the other cache nodes. 181 * @param address the address of the remote cache. 182 * @param port the port of the remote cache. 183 */ 184 public void addCacheAddr(InetAddress address, int port) { 185 //cacheAddresses.add(address + ":" + port); 186 } 187 188 /** 189 * returns an Enumeration of Strings representing the address for all the 190 * cache addresses configured. if no address were configured, the default 191 * value is returned. The address is in the form ipaddress:port 192 * 193 * @return an Enumeration of Strings representing the address for all the 194 * cache addresses configured. 195 */ 196 public Enumeration getCacheAddr() { 197 if (cache!=null) { 198 return cache.getDistributionEngine().getCacheAddr(); 199 } 200 return new Vector().elements(); 201 } 202 203 /** 204 * returns the cachelogger for this cache. 205 * 206 * @return the cachelogger for this cache. 207 */ 208 public CacheLogger getLogger() { 209 return logger; 210 } 211 212 /** 213 * returns an boolean indication wether this cache is distributed or not. 214 * 215 * @return an boolean indication wether this cache is distributed or not. 216 */ 217 public boolean isDistributed() { 218 return !local; 219 } 220 221 /** 222 * gets the attribute indicating the root location for the disk cache. 223 * 224 * @return the attribute indicating the root location for the disk cache. 225 */ 226 public String getDiskPath() { 227 return diskPath; 228 } 229 /** 230 * Returns a string representation of these CacheAttributes. 231 * 232 * @return a string representation of these CacheAttributes. 233 */ 234 public String toString() { 235 StringBuffer ret = new StringBuffer(128); 236 ret.append("distributed:" + !local); 237 ret.append(", clean interval:" + cleanInterval); 238 ret.append(", memory size:" + maxSize); 239 ret.append(", max number of objects:" + maxObjects); 240 ret.append(", disk size:" + diskSize); 241 ret.append(", disk path:" + diskPath); 242 ret.append(", logger:" + logger); 243 if (!local) { 244 ret.append(", cacheAddresses:" + logger); 245 } 246 return ret.toString(); 247 } 248 /** 249 * Gets the attribute to indicate the maximum size of the memory cache. 250 * 251 * @return the attribute to indicate the maximum size of the memory cache. 252 */ 253 public int getMemoryCacheSize() { 254 return maxSize; 255 } 256 257 /** 258 * Returns a copy of these CacheAttributes. 259 * 260 * @return a copy of these CacheAttributes. 261 * @todo return value is never used. 262 */ 263 264 public CacheAttributes copy() { 265 try { 266 return (CacheAttributes) this.clone(); 267 } catch (CloneNotSupportedException e) { 268 throw new IllegalStateException(this.getClass().getName() 269 + " is not Cloneable."); 270 } 271 } 272 /** Returns the cleanInterval. 273 * @return an int representing the cleanInterval. 274 */ 275 public int getCleanInterval() { 276 return cleanInterval; 277 } 278 279 public static CacheAttributes getDefaultCacheAttributes() { 280 CacheAttributes attr = new CacheAttributes(); 281 attr.getLogger().init(attr.defaultLogFileName, CacheLogger.DEFAULT); 282 return attr; 283 284 } 285 286 /**Returns the maximum number of Objects. 287 * @return Returns the maximum number of Objects. 288 */ 289 public final int getMaxObjects() { 290 return maxObjects; 291 } 292 /** 293 * Returns the maximum size for the disk cache. 294 * @return the maximum size for the disk cache. 295 */ 296 public final int getDiskSize() { 297 return diskSize; 298 } 299 }