KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nextime > ion > backoffice > messageBoard > MessageBoard


1 package org.nextime.ion.backoffice.messageBoard;
2
3 import java.io.FileInputStream JavaDoc;
4 import java.io.FileOutputStream JavaDoc;
5 import java.io.ObjectInputStream JavaDoc;
6 import java.io.ObjectOutputStream JavaDoc;
7 import java.io.Serializable JavaDoc;
8 import java.util.Collections JavaDoc;
9 import java.util.Vector JavaDoc;
10
11 import javax.servlet.http.HttpServlet JavaDoc;
12
13 public class MessageBoard implements Serializable JavaDoc {
14
15     private Vector JavaDoc messages;
16     private static MessageBoard instance;
17     private static String JavaDoc storePath;
18
19     public Vector JavaDoc getMessages() {
20         Collections.sort(messages);
21         return messages;
22     }
23
24     public void addMessage(Message m) {
25         messages.add(m);
26         store();
27     }
28
29     public void removeMessage(Message m) {
30         messages.remove(m);
31         store();
32     }
33
34     private MessageBoard() {
35         messages = new Vector JavaDoc();
36     }
37
38     public static MessageBoard getInstance() {
39         if (instance == null) {
40             try {
41                 FileInputStream JavaDoc fis = new FileInputStream JavaDoc(storePath);
42                 ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(fis);
43                 instance = (MessageBoard) ois.readObject();
44             } catch (Exception JavaDoc e) {
45                 instance = new MessageBoard();
46                 e.printStackTrace();
47             }
48         }
49         return instance;
50     }
51
52     protected static void init(HttpServlet JavaDoc servlet) {
53         storePath =
54             servlet.getServletContext().getRealPath(
55                 "/WEB-INF/messageBoard/store");
56     }
57
58     private void store() {
59         try {
60             FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(storePath);
61             ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(fos);
62             oos.writeObject(this);
63         } catch (Exception JavaDoc e) {
64             e.printStackTrace();
65         }
66     }
67
68 }
69
Popular Tags