?? xmlstorage.java
字號:
package siuying.gm.app.gmailer4j;
import java.util.*;
import siuying.gm.structure.*;
import java.io.*;
import java.util.logging.*;
import com.tagtraum.sixbs.*;
import com.tagtraum.sixbs.adapters.util.*;
/**
* Open a directory, use files under this directorys as data store
* @todo use some caching technique to improve performance
* @version 0.1
*/
public class XMLStorage implements GMStorage{
/**
* if the storage is opened
*/
private boolean opened;
/**
* url of the storage
*/
private String url;
/**
* file postfix that used by the data store, default ".xml"
*/
private String postfix;
/**
* list of all items in storage
*/
protected HashSet items;
protected static Logger logger = Logger.getLogger(XMLStorage.class.getName());
/**
* Filefilter that list only xml
*/
private static FileFilter filter;
private XMLStorage(){
items = new HashSet();
postfix = ".xml";
opened = false;
}
public String getPostfix(){
return postfix;
}
public void setPostfix(String postfix){
this.postfix = postfix;
this.filter = new PostfixFileFilter(postfix);
}
/**
* Open a XMLThreadStorage by url
* @param String pathname directory to store the XML files
* @throws IOException when open directory failed
*/
public static XMLStorage getXMLStorage(String path) throws IOException{
logger.info("Setting up XML Thread Storage ... ");
File dataDir = new File(path);
if (!dataDir.exists()){
// try to create the directory if not exist
dataDir.mkdirs();
}
if (!dataDir.isDirectory()){
throw new IOException("Parameter path not exist or not a directory: " + path);
}
XMLStorage newStore = new XMLStorage();
newStore.setUrl(path);
newStore.setPostfix(".xml");
return newStore;
}
/**
* Open a XMLThreadStorage by url
* @param String pathname directory to store the XML files
* @param String postfix the files type treated as xml files, e.g. ".xml"
* @throws IOException when open directory failed
*/
public static XMLStorage getXMLStorage(String path, String postfix) throws IOException{
XMLStorage store = XMLStorage.getXMLStorage(path);
store.setPostfix(postfix);
return store;
}
public void setUrl(String url) throws NullPointerException{
if (url == null) throw new NullPointerException("URL cannot be null. ");
if (url.equals("")) throw new IllegalArgumentException("URL must not be empty!");
this.url = url;
}
public String getUrl(){
return url;
}
public void open() throws IOException {
logger.info("Opening directory ... " + url);
File dataDir = new File(url);
if (!dataDir.exists()){
// try to create the directory if not exist
dataDir.mkdirs();
}
if (!dataDir.isDirectory()){
throw new IOException("Parameter path not exist or not a directory: " +
url + " (" + dataDir.getAbsolutePath() + ")");
}
logger.fine("Listing file ... ");
File[] xmlFiles = dataDir.listFiles(filter);
// add files to store if it match the postfix we specified
for(int i=0; i<xmlFiles.length; i++){
items.add(xmlFiles[i].getName());
}
logger.info("Number of files added: " + items.size());
opened = true;
}
public void close(){
opened = false;
}
/**
* check if the thread with id specified existed in the store
* @param id the id of the thread
*/
public boolean containsId(String id){
// check if the file have been downloaded
return items.contains((id + postfix).intern());
}
/**
* save thread as xml
* @throws NullPointerException when id or thread is null
* @throws IllegalArgumentException when id is empty string
*/
public void put(String id, Object thread) throws IOException, NullPointerException {
if (id == null || thread == null){
throw new NullPointerException("id or thread cannot be null!");
}
if (id.equals("")) {
throw new IllegalArgumentException("id must not be empty!");
}
FileWriter writer = null;
SIXBSWriter out = null;
try{
writer = new FileWriter(url + File.separator + id + postfix);
out = new SIXBSWriter(writer);
out.writeObject(thread);
}finally{
try{
out.close();
writer.close();
}catch(Exception e){}
}
items.add(id + postfix);
logger.info("Thread added, id: " + url + File.separator + id + postfix );
}
/**
* get thread from xml store by id
* @throws NullPointerException when id or thread is null
* @throws IllegalArgumentException when id is empty string
*/
public Object get(String id) throws IOException, NullPointerException{
if (id == null) throw new NullPointerException("id cannot be null. ");
if (id.equals("")) throw new IllegalArgumentException("id must not be empty!");
FileReader reader = new FileReader(new File(url + File.separator + id + postfix));
SIXBSReader in = null;
Object thread = null;
try{
in = new SIXBSReader(reader);
thread = in.readObject();
}finally{
try{
in.close();
reader.close();
}catch(Exception e){}
}
logger.info("Object loaded, id: " + id );
return thread;
}
/**
* remove thread xml file
* @throws NullPointerException when id is null
*/
public void remove(String id) throws NullPointerException, SecurityException {
if (id == null) throw new NullPointerException("id cannot be null. ");
if (id.equals("")) {
throw new IllegalArgumentException("id must not be empty!");
}
File file = new File(url + File.separator + id + postfix);
boolean exist = file.exists();
if(exist){
file.delete();
}
logger.info("Thread deleted, id: " + id );
items.remove(id + postfix );
}
/**
* do nothing in this implementation
* as we save every time we access file
*/
public void flush() throws IOException {
}
public Iterator iterator() {
return items.iterator();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -