KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > imapserver > DefaultRecordRepository


1 /***********************************************************************
2  * Copyright (c) 2000-2004 The Apache Software Foundation. *
3  * All rights reserved. *
4  * ------------------------------------------------------------------- *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you *
6  * may not use this file except in compliance with the License. You *
7  * may obtain a copy of the License at: *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or *
14  * implied. See the License for the specific language governing *
15  * permissions and limitations under the License. *
16  ***********************************************************************/

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 JavaDoc;
25 import java.util.Calendar JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.Iterator JavaDoc;
28
29
30 /**
31  * Implementation of a RecordRepository on a FileSystem.
32  *
33  * @version 0.2 on 04 Aug 2002
34  * @see RecordRepository
35  */

36 public class DefaultRecordRepository
37     extends AbstractLogEnabled
38     implements RecordRepository {
39  
40     private String JavaDoc path;
41     private File repository;
42
43     /**
44      * Returns the a unique UID validity value for this Host.
45      * UID validity values are used to differentiate messages in 2 mailboxes with the same names
46      * (when one is deleted).
47      */

48     public int nextUIDValidity()
49     {
50         // TODO - make this a better unique value
51
// ( although this will probably never break in practice,
52
// should be incrementing a persisted value.
53
return Math.abs( Calendar.getInstance().hashCode() );
54     }
55
56     /**
57      * Deletes the FolderRecord from the repository.
58      */

59     public synchronized void deleteRecord( FolderRecord fr )
60     {
61         try {
62             String JavaDoc 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 JavaDoc e) {
71             e.printStackTrace();
72             throw new
73                 RuntimeException JavaDoc("Exception caught while storing Folder Record: " + e);
74         }
75     }
76
77     public void setPath(final String JavaDoc rootPath) {
78         if (path != null) {
79             throw new RuntimeException JavaDoc("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 JavaDoc("Error: Cannot create directory for AvalonRecordRepository at: " + rootPath);
88             }
89         } else if (!repository.canWrite()) {
90             throw new RuntimeException JavaDoc("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 JavaDoc 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 JavaDoc e) {
106             if (out != null) {
107                 try {
108                     out.close();
109                 } catch (Exception JavaDoc ignored) {
110                 }
111             }
112             e.printStackTrace();
113             throw new
114                 RuntimeException JavaDoc("Exception caught while storing Folder Record: " + e);
115         }
116     }
117
118     public synchronized Iterator JavaDoc getAbsoluteNames() {
119         String JavaDoc[] names = repository.list();
120         return Collections.unmodifiableList(Arrays.asList(names)).iterator();
121     }
122
123     public synchronized FolderRecord retrieve(final String JavaDoc folderAbsoluteName) {
124         FolderRecord fr = null;
125         ObjectInputStream in = null;
126         try {
127             String JavaDoc key = path + File.separator + folderAbsoluteName;
128             in = new ObjectInputStream( new FileInputStream(key) );
129             fr = (FolderRecord) in.readObject();
130             in.close();
131   
132         } catch (Exception JavaDoc e) {
133             if (in != null) {
134                 try {
135                     in.close();
136                 } catch (Exception JavaDoc ignored) {
137                 }
138             }
139             e.printStackTrace();
140             throw new
141                 RuntimeException JavaDoc("Exception caught while reading Folder Record: " + e);
142         } finally {
143             notifyAll();
144         }
145         return fr;
146     }
147        
148     public boolean containsRecord(String JavaDoc folderAbsoluteName) {
149         File testFile = new File(repository, folderAbsoluteName);
150         return testFile.exists();
151     }
152 }
153
154     
155
Popular Tags