1 21 package com.jaspersoft.jasperserver.api.engine.jasperreports.util; 22 23 import java.util.Collections ; 24 import java.util.HashMap ; 25 import java.util.Iterator ; 26 import java.util.Map ; 27 28 import com.jaspersoft.jasperserver.api.common.domain.ExecutionContext; 29 import com.jaspersoft.jasperserver.api.metadata.common.domain.FileResource; 30 import com.jaspersoft.jasperserver.api.metadata.common.service.RepositoryService; 31 32 36 public class RepositoryCacheMap { 37 38 public static interface ObjectCache { 39 boolean isValid(Object o); 40 Object create(ExecutionContext context, FileResource res); 41 void release(Object o); 42 } 43 44 protected static class VersionObject { 45 public final Object o; 46 public final String referenceURI; 47 public final int version; 48 49 public VersionObject(Object o, int version) { 50 this.o = o; 51 this.referenceURI = null; 52 this.version = version; 53 } 54 55 public VersionObject(String referenceURI, int version) { 56 this.o = null; 57 this.referenceURI = referenceURI; 58 this.version = version; 59 } 60 61 public boolean isReference() { 62 return referenceURI != null; 63 } 64 } 65 66 public static class CacheObject { 67 private final Object o; 68 private final boolean cached; 69 70 public CacheObject(Object o, boolean cached) { 71 this.o = o; 72 this.cached = cached; 73 } 74 75 public Object getObject() { 76 return o; 77 } 78 79 public boolean isCached() { 80 return cached; 81 } 82 } 83 84 private final RepositoryService repository; 85 private final ObjectCache objectCache; 86 private final Map map; 87 88 public RepositoryCacheMap(RepositoryService repository, ObjectCache objectCache) { 89 this.repository = repository; 90 this.objectCache = objectCache; 91 map = Collections.synchronizedMap(new HashMap ()); 92 } 93 94 public CacheObject cache(ExecutionContext context, FileResource res, boolean cacheFirst) { 95 Object ret; 96 97 VersionObject val = null; 98 if (cacheFirst) { 99 val = (VersionObject) map.get(res.getURIString()); 100 } 101 102 boolean cached = true; 103 if (val != null && val.version >= res.getVersion() && (val.isReference() || objectCache.isValid(val.o))) { 104 ret = val.o; 105 } else { 106 if (res.isReference()) { 107 FileResource ref = (FileResource) repository.getResource(context, res.getReferenceURI()); 108 CacheObject refCache = cache(context, ref, true); 109 ret = refCache.getObject(); 110 if (cacheFirst) { 111 VersionObject value = new VersionObject(res.getReferenceURI(), res.getVersion()); 112 put(res, value); 113 } 114 } else { 115 ret = objectCache.create(context, res); 116 if (cacheFirst) { 117 VersionObject value = new VersionObject(ret, res.getVersion()); 118 put(res, value); 119 } else { 120 cached = false; 121 } 122 } 123 } 124 125 return new CacheObject(ret, cached); 126 } 127 128 protected void put(FileResource res, VersionObject value) { 129 VersionObject old = (VersionObject) map.put(res.getURIString(), value); 130 if (old != null && !old.isReference()) { 131 objectCache.release(old.o); 132 } 133 } 134 135 public void release() { 136 for (Iterator it = map.values().iterator(); it.hasNext();) { 137 VersionObject val = (VersionObject) it.next(); 138 if (!val.isReference()) { 139 objectCache.release(val.o); 140 } 141 } 142 } 143 144 protected void finalize() throws Throwable { 145 release(); 146 } 147 } 148 | Popular Tags |