KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jaspersoft > jasperserver > api > engine > jasperreports > util > RepositoryCacheMap


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.engine.jasperreports.util;
22
23 import java.util.Collections JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Map JavaDoc;
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 /**
33  * @author Lucian Chirita (lucianc@users.sourceforge.net)
34  * @version $Id: RepositoryCacheMap.java 3089 2006-04-14 14:13:09Z lucian $
35  */

36 public class RepositoryCacheMap {
37
38     public static interface ObjectCache {
39         boolean isValid(Object JavaDoc o);
40         Object JavaDoc create(ExecutionContext context, FileResource res);
41         void release(Object JavaDoc o);
42     }
43
44     protected static class VersionObject {
45         public final Object JavaDoc o;
46         public final String JavaDoc referenceURI;
47         public final int version;
48
49         public VersionObject(Object JavaDoc o, int version) {
50             this.o = o;
51             this.referenceURI = null;
52             this.version = version;
53         }
54
55         public VersionObject(String JavaDoc 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 JavaDoc o;
68         private final boolean cached;
69         
70         public CacheObject(Object JavaDoc o, boolean cached) {
71             this.o = o;
72             this.cached = cached;
73         }
74         
75         public Object JavaDoc 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 JavaDoc map;
87     
88     public RepositoryCacheMap(RepositoryService repository, ObjectCache objectCache) {
89         this.repository = repository;
90         this.objectCache = objectCache;
91         map = Collections.synchronizedMap(new HashMap JavaDoc());
92     }
93
94     public CacheObject cache(ExecutionContext context, FileResource res, boolean cacheFirst) {
95         Object JavaDoc 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 JavaDoc 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 JavaDoc {
145         release();
146     }
147 }
148
Popular Tags