001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: * The Original Software is NetBeans. The Initial Developer of the Original
026: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
027: * Microsystems, Inc. All Rights Reserved.
028: *
029: * If you wish your version of this file to be governed by only the CDDL
030: * or only the GPL Version 2, indicate your decision by adding
031: * "[Contributor] elects to include this software in this distribution
032: * under the [CDDL or GPL Version 2] license." If you do not indicate a
033: * single choice of license, a recipient has the option to distribute
034: * your version of this file under either the CDDL, the GPL Version 2 or
035: * to extend the choice of license to its licensees as provided above.
036: * However, if you add GPL Version 2 code and therefore, elected the GPL
037: * Version 2 license, then the option applies only if the new code is
038: * made subject to such option by the copyright holder.
039: */
040:
041: package org.netbeans.lib.profiler.results;
042:
043: import org.netbeans.lib.profiler.ProfilerClient;
044: import java.util.Collections;
045: import java.util.HashSet;
046: import java.util.Iterator;
047: import java.util.Set;
048: import java.util.logging.Level;
049: import java.util.logging.Logger;
050:
051: /**
052: *
053: * @author Jaroslav Bachorik
054: */
055: public class EventBufferResultsProvider implements
056: ProfilingResultsProvider {
057: //~ Static fields/initializers -----------------------------------------------------------------------------------------------
058:
059: private static final Logger LOGGER = Logger
060: .getLogger(EventBufferResultsProvider.class.getName());
061: private static final EventBufferResultsProvider instance = new EventBufferResultsProvider();
062:
063: //~ Instance fields ----------------------------------------------------------------------------------------------------------
064:
065: private final Set listeners = Collections
066: .synchronizedSet(new HashSet());
067:
068: //~ Constructors -------------------------------------------------------------------------------------------------------------
069:
070: /** Creates a new instance of RawProfilingResultsCollector */
071: private EventBufferResultsProvider() {
072: }
073:
074: //~ Methods ------------------------------------------------------------------------------------------------------------------
075:
076: public static EventBufferResultsProvider getDefault() {
077: return instance;
078: }
079:
080: public void addDispatcher(
081: ProfilingResultsProvider.Dispatcher dispatcher) {
082: listeners.add(dispatcher);
083: }
084:
085: public void dataReady(int buffsize, int instrumentationType) {
086: if (LOGGER.isLoggable(Level.FINEST)) {
087: LOGGER.finest("Profiling data ready"); // NOI18N
088: }
089:
090: byte[] data = new byte[buffsize];
091: System
092: .arraycopy(EventBufferProcessor.buf, 0, data, 0,
093: buffsize);
094: fireProcessData(data, instrumentationType);
095: }
096:
097: public void removeDispatcher(
098: ProfilingResultsProvider.Dispatcher dispatcher) {
099: listeners.remove(dispatcher);
100: }
101:
102: public void shutdown() {
103: if (LOGGER.isLoggable(Level.FINEST)) {
104: LOGGER.finest("Shutting down profiler"); // NOI18N
105: }
106:
107: fireShutdown();
108: }
109:
110: public void startup(ProfilerClient client) {
111: if (LOGGER.isLoggable(Level.FINEST)) {
112: LOGGER.finest("Starting up profiler"); // NOI18N
113: }
114:
115: fireStartup(client);
116: }
117:
118: private void fireProcessData(final byte[] data,
119: final int instrumentationType) {
120: for (Iterator iter = listeners.iterator(); iter.hasNext();) {
121: ProfilingResultsProvider.Dispatcher dispatcher = (ProfilingResultsProvider.Dispatcher) iter
122: .next();
123: dispatcher.dataFrameReceived(data, instrumentationType);
124: }
125: }
126:
127: private void fireShutdown() {
128: for (Iterator iter = listeners.iterator(); iter.hasNext();) {
129: ProfilingResultsProvider.Dispatcher dispatcher = (ProfilingResultsProvider.Dispatcher) iter
130: .next();
131: dispatcher.shutdown();
132: }
133: }
134:
135: private void fireStartup(ProfilerClient client) {
136: for (Iterator iter = listeners.iterator(); iter.hasNext();) {
137: ProfilingResultsProvider.Dispatcher dispatcher = (ProfilingResultsProvider.Dispatcher) iter
138: .next();
139: dispatcher.startup(client);
140: }
141: }
142: }
|