KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmanage > core > alert > delivery > PersistedAlerts


1 /**
2 * Copyright (c) 2004-2005 jManage.org. All rights reserved.
3 *
4 * This is a free software; you can redistribute it and/or
5 * modify it under the terms of the license at
6 * http://www.jmanage.org.
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 */

14 package org.jmanage.core.alert.delivery;
15
16 import org.jmanage.core.alert.AlertInfo;
17
18 import java.util.Map JavaDoc;
19 import java.util.Collections JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.beans.XMLEncoder JavaDoc;
23 import java.beans.XMLDecoder JavaDoc;
24 import java.io.*;
25
26 /**
27  *
28  * <p>
29  * Date: Nov 6, 2005
30  * @author Rakesh Kalra
31  */

32 public abstract class PersistedAlerts {
33
34     private Map JavaDoc alerts = Collections.synchronizedMap(new HashMap JavaDoc());
35
36     protected PersistedAlerts(){
37         // read from file
38
read();
39     }
40
41     public void add(AlertInfo alertInfo){
42         Object JavaDoc prevValue = alerts.put(alertInfo.getAlertId(), alertInfo);
43         assert prevValue == null;
44         save();
45     }
46
47     public void remove(String JavaDoc alertId){
48         alerts.remove(alertId);
49         save();
50     }
51
52     public AlertInfo get(String JavaDoc alertId){
53         return (AlertInfo)alerts.get(alertId);
54     }
55
56     public Collection JavaDoc getAll(){
57         return alerts.values();
58     }
59
60     public void removeAll(){
61         alerts.clear();
62         save();
63     }
64
65     /**
66      * Removes the next element from the Map
67      * @return next element if any, null otherwise
68      */

69     public AlertInfo remove(){
70         AlertInfo alertInfo = null;
71         synchronized(alerts){
72             if(alerts.size() > 0){
73                 String JavaDoc alertId =
74                         (String JavaDoc)alerts.keySet().iterator().next();
75                 alertInfo = (AlertInfo)alerts.remove(alertId);
76                 save();
77             }
78         }
79         return alertInfo;
80     }
81
82     protected abstract String JavaDoc getPersistedFileName();
83
84     private void save(){
85         try {
86             XMLEncoder JavaDoc encoder = new XMLEncoder JavaDoc(
87                     new BufferedOutputStream(
88                             new FileOutputStream(getPersistedFileName())));
89             Map JavaDoc persistedAlerts = new HashMap JavaDoc();
90             persistedAlerts.putAll(alerts);
91             encoder.writeObject(persistedAlerts);
92             encoder.close();
93         } catch (FileNotFoundException e) {
94             throw new RuntimeException JavaDoc(e);
95         }
96     }
97
98     private void read(){
99         try {
100             File file = new File(getPersistedFileName());
101             if(file.exists()){
102                 XMLDecoder JavaDoc decoder = new XMLDecoder JavaDoc(
103                                     new BufferedInputStream(
104                                         new FileInputStream(getPersistedFileName())));
105                 Map JavaDoc persistedAlerts = (Map JavaDoc)decoder.readObject();
106                 alerts.putAll(persistedAlerts);
107                 decoder.close();
108             }
109         } catch (FileNotFoundException e) {
110             throw new RuntimeException JavaDoc(e);
111         }
112     }
113 }
114
Popular Tags