[jboss-cvs] JBoss Profiler SVN: r485 - branches/JBossProfiler2/src/main/org/jboss/profiler/agent.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Oct 22 15:50:55 EDT 2008


Author: jesper.pedersen
Date: 2008-10-22 15:50:55 -0400 (Wed, 22 Oct 2008)
New Revision: 485

Added:
   branches/JBossProfiler2/src/main/org/jboss/profiler/agent/NopProfilerThreadImpl.java
Modified:
   branches/JBossProfiler2/src/main/org/jboss/profiler/agent/Profiler.java
Log:
Add a NOP profiler thread implementation to guard against NPEs in corner use cases

Added: branches/JBossProfiler2/src/main/org/jboss/profiler/agent/NopProfilerThreadImpl.java
===================================================================
--- branches/JBossProfiler2/src/main/org/jboss/profiler/agent/NopProfilerThreadImpl.java	                        (rev 0)
+++ branches/JBossProfiler2/src/main/org/jboss/profiler/agent/NopProfilerThreadImpl.java	2008-10-22 19:50:55 UTC (rev 485)
@@ -0,0 +1,75 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2007-2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.profiler.agent;
+
+/**
+ * The no operation profiler thread implementation
+ * @author Jesper Pedersen <jesper.pedersen at jboss.org>
+ */
+public class NopProfilerThreadImpl implements ProfilerThread {
+
+  /**
+   * Constructor
+   * @param thread The thread
+   */
+  NopProfilerThreadImpl() {
+  }
+
+  /**
+   * Register start time for a method
+   * @param className The class name
+   * @param methodName The method name
+   */
+  public void start(String className, String methodName) {
+  }
+  
+  /**
+   * Register end time for a method
+   * @param className The class name
+   * @param methodName The method name
+   */
+  public void end(String className, String methodName) {
+  }
+  
+  /**
+   * Register start wait time for a method
+   * @param className The class name
+   * @param methodName The method name
+   */
+  public void beginWait(String className, String methodName) {
+  }
+  
+  /**
+   * Register end wait time for a method
+   * @param className The class name
+   * @param methodName The method name
+   */
+  public void endWait(String className, String methodName) {
+  }
+  
+  /**
+   * Class allocation
+   * @param className The class being allocated
+   */
+  public void allocation(String className) {
+  }
+}

Modified: branches/JBossProfiler2/src/main/org/jboss/profiler/agent/Profiler.java
===================================================================
--- branches/JBossProfiler2/src/main/org/jboss/profiler/agent/Profiler.java	2008-10-22 19:49:58 UTC (rev 484)
+++ branches/JBossProfiler2/src/main/org/jboss/profiler/agent/Profiler.java	2008-10-22 19:50:55 UTC (rev 485)
@@ -58,6 +58,9 @@
   /** The class name */
   public static final String CLASSNAME = "org/jboss/profiler/agent/Profiler"; 
 
+  /** NOP profiler thread */
+  private static final NopProfilerThreadImpl NOP_PROFILER_THREAD = new NopProfilerThreadImpl();
+
   /** The profiler threads map */
   private static ConcurrentMap<Long, ProfilerThreadImpl> profilerThreadMap;
 
@@ -303,17 +306,20 @@
    * @return The profiler thread
    */
   public static ProfilerThread getProfilerThread(Thread thread) {
-    ProfilerThreadImpl result = profilerThreadMap.get(Long.valueOf(thread.getId()));
-    if (result == null) {
-      // ProfilerThreadImpl does not yet exist
-      ProfilerThreadImpl newResult = new ProfilerThreadImpl(thread);
-      result = profilerThreadMap.putIfAbsent(Long.valueOf(thread.getId()), newResult);
+    if (running.get()) {
+      ProfilerThreadImpl result = profilerThreadMap.get(Long.valueOf(thread.getId()));
       if (result == null) {
-        // put succeeded, use new value
-        result = newResult;
+        // ProfilerThreadImpl does not yet exist
+        ProfilerThreadImpl newResult = new ProfilerThreadImpl(thread);
+        result = profilerThreadMap.putIfAbsent(Long.valueOf(thread.getId()), newResult);
+        if (result == null) {
+          // put succeeded, use new value
+          result = newResult;
+        }
       }
+      return result;
     }
-    return result;
+    return NOP_PROFILER_THREAD;
   }
 
   /**




More information about the jboss-cvs-commits mailing list