KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jaspersoft > jasperserver > api > metadata > common > service > impl > hibernate > HibernateRepositoryCache


1 /*
2  * Copyright (C) 2006 JasperSoft http://www.jaspersoft.com
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed WITHOUT ANY WARRANTY; and without the
10  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
15  * or write to:
16  *
17  * Free Software Foundation, Inc.,
18  * 59 Temple Place - Suite 330,
19  * Boston, MA USA 02111-1307
20  */

21 package com.jaspersoft.jasperserver.api.metadata.common.service.impl.hibernate;
22
23 import java.io.ByteArrayInputStream JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.util.List JavaDoc;
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 /**
41  * @author Lucian Chirita (lucianc@users.sourceforge.net)
42  * @version $Id: HibernateRepositoryCache.java 3707 2006-06-15 09:53:44Z lucian $
43  */

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 JavaDoc 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 JavaDoc(cachedItem.getDataBytes());
63     }
64
65     public InputStream JavaDoc cache(ExecutionContext context, String JavaDoc 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 JavaDoc uri, RepositoryCacheableItem cacheableItem) {
90         DetachedCriteria criteria = DetachedCriteria.forClass(CachedItem.class);
91         criteria.add(Restrictions.naturalId().set("cacheName", cacheableItem.getCacheName()).set("uri", uri));
92         List JavaDoc 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 JavaDoc uri, final RepositoryCacheableItem cacheableItem) {
159         executeWriteCallback(new DaoCallback() {
160             public Object JavaDoc execute() {
161                 removeCached(uri, cacheableItem);
162                 return null;
163             }
164         }, false);
165     }
166
167     protected void removeCached(String JavaDoc 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