PageRenderTime 20ms CodeModel.GetById 10ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/hudson-remoting/src/main/java/hudson/remoting/Command.java

http://github.com/hudson/hudson
Java | 90 lines | 31 code | 12 blank | 47 comment | 2 complexity | c5725fcd391192ee0513a96cb99e0780 MD5 | raw file
 1/*
 2 * The MIT License
 3 * 
 4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
 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.remoting;
25
26import java.io.Serializable;
27
28/**
29 * One-way command to be sent over to the remote system and executed there.
30 * This is layer 0, the lower most layer.
31 * <p/>
32 * <p/>
33 * At this level, remoting of class files are not provided, so both {@link Channel}s
34 * need to have the definition of {@link Command}-implementation.
35 *
36 * @author Kohsuke Kawaguchi
37 */
38abstract class Command implements Serializable {
39    /**
40     * This exception captures the stack trace of where the Command object is created.
41     * This is useful for diagnosing the error when command fails to execute on the remote peer.
42     */
43    public final Exception createdAt;
44
45
46    protected Command() {
47        this(true);
48    }
49
50    protected Command(Throwable cause) {
51        this.createdAt = new Source(cause);
52    }
53
54    /**
55     * @param recordCreatedAt If false, skip the recording of where the command is created. This makes the trouble-shooting
56     * and cause/effect correlation hard in case of a failure, but it will reduce the amount of the data
57     * transferred.
58     */
59    protected Command(boolean recordCreatedAt) {
60        if (recordCreatedAt) {
61            this.createdAt = new Source();
62        } else {
63            this.createdAt = null;
64        }
65    }
66
67    /**
68     * Called on a remote system to perform this command.
69     *
70     * @param channel The {@link Channel} of the remote system.
71     */
72    protected abstract void execute(Channel channel);
73
74    private static final long serialVersionUID = 1L;
75
76    private final class Source extends Exception {
77        public Source() {
78        }
79
80        private Source(Throwable cause) {
81            super(cause);
82        }
83
84        public String toString() {
85            return "Command " + Command.this.toString() + " created at";
86        }
87
88        private static final long serialVersionUID = 1L;
89    }
90}