PageRenderTime 49ms CodeModel.GetById 15ms app.highlight 28ms RepoModel.GetById 1ms app.codeStats 1ms

/hudson-core/src/main/java/hudson/util/RemotingDiagnostics.java

http://github.com/hudson/hudson
Java | 203 lines | 135 code | 21 blank | 47 comment | 7 complexity | 5a204d8a1c2df9e861455e884c63a08e MD5 | raw file
  1/*
  2 * The MIT License
  3 * 
  4 * Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi, CloudBees, Inc.
  5 * 
  6 * Permission is hereby granted, free of charge, to any person obtaining a copy
  7 * of this software and associated documentation files (the "Software"), to deal
  8 * in the Software without restriction, including without limitation the rights
  9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 10 * copies of the Software, and to permit persons to whom the Software is
 11 * furnished to do so, subject to the following conditions:
 12 * 
 13 * The above copyright notice and this permission notice shall be included in
 14 * all copies or substantial portions of the Software.
 15 * 
 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 22 * THE SOFTWARE.
 23 */
 24package hudson.util;
 25
 26import groovy.lang.GroovyShell;
 27import hudson.FilePath;
 28import hudson.Functions;
 29import hudson.model.Hudson;
 30import hudson.remoting.Callable;
 31import hudson.remoting.DelegatingCallable;
 32import hudson.remoting.VirtualChannel;
 33import hudson.security.AccessControlled;
 34import org.kohsuke.stapler.StaplerRequest;
 35import org.kohsuke.stapler.StaplerResponse;
 36import org.kohsuke.stapler.WebMethod;
 37
 38import javax.management.JMException;
 39import javax.management.MBeanServer;
 40import javax.management.ObjectName;
 41import java.io.File;
 42import java.io.IOException;
 43import java.io.PrintWriter;
 44import java.io.StringWriter;
 45import java.lang.management.ManagementFactory;
 46import java.lang.management.ThreadInfo;
 47import java.util.Collections;
 48import java.util.LinkedHashMap;
 49import java.util.Map;
 50import java.util.TreeMap;
 51
 52/**
 53 * Various remoting operations related to diagnostics.
 54 *
 55 * <p>
 56 * These code are useful wherever {@link VirtualChannel} is used, such as master, slaves, Maven JVMs, etc.
 57 *
 58 * @author Kohsuke Kawaguchi
 59 * @since 1.175
 60 */
 61public final class RemotingDiagnostics {
 62    public static Map<Object,Object> getSystemProperties(VirtualChannel channel) throws IOException, InterruptedException {
 63        if(channel==null)
 64            return Collections.<Object,Object>singletonMap("N/A","N/A");
 65        return channel.call(new GetSystemProperties());
 66    }
 67
 68    private static final class GetSystemProperties implements Callable<Map<Object,Object>,RuntimeException> {
 69        public Map<Object,Object> call() {
 70            return new TreeMap<Object,Object>(System.getProperties());
 71        }
 72        private static final long serialVersionUID = 1L;
 73    }
 74
 75    public static Map<String,String> getThreadDump(VirtualChannel channel) throws IOException, InterruptedException {
 76        if(channel==null)
 77            return Collections.singletonMap("N/A","N/A");
 78        return channel.call(new GetThreadDump());
 79    }
 80
 81    private static final class GetThreadDump implements Callable<Map<String,String>,RuntimeException> {
 82        public Map<String,String> call() {
 83            Map<String,String> r = new LinkedHashMap<String,String>();
 84            try {
 85                ThreadInfo[] data = Functions.getThreadInfos();
 86                Functions.ThreadGroupMap map = Functions.sortThreadsAndGetGroupMap(data);
 87                for (ThreadInfo ti : data)
 88                    r.put(ti.getThreadName(),Functions.dumpThreadInfo(ti,map));
 89            } catch (LinkageError _) {
 90                // not in JDK6. fall back to JDK5
 91                r.clear();
 92                for (Map.Entry<Thread,StackTraceElement[]> t : Functions.dumpAllThreads().entrySet()) {
 93                    StringBuilder buf = new StringBuilder();
 94                    for (StackTraceElement e : t.getValue())
 95                        buf.append(e).append('\n');
 96                    r.put(t.getKey().getName(),buf.toString());
 97                }
 98            }
 99            return r;
100        }
101        private static final long serialVersionUID = 1L;
102    }
103
104    /**
105     * Executes Groovy script remotely.
106     */
107    public static String executeGroovy(String script, VirtualChannel channel) throws IOException, InterruptedException {
108        return channel.call(new Script(script));
109    }
110
111    private static final class Script implements DelegatingCallable<String,RuntimeException> {
112        private final String script;
113        private transient ClassLoader cl;
114
115        private Script(String script) {
116            this.script = script;
117            cl = getClassLoader();
118        }
119
120        public ClassLoader getClassLoader() {
121            return Hudson.getInstance().getPluginManager().uberClassLoader;
122        }
123
124        public String call() throws RuntimeException {
125            // if we run locally, cl!=null. Otherwise the delegating classloader will be available as context classloader.
126            if (cl==null)       cl = Thread.currentThread().getContextClassLoader();
127            GroovyShell shell = new GroovyShell(cl);
128
129            StringWriter out = new StringWriter();
130            PrintWriter pw = new PrintWriter(out);
131            shell.setVariable("out", pw);
132            try {
133                Object output = shell.evaluate(script);
134                if(output!=null)
135                pw.print(output);
136            } catch (Throwable t) {
137                t.printStackTrace(pw);
138            }
139            return out.toString();
140        }
141    }
142
143    /**
144     * Obtains the heap dump in an HPROF file.
145     */
146    public static FilePath getHeapDump(VirtualChannel channel) throws IOException, InterruptedException {
147        return channel.call(new Callable<FilePath, IOException>() {
148            public FilePath call() throws IOException {
149                final File hprof = File.createTempFile("hudson-heapdump", "hprof");
150                hprof.delete();
151                try {
152                    MBeanServer server = ManagementFactory.getPlatformMBeanServer();
153                    server.invoke(new ObjectName("com.sun.management:type=HotSpotDiagnostic"), "dumpHeap",
154                            new Object[]{hprof.getAbsolutePath(), true}, new String[]{String.class.getName(), boolean.class.getName()});
155
156                    return new FilePath(hprof);
157                } catch (JMException e) {
158                    throw new IOException2(e);
159                }
160            }
161
162            private static final long serialVersionUID = 1L;
163        });
164    }
165
166    /**
167     * Heap dump, exposable to URL via Stapler.
168     *
169     */
170    public static class HeapDump {
171        private final AccessControlled owner;
172        private final VirtualChannel channel;
173
174        public HeapDump(AccessControlled owner, VirtualChannel channel) {
175            this.owner = owner;
176            this.channel = channel;
177        }
178
179        /**
180         * Obtains the heap dump.
181         */
182        public void doIndex(StaplerResponse rsp) throws IOException {
183            rsp.sendRedirect("heapdump.hprof");
184        }
185
186        @WebMethod(name="heapdump.hprof")
187        public void doHeapDump(StaplerRequest req, StaplerResponse rsp) throws IOException, InterruptedException {
188            owner.checkPermission(Hudson.ADMINISTER);
189            rsp.setContentType("application/octet-stream");
190
191            FilePath dump = obtain();
192            try {
193                dump.copyTo(rsp.getCompressedOutputStream(req));
194            } finally {
195                dump.delete();
196            }
197        }
198
199        public FilePath obtain() throws IOException, InterruptedException {
200            return RemotingDiagnostics.getHeapDump(channel);
201        }
202    }
203}