KickJava   Java API By Example, From Geeks To Geeks.

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


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.AlertSourceConfig;
17 import org.jmanage.core.management.ServerConnection;
18 import org.jmanage.core.management.ServerConnector;
19 import org.jmanage.core.management.ConnectionFailedException;
20 import org.jmanage.core.util.Loggers;
21
22 import java.util.logging.Logger JavaDoc;
23 import java.util.logging.Level JavaDoc;
24 import java.io.IOException JavaDoc;
25
26 /**
27  *
28  * Date: Jul 1, 2005
29  * @author Rakesh Kalra
30  */

31 public abstract class AlertSource {
32
33     private static final Logger JavaDoc logger = Loggers.getLogger(AlertSource.class);
34
35     protected final AlertSourceConfig sourceConfig;
36     protected AlertHandler handler;
37     protected ServerConnection connection;
38     protected String JavaDoc alertId;
39     protected String JavaDoc alertName;
40     private boolean stopThreads = false;
41
42
43     public AlertSource(AlertSourceConfig sourceConfig){
44         assert sourceConfig != null;
45         this.sourceConfig = sourceConfig;
46     }
47
48     /** Called by the ConnectionMonitor thread, when the connection goes down*/
49     private void connectionClosed(){
50         try {
51             this.unregisterInternal();
52         } catch (Exception JavaDoc e) {
53             logger.severe("Error unregistering for monitoring. error=" +
54                     e.getMessage());
55         }
56         /* close the connection */
57         closeConnection();
58
59         /* need to re-establish the connection when the server comes up */
60         new EstablishConnection().start();
61     }
62
63     private void closeConnection(){
64         try {
65            connection.close();
66         } catch (IOException JavaDoc e) {
67            logger.log(Level.WARNING, "Error while closing connection. error: " +
68                    e.getMessage());
69         }
70     }
71
72     private void connectionOpen(){
73         try {
74             /* start monitoring */
75             registerInternal();
76             logger.info("Registered for alert: " + alertName +
77                     ", application: " +
78                     sourceConfig.getApplicationConfig().getName());
79         } catch (Exception JavaDoc e) {
80             /* something happen while registering for alert. There is no point
81                 retrying. Just log the error and continue */

82             logger.log(Level.SEVERE, "Error registering alert: " + alertName, e);
83             /* close the connection that was openend*/
84             closeConnection();
85             return;
86         }
87         /* start the connection monitoring thread */
88         new ConnectionMonitor().start();
89     }
90
91     public void register(AlertHandler handler,
92                          String JavaDoc alertId,
93                          String JavaDoc alertName){
94         assert this.handler == null;
95         assert connection == null;
96
97         this.handler = handler;
98         this.alertId = alertId;
99         this.alertName = alertName;
100
101         /* need to establish the connection to the server*/
102         new EstablishConnection().start();
103     }
104
105     public void unregister(){
106         stopThreads = true;
107         unregisterInternal();
108
109         /* close the connection */
110         try {
111            connection.close();
112         } catch (IOException JavaDoc e) {
113            logger.log(Level.WARNING, "Error while closing connection. error: " +
114                    e.getMessage());
115         }
116
117         logger.info("Un-registered for alert: " + alertName +
118                     ", application: " +
119                     sourceConfig.getApplicationConfig().getName());
120
121         connection = null;
122         handler = null;
123     }
124
125     protected abstract void registerInternal();
126
127     protected abstract void unregisterInternal();
128
129     private class ConnectionMonitor extends Thread JavaDoc{
130
131         public void run(){
132             while(!stopThreads){
133                 if(!connection.isOpen()){
134                     /* connection went down */
135                     logger.warning("Connection lost to application: " +
136                             sourceConfig.getApplicationConfig().getName());
137                     AlertSource.this.connectionClosed();
138                     return;
139                 }
140                 try {
141                     sleep(500);
142                 } catch (InterruptedException JavaDoc e) {}
143             }
144         }
145     }
146
147     private class EstablishConnection extends Thread JavaDoc{
148
149         public void run(){
150             while(!stopThreads){
151
152                 try {
153                     /* get connection */
154                     connection = ServerConnector.getServerConnection(
155                             sourceConfig.getApplicationConfig());
156                     if(connection.isOpen()){
157                         /* connection is now open */
158                         logger.info("Established connection to application: "
159                             + sourceConfig.getApplicationConfig().getName());
160                         AlertSource.this.connectionOpen();
161                         return;
162                     }
163                 } catch (Exception JavaDoc e) {
164                     logger.info("Failed to establish connection to application: "
165                             + sourceConfig.getApplicationConfig().getName()
166                             + ". message=" + e.getMessage());
167                 }
168
169                 try {
170                     sleep(30 * 1000);
171                 } catch (InterruptedException JavaDoc e) {}
172             }
173         }
174     }
175 }
176
Popular Tags