1 19 package javax.util.jcache; 20 21 import java.io.File ; 22 import java.io.OutputStream ; 23 import java.io.Serializable ; 24 import org.fjank.jcache.CacheImpl; 25 import org.fjank.jcache.CacheObject; 26 import org.fjank.jcache.CacheRegion; 27 import org.fjank.jcache.StreamCacheObject; 28 29 30 37 public abstract class CacheLoader implements Serializable { 38 54 public abstract Object load(final Object handle, final Object arguments) 55 throws CacheException; 56 57 67 public final void setAttributes(final Object handle, 68 final Attributes attributes) throws CacheException { 69 CacheObject cacheObj = convertHandle(handle); 70 if (attributes == null) { 71 cacheObj.setAttributes(CacheAccessFactory.getInstance().getDefaultAttributes()); 72 } else { 73 cacheObj.setAttributes(attributes); 74 } 75 } 76 77 87 private CacheObject convertHandle(final Object handle) 88 throws InvalidArgumentException { 89 if (handle == null) { 90 throw new InvalidArgumentException( 91 "The handle passed in to this method was null."); 92 } 93 if (!(handle instanceof CacheObject)) { 94 throw new InvalidArgumentException( 95 "The handle was not a valid CacheObject."); 96 } 97 CacheObject cacheObj = ((CacheObject) handle); 98 return cacheObj; 99 } 100 101 112 protected final Object getName(final Object handle) 113 throws CacheException { 114 return convertHandle(handle).getKey(); 115 } 116 117 130 protected final String getRegion(final Object handle) 131 throws CacheException { 132 final CacheObject cacheObj = convertHandle(handle); 133 final CacheRegion region = cacheObj.getRegion(); 134 if (region == null) { 135 throw new NullObjectException("The object " + cacheObj 136 + " is not attached to a region."); 137 } 138 final Object name = region.getName(); 139 if (name != null) { 140 return name.toString(); 141 } else { 142 return null; 143 } 144 } 145 146 162 protected final Object netSearch(final Object handle, final int timeout) 163 throws CacheException { 164 throw new NotImplementedException(); 165 } 166 167 184 public final OutputStream createStream(final Object handle) 185 throws ObjectExistsException, InvalidArgumentException { 186 CacheObject co = convertHandle(handle); 187 try { 188 StreamCacheObject str = 189 new StreamCacheObject(co.getKey(), null, co.getGroup(), 190 co.getRegion(), CacheImpl.getCache(true).getReferenceQueue()); 191 return str.getOutputStream(); 192 } catch (CacheNotAvailableException e) { 193 throw new InvalidArgumentException("The cache is not available."); 194 } catch (CacheException e) { 195 throw new InvalidArgumentException( 196 "The cache is not available, as an error occured." 197 + e.getMessage()); 198 } 199 } 200 201 219 public final OutputStream createStream(final Object handle, 220 final Attributes attributes) throws CacheException { 221 setAttributes(handle, attributes); 222 return createStream(handle); 223 } 224 225 244 public final File createDiskObject(final Object handle, 245 final String extension) throws CacheException { 246 CacheObject cacheObj = convertHandle(handle); 247 CacheAttributes att = getCacheAttributes(); 248 String rootPath = att.getDiskPath(); 249 String ext; 250 if (extension == null) { 251 ext = ""; 252 } else { 253 ext = '.' + extension; 254 } 255 return new File (rootPath + File.separatorChar + cacheObj.getKey() + ext); 256 } 257 258 266 private CacheAttributes getCacheAttributes() 267 throws CacheNotAvailableException, CacheException { 268 CacheAccessFactory fact = CacheAccessFactory.getInstance(); 269 Cache cache = fact.getCache(); 270 return cache.getAttributes(); 271 } 272 273 294 public final File createDiskObject(final Object handle, 295 final Attributes attributes, final String extension) 296 throws CacheException { 297 setAttributes(handle, attributes); 298 return createDiskObject(handle, extension); 299 } 300 301 310 public final void log(final String msg) { 311 try { 312 CacheAttributes cacheAttributes = getCacheAttributes(); 313 CacheLogger logger = cacheAttributes.getLogger(); 314 logger.log(msg); 315 } catch (CacheNotAvailableException e) { 316 } catch (CacheException e) { 318 } 320 } 321 322 338 public final CacheException exceptionHandler(final String msg, 339 final Exception exception) { 340 return new CacheException(msg, exception); 341 } 342 } 343 | Popular Tags |