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

NNTPGroupImpl

public class NNTPGroupImpl extends org.apache.avalon.framework.logger.AbstractLogEnabled implements NNTPGroup
Group is represented by a directory. Articles are stored in files with the name of file == article number

Fields Summary
private final File
root
The directory to which this group maps.
private int
lastArticle
The last article number in the group
private int
firstArticle
The last article number in the group
private int
numOfArticles
The number of articles in the group.
private boolean
articleRangeInfoCollected
Whether the first, last, and total number of articles in the group have been read from disk. An instance may collect range info once. This involves disk I/O
Constructors Summary
NNTPGroupImpl(File root)
The sole constructor for this particular NNTPGroupImpl.

param
root the directory containing the articles


                       
      
        this.root = root;
    
Methods Summary
public NNTPArticleaddArticle(java.io.InputStream newsStream)

see
org.apache.james.nntpserver.repository.NNTPGroup#addArticle(InputStream)

        File articleFile = null;
        synchronized (this) {
            int artNum;
            if (numOfArticles < 0)
                throw new IllegalStateException("NNTP Group is corrupt (articles < 0).");
            else if (numOfArticles == 0) {
                firstArticle = 1;
                artNum = 1;
            } else {
                artNum = getLastArticleNumber() + 1;
            }
            
            articleFile = new File(root, artNum + "");
            articleFile.createNewFile();
            lastArticle = artNum;
            numOfArticles++;
        }
        if (getLogger().isDebugEnabled()) {
            getLogger().debug("Copying message to: "+articleFile.getAbsolutePath());
        }
        FileOutputStream fout = null;
        try {
            fout = new FileOutputStream(articleFile);
            IOUtil.copy(newsStream,fout);
            fout.flush();
        } finally {
            try {
                if (fout != null) {
                    fout.close();
                }
            } catch (IOException ioe) {
                // Ignore this exception so we don't
                // trash any "real" exceptions
            }
        }
        return new NNTPArticleImpl(this, articleFile);
    
private voidcollectArticleRangeInfo()
Generates the first, last, and number of articles from the information in the group directory.

        if ( articleRangeInfoCollected ) {
            return;
        }
        String[] list = root.list();
        int first = -1;
        int last = -1;
        for ( int i = 0 ; i < list.length ; i++ ) {
            int num = Integer.parseInt(list[i]);
            if ( first == -1 || num < first ) {
                first = num;
            }
            if ( num > last ) {
                last = num;
            }
        }
        numOfArticles = list.length;
        firstArticle = Math.max(first,0);
        lastArticle = Math.max(last,0);
        articleRangeInfoCollected = true;
    
public NNTPArticlegetArticle(int number)

see
org.apache.james.nntpserver.NNTPGroup#getArticle(int)

        File f = new File(root,number + "");
        return f.exists() ? new NNTPArticleImpl(this, f) : null;
    
public java.util.IteratorgetArticles()

see
org.apache.james.nntpserver.NNTPGroup#getArticles()

        File[] f = root.listFiles();
        List list = new ArrayList();
        for ( int i = 0 ; i < f.length ; i++ )
            list.add(new NNTPArticleImpl(this, f[i]));
        return list.iterator();
    
public java.util.IteratorgetArticlesSince(java.util.Date dt)

see
org.apache.james.nntpserver.NNTPGroup#getArticlesSince(Date)

        File[] f = root.listFiles(new AndFileFilter
            (new DateSinceFileFilter(dt.getTime()),
             new InvertedFileFilter(new ExtensionFileFilter(".id"))));
        List list = new ArrayList();
        for ( int i = 0 ; i < f.length ; i++ ) {
            list.add(new NNTPArticleImpl(this, f[i]));
        }
        return list.iterator();
    
public java.lang.StringgetDescription()

see
org.apache.james.nntpserver.NNTPGroup#getDescription()

        return getName();
    
public intgetFirstArticleNumber()

see
org.apache.james.nntpserver.NNTPGroup#getFirstArticleNumber()

        collectArticleRangeInfo();
        return firstArticle;
    
public intgetLastArticleNumber()

see
org.apache.james.nntpserver.NNTPGroup#getLastArticleNumber()

        collectArticleRangeInfo();
        return lastArticle;
    
public java.lang.StringgetListFormat()

see
org.apache.james.nntpserver.repository.NNTPGroup#getListFormat()

        StringBuffer showBuffer =
            new StringBuffer(128)
                .append(getName())
                .append(" ")
                .append(getLastArticleNumber())
                .append(" ")
                .append(getFirstArticleNumber())
                .append(" ")
                .append((isPostAllowed() ? "y":"n"));
        return showBuffer.toString();
    
public java.lang.StringgetListNewsgroupsFormat()

see
org.apache.james.nntpserver.repository.NNTPGroup#getListNewsgroupsFormat()

        StringBuffer showBuffer =
            new StringBuffer(128)
                .append(getName())
                .append(" ")
                .append(getDescription());
         return showBuffer.toString();
    
public java.lang.StringgetName()

see
org.apache.james.nntpserver.NNTPGroup#getName()

        return root.getName();
    
public intgetNumberOfArticles()

see
org.apache.james.nntpserver.NNTPGroup#getNumberOfArticles()

        collectArticleRangeInfo();
        return numOfArticles;
    
public booleanisPostAllowed()

see
org.apache.james.nntpserver.NNTPGroup#isPostAllowed()

        return true;