Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
BEGIN LICENSE BLOCK ***** Version: CPL 1.0/GPL 2.0/LGPL 2.1 The contents of this file are subject to the Common Public License Version 1.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. Copyright (C) 2007-2011 Nick Sieger <> Copyright (C) 2009 Joseph LaFata <> Alternatively, the contents of this file may be used under the terms of either of the GNU General Public License Version 2 or later (the "GPL"), or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which case the provisions of the GPL or the LGPL are applicable instead of those above. If you wish to allow use of your version of this file only under the terms of either the GPL or the LGPL, and not to allow others to use your version of this file under the terms of the CPL, indicate your decision by deleting the provisions above and replace them with the notice and other provisions required by the GPL or the LGPL. If you do not delete the provisions above, a recipient may use your version of this file under the terms of any one of the CPL, the GPL or the LGPL. END LICENSE BLOCK ***
 import org.jruby.Ruby;
 public class Runtime implements RuntimeMBean {
     private final SoftReference<Rubyruby;
     public Runtime(Ruby ruby) {
         this. = new SoftReference<Ruby>(ruby);
     public int getExceptionCount() {
         return .get().getExceptionCount();
     public int getBacktraceCount() {
         return .get().getBacktraceCount();
     public int getCallerCount() {
         return .get().getCallerCount();
     public String threadDump() {
         return dumpThreads(.);
     public String rawThreadDump() {
         return dumpThreads(.);
     public String fullThreadDump() {
         return dumpThreads(.);
     public String dumpThreads(Gather gather) {
         Ruby ruby = this..get();
         RubyThread[] thrs = ruby.getThreadService().getActiveRubyThreads();
         StringWriter sw = new StringWriter();
         PrintWriter pw = new PrintWriter(sw);
         pw.println("All threads known to Ruby instance " + ruby.hashCode());
         for (RubyThread th : thrs) {
         return sw.toString();
     private static void dumpThread(Ruby rubyRubyThread thGather gatherPrintWriter pw) {
         pw.println("Thread: " + th.getNativeThread().getName());
         ThreadContext tc = th.getContext();
         if (tc != null) {
             RubyException exc = new RubyException(rubyruby.getRuntimeError(), "thread dump");
             exc.setBacktraceData(gather.getBacktraceData(tcth.getNativeThread().getStackTrace(), true));
         } else {
            pw.println("    [no longer alive]");
    public String executeRuby(final String code) {
        final String[] result = new String[1];
        Thread t = new Thread() {
            public void run() {
                try {
                    result[0] = .get().evalScriptlet(code).toString();
                } catch (RaiseException re) {
                    result[0] = .get().getInstanceConfig().getTraceType().printBacktrace(re.getException(), false);
                } catch (Throwable t) {
                    StringWriter sw = new StringWriter();
                    t.printStackTrace(new PrintWriter(sw));
                    result[0] = sw.toString();
        try {
        } catch (InterruptedException ie) {
            // ignore
        return result[0];
New to GrepCode? Check out our FAQ X