1 21 package com.jaspersoft.jasperserver.api.metadata.common.service.impl.hibernate; 22 23 import java.io.ByteArrayInputStream ; 24 import java.io.InputStream ; 25 import java.util.List ; 26 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 import org.hibernate.criterion.DetachedCriteria; 30 import org.hibernate.criterion.Restrictions; 31 32 import com.jaspersoft.jasperserver.api.common.domain.ExecutionContext; 33 import com.jaspersoft.jasperserver.api.metadata.common.domain.FileResource; 34 import com.jaspersoft.jasperserver.api.metadata.common.service.RepositoryCache; 35 import com.jaspersoft.jasperserver.api.metadata.common.service.RepositoryCacheableItem; 36 import com.jaspersoft.jasperserver.api.metadata.common.service.RepositoryService; 37 import com.jaspersoft.jasperserver.api.metadata.common.service.impl.HibernateDaoImpl; 38 import com.jaspersoft.jasperserver.api.metadata.common.service.impl.hibernate.persistent.CachedItem; 39 40 44 public class HibernateRepositoryCache extends HibernateDaoImpl implements RepositoryCache { 45 private static final Log log = LogFactory.getLog(HibernateRepositoryCache.class); 46 47 private RepositoryService repository; 48 49 public RepositoryService getRepository() { 50 return repository; 51 } 52 53 public void setRepository(RepositoryService repository) { 54 this.repository = repository; 55 } 56 57 public InputStream cache(ExecutionContext context, FileResource resource, RepositoryCacheableItem cacheableItem) { 58 CachedItem cachedItem = getCachedItem(context, resource, cacheableItem); 59 while(cachedItem.isReference()) { 60 cachedItem = cachedItem.getReference(); 61 } 62 return new ByteArrayInputStream (cachedItem.getDataBytes()); 63 } 64 65 public InputStream cache(ExecutionContext context, String uri, RepositoryCacheableItem cacheableItem) { 66 FileResource resource = (FileResource) repository.getResource(context, uri); 67 return cache(context, resource, cacheableItem); 68 } 69 70 protected CachedItem getCachedItem(ExecutionContext context, FileResource resource, RepositoryCacheableItem cacheableItem) { 71 if (log.isDebugEnabled()) { 72 log.debug("Looking in repository cache \"" + cacheableItem.getCacheName() + "\" for resource \"" + resource.getURIString() + 73 "\", version " + resource.getVersion() + "\", version date " + resource.getCreationDate()); 74 } 75 CachedItem cachedItem = getCachedItem(resource.getURIString(), cacheableItem); 76 if (cachedItem == null || 77 cachedItem.getVersion() < resource.getVersion() || 78 cachedItem.getVersionDate() == null || 79 cachedItem.getVersionDate().before(resource.getCreationDate())) { 80 if (resource.isReference()) { 81 cachedItem = saveRefence(context, resource, cachedItem, cacheableItem); 82 } else { 83 cachedItem = saveData(context, resource, cachedItem, cacheableItem); 84 } 85 } 86 return cachedItem; 87 } 88 89 protected CachedItem getCachedItem(String uri, RepositoryCacheableItem cacheableItem) { 90 DetachedCriteria criteria = DetachedCriteria.forClass(CachedItem.class); 91 criteria.add(Restrictions.naturalId().set("cacheName", cacheableItem.getCacheName()).set("uri", uri)); 92 List list = getHibernateTemplate().findByCriteria(criteria); 93 CachedItem item; 94 if (list.isEmpty()) { 95 item = null; 96 } else { 97 item = (CachedItem) list.get(0); 98 } 99 return item; 100 } 101 102 protected CachedItem saveRefence(ExecutionContext context, FileResource resource, CachedItem item, RepositoryCacheableItem cacheableItem) { 103 FileResource ref = (FileResource) repository.getResource(context, resource.getReferenceURI()); 104 CachedItem refItem = getCachedItem(context, ref, cacheableItem); 105 106 CachedItem saveItem; 107 if (item == null) { 108 saveItem = new CachedItem(); 109 } else { 110 saveItem = item; 111 } 112 saveItem.setCacheName(cacheableItem.getCacheName()); 113 saveItem.setData(null); 114 saveItem.setReference(refItem); 115 saveItem.setUri(resource.getURIString()); 116 saveItem.setVersion(resource.getVersion()); 117 saveItem.setVersionDate(resource.getCreationDate()); 118 119 if (item == null) { 120 getHibernateTemplate().save(saveItem); 121 } else { 122 getHibernateTemplate().update(saveItem); 123 } 124 125 return saveItem; 126 } 127 128 protected CachedItem saveData(ExecutionContext context, FileResource resource, CachedItem item, RepositoryCacheableItem cacheableItem) { 129 if (log.isDebugEnabled()) { 130 log.debug("Saving repository cache \"" + cacheableItem.getCacheName() + "\" for resource \"" + resource.getURIString() + 131 "\", version " + resource.getVersion() + "\", version date " + resource.getCreationDate()); 132 } 133 134 byte[] data = cacheableItem.getData(context, resource); 135 136 CachedItem saveItem; 137 if (item == null) { 138 saveItem = new CachedItem(); 139 } else { 140 saveItem = item; 141 } 142 saveItem.setCacheName(cacheableItem.getCacheName()); 143 saveItem.setDataBytes(data); 144 saveItem.setReference(null); 145 saveItem.setUri(resource.getURIString()); 146 saveItem.setVersion(resource.getVersion()); 147 saveItem.setVersionDate(resource.getCreationDate()); 148 149 if (item == null) { 150 getHibernateTemplate().save(saveItem); 151 } else { 152 getHibernateTemplate().update(saveItem); 153 } 154 155 return saveItem; 156 } 157 158 public void clearCache(final String uri, final RepositoryCacheableItem cacheableItem) { 159 executeWriteCallback(new DaoCallback() { 160 public Object execute() { 161 removeCached(uri, cacheableItem); 162 return null; 163 } 164 }, false); 165 } 166 167 protected void removeCached(String uri, RepositoryCacheableItem cacheableItem) { 168 if (log.isDebugEnabled()) { 169 log.debug("Clearing cache " + cacheableItem.getCacheName() + " for resource " + uri); 170 } 171 172 CachedItem cachedItem = getCachedItem(uri, cacheableItem); 173 if (cachedItem != null) { 174 getHibernateTemplate().delete(cachedItem); 175 } 176 } 177 } 178 | Popular Tags |