1 17 18 package org.apache.james.imapserver; 19 20 import org.apache.avalon.framework.logger.AbstractLogEnabled; 21 import org.apache.james.util.Assert; 22 23 import java.io.*; 24 import java.util.Arrays ; 25 import java.util.Calendar ; 26 import java.util.Collections ; 27 import java.util.Iterator ; 28 29 30 36 public class DefaultRecordRepository 37 extends AbstractLogEnabled 38 implements RecordRepository { 39 40 private String path; 41 private File repository; 42 43 48 public int nextUIDValidity() 49 { 50 return Math.abs( Calendar.getInstance().hashCode() ); 54 } 55 56 59 public synchronized void deleteRecord( FolderRecord fr ) 60 { 61 try { 62 String key = path + File.separator + fr.getAbsoluteName(); 63 File record = new File( key ); 64 Assert.isTrue( Assert.ON && 65 record.exists() ); 66 record.delete(); 67 getLogger().info("Record deleted for: " + fr.getAbsoluteName()); 68 notifyAll(); 69 } 70 catch (Exception e) { 71 e.printStackTrace(); 72 throw new 73 RuntimeException ("Exception caught while storing Folder Record: " + e); 74 } 75 } 76 77 public void setPath(final String rootPath) { 78 if (path != null) { 79 throw new RuntimeException ("Error: Attempt to reset AvalonRecordRepository"); 80 } 81 path = rootPath; 82 83 repository = new File(rootPath); 84 85 if (!repository.isDirectory()) { 86 if (! repository.mkdirs()){ 87 throw new RuntimeException ("Error: Cannot create directory for AvalonRecordRepository at: " + rootPath); 88 } 89 } else if (!repository.canWrite()) { 90 throw new RuntimeException ("Error: Cannot write to directory for AvalonRecordRepository at: " + rootPath); 91 } 92 93 94 } 95 96 public synchronized void store( final FolderRecord fr) { 97 ObjectOutputStream out = null; 98 try { 99 String key = path + File.separator + fr.getAbsoluteName(); 100 out = new ObjectOutputStream( new FileOutputStream(key) ); 101 out.writeObject(fr); 102 out.close(); 103 getLogger().info("Record stored for: " + fr.getAbsoluteName()); 104 notifyAll(); 105 } catch (Exception e) { 106 if (out != null) { 107 try { 108 out.close(); 109 } catch (Exception ignored) { 110 } 111 } 112 e.printStackTrace(); 113 throw new 114 RuntimeException ("Exception caught while storing Folder Record: " + e); 115 } 116 } 117 118 public synchronized Iterator getAbsoluteNames() { 119 String [] names = repository.list(); 120 return Collections.unmodifiableList(Arrays.asList(names)).iterator(); 121 } 122 123 public synchronized FolderRecord retrieve(final String folderAbsoluteName) { 124 FolderRecord fr = null; 125 ObjectInputStream in = null; 126 try { 127 String key = path + File.separator + folderAbsoluteName; 128 in = new ObjectInputStream( new FileInputStream(key) ); 129 fr = (FolderRecord) in.readObject(); 130 in.close(); 131 132 } catch (Exception e) { 133 if (in != null) { 134 try { 135 in.close(); 136 } catch (Exception ignored) { 137 } 138 } 139 e.printStackTrace(); 140 throw new 141 RuntimeException ("Exception caught while reading Folder Record: " + e); 142 } finally { 143 notifyAll(); 144 } 145 return fr; 146 } 147 148 public boolean containsRecord(String folderAbsoluteName) { 149 File testFile = new File(repository, folderAbsoluteName); 150 return testFile.exists(); 151 } 152 } 153 154 155 | Popular Tags |