KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmanage > core > alert > AlertEngine


1 /**
2 * Copyright (c) 2004-2005 jManage.org
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;
15
16 import org.jmanage.core.config.ApplicationConfigManager;
17 import org.jmanage.core.config.AlertConfig;
18 import org.jmanage.core.config.ApplicationConfig;
19 import org.jmanage.core.util.Loggers;
20 import org.jmanage.core.alert.delivery.EmailAlerts;
21
22 import java.util.List JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.LinkedList JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.logging.Logger JavaDoc;
27
28 /**
29  *
30  * Date: Jul 31, 2005
31  * @author Rakesh Kalra
32  */

33 public class AlertEngine {
34
35     private static final Logger JavaDoc logger = Loggers.getLogger(AlertEngine.class);
36
37     private static final AlertEngine alertEngine = new AlertEngine();
38
39     public static AlertEngine getInstance(){
40         return alertEngine;
41     }
42
43     private List JavaDoc alerts = Collections.synchronizedList(new LinkedList JavaDoc());
44
45     private AlertEngine(){}
46
47     public void start(){
48         List JavaDoc alertConfigs = ApplicationConfigManager.getAllAlerts();
49         for(Iterator JavaDoc it=alertConfigs.iterator(); it.hasNext();){
50             AlertConfig alertConfig = (AlertConfig)it.next();
51             Alert alert = new Alert(alertConfig);
52             alert.register();
53         }
54         /* get EmailAlerts to start the email delivery thread */
55         EmailAlerts.getInstance();
56
57         logger.info("AlertEngine started.");
58     }
59
60     public void stop(){
61         for(Iterator JavaDoc it=alerts.iterator(); it.hasNext(); ){
62             Alert alert = (Alert)it.next();
63             alert.unregister();
64         }
65         // remove all alerts
66
alerts.clear();
67         logger.info("AlertEngine stopped.");
68     }
69
70     public synchronized void updateAlertConfig(AlertConfig alertConfig) {
71         Alert alert = new Alert(alertConfig);
72         int index = alerts.indexOf(alert);
73         if(index != -1){
74             // its an existing alert
75
Alert oldAlert = (Alert)alerts.remove(index);
76             oldAlert.unregister();
77         }
78         // register the alert
79
alert.register();
80         // add it to the list
81
alerts.add(alert);
82     }
83
84     public synchronized void removeAlertConfig(AlertConfig alertConfig){
85         Alert alert = new Alert(alertConfig);
86         int index = alerts.indexOf(alert);
87         if(index != -1){
88             Alert oldAlert = (Alert)alerts.remove(index);
89             oldAlert.unregister();
90         }
91     }
92
93     public synchronized void updateApplication(ApplicationConfig config){
94         for(Iterator JavaDoc it=config.getAlerts().iterator(); it.hasNext(); ){
95             AlertConfig alertConfig = (AlertConfig)it.next();
96             updateAlertConfig(alertConfig);
97         }
98     }
99
100     public synchronized void removeApplication(ApplicationConfig config){
101         for(Iterator JavaDoc it=config.getAlerts().iterator(); it.hasNext(); ){
102             AlertConfig alertConfig = (AlertConfig)it.next();
103             removeAlertConfig(alertConfig);
104         }
105     }
106 }
107
Popular Tags