FileDocCategorySizeDatePackage
ArticleIDRepository.javaAPI DocApache James 2.3.16380Fri Jan 12 12:56:24 GMT 2007org.apache.james.nntpserver.repository

ArticleIDRepository

public class ArticleIDRepository extends Object
ArticleIDRepository: contains one file for each article. the file name is Base64 encoded article ID The first line of the file is '# the rest of line have =
Allows fast lookup of a message by message id. This class allows a process to iterate and synchronize messages with other NNTP Servers. This may be inefficient. It may be better to use an alternate, more efficient process for synchronization and this class for sanity check.

Fields Summary
private final File
root
The root of the repository in the file system
private final String
articleIDDomainSuffix
The suffix appended to the articleIDs
private int
counter
A counter of the number of article IDs TODO: Potentially serious threading problem here
Constructors Summary
ArticleIDRepository(File root, String articleIDDomainSuffix)


       
        this.root = root;
        this.articleIDDomainSuffix = articleIDDomainSuffix;
    
Methods Summary
voidaddArticle(java.lang.String articleID, java.util.Properties prop)
Add the article information to the repository.

param
prop contains the newsgroup name and article number.

        if ( articleID == null ) {
            articleID = generateArticleID();
        }
        FileOutputStream fout = null;
        try {
            fout = new FileOutputStream(getFileFromID(articleID));
            prop.store(fout,new Date().toString());
        } finally {
            if (fout != null) {
                fout.close();
            }
        }
    
java.lang.StringgenerateArticleID()
Generate a new article ID for use in the repository.

        int idx = Math.abs(counter++);
        StringBuffer idBuffer =
            new StringBuffer(256)
                    .append("<")
                    .append(Thread.currentThread().hashCode())
                    .append(".")
                    .append(System.currentTimeMillis())
                    .append(".")
                    .append(idx)
                    .append("@")
                    .append(articleIDDomainSuffix)
                    .append(">");
        return idBuffer.toString();
    
NNTPArticlegetArticle(NNTPRepository repo, java.lang.String id)
Get the article from the NNTP respository with the specified id.

param
repo the NNTP repository where the article is stored
param
id the id of the article to retrieve
return
the article
throws
IOException if the ID information cannot be loaded

        File f = getFileFromID(id);
        if ( f.exists() == false ) {
            return null;
        }
        FileInputStream fin = null;
        Properties prop = new Properties();
        try {
            fin = new FileInputStream(f);
            prop.load(fin);
        } finally {
            if (fin != null) {
                fin.close();
            }
        }
        Enumeration enumeration = prop.keys();
        NNTPArticle article = null;
        while ( article == null && enumeration.hasMoreElements() ) {
            String groupName = (String)enumeration.nextElement();
            int number = Integer.parseInt(prop.getProperty(groupName));
            NNTPGroup group = repo.getGroup(groupName);
            if ( group != null ) {
                article = group.getArticle(number);
            }
        }
        return article;
    
java.io.FilegetFileFromID(java.lang.String articleID)
Returns the file in the repository corresponding to the specified article ID.

param
articleID the article ID
return
the repository file

        String b64Id;
        try {
            b64Id = removeCRLF(Base64.encodeAsString(articleID));
        } catch (Exception e) {
            throw new RuntimeException("This shouldn't happen: " + e);
        }
        return new File(root, b64Id);
    
booleanisExists(java.lang.String articleID)
Returns whether the article ID is in the repository

param
articleID the article ID
return
whether the article ID is in the repository

        return ( articleID == null ) ? false : getFileFromID(articleID).exists();
    
private static java.lang.StringremoveCRLF(java.lang.String str)
the base64 encode from javax.mail.internet.MimeUtility adds line feeds to the encoded stream. This removes them, since we will use the String as a filename.

        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (c != '\r" && c != '\n") {
                buffer.append(c);
            }
        }
        return buffer.toString();