2 * Created on 13.04.2003
4 * To change the template for this generated file go to
5 * Window>Preferences>Java>Code Generation>Code and Comments
7 package de.laures.cewolf.taglib;
9 import java.io.Serializable;
10 import java.util.Date;
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.jfree.data.general.Dataset;
17 import de.laures.cewolf.DatasetProduceException;
18 import de.laures.cewolf.DatasetProducer;
19 import de.laures.cewolf.taglib.util.DatasetProductionTimeStore;
20 import de.laures.cewolf.taglib.util.KeyGenerator;
21 import de.laures.cewolf.util.Assert;
26 * To change the template for this generated type comment go to
27 * Window>Preferences>Java>Code Generation>Code and Comments
29 public class DataContainer implements DataAware, Serializable {
31 private static transient final Log log = LogFactory.getLog(ChartImageDefinition.class);
33 private transient Dataset data;
34 private transient DatasetProducer producer;
36 private Map datasetProductionParams;
37 private long datasetProduceTime;
38 private boolean useCache = true;
40 public void setDataProductionConfig(DatasetProducer dsp, Map params, boolean useCache) {
41 log.debug("setDataProductionConfig");
43 datasetProductionParams = params;
44 this.useCache = useCache;
45 checkDataProductionNeed();
48 public Object getDataset() throws DatasetProduceException {
49 Assert.check(producer != null, "you need to specifiy a producer for the data of the chart.");
50 log.debug("getting data..");
52 log.debug("producing new dataset for " + producer.getProducerId());
53 data = (Dataset) producer.produceDataset(datasetProductionParams);
54 DatasetProductionTimeStore dataCache = DatasetProductionTimeStore.getInstance();
55 dataCache.addEntry(producer.getProducerId(), datasetProductionParams, new Date(datasetProduceTime));
57 Assert.check(data != null, "your producer of type " + producer.getClass().getName() + " produced a null dataset.");
62 * This method checks if there has been a dataset production
63 * for the same DatasetProvider and parameters. If so the DatasetProducer
64 * is consulted to check the expiry of this data.
65 * If the data has expired the retrieval of a cached image of this
66 * ChartDefinition is avoided by setting the datasetProduceTime to the
67 * actual time. After this the hash code of this object can not be
68 * present in the image cache and so a new image with new data will
70 * If the data did not expire the last dataset production time is stored
71 * as a memeber to reach the same hash code for this object as the one
73 * This method is called during serialization to ensure the same serialized
74 * representation of this and a eventually formally stored object.
76 private void checkDataProductionNeed() {
77 log.debug("checking data actuality..." + producer + ", " + datasetProductionParams);
78 final String prodId = producer.getProducerId();
79 log.debug(prodId + ", " + KeyGenerator.generateKey((Serializable)datasetProductionParams));
80 log.debug("useCache = " + useCache);
81 DatasetProductionTimeStore dataCache = DatasetProductionTimeStore.getInstance();
82 if (useCache && dataCache.containsEntry(prodId, datasetProductionParams)) {
83 Date produceTime = dataCache.getProductionTime(prodId, datasetProductionParams);
84 log.debug("cached data available.");
85 // cached data available
86 if (!producer.hasExpired(datasetProductionParams, produceTime)) {
87 log.debug("cached data is still valid.");
88 this.datasetProduceTime = produceTime.getTime();
91 dataCache.removeEntry(prodId, datasetProductionParams);
93 datasetProduceTime = System.currentTimeMillis();