1 package org.nextime.ion.backoffice.messageBoard; 2 3 import java.io.FileInputStream ; 4 import java.io.FileOutputStream ; 5 import java.io.ObjectInputStream ; 6 import java.io.ObjectOutputStream ; 7 import java.io.Serializable ; 8 import java.util.Collections ; 9 import java.util.Vector ; 10 11 import javax.servlet.http.HttpServlet ; 12 13 public class MessageBoard implements Serializable { 14 15 private Vector messages; 16 private static MessageBoard instance; 17 private static String storePath; 18 19 public Vector 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 (); 36 } 37 38 public static MessageBoard getInstance() { 39 if (instance == null) { 40 try { 41 FileInputStream fis = new FileInputStream (storePath); 42 ObjectInputStream ois = new ObjectInputStream (fis); 43 instance = (MessageBoard) ois.readObject(); 44 } catch (Exception e) { 45 instance = new MessageBoard(); 46 e.printStackTrace(); 47 } 48 } 49 return instance; 50 } 51 52 protected static void init(HttpServlet servlet) { 53 storePath = 54 servlet.getServletContext().getRealPath( 55 "/WEB-INF/messageBoard/store"); 56 } 57 58 private void store() { 59 try { 60 FileOutputStream fos = new FileOutputStream (storePath); 61 ObjectOutputStream oos = new ObjectOutputStream (fos); 62 oos.writeObject(this); 63 } catch (Exception e) { 64 e.printStackTrace(); 65 } 66 } 67 68 } 69 | Popular Tags |