001: package pygmy.nntp;
002:
003: import pygmy.core.InternetOutputStream;
004: import pygmy.core.UUID;
005:
006: import java.util.*;
007: import java.io.*;
008:
009: public class NewsGroup {
010:
011: File newsgroupFile;
012:
013: String name;
014:
015: int firstIndex;
016:
017: int lastIndex;
018:
019: public NewsGroup(File newsgroupFile, String name) {
020: this .newsgroupFile = newsgroupFile;
021: if (!newsgroupFile.exists()) {
022: newsgroupFile.mkdir();
023: }
024: this .name = name;
025: String[] names = newsgroupFile.list();
026: firstIndex = Integer.MAX_VALUE;
027: lastIndex = 0;
028: for (int i = 0; names != null && i < names.length; i++) {
029: try {
030: int index = getIndex(names[i]);
031: if (index <= firstIndex) {
032: firstIndex = index;
033: }
034: if (index >= lastIndex) {
035: lastIndex = index;
036: }
037: } catch (NumberFormatException ignore) {
038: }
039: }
040: }
041:
042: public synchronized int size() {
043: int size = lastIndex - firstIndex + 1;
044: return size > 0 ? size : 0;
045: }
046:
047: public String getName() {
048: return name;
049: }
050:
051: public Date getLastModified() {
052: return new Date(newsgroupFile.lastModified());
053: }
054:
055: public void addArticle(Article article, String host)
056: throws IOException {
057: if (article.getMessageId() == null) {
058: article.setMessageId(UUID.createUUID() + "@" + host);
059: }
060: article.addPath(host);
061: article.setDateReceived(new Date());
062: article.setArticleNumber(nextIndex());
063: saveArticle(article);
064: }
065:
066: private void saveArticle(Article article) throws IOException {
067: File articleFile = new File(newsgroupFile, getIndexFile(article
068: .getArticleNumber()));
069: InternetOutputStream stream = new InternetOutputStream(
070: new FileOutputStream(articleFile));
071: try {
072: article.save(stream);
073: } finally {
074: stream.flush();
075: stream.close();
076: }
077: }
078:
079: private String getIndexFile(int index) {
080: return String.valueOf(index);
081: }
082:
083: private int getIndex(String filename) {
084: return Integer.parseInt(filename);
085: }
086:
087: private synchronized int nextIndex() {
088: lastIndex++;
089: if (firstIndex > lastIndex) {
090: firstIndex = lastIndex;
091: }
092: return lastIndex;
093: }
094:
095: public synchronized int getLastIndex() {
096: return lastIndex;
097: }
098:
099: public synchronized int getFirstIndex() {
100: return firstIndex;
101: }
102:
103: public boolean getPostingAllowed() {
104: return true;
105: }
106:
107: public Article getMessage(int messageIndex) throws IOException {
108: NntpInputStream is = null;
109: try {
110: is = getMessageInputStream(messageIndex);
111: Article article = new Article(is);
112: article.setArticleNumber(messageIndex);
113: return article;
114: } finally {
115: if (is != null) {
116: is.close();
117: }
118: }
119: }
120:
121: public NntpInputStream getMessageInputStream(int messageIndex)
122: throws FileNotFoundException {
123: return new NntpInputStream(new FileInputStream(new File(
124: newsgroupFile, getIndexFile(messageIndex))));
125: }
126:
127: public File getDirectory() {
128: return newsgroupFile;
129: }
130:
131: // public Article getMessage( String messageId ) {
132: // return (Article)messages.get( messageId );
133: // }
134: }
|