1 17 18 package org.apache.james.nntpserver.repository; 19 20 import org.apache.avalon.excalibur.io.AndFileFilter; 21 import org.apache.avalon.excalibur.io.ExtensionFileFilter; 22 import org.apache.avalon.excalibur.io.InvertedFileFilter; 23 import org.apache.avalon.excalibur.io.IOUtil; 24 import org.apache.avalon.framework.logger.AbstractLogEnabled; 25 import org.apache.james.nntpserver.DateSinceFileFilter; 26 27 import java.io.File ; 28 import java.io.FileOutputStream ; 29 import java.io.InputStream ; 30 import java.io.IOException ; 31 import java.util.ArrayList ; 32 import java.util.Date ; 33 import java.util.Iterator ; 34 import java.util.List ; 35 36 41 class NNTPGroupImpl extends AbstractLogEnabled implements NNTPGroup { 42 43 46 private final File root; 47 48 51 private int lastArticle; 52 53 56 private int firstArticle; 57 58 61 private int numOfArticles; 62 63 68 private boolean articleRangeInfoCollected = false; 69 70 75 NNTPGroupImpl(File root) { 76 this.root = root; 77 } 78 79 82 public String getName() { 83 return root.getName(); 84 } 85 86 89 public String getDescription() { 90 return getName(); 91 } 92 93 96 public boolean isPostAllowed() { 97 return true; 98 } 99 100 104 private void collectArticleRangeInfo() { 105 if ( articleRangeInfoCollected ) { 106 return; 107 } 108 String [] list = root.list(); 109 int first = -1; 110 int last = -1; 111 for ( int i = 0 ; i < list.length ; i++ ) { 112 int num = Integer.parseInt(list[i]); 113 if ( first == -1 || num < first ) { 114 first = num; 115 } 116 if ( num > last ) { 117 last = num; 118 } 119 } 120 numOfArticles = list.length; 121 firstArticle = Math.max(first,0); 122 lastArticle = Math.max(last,0); 123 articleRangeInfoCollected = true; 124 } 125 126 129 public int getNumberOfArticles() { 130 collectArticleRangeInfo(); 131 return numOfArticles; 132 } 133 134 137 public int getFirstArticleNumber() { 138 collectArticleRangeInfo(); 139 return firstArticle; 140 } 141 142 145 public int getLastArticleNumber() { 146 collectArticleRangeInfo(); 147 return lastArticle; 148 } 149 150 153 public NNTPArticle getArticle(int number) { 154 File f = new File (root,number + ""); 155 return f.exists() ? new NNTPArticleImpl(this, f) : null; 156 } 157 158 161 public Iterator getArticlesSince(Date dt) { 162 File [] f = root.listFiles(new AndFileFilter 163 (new DateSinceFileFilter(dt.getTime()), 164 new InvertedFileFilter(new ExtensionFileFilter(".id")))); 165 List list = new ArrayList (); 166 for ( int i = 0 ; i < f.length ; i++ ) { 167 list.add(new NNTPArticleImpl(this, f[i])); 168 } 169 return list.iterator(); 170 } 171 172 175 public Iterator getArticles() { 176 File [] f = root.listFiles(); 177 List list = new ArrayList (); 178 for ( int i = 0 ; i < f.length ; i++ ) 179 list.add(new NNTPArticleImpl(this, f[i])); 180 return list.iterator(); 181 } 182 183 186 public String getListFormat() { 187 StringBuffer showBuffer = 188 new StringBuffer (128) 189 .append(getName()) 190 .append(" ") 191 .append(getLastArticleNumber()) 192 .append(" ") 193 .append(getFirstArticleNumber()) 194 .append(" ") 195 .append((isPostAllowed() ? "y":"n")); 196 return showBuffer.toString(); 197 } 198 199 202 public String getListNewsgroupsFormat() { 203 StringBuffer showBuffer = 204 new StringBuffer (128) 205 .append(getName()) 206 .append(" ") 207 .append(getDescription()); 208 return showBuffer.toString(); 209 } 210 211 214 public NNTPArticle addArticle(InputStream newsStream) 215 throws IOException { 216 File articleFile = null; 217 synchronized (this) { 218 int artNum; 219 if (numOfArticles < 0) 220 throw new IllegalStateException ("NNTP Group is corrupt (articles < 0)."); 221 else if (numOfArticles == 0) { 222 firstArticle = 1; 223 artNum = 1; 224 } else { 225 artNum = getLastArticleNumber() + 1; 226 } 227 228 articleFile = new File (root, artNum + ""); 229 articleFile.createNewFile(); 230 lastArticle = artNum; 231 numOfArticles++; 232 } 233 if (getLogger().isDebugEnabled()) { 234 getLogger().debug("Copying message to: "+articleFile.getAbsolutePath()); 235 } 236 FileOutputStream fout = null; 237 try { 238 fout = new FileOutputStream (articleFile); 239 IOUtil.copy(newsStream,fout); 240 fout.flush(); 241 } finally { 242 try { 243 if (fout != null) { 244 fout.close(); 245 } 246 } catch (IOException ioe) { 247 } 250 } 251 return new NNTPArticleImpl(this, articleFile); 252 } 253 254 277 } 278 | Popular Tags |