001: /**
002: * Copyright (c) 2004-2005 jManage.org
003: *
004: * This is a free software; you can redistribute it and/or
005: * modify it under the terms of the license at
006: * http://www.jmanage.org.
007: *
008: * Unless required by applicable law or agreed to in writing, software
009: * distributed under the License is distributed on an "AS IS" BASIS,
010: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011: * See the License for the specific language governing permissions and
012: * limitations under the License.
013: */package org.jmanage.core.alert;
014:
015: import org.jmanage.core.config.AlertSourceConfig;
016: import org.jmanage.core.management.ServerConnection;
017: import org.jmanage.core.management.ServerConnector;
018: import org.jmanage.core.management.ConnectionFailedException;
019: import org.jmanage.core.util.Loggers;
020:
021: import java.util.logging.Logger;
022: import java.util.logging.Level;
023: import java.io.IOException;
024:
025: /**
026: *
027: * Date: Jul 1, 2005
028: * @author Rakesh Kalra
029: */
030: public abstract class AlertSource {
031:
032: private static final Logger logger = Loggers
033: .getLogger(AlertSource.class);
034:
035: protected final AlertSourceConfig sourceConfig;
036: protected AlertHandler handler;
037: protected ServerConnection connection;
038: protected String alertId;
039: protected String alertName;
040: private boolean stopThreads = false;
041:
042: public AlertSource(AlertSourceConfig sourceConfig) {
043: assert sourceConfig != null;
044: this .sourceConfig = sourceConfig;
045: }
046:
047: /** Called by the ConnectionMonitor thread, when the connection goes down*/
048: private void connectionClosed() {
049: try {
050: this .unregisterInternal();
051: } catch (Exception e) {
052: logger.severe("Error unregistering for monitoring. error="
053: + e.getMessage());
054: }
055: /* close the connection */
056: closeConnection();
057:
058: /* need to re-establish the connection when the server comes up */
059: new EstablishConnection().start();
060: }
061:
062: private void closeConnection() {
063: try {
064: connection.close();
065: } catch (IOException e) {
066: logger.log(Level.WARNING,
067: "Error while closing connection. error: "
068: + e.getMessage());
069: }
070: }
071:
072: private void connectionOpen() {
073: try {
074: /* start monitoring */
075: registerInternal();
076: logger.info("Registered for alert: " + alertName
077: + ", application: "
078: + sourceConfig.getApplicationConfig().getName());
079: } catch (Exception e) {
080: /* something happen while registering for alert. There is no point
081: retrying. Just log the error and continue */
082: logger.log(Level.SEVERE, "Error registering alert: "
083: + alertName, e);
084: /* close the connection that was openend*/
085: closeConnection();
086: return;
087: }
088: /* start the connection monitoring thread */
089: new ConnectionMonitor().start();
090: }
091:
092: public void register(AlertHandler handler, String alertId,
093: String alertName) {
094: assert this .handler == null;
095: assert connection == null;
096:
097: this .handler = handler;
098: this .alertId = alertId;
099: 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 e) {
113: logger.log(Level.WARNING,
114: "Error while closing connection. error: "
115: + e.getMessage());
116: }
117:
118: logger.info("Un-registered for alert: " + alertName
119: + ", application: "
120: + sourceConfig.getApplicationConfig().getName());
121:
122: connection = null;
123: handler = null;
124: }
125:
126: protected abstract void registerInternal();
127:
128: protected abstract void unregisterInternal();
129:
130: private class ConnectionMonitor extends Thread {
131:
132: public void run() {
133: while (!stopThreads) {
134: if (!connection.isOpen()) {
135: /* connection went down */
136: logger.warning("Connection lost to application: "
137: + sourceConfig.getApplicationConfig()
138: .getName());
139: AlertSource.this .connectionClosed();
140: return;
141: }
142: try {
143: sleep(500);
144: } catch (InterruptedException e) {
145: }
146: }
147: }
148: }
149:
150: private class EstablishConnection extends Thread {
151:
152: public void run() {
153: while (!stopThreads) {
154:
155: try {
156: /* get connection */
157: connection = ServerConnector
158: .getServerConnection(sourceConfig
159: .getApplicationConfig());
160: if (connection.isOpen()) {
161: /* connection is now open */
162: logger
163: .info("Established connection to application: "
164: + sourceConfig
165: .getApplicationConfig()
166: .getName());
167: AlertSource.this .connectionOpen();
168: return;
169: }
170: } catch (Exception e) {
171: logger
172: .info("Failed to establish connection to application: "
173: + sourceConfig
174: .getApplicationConfig()
175: .getName()
176: + ". message="
177: + e.getMessage());
178: }
179:
180: try {
181: sleep(30 * 1000);
182: } catch (InterruptedException e) {
183: }
184: }
185: }
186: }
187: }
|