Source Code Cross Referenced for PerfLogging.java in  » J2EE » Pustefix » de » schlund » pfixxml » perflogging » 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 » J2EE » Pustefix » de.schlund.pfixxml.perflogging 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Created on 08.06.2005
003:         *
004:         * TODO To change the template for this generated file go to
005:         * Window - Preferences - Java - Code Style - Code Templates
006:         */
007:        package de.schlund.pfixxml.perflogging;
008:
009:        import java.lang.management.ManagementFactory;
010:        import java.util.Map;
011:        import java.util.Properties;
012:
013:        import javax.management.MBeanServer;
014:        import javax.management.Notification;
015:        import javax.management.NotificationBroadcasterSupport;
016:        import javax.management.ObjectName;
017:
018:        import org.apache.log4j.Logger;
019:
020:        /**
021:         * @author jh
022:         *
023:         */
024:        public class PerfLogging extends NotificationBroadcasterSupport
025:                implements  PerfLoggingMBean {
026:
027:            private static PerfLogging instance = new PerfLogging();
028:            private final static Logger LOG = Logger
029:                    .getLogger(PerfLogging.class);
030:            private String PROP_BUFFER_SIZE = "perflogging.buffersize";
031:            private String PROP_ENABLED = "perflogging.enabled";
032:            private String PROP_AUTOSTART = "perflogging.autostart";
033:            private String PROP_OFFER_MAX_WAIT = "perflogging.offermaxwait";
034:
035:            private String OFF = "0";
036:            private String ON = "1";
037:            private int DEFAULT_BUFFER_SIZE = 1000;
038:            private int DEFAULT_OFFER_MAX_WAIT = 5;
039:            private boolean perfLoggingEnabled = false;
040:            private boolean perfActive = false;
041:            private BoundedBufferWrapper boundedBuffer;
042:            private PerfEventTakeThread perfEventTakeThread;
043:            private long seqNo;
044:
045:            private PerfLogging() {
046:            }
047:
048:            public static PerfLogging getInstance() {
049:                return instance;
050:            }
051:
052:            public void init(Properties props) {
053:
054:                LOG.info("Perflogging init");
055:
056:                String prop_enabled = props.getProperty(PROP_ENABLED, OFF);
057:                if (prop_enabled.equals(ON)) {
058:                    perfLoggingEnabled = true;
059:                } else {
060:                    perfLoggingEnabled = false;
061:                }
062:                LOG.info("Perflogging enabled: " + perfLoggingEnabled);
063:                if (!perfLoggingEnabled)
064:                    return;
065:
066:                String prop_buffs = props.getProperty(PROP_BUFFER_SIZE);
067:                int size = 0;
068:                if (prop_buffs == null || prop_buffs.length() < 1) {
069:                    LOG.warn("Property " + PROP_BUFFER_SIZE
070:                            + " not found. Using default: "
071:                            + DEFAULT_BUFFER_SIZE);
072:                    size = DEFAULT_BUFFER_SIZE;
073:                } else {
074:                    size = Integer.parseInt(prop_buffs);
075:                }
076:
077:                String prop_offerwait = props.getProperty(PROP_OFFER_MAX_WAIT);
078:                int wait = 0;
079:                if (prop_offerwait == null || prop_offerwait.length() < 1) {
080:                    LOG.warn("Property " + PROP_OFFER_MAX_WAIT
081:                            + " not found. Using default: "
082:                            + DEFAULT_OFFER_MAX_WAIT);
083:                    wait = DEFAULT_OFFER_MAX_WAIT;
084:                } else {
085:                    wait = Integer.parseInt(prop_offerwait);
086:                }
087:                boundedBuffer = new BoundedBufferWrapper(size, wait);
088:
089:                String prop_autostart = props.getProperty(PROP_AUTOSTART, OFF);
090:
091:                if (LOG.isInfoEnabled()) {
092:                    StringBuffer sb = new StringBuffer();
093:                    sb.append("After init: \n").append(
094:                            "Enabled: " + perfLoggingEnabled + "\n").append(
095:                            "Active: " + perfActive + "\n").append(
096:                            "Buffersize: " + size + "\n").append(
097:                            "Bufferwait: " + wait + "\n");
098:                    LOG.info(sb.toString());
099:
100:                }
101:
102:                if (prop_autostart.equals(ON)) {
103:                    activatePerflogging();
104:                } else {
105:                    perfActive = false;
106:                }
107:
108:                try {
109:                    MBeanServer mbeanServer = ManagementFactory
110:                            .getPlatformMBeanServer();
111:                    ObjectName objectName = new ObjectName(
112:                            "Pustefix:type=PerfLogging");
113:                    mbeanServer.registerMBean(this , objectName);
114:                } catch (Exception x) {
115:                    LOG.error("Can't register PerfLogging MBean!", x);
116:                }
117:
118:            }
119:
120:            public boolean isPerfLogggingEnabled() {
121:                return perfLoggingEnabled;
122:            }
123:
124:            public boolean isPerfLoggingActive() {
125:                return perfActive;
126:            }
127:
128:            public synchronized void activatePerflogging() {
129:                if (!perfLoggingEnabled) {
130:                    LOG.warn("Perflogging is disabled");
131:                    return;
132:                }
133:                if (!perfActive) {
134:                    LOG.info("Activating perflogging");
135:                    boundedBuffer.init();
136:                    PerfEventPut.getInstance().setBuffer(boundedBuffer);
137:                    startPerfEventTakeThread();
138:                    perfActive = true;
139:                    LOG.info("Perflogging now active");
140:                } else {
141:                    LOG.info("perflogging already active");
142:                }
143:            }
144:
145:            public synchronized String inactivatePerflogging() {
146:                if (!perfLoggingEnabled) {
147:                    LOG.warn("Perflogging is disabled");
148:                    return null;
149:                }
150:                if (perfActive) {
151:                    LOG.info("Inactivating perflogging");
152:                    perfActive = false;
153:                    String xml = PerfStatistic.getInstance().toXML();
154:                    PerfStatistic.getInstance().reset();
155:                    stopPerfEventTakeThread();
156:                    boundedBuffer.reset();
157:                    return xml;
158:                } else {
159:                    LOG.info("perflogging already inactive");
160:                    return null;
161:                }
162:            }
163:
164:            public synchronized Map<String, Map<String, int[]>> inactivatePerfloggingMap() {
165:                if (!perfLoggingEnabled) {
166:                    LOG.warn("Perflogging is disabled");
167:                    return null;
168:                }
169:                if (perfActive) {
170:                    LOG.info("Inactivating perflogging");
171:                    perfActive = false;
172:                    Map<String, Map<String, int[]>> map = PerfStatistic
173:                            .getInstance().toMap();
174:                    PerfStatistic.getInstance().reset();
175:                    stopPerfEventTakeThread();
176:                    boundedBuffer.reset();
177:                    return map;
178:                } else {
179:                    LOG.info("perflogging already inactive");
180:                    return null;
181:                }
182:            }
183:
184:            private void startPerfEventTakeThread() {
185:                LOG.info("Starting new Take-Thread for Buffer");
186:                perfEventTakeThread = new PerfEventTakeThread(boundedBuffer);
187:                perfEventTakeThread.start();
188:            }
189:
190:            private void stopPerfEventTakeThread() {
191:                LOG.info("Interrupting existing Take-Thread for Buffer");
192:                perfEventTakeThread.interrupt();
193:            }
194:
195:            //accessible via JMX:
196:
197:            public synchronized boolean isPerfLoggingEnabled() {
198:                return isPerfLogggingEnabled();
199:            }
200:
201:            public synchronized boolean isPerfLoggingRunning() {
202:                return isPerfLoggingActive();
203:            }
204:
205:            public synchronized void startPerfLogging() {
206:                if (!isPerfLoggingActive()) {
207:                    try {
208:                        activatePerflogging();
209:                    } finally {
210:                        if (isPerfLoggingActive()) {
211:                            Notification n = new Notification(
212:                                    "PerfLogging.started", this , seqNo++,
213:                                    System.currentTimeMillis(),
214:                                    "Started performance logging");
215:                            sendNotification(n);
216:                        }
217:                    }
218:                } else
219:                    throw new IllegalStateException(
220:                            "Performance logging is already running.");
221:            }
222:
223:            public synchronized String stopPerfLogging() {
224:                if (isPerfLoggingActive()) {
225:                    try {
226:                        return inactivatePerflogging();
227:                    } finally {
228:                        if (!isPerfLoggingActive()) {
229:                            Notification n = new Notification(
230:                                    "PerfLogging.stopped", this , seqNo++,
231:                                    System.currentTimeMillis(),
232:                                    "Stopped performance logging");
233:                            sendNotification(n);
234:                        }
235:                    }
236:                } else
237:                    throw new IllegalStateException(
238:                            "Performance logging isn't running.");
239:            }
240:
241:            public synchronized Map<String, Map<String, int[]>> stopPerfLoggingMap() {
242:                if (isPerfLoggingActive()) {
243:                    try {
244:                        return inactivatePerfloggingMap();
245:                    } finally {
246:                        if (!isPerfLoggingActive()) {
247:                            Notification n = new Notification(
248:                                    "PerfLogging.stopped", this , seqNo++,
249:                                    System.currentTimeMillis(),
250:                                    "Stopped performance logging");
251:                            sendNotification(n);
252:                        }
253:                    }
254:                } else
255:                    throw new IllegalStateException(
256:                            "Performance logging isn't running.");
257:            }
258:
259:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.