001: package it.geosolutions.imageio.plugins.jhdf.pool;
002:
003: import it.geosolutions.imageio.plugins.jhdf.pool.DatasetPool.DatasetCopy;
004:
005: import java.io.File;
006: import java.util.TreeMap;
007: import java.util.logging.Logger;
008:
009: /**
010: * This class provides to manage different dataset pools (A dataset pool for
011: * each (sub)dataset contained within the originating source. An ImageReader
012: * should ask t
013: *
014: *
015: * @author Romagnoli Daniele
016: * @author Giannecchini Simone
017: *
018: */
019:
020: public class DatasetPoolManager {
021:
022: private static final Logger LOGGER = org.geotools.util.logging.Logging
023: .getLogger("it.geosolutions.imageio.plugins.jhdf.pool");
024:
025: /** Hashmap containing couples (key,DatasetPool). */
026: // protected Map pooledDatasetsMap = Collections.synchronizedMap(new
027: // HashMap(
028: // 10));
029: /** Map to associate keys to index */
030: // protected TreeMap indexToKeyMap = new TreeMap();
031: protected TreeMap indexToPoolMap = new TreeMap();
032:
033: /**
034: * The number of pooled Datasets. If a HDF file contains 4 subdatasets, this *
035: * variable may assume as value, atmost 4. The number of copies of the same
036: * dataset within a pool is not related with this value.
037: */
038: private int differentPooledDatasets;
039:
040: /** The number of subdatasets contained within the source file */
041: private int subdatasetNumber;
042:
043: private File originatingFile;
044:
045: // public DatasetCopy getDatasetCopy(final int datasetIndex) {
046: // return getDatasetCopy(getKeyFromIndex(datasetIndex));
047: // }
048: //
049: // // TODO: I need to set a proper type for the search key within the
050: // hashmap.
051: // // Maybe "String" is not the proper one.
052: // private DatasetCopy getDatasetCopy(final String key) {
053: // DatasetCopy dsc = null;
054: // synchronized (pooledDatasetsMap) {
055: // if (pooledDatasetsMap == null
056: // || key==null || !pooledDatasetsMap.containsKey(key)){
057: // // populate the pool
058: // ;
059: //
060: // }
061: //
062: //
063: // if (pooledDatasetsMap.containsKey(key)) {
064: // DatasetPool pool = (DatasetPool) pooledDatasetsMap.get(key);
065: // synchronized (pool) {
066: // if (pool.hasAvailableDatasets()) {
067: // dsc = pool.getDatasetCopy();
068: // }
069: // }
070: // }
071: // }
072: // return dsc;
073: // }
074: //
075: // public void getBackDatasetCopy(final int datasetIndex,
076: // final int copyID, final boolean hasBeenModified) {
077: // getBackDatasetCopy(getKeyFromIndex(datasetIndex), copyID,
078: // hasBeenModified);
079: // }
080: //
081: // private void getBackDatasetCopy(final String key, final int copyID,
082: // final boolean hasBeenModified) {
083: // synchronized (pooledDatasetsMap) {
084: // if (pooledDatasetsMap.containsKey(key)) {
085: // DatasetPool pool = (DatasetPool) pooledDatasetsMap.get(key);
086: // synchronized (pool) {
087: // pool.getBackDatasetCopy(copyID, hasBeenModified);
088: // }
089: // }
090: // }
091: // }
092: //
093: // /**
094: // * A simple method providing to retrieve the key which is related to a
095: // * dataset from dataset index.
096: // *
097: // * @param datasetIndex
098: // * @return
099: // */
100: // protected String getKeyFromIndex(final int datasetIndex) {
101: // String key = null;
102: // synchronized (indexToKeyMap) {
103: // if (indexToKeyMap != null && !indexToKeyMap.isEmpty()
104: // && indexToKeyMap.containsKey(datasetIndex))
105: // key = (String) indexToKeyMap.get(datasetIndex);
106: // }
107: // return key;
108: // }
109:
110: public DatasetCopy getDatasetCopy(int imageIndex) {
111: DatasetCopy dsc = null;
112: System.out.print("Entering synchronized map-->");
113: synchronized (indexToPoolMap) {
114: // TODO: REMOVE this MESSAGE
115: System.out.print("ENTERED... ->");
116: System.out.print("MANAGER: getting DatasetCopy ");
117: if (indexToPoolMap == null
118: || !indexToPoolMap.containsKey(Integer
119: .valueOf(imageIndex))) {
120: // populate the pool
121: indexToPoolMap.put(Integer.valueOf(imageIndex),
122: new DatasetPool(imageIndex, originatingFile));
123: differentPooledDatasets++;
124: }
125: if (indexToPoolMap.containsKey(Integer.valueOf(imageIndex))) {
126: DatasetPool pool = (DatasetPool) indexToPoolMap
127: .get(Integer.valueOf(imageIndex));
128: // synchronized (pool) {
129: dsc = pool.getDatasetCopy();
130: // }
131: }
132: }
133: // TODO: REMOVE this MESSAGE
134: System.out.print(dsc.getCopyID() + "\n");
135: return dsc;
136: }
137:
138: public void getBackDatasetCopy(final int key, final int copyID,
139: final boolean hasBeenModified) {
140: synchronized (indexToPoolMap) {
141: // TODO: REMOVE this MESSAGE
142: System.out.print("MANAGER: getting back DatasetCopy "
143: + String.valueOf(copyID) + "\n");
144: if (indexToPoolMap.containsKey(Integer.valueOf(key))) {
145: DatasetPool pool = (DatasetPool) indexToPoolMap
146: .get(Integer.valueOf(key));
147: // synchronized (pool) {
148: pool.getBackDatasetCopy(copyID, hasBeenModified);
149: // }
150: }
151: }
152: }
153:
154: public File getOriginatingFile() {
155: return originatingFile;
156: }
157:
158: public void setOriginatingFile(File originatingFile) {
159: this.originatingFile = originatingFile;
160: }
161: }
|