KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.*;
21 import java.util.*;
22
23 /**
24  * Reference: RFC 2060
25  * @version 0.1 on 15 Aug 2002
26  */

27 public class FileStoreHighestUID implements HighestUID {
28
29     private int highestUID;
30     private int whenToWrite;
31     private static final int WRITE_STEP = 3;
32     private File file;
33   
34     public FileStoreHighestUID(File f) {
35         file = f;
36         highestUID = 0;
37     
38         if (file.exists()) {
39             ObjectInputStream is = null;
40             try {
41                 is = new ObjectInputStream(new FileInputStream(file));
42                 Integer JavaDoc i = (Integer JavaDoc) is.readObject();
43                 highestUID = i.intValue();
44                 is.close();
45                 is = null;
46             } catch (Exception JavaDoc ex) {
47                 // log here
48
ex.printStackTrace();
49                 if (is != null) {
50                     try {
51                         is.close();
52                     } catch (Exception JavaDoc ignored) {}
53                 }
54                 throw new RuntimeException JavaDoc("Could not load highestUID!");
55             }
56             // maybe james was stopped without writing correct highestUID, therefore add
57
// STEP_HIGHEST_UID, to ensure uniqeness of highestUID.
58
highestUID += WRITE_STEP;
59         }
60         write();
61         whenToWrite = highestUID+WRITE_STEP;
62         System.out.println("Initialized highestUID="+highestUID);
63     }
64   
65     public synchronized int get() {
66         return highestUID;
67     }
68   
69     public synchronized void increase() {
70         highestUID++;
71         if (highestUID >= whenToWrite) {
72             // save this highestUID
73
whenToWrite = highestUID+WRITE_STEP;
74             // make it persistent
75
write();
76         }
77     }
78   
79     private void write() {
80         ObjectOutputStream os = null;
81         try {
82             os = new ObjectOutputStream( new FileOutputStream(file));
83             os.writeObject(new Integer JavaDoc(highestUID));
84             os.close();
85             os = null;
86         } catch (Exception JavaDoc ex) {
87             // log here
88
ex.printStackTrace();
89             if (os != null) {
90                 try {
91                     os.close();
92                 } catch (Exception JavaDoc ignored) {}
93             }
94             throw new RuntimeException JavaDoc("Failed to save highestUID!");
95         }
96     }
97 }
98
Popular Tags