1 28 package net.sf.jasperreports.engine.fill; 29 30 import java.io.Serializable ; 31 import java.util.HashMap ; 32 import java.util.Map ; 33 34 import org.apache.commons.collections.ReferenceMap; 35 import org.apache.commons.logging.Log; 36 import org.apache.commons.logging.LogFactory; 37 38 import net.sf.jasperreports.engine.JRConstants; 39 import net.sf.jasperreports.engine.JRPrintImage; 40 import net.sf.jasperreports.engine.JRRenderable; 41 import net.sf.jasperreports.engine.JasperPrint; 42 43 49 public class JRVirtualizationContext implements Serializable 50 { 51 private static final long serialVersionUID = JRConstants.SERIAL_VERSION_UID; 52 53 private static final Log log = LogFactory.getLog(JRVirtualizationContext.class); 54 55 private static final ReferenceMap contexts = new ReferenceMap(ReferenceMap.WEAK, ReferenceMap.WEAK); 56 57 private Map cachedRenderers; 58 private Map cachedTemplates; 59 60 private boolean readOnly; 61 62 65 public JRVirtualizationContext() 66 { 67 cachedRenderers = new HashMap (); 68 cachedTemplates = new HashMap (); 69 } 70 71 72 77 public void cacheRenderer(JRPrintImage image) 78 { 79 JRRenderable renderer = image.getRenderer(); 80 if (renderer != null) 81 { 82 cachedRenderers.put(renderer.getId(), renderer); 83 } 84 } 85 86 87 93 public JRRenderable getCachedRenderer(String id) 94 { 95 return (JRRenderable) cachedRenderers.get(id); 96 } 97 98 99 105 public boolean hasCachedRenderer(String id) 106 { 107 return cachedRenderers.containsKey(id); 108 } 109 110 111 117 public boolean hasCachedTemplate(String id) 118 { 119 return cachedTemplates.containsKey(id); 120 } 121 122 123 128 public void cacheTemplate(JRTemplateElement template) 129 { 130 Object old = cachedTemplates.put(template.getId(), template); 131 if (old == null && log.isDebugEnabled()) 132 { 133 log.debug("Cached template " + template + " having id " + template.getId()); 134 } 135 } 136 137 138 144 public JRTemplateElement getCachedTemplate(String templateId) 145 { 146 return (JRTemplateElement) cachedTemplates.get(templateId); 147 } 148 149 150 156 public boolean isReadOnly() 157 { 158 return readOnly; 159 } 160 161 162 171 public void setReadOnly(boolean readOnly) 172 { 173 this.readOnly = readOnly; 174 } 175 176 177 183 public static void register(JRVirtualizationContext context, JasperPrint print) 184 { 185 synchronized (contexts) 186 { 187 contexts.put(print, context); 188 } 189 } 190 191 192 203 public static JRVirtualizationContext getRegistered(JasperPrint print) 204 { 205 synchronized (contexts) 206 { 207 return (JRVirtualizationContext) contexts.get(print); 208 } 209 } 210 } 211 | Popular Tags |