Show Pool.java syntax highlighted
package fr.loria.ecoo.wooki.woot.core;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Vector;
import fr.loria.ecoo.wooki.woot.op.WootOp;
/**
*
* @author nabil
*/
public class Pool implements Serializable {
private File poolFile;// where to put ops
private Vector<WootOp> content;
/**
* Creates a new instance of Pool
*/
public Pool(String location) {
poolFile = new File(location);
this.setPoolFile(poolFile);
initializePool(false);
}
public void initializePool(boolean overwrite) {
if ((getPoolFile().exists() && overwrite) || !(getPoolFile().exists())) {
try {
FileOutputStream fout = new FileOutputStream(getPoolFile());
ObjectOutputStream oos = new ObjectOutputStream(fout);
Vector<WootOp> elem = new Vector<WootOp>();
oos.writeObject(elem);
oos.flush();
oos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public synchronized void loadPool() {
FileInputStream fin = null;
ObjectInputStream ois = null;
try {
fin = new FileInputStream(getPoolFile());
ois = new ObjectInputStream(fin);
setContent((Vector<WootOp>) ois.readObject());
} catch (Exception e) {
e.printStackTrace();
// make sure the file is properly close
} finally {
if (fin != null) {
try {
fin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public synchronized void unLoadPool() {
FileOutputStream fout = null;
ObjectOutputStream oos = null;
try {
fout = new FileOutputStream(getPoolFile());
oos = new ObjectOutputStream(fout);
oos.writeObject(getContent());
oos.flush();
// free the resource by calling setting the content to empty
// and calling garbage collector
free();
} catch (Exception e) {
e.printStackTrace();
// make sure the file is properly close
} finally {
if (fout != null) {
try {
fout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public synchronized void storePool() {
FileOutputStream fout = null;
ObjectOutputStream oos = null;
try {
fout = new FileOutputStream(getPoolFile());
oos = new ObjectOutputStream(fout);
oos.writeObject(getContent());
oos.flush();
} catch (Exception e) {
e.printStackTrace();
// make sure the file is properly close
} finally {
if (fout != null) {
try {
fout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* Initialize the log, by creating the file that will hold the serialization
* of the content (see also: {@link #getContent()})
*
* @param override
* Indicate if the method must override any existing log
*/
public void initializeLog(boolean override) {
if ((getPoolFile().exists() && override) || !(getPoolFile().exists())) {
try {
FileOutputStream fout = new FileOutputStream(getPoolFile());
ObjectOutputStream oos = new ObjectOutputStream(fout);
Vector<WootOp> elem = new Vector();
oos.writeObject(elem);
oos.flush();
oos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* Call it frequently after doing interaction with log file (loading the
* content, for example) this method let free some resource by calling the
* garbage collecotr.
*/
public void free() {
getContent().clear();
setContent(new Vector());
System.runFinalization();
System.gc();
}
/**
* Give back the location of log
*
* @return The abstract representation of the location for log
*/
public File getPoolFile() {
return poolFile;
}
/**
* Provide the location for log
*
* @param logFile
* The abstract representation of the location for log
*/
public void setPoolFile(File poolFile) {
this.poolFile = poolFile;
}
/**
* Return the whole content of the log
*
* @return A Vector, each entry in this Vector is a {@link WootOp} instance
*/
public Vector<WootOp> getContent() {
return content;
}
/**
* Provide the log content
*
* @param content
* A Vector, each entry in this Vector represent a {@link WootOp}
* instance
*/
public void setContent(Vector<WootOp> content) {
this.content = content;
}
}
See more files for this project here