Source Code Cross Referenced for Monitor.java in  » JMX » jfoxmx » javax » management » monitor » 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 » jfoxmx » javax.management.monitor 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* JFox, the OpenSource J2EE Application Server
002:         *
003:         * Copyright (C) 2002 huihoo.org
004:         * Distributable under GNU LGPL license
005:         * See the GNU Lesser General Public License for more details.
006:         */
007:
008:        package javax.management.monitor;
009:
010:        import java.util.Timer;
011:        import java.util.TimerTask;
012:        import java.util.Iterator;
013:        import java.util.HashMap;
014:        import java.util.Map;
015:        import javax.management.NotificationBroadcasterSupport;
016:        import javax.management.MBeanRegistration;
017:        import javax.management.ObjectName;
018:        import javax.management.MBeanServer;
019:        import javax.management.InstanceNotFoundException;
020:        import javax.management.AttributeNotFoundException;
021:        import javax.management.MBeanException;
022:        import javax.management.ReflectionException;
023:
024:        /**
025:         * Defines the common part to all monitor MBeans.
026:         * A monitor MBean monitors values of an attribute in an observed MBean. The
027:         * observed attribute is monitored at intervals specified by the granularity
028:         * period. A gauge value (derived gauge) is derived from the values of
029:         * the observed attribute.
030:         * 
031:         * @author <a href="mailto:young_yy@hotmail.org">Young Yang</a>
032:         */
033:
034:        public abstract class Monitor extends NotificationBroadcasterSupport
035:                implements  MonitorMBean, MBeanRegistration {
036:
037:            //  private ObjectName observedObject = null;
038:            protected Map observedObjects = new HashMap();
039:
040:            private String observedAttribute = null;
041:            private long granularityPeriod = 10000L;
042:            private MBeanServer server = null;
043:
044:            private volatile boolean isActive = false;
045:            private volatile long sequenceNumber = 0L;
046:
047:            private Timer timer = null;
048:            private MonitorTask task = null;
049:
050:            public Monitor() {
051:
052:            }
053:
054:            public ObjectName preRegister(MBeanServer mbeanserver,
055:                    ObjectName objectname) throws Exception {
056:                server = mbeanserver;
057:                if (objectname == null)
058:                    objectname = new ObjectName(server.getDefaultDomain(),
059:                            "Service", this .toString());
060:                return objectname;
061:            }
062:
063:            public void postRegister(Boolean value) {
064:
065:            }
066:
067:            public void preDeregister() throws Exception {
068:                stop();
069:            }
070:
071:            public void postDeregister() {
072:
073:            }
074:
075:            public abstract void addObservedObject(ObjectName objectName)
076:                    throws java.lang.IllegalArgumentException;
077:
078:            //  {
079:            //    if(objectName == null) throw new IllegalArgumentException("The object to observe cannot be null.");
080:            //    observedObjects.add(objectName);
081:            //  }
082:
083:            public ObjectName[] getObservedObjects() {
084:                return (ObjectName[]) observedObjects.keySet().toArray(
085:                        new ObjectName[0]);
086:            }
087:
088:            public boolean containsObservedObject(ObjectName objectName) {
089:                return observedObjects.containsKey(objectName);
090:            }
091:
092:            public void removeObservedObject(ObjectName objectName) {
093:                observedObjects.remove(objectName);
094:            }
095:
096:            public String getObservedAttribute() {
097:                return observedAttribute;
098:            }
099:
100:            public void setObservedAttribute(String attribute)
101:                    throws IllegalArgumentException {
102:                if (attribute == null)
103:                    throw new IllegalArgumentException(
104:                            "The attribute to observe cannot be null.");
105:                observedAttribute = attribute;
106:            }
107:
108:            public long getGranularityPeriod() {
109:                return granularityPeriod;
110:            }
111:
112:            public void setGranularityPeriod(long period)
113:                    throws IllegalArgumentException {
114:                if (period <= 0L)
115:                    throw new IllegalArgumentException(
116:                            "The granularity period must be greater than zero.");
117:                granularityPeriod = period;
118:            }
119:
120:            public boolean isActive() {
121:                return isActive;
122:            }
123:
124:            public abstract void start();
125:
126:            public abstract void stop();
127:
128:            protected void sendNotification(String type, long timeStamp,
129:                    String message, Object derivedValue, Object trigger,
130:                    ObjectName observedObject) {
131:                sequenceNumber = sequenceNumber + 1L;
132:                MonitorNotification notification = new MonitorNotification(
133:                        type, this , sequenceNumber, timeStamp, message,
134:                        observedObject, observedAttribute, derivedValue,
135:                        trigger);
136:                super 
137:                        .sendNotification(((javax.management.Notification) (notification)));
138:            }
139:
140:            protected void _start() {
141:                if (isActive)
142:                    return;
143:                isActive = true;
144:                timer = new Timer();
145:                task = new MonitorTask();
146:                timer.schedule(task, granularityPeriod, granularityPeriod);
147:            }
148:
149:            protected void _stop() {
150:                if (!isActive)
151:                    return;
152:                isActive = false;
153:                task.cancel();
154:                timer.cancel();
155:                // clear the Observed Objects
156:                observedObjects.clear();
157:            }
158:
159:            protected MonitorData getMonitorData(ObjectName observedObject) {
160:                return (MonitorData) observedObjects.get(observedObject);
161:            }
162:
163:            // implemented by every entity monitor
164:            protected abstract void doMonitor(ObjectName observedObject,
165:                    Object value);
166:
167:            // inner class MonitorTask for scheduled
168:            private class MonitorTask extends TimerTask {
169:
170:                public void run() {
171:                    if (!isActive)
172:                        return;
173:
174:                    //      if(getObservedObject() == null) {
175:                    //        sendNotification(MonitorNotification.OBSERVED_OBJECT_ERROR,System.currentTimeMillis(),"The observed object must be not null",null,null);
176:                    //        return;
177:                    //      }
178:                    for (Iterator iter = observedObjects.keySet().iterator(); iter
179:                            .hasNext();) {
180:                        ObjectName observedObject = (ObjectName) iter.next();
181:                        //        System.out.println("MonitorTask.run()" + observedObject);
182:                        if (getObservedAttribute() == null) {
183:                            sendNotification(
184:                                    MonitorNotification.OBSERVED_ATTRIBUTE_ERROR,
185:                                    System.currentTimeMillis(),
186:                                    "The observed attribute must be not null",
187:                                    null, null, observedObject);
188:                            return;
189:                        }
190:                        try {
191:                            Object value = server.getAttribute(observedObject,
192:                                    observedAttribute);
193:                            // call back
194:                            doMonitor(observedObject, value);
195:                        } catch (InstanceNotFoundException e) {
196:                            e.printStackTrace();
197:                            sendNotification(
198:                                    MonitorNotification.OBSERVED_OBJECT_ERROR,
199:                                    System.currentTimeMillis(),
200:                                    "The observed object must be registered in the MBean server.",
201:                                    null, null, observedObject);
202:                        } catch (AttributeNotFoundException e) {
203:                            e.printStackTrace();
204:                            sendNotification(
205:                                    MonitorNotification.OBSERVED_ATTRIBUTE_ERROR,
206:                                    System.currentTimeMillis(),
207:                                    "The observed attribute must be accessible in the observed object.",
208:                                    null, null, observedObject);
209:                        } catch (MBeanException e) {
210:                            e.printStackTrace();
211:                            sendNotification(MonitorNotification.RUNTIME_ERROR,
212:                                    System.currentTimeMillis(), e
213:                                            .getTargetException().getMessage(),
214:                                    null, null, observedObject);
215:                        } catch (ReflectionException e) {
216:                            e.printStackTrace();
217:                            sendNotification(
218:                                    MonitorNotification.OBSERVED_ATTRIBUTE_ERROR,
219:                                    System.currentTimeMillis(), e
220:                                            .getTargetException().getMessage(),
221:                                    null, null, observedObject);
222:                        } catch (Exception e) {
223:                            e.printStackTrace();
224:                            sendNotification(MonitorNotification.RUNTIME_ERROR,
225:                                    System.currentTimeMillis(), e.getMessage(),
226:                                    null, null, observedObject);
227:                        }
228:                    }
229:                }
230:            }
231:        }
232:
233:        abstract class MonitorData {
234:            protected ObjectName objectName;
235:            protected long derivedGaugeTimestamp = System.currentTimeMillis();
236:
237:            public MonitorData(ObjectName objectName) {
238:                if (objectName == null)
239:                    throw new IllegalArgumentException(
240:                            "The object to observe cannot be null.");
241:                this .objectName = objectName;
242:            }
243:
244:            public ObjectName getObjectName() {
245:                return objectName;
246:            }
247:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.