Source Code Cross Referenced for AlertSource.java in  » JMX » jmanage » org » jmanage » core » alert » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » JMX » jmanage » org.jmanage.core.alert 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.