package org.jboss.remoting;
import org.jboss.logging.Logger;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Map;
public class CallbackStore implements CallbackStoreMBean
{
private String filePath = null;
private String fileSuffix = "ser";
private boolean isStarted = false;
private boolean purgeOnShutdown = false;
public static final String FILE_PATH_KEY = "StoreFilePath";
public static final String FILE_SUFFIX_KEY = "StoreFileSuffix";
private static final Logger log = Logger.getLogger(CallbackStore.class);
public CallbackStore()
{
}
public CallbackStore(boolean purgeOnDestroy)
{
this.purgeOnShutdown = purgeOnDestroy;
}
public void start() throws Exception
{
if(!isStarted)
{
if(filePath == null)
{
filePath = System.getProperty("jboss.server.data.dir", "data");
}
File storeFile = new File(filePath);
if(!storeFile.exists())
{
boolean madeDir = storeFile.mkdirs();
if(!madeDir)
{
throw new IOException("Can not create directory for store. Path given: " + filePath);
}
}
isStarted = true;
}
}
public void setPurgeOnShutdown(boolean purgeOnShutdown)
{
this.purgeOnShutdown = purgeOnShutdown;
}
public boolean getPurgeOnShutdown()
{
return purgeOnShutdown;
}
public void create() throws Exception
{
}
public void stop()
{
isStarted = false;
}
public void destroy()
{
if(purgeOnShutdown)
{
purgeFiles();
}
}
public void purgeFiles()
{
String[] fileList = getObjectFileList();
String fileToDelete = null;
for(int x = 0; x < fileList.length; x++)
{
try
{
fileToDelete = filePath + System.getProperty("file.separator") + fileList[x];
File currentFile = new File(fileToDelete);
boolean deleted = currentFile.delete();
if(!deleted)
{
log.warn("Error purging file " + fileToDelete);
}
}
catch(Exception e)
{
log.warn("Error purging file " + fileToDelete);
}
}
}
public void setConfig(Map config)
{
if(config != null)
{
String newFilePath = (String) config.get(FILE_PATH_KEY);
if(newFilePath != null)
{
filePath = newFilePath;
}
String newFileSuffix = (String) config.get(FILE_SUFFIX_KEY);
if(newFileSuffix != null)
{
fileSuffix = newFileSuffix;
}
}
}
public String getStoreFilePath()
{
return filePath;
}
public void setStoreFilePath(String filePath)
{
this.filePath = filePath;
}
public String getStoreFileSuffix()
{
return fileSuffix;
}
public void setStoreFileSuffix(String fileSuffix)
{
this.fileSuffix = fileSuffix;
}
public int size()
{
verifyStarted();
String[] objectFileList = getObjectFileList();
if(objectFileList != null)
{
return objectFileList.length;
}
else
{
return 0;
}
}
private void verifyStarted()
{
if(!isStarted)
{
throw new RuntimeException("Can not call upon this store method before it has been started.");
}
}
public Object getNext() throws IOException
{
verifyStarted();
Object obj = null;
String objectFilePath = null;
synchronized(filePath)
{
String[] objectFileList = getObjectFileList();
FileInputStream inFile = null;
ObjectInputStream in = null;
if(objectFileList != null && objectFileList.length > 0)
{
try
{
objectFilePath = filePath + System.getProperty("file.separator") + objectFileList[0];
inFile = new FileInputStream(objectFilePath);
in = new ObjectInputStream(inFile);
try
{
obj = in.readObject();
}
catch(ClassNotFoundException e)
{
throw new IOException("Error loading persisted object. Could not load class (" + e.getMessage() + ").");
}
}
finally
{
if(inFile != null)
{
try
{
inFile.close();
}
catch(IOException ioe)
{
log.debug("Error closing FileInputStream.", ioe);
}
}
if(in != null)
{
try
{
in.close();
}
catch(IOException ioe)
{
log.debug("Error closing ObjectInputStream.", ioe);
}
}
}
}
}
if(objectFilePath != null)
{
File objectFile = new File(objectFilePath);
boolean isDeleted = objectFile.delete();
if(log.isTraceEnabled())
{
log.trace("object file (" + objectFilePath + ") has been deleted - " + isDeleted);
}
}
return obj;
}
private String[] getObjectFileList()
{
File storePath = new File(filePath);
String[] objectFileList = storePath.list(new StoreFileFilter());
return objectFileList;
}
public void add(Serializable object) throws IOException
{
verifyStarted();
synchronized(filePath)
{
long currentTimestamp = System.currentTimeMillis();
File storeFile = new File(filePath + System.getProperty("file.separator") + String.valueOf(currentTimestamp) + "." + fileSuffix);
FileOutputStream outFile = null;
ObjectOutputStream out = null;
try
{
outFile = new FileOutputStream(storeFile, false);
out = new ObjectOutputStream(outFile);
out.writeObject(object);
out.flush();
}
finally
{
if(outFile != null)
{
try
{
outFile.close();
}
catch(IOException ioe)
{
log.debug("Error closing FileInputStream.", ioe);
}
}
if(out != null)
{
try
{
out.close();
}
catch(IOException ioe)
{
log.debug("Error closing ObjectInputStream.", ioe);
}
}
}
}
}
public class StoreFileFilter implements FilenameFilter
{
public boolean accept(File dir, String name)
{
if(name.endsWith(fileSuffix))
{
return true;
}
else
{
return false;
}
}
}
}