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