package org.saf.pdu;
import java.io.File;
import java.util.Hashtable;
import java.util.Vector;
import org.apache.log4j.Logger;
import org.saf.business.BaseMTInfo;
import org.saf.common.Globals;
public abstract class AbstractPDUCache {
protected static final Logger logger = Logger.getLogger("PDUCache");
protected static final String SUFFIX_SMTOTRASMIT_FILE_NAME = "p2ptotrasmit.cache";
protected Hashtable<String, Vector<BaseMTInfo>> submitSMToTransmit = null;
public synchronized void store() {
String cacheDir = Globals.getInstance().getCacheDirPath();
File fCacheDir = new File(cacheDir);
if(fCacheDir.exists() == false) {
fCacheDir.mkdirs();
}
storeSubmitSMToTransmit(cacheDir);
customStore(cacheDir);
}
public synchronized void load(String channelName) {
loadSubmitSMToTransmit(channelName);
customLoad(channelName);
}
public synchronized void addSubmitSMToTransmit(
BaseMTInfo info, String channel) {
if(info != null && channel != null) {
logger.info("adding: " + info.getRequest().debugString() +
" for channel " + channel);
Vector<BaseMTInfo> channelSubmitSMToTransmit = null;
if(this.submitSMToTransmit.containsKey(channel) == true) {
channelSubmitSMToTransmit =
this.submitSMToTransmit.get(channel);
} else {
channelSubmitSMToTransmit = new Vector<BaseMTInfo>();
this.submitSMToTransmit.put(channel, channelSubmitSMToTransmit);
}
channelSubmitSMToTransmit.add(info);
} else {
logger.fatal("no correct data, probably null");
}
}
public synchronized void removeSubmitSMToTransmit(
BaseMTInfo info, String channel) {
if(info != null && channel != null) {
Vector<BaseMTInfo> channelSubmitSMToTransmit =
this.submitSMToTransmit.get(channel);
if(channelSubmitSMToTransmit != null) {
channelSubmitSMToTransmit.remove(info);
info = null;
}
}
}
public int getSizeSubmitSMToTransmit(String channel) {
int ret = 0;
if(channel != null) {
Vector<BaseMTInfo> v = this.submitSMToTransmit.get(channel);
if(v != null ) {
ret = v.size();
}
}
return ret;
}
public synchronized BaseMTInfo getSubmitSMToTransmit(String channel, int idx) {
BaseMTInfo info = null;
if(channel != null) {
Vector<BaseMTInfo> v = this.submitSMToTransmit.get(channel);
if(v != null && idx < v.size()) {
info = v.elementAt(idx);
}
}
return info;
}
public synchronized void clearSubmitSMToTransmit(String channel) {
Vector<BaseMTInfo> vector = this.submitSMToTransmit.remove(channel);
if(vector != null) {
vector.clear();
vector = null;
System.gc();
}
}
protected abstract void customLoad(String channelName);
protected abstract void customStore(String cacheDir);
protected abstract void loadSubmitSMToTransmit(String channelName);
protected abstract void storeSubmitSMToTransmit(String cacheDir);
protected AbstractPDUCache() {
init();
}
private void init() {
this.submitSMToTransmit = new Hashtable<String, Vector<BaseMTInfo>>();
}
}