Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * The MIT License
   *
   * Copyright 2010 Sony Ericsson Mobile Communications. All rights reserved.
   *
   * 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 com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.actions.manual;
 
 
 import java.util.List;

Keeps track of the lifecycle of a GerritEvent.

Author(s):
Robert Sandell <robert.sandell@sonyericsson.com>
 
 public class TriggerMonitor implements GerritEventLifecycleListener {
 
     private List<EventStateevents = new LinkedList<EventState>();

    
Adds the event and a holder for its state to the list of triggered events. And adds this TriggerMonitor as a listener to the event. Unless it doesn't already exists in the list of events.

Parameters:
event the event.
 
     public synchronized void add(PatchsetCreated event) {
         if (!contains(event)) {
             event.addListener(this);
             .add(new EventState(event));
         }
     }

    
Checks to see if the list of triggered events and their states contains the given event.

Parameters:
event the event to check.
Returns:
true if it exests in the list.
See also:
getEvents()
 
     public synchronized boolean contains(PatchsetCreated event) {
         for (EventState state : ) {
             if (state.event != null && state.event.equals(event)) {
                 return true;
             }
         }
         return false;
     }

    
Finds the EventState containing the given event.

Parameters:
event the event.
Returns:
the state, or null if there is none.
 
     private synchronized EventState findState(PatchsetCreated event) {
         for (EventState state : ) {
             if (state.event != null && state.event.equals(event)) {
                 return state;
             }
         }
         return null;
     }
 
     @Override
     public synchronized void triggerScanStarting(PatchsetCreated event) {
         EventState state = findState(event);
         if (state != null) {
             state.triggerScanStarted = true;
         }
     }
 
     @Override
     public synchronized void triggerScanDone(PatchsetCreated event) {
         EventState state = findState(event);
        if (state != null) {
            state.triggerScanDone = true;
        }
    }
    @Override
    public synchronized void projectTriggered(PatchsetCreated eventAbstractProject project) {
        EventState state = findState(event);
        if (state != null) {
            state.addProject(project);
        }
    }
    @Override
    public synchronized void buildStarted(PatchsetCreated eventAbstractBuild build) {
        EventState state = findState(event);
        if (state != null) {
            state.setBuild(build);
        }
    }
    @Override
    public synchronized void buildCompleted(PatchsetCreated eventAbstractBuild build) {
        EventState state = findState(event);
        if (state != null) {
            if (state.allBuildsCompleted && state.isReallyAllBuildsCompleted()) {
                event.removeListener(this);
            }
        }
    }
    @Override
    public synchronized void allBuildsCompleted(PatchsetCreated event) {
        EventState state = findState(event);
        if (state != null) {
            state.allBuildsCompleted = true;
            if (state.isReallyAllBuildsCompleted()) {
                event.removeListener(this);
            }
        }
    }

    
The list of events and their states.

Returns:
a list.
    public synchronized List<EventStategetEvents() {
        return ;
    }

    
An iterator of the list of events and their states.

Returns:
a iterator.
    @SuppressWarnings("unused"//called from Jelly
    public synchronized Iterator<EventStategetEventsIterator() {
        return .iterator();
    }

    
State information about an event.
    public static class EventState {
        private PatchsetCreated event;
        private boolean triggerScanStarted = false;
        private boolean triggerScanDone = false;
        private boolean allBuildsCompleted = false;
        private List<TriggeredItemEntitybuilds;

        
Standard constructor.

Parameters:
event the event to track.
        EventState(PatchsetCreated event) {
            this. = event;
             = new LinkedList<TriggeredItemEntity>();
        }

        
Adds a project to the list of triggered projects.

Parameters:
project the project.
        void addProject(AbstractProject project) {
            .add(new TriggeredItemEntity(project));
        }

        
Sets the started build to an already triggered project.

Parameters:
build the build.
        void setBuild(AbstractBuild build) {
            for (TriggeredItemEntity entity : ) {
                if (entity.equals(build.getProject())) {
                    entity.setBuild(build);
                }
            }
        }

        
Returns the appropriate ball color for the current status of this event and its builds.

Returns:
the path to the image of the ball.
See also:
hudson.model.Result.color
hudson.model.BallColor.getImage()
        @SuppressWarnings("unused"//called from jelly
        public String getBallColor() {
            if (!) {
                return ..getImage();
            } else if (!) {
                return ..getImage();
            } else if (isUnTriggered()) {
                return ..getImage();
            } else {
                Result result = getLeastFavorableResult();
                if (result != null) {
                    return result.color.getImage();
                } else {
                    return ..getImage();
                }
            }
        }

        
Gives the least favorable hudson.model.Run.getResult() in the list of build, if there is any results.

Returns:
the result or null if there is none.
        private Result getLeastFavorableResult() {
            Result least = null;
            for (TriggeredItemEntity entity : ) {
                if (entity.getBuild() != null && entity.getBuild().getResult() != null) {
                    Result tmp = entity.getBuild().getResult();
                    if (least == null) {
                        least = tmp;
                    } else {
                        least = least.combine(tmp);
                    }
                }
            }
            return least;
        }

        
If no active triggers where interested in this event. Determined by: isTriggerScanDone() && getBuilds().size() <= 0 unless not isTriggerScanStarted().

Returns:
true if so.
        public boolean isUnTriggered() {
            if (!) {
                return false;
            } else {
                return  && .size() <= 0;
            }
        }

        
The event.

Returns:
the event.
        public PatchsetCreated getEvent() {
            return ;
        }

        
If the active triggers in the system has started to be notified.

Returns:
true if so.
        public boolean isTriggerScanStarted() {
            return ;
        }

        
If all the active triggers in the system has been notified about this event.

Returns:
true if so.
        public boolean isTriggerScanDone() {
            return ;
        }

        
If all builds started by this event has completed.

Returns:
true if so.
        public boolean isAllBuildsCompleted() {
            return ;
        }

        
Gets the builds that has started for this event.

Returns:
the builds.
        public List<TriggeredItemEntitygetBuilds() {
            return ;
        }

        
Goes through the list of builds and checks if anyone is still building. Even though the event TriggerMonitor.allBuildsCompleted(com.sonyericsson.hudson.plugins.gerrit.gerritevents.dto.events.PatchsetCreated) has been called that only applies to non silent builds, an extra check is needed.

Returns:
true if all builds has completed.
        public boolean isReallyAllBuildsCompleted() {
            for (TriggeredItemEntity entity : ) {
                if (entity.getBuild() == null || !entity.getBuild().isLogUpdated()) {
                    return false;
                }
            }
            return true;
        }
    }
New to GrepCode? Check out our FAQ X