CauseOfInterruption.java 4.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * The MIT License
 *
 * Copyright (c) 2011, CloudBees, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package jenkins.model;

26
import hudson.console.ModelHyperlinkNote;
27 28 29 30 31 32
import hudson.model.Executor;
import hudson.model.TaskListener;
import hudson.model.User;
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;

K
Kohsuke Kawaguchi 已提交
33
import java.io.Serializable;
34 35
import java.util.Collections;
import javax.annotation.CheckForNull;
36
import javax.annotation.Nonnull;
K
Kohsuke Kawaguchi 已提交
37

38 39 40 41 42 43 44 45 46 47 48 49 50
/**
 * Records why an {@linkplain Executor#interrupt() executor is interrupted}.
 *
 * <h2>View</h2>
 * <tt>summary.groovy/.jelly</tt> should do one-line HTML rendering to be used while rendering
 * "build history" widget, next to the blocking build. By default it simply renders
 * {@link #getShortDescription()} text.
 *
 * <h2>Value equality semantics</h2>
 * <p>
 * Two {@link CauseOfInterruption}s that are {@linkplain Object#equals(Object) equal} will get
 * merged together.
 *
K
Kohsuke Kawaguchi 已提交
51 52 53
 * <h2>Persistence</h2>
 * The implementation should be serializable both in Java serialization and XStream.
 *
54 55 56 57 58 59
 * @author Kohsuke Kawaguchi
 * @since 1.425
 * @see Executor#interrupt(Result, CauseOfInterruption...)
 * @see InterruptedBuildAction
 */
@ExportedBean
K
Kohsuke Kawaguchi 已提交
60
public abstract class CauseOfInterruption implements Serializable {
61 62 63 64 65 66 67 68 69 70 71 72 73 74
    /**
     * Human readable description of why the build is cancelled.
     */
    @Exported
    public abstract String getShortDescription();

    /**
     * Report a line to the listener about this cause.
     */
    public void print(TaskListener listener) {
        listener.getLogger().println(getShortDescription());
    }

    /**
J
Jesse Glick 已提交
75
     * Indicates that the build was interrupted from UI.
76 77
     */
    public static final class UserInterruption extends CauseOfInterruption {
78 79
        
        @Nonnull
80 81
        private final String user;

82
        public UserInterruption(@Nonnull User user) {
83 84 85
            this.user = user.getId();
        }

86
        public UserInterruption(@Nonnull String userId) {
87 88 89
            this.user = userId;
        }

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
        /**
         * Gets ID of the user, who interrupted the build.
         * @return User ID
         * @since TODO
         */
        @Nonnull
        public String getUserId() {
            return user;
        }
        
        /**
         * Gets user, who caused the interruption.
         * @return User instance if it can be located.
         *         Result of {@link User#getUnknown()} otherwise
         */
        @Nonnull
106
        public User getUser() {
107 108 109 110 111 112 113 114 115 116 117
            final User userInstance = getUserOrNull();
            return userInstance != null ? userInstance : User.getUnknown();
        }
        
        /**
         * Gets user, who caused the interruption.
         * @return User or {@code null} if it has not been found
         * @since TODO
         */
        @CheckForNull
        public User getUserOrNull() {
118
            return User.get(user, false, Collections.emptyMap());
119 120 121 122 123 124 125 126
        }

        public String getShortDescription() {
            return Messages.CauseOfInterruption_ShortDescription(user);
        }

        @Override
        public void print(TaskListener listener) {
127
            final User userInstance = getUser();
128
            listener.getLogger().println(
129 130
                Messages.CauseOfInterruption_ShortDescription(
                        userInstance != null ? ModelHyperlinkNote.encodeTo(userInstance) : user));
131 132 133 134 135 136 137 138 139 140 141 142 143
        }

        @Override
        public boolean equals(Object o) {
            if (o == null || getClass() != o.getClass()) return false;
            UserInterruption that = (UserInterruption) o;
            return user.equals(that.user);
        }

        @Override
        public int hashCode() {
            return user.hashCode();
        }
K
Kohsuke Kawaguchi 已提交
144 145

        private static final long serialVersionUID = 1L;
146
    }
K
Kohsuke Kawaguchi 已提交
147 148

    private static final long serialVersionUID = 1L;
149
}