001: /*
002: * Created on 13.04.2003
003: *
004: * To change the template for this generated file go to
005: * Window>Preferences>Java>Code Generation>Code and Comments
006: */
007: package de.laures.cewolf.taglib;
008:
009: import java.io.Serializable;
010: import java.util.Date;
011: import java.util.Map;
012:
013: import org.apache.commons.logging.Log;
014: import org.apache.commons.logging.LogFactory;
015: import org.jfree.data.general.Dataset;
016:
017: import de.laures.cewolf.DatasetProduceException;
018: import de.laures.cewolf.DatasetProducer;
019: import de.laures.cewolf.taglib.util.DatasetProductionTimeStore;
020: import de.laures.cewolf.taglib.util.KeyGenerator;
021: import de.laures.cewolf.util.Assert;
022:
023: /**
024: * @author guido
025: *
026: * To change the template for this generated type comment go to
027: * Window>Preferences>Java>Code Generation>Code and Comments
028: */
029: public class DataContainer implements DataAware, Serializable {
030:
031: private static transient final Log log = LogFactory
032: .getLog(ChartImageDefinition.class);
033:
034: private transient Dataset data;
035: private transient DatasetProducer producer;
036:
037: private Map datasetProductionParams;
038: private long datasetProduceTime;
039: private boolean useCache = true;
040:
041: public void setDataProductionConfig(DatasetProducer dsp,
042: Map params, boolean useCache) {
043: log.debug("setDataProductionConfig");
044: producer = dsp;
045: datasetProductionParams = params;
046: this .useCache = useCache;
047: checkDataProductionNeed();
048: }
049:
050: public Object getDataset() throws DatasetProduceException {
051: Assert
052: .check(producer != null,
053: "you need to specifiy a producer for the data of the chart.");
054: log.debug("getting data..");
055: if (data == null) {
056: log.debug("producing new dataset for "
057: + producer.getProducerId());
058: data = (Dataset) producer
059: .produceDataset(datasetProductionParams);
060: DatasetProductionTimeStore dataCache = DatasetProductionTimeStore
061: .getInstance();
062: dataCache.addEntry(producer.getProducerId(),
063: datasetProductionParams, new Date(
064: datasetProduceTime));
065: }
066: Assert.check(data != null, "your producer of type "
067: + producer.getClass().getName()
068: + " produced a null dataset.");
069: return data;
070: }
071:
072: /**
073: * This method checks if there has been a dataset production
074: * for the same DatasetProvider and parameters. If so the DatasetProducer
075: * is consulted to check the expiry of this data.
076: * If the data has expired the retrieval of a cached image of this
077: * ChartDefinition is avoided by setting the datasetProduceTime to the
078: * actual time. After this the hash code of this object can not be
079: * present in the image cache and so a new image with new data will
080: * be rendered.
081: * If the data did not expire the last dataset production time is stored
082: * as a memeber to reach the same hash code for this object as the one
083: * before if possible.
084: * This method is called during serialization to ensure the same serialized
085: * representation of this and a eventually formally stored object.
086: */
087: private void checkDataProductionNeed() {
088: log.debug("checking data actuality..." + producer + ", "
089: + datasetProductionParams);
090: final String prodId = producer.getProducerId();
091: log
092: .debug(prodId
093: + ", "
094: + KeyGenerator
095: .generateKey((Serializable) datasetProductionParams));
096: log.debug("useCache = " + useCache);
097: DatasetProductionTimeStore dataCache = DatasetProductionTimeStore
098: .getInstance();
099: if (useCache
100: && dataCache.containsEntry(prodId,
101: datasetProductionParams)) {
102: Date produceTime = dataCache.getProductionTime(prodId,
103: datasetProductionParams);
104: log.debug("cached data available.");
105: // cached data available
106: if (!producer.hasExpired(datasetProductionParams,
107: produceTime)) {
108: log.debug("cached data is still valid.");
109: this.datasetProduceTime = produceTime.getTime();
110: return;
111: }
112: dataCache.removeEntry(prodId, datasetProductionParams);
113: }
114: datasetProduceTime = System.currentTimeMillis();
115: }
116:
117: }
|