Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  The MIT License
   *
   *  Copyright 2010 Sony Ericsson Mobile Communications. All rights reserved.
   *  Copyright 2012 Sony Mobile Communications AB. 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.gerritnotifier.model;
 
 
 
 
 import java.util.List;
Keeps track of what builds have been triggered and if all builds are done for specific events.

Author(s):
Robert Sandell <robert.sandell@sonyericsson.com>
 
 public class BuildMemory {

    
Compares GerritTriggeredEvents using the Object.hashCode() method. This ensures that every event received from Gerrit is kept track of individually.

Author(s):
James E. Blair <jeblair@hp.com>
 
     private class GerritTriggeredEventComparator implements Comparator<GerritTriggeredEvent> {
         @Override
         public int compare(GerritTriggeredEvent o1GerritTriggeredEvent o2) {
             return new Integer(((java.lang.Object)o1).hashCode()).compareTo(
                     new Integer(((java.lang.Object)o2).hashCode()));
         }
     }
 
             new TreeMap<GerritTriggeredEventMemoryImprint>(
                     new GerritTriggeredEventComparator());
     private static final Logger logger = LoggerFactory.getLogger(BuildMemory.class);

    
Gets the memory of a specific event.

Parameters:
event the event.
Returns:
the memory.
 
     public synchronized MemoryImprint getMemoryImprint(GerritTriggeredEvent event) {
         return .get(event);
     }

    
Tells if all triggered builds have started for a specific memory imprint.

Parameters:
event the event.
Returns:
true if it is so.
 
     public synchronized boolean isAllBuildsCompleted(GerritTriggeredEvent event) {
         MemoryImprint pb = .get(event);
         if (pb != null) {
             return pb.isAllBuildsCompleted();
         } else {
             return false;
         }
     }

    
Gets the statistics of started builds for a specific memory imprint.

Parameters:
event the event.
Returns:
the statistics.
    public synchronized BuildsStartedStats getBuildsStartedStats(GerritTriggeredEvent event) {
        MemoryImprint pb = .get(event);
        if (pb != null) {
            return pb.getBuildsStartedStats();
        } else {
            return null;
        }
    }

    
Returns the status report for the given MemoryImprint.

Parameters:
event the event.
Returns:
the status as it is now.
See also:
BuildMemory.MemoryImprint.getStatusReport()
    public synchronized String getStatusReport(GerritTriggeredEvent event) {
        MemoryImprint pb = .get(event);
        if (pb != null) {
            return pb.getStatusReport();
        } else {
            return null;
        }
    }

    
Tells if all triggered builds have started for a specific memory imprint.

Parameters:
event the event.
Returns:
true if it is so.
    public synchronized boolean isAllBuildsStarted(GerritTriggeredEvent event) {
        MemoryImprint pb = .get(event);
        if (pb != null) {
            return pb.isAllBuildsSet();
        } else {
            return false;
        }
    }

    
Sets the memory that a build is completed for an event.

Parameters:
event the event
build the build.
    public synchronized void completed(GerritTriggeredEvent eventAbstractBuild build) {
        MemoryImprint pb = .get(event);
        if (pb == null) {
            //Shoudn't happen but just in case, keep the memory.
            pb = new MemoryImprint(event);
            .put(eventpb);
        }
        pb.set(build.getProject(), buildtrue);
    }

    
Sets the memory that a build has started for an event.

Parameters:
event the event.
build the build.
    public synchronized void started(GerritTriggeredEvent eventAbstractBuild build) {
        MemoryImprint pb = .get(event);
        if (pb == null) {
            //A build should not start for a job that hasn't been registered. Keep the memory anyway.
            pb = new MemoryImprint(event);
            .warn("Build started without being registered first.");
            .put(eventpb);
        }
        pb.set(build.getProject(), build);
    }

    
Adds a new memory about a build that has been/will be triggered.

Parameters:
event the event that triggered it.
project the project that was triggered.
    public synchronized void triggered(GerritTriggeredEvent eventAbstractProject project) {
        MemoryImprint pb = .get(event);
        if (pb == null) {
            pb = new MemoryImprint(event);
            .put(eventpb);
        }
        pb.set(project);
    }

    
Adds a new memory about a build that has been retriggered. If there is an active memory about the provided event, then the project is reset with no build info. Otherwise the memory is recreated from the list of other builds and their result.

Parameters:
event the event to be retriggered.
project the project that has been retriggered.
otherBuilds the list of other builds that was in the "old" memory.
    public synchronized void retriggered(
            GerritTriggeredEvent event,
            AbstractProject project,
            List<AbstractBuildotherBuilds) {
        MemoryImprint pb = .get(event);
        if (pb == null) {
            pb = new MemoryImprint(event);
            .put(eventpb);
            if (otherBuilds != null) {
                //It is a new memory so it wasn't building, let's populate with old build info
                for (AbstractBuild build : otherBuilds) {
                    pb.set(build.getProject(), build, !build.isBuilding());
                }
            }
        }
        pb.reset(project);
    }

    
Removes the memory for the event.

Parameters:
event the event.
    public synchronized void forget(GerritTriggeredEvent event) {
        .remove(event);
    }

    
Updates the com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data.TriggerContext for the event. The cause and build is the "focal point" for the update, but all memory entities will be updated, but only the current context will be com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data.TriggerContext.setThisBuild(hudson.model.AbstractBuild)updated.

Parameters:
cause the cause.
r the build the cause is in.
    public synchronized void updateTriggerContext(GerritCause causeAbstractBuild r) {
        MemoryImprint imprint = getMemoryImprint(cause.getEvent());
        TriggerContext context = cause.getContext();
        context.setThisBuild(r);
        for (MemoryImprint.Entry entry : imprint.getEntries()) {
            if (entry.getBuild() != null && !entry.getBuild().equals(r)) {
                context.addOtherBuild(entry.getBuild());
                updateTriggerContext(entryimprint);
            } else if (entry.getBuild() == null && !entry.getProject().equals(r.getProject())) {
                context.addOtherProject(entry.getProject());
            }
        }
        if (!r.hasntStartedYet() && !r.isBuilding()) {
            try {
                r.save();
            } catch (IOException ex) {
                .error("Could not save build state for build " + rex);
            }
        }
    }

    
Updates the com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data.TriggerContext for the provided entry.

Parameters:
entryToUpdate the entry to update.
imprint the information for the update.
    private synchronized void updateTriggerContext(Entry entryToUpdateMemoryImprint imprint) {
        if (entryToUpdate.getBuild() != null) {
            GerritCause cause = (GerritCause)entryToUpdate.getBuild().getCause(GerritCause.class);
            if (cause != null) {
                TriggerContext context = cause.getContext();
                for (MemoryImprint.Entry ent : imprint.getEntries()) {
                    if (ent.getBuild() != null && !ent.getBuild().equals(entryToUpdate.getBuild())) {
                        context.addOtherBuild(ent.getBuild());
                    } else if (ent.getBuild() == null && !ent.getProject().equals(entryToUpdate.getProject())) {
                        context.addOtherProject(ent.getProject());
                    }
                }
                if (!entryToUpdate.getBuild().hasntStartedYet() && !entryToUpdate.getBuild().isBuilding()) {
                    try {
                        entryToUpdate.getBuild().save();
                    } catch (IOException ex) {
                        .error("Could not save state for build " + entryToUpdate.getBuild(), ex);
                    }
                }
            }
        }
    }

    
Checks in memory if the project is building the event.

Parameters:
event the event.
project the project.
Returns:
true if so.
    public synchronized boolean isBuilding(GerritTriggeredEvent eventAbstractProject project) {
        MemoryImprint pb = .get(event);
        if (pb == null) {
            return false;
        } else {
            for (Entry entry : pb.getEntries()) {
                if (entry.getProject().equals(project)) {
                    if (entry.getBuild() != null) {
                        return !entry.isBuildCompleted();
                    } else {
                        return true;
                    }
                }
            }
            return false;
        }
    }

    
Checks if the provided event exists in this memory.

Parameters:
event the event to look for.
Returns:
true if so.
    public synchronized boolean isBuilding(GerritTriggeredEvent event) {
        MemoryImprint pb = .get(event);
        return pb != null;
    }

    
Returns all started builds in memory for the event.

Parameters:
event the event.
Returns:
the list of builds, or null if there is no memory.
    public synchronized List<AbstractBuildgetBuilds(GerritTriggeredEvent event) {
        MemoryImprint pb = .get(event);
        if (pb != null) {
            List<AbstractBuildlist = new LinkedList<AbstractBuild>();
            for (Entry entry : pb.getEntries()) {
                if (entry.getBuild() != null) {
                    list.add(entry.getBuild());
                }
            }
            return list;
        } else {
            return null;
        }
    }

    
Records the failure message for the given build.

Parameters:
event the event.
r the build that caused the failure.
failureMessage the failure message
    public void setEntryFailureMessage(GerritTriggeredEvent eventAbstractBuild rString failureMessage) {
        MemoryImprint pb = getMemoryImprint(event);
        if (pb != null) {
            Entry entry = pb.getEntry(r.getProject());
            if (entry != null) {
                .info("Recording unsuccessful message for {}: {}"eventfailureMessage);
                entry.setUnsuccessfulMessage(failureMessage);
            }
        }
    }

    
A holder for all builds triggered by one event.
    public static class MemoryImprint {
        private GerritTriggeredEvent event;
        private List<Entrylist = new ArrayList<Entry>();

        
Constructor.

Parameters:
event the event.
        public MemoryImprint(GerritTriggeredEvent event) {
            this. = event;
        }

        
Constructor.

Parameters:
event the event.
project the first project.
        public MemoryImprint(GerritTriggeredEvent eventAbstractProject project) {
            this. = event;
            set(project);
        }

        
The event.

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

        
A list of Project-Build tuple entries.

Returns:
the memory entries.
        public synchronized Entry[] getEntries() {
            return .toArray(new Entry[.size()]);
        }

        
Sets the build to a project or adds the project to the list.

Parameters:
project the project.
build the build.
        protected synchronized void set(AbstractProject projectAbstractBuild build) {
            Entry entry = getEntry(project);
            if (entry == null) {
                entry = new Entry(projectbuild);
                .add(entry);
            } else {
                entry.setBuild(build);
            }
        }

        
Adds the project to the list.

Parameters:
project the project.
        protected synchronized void set(AbstractProject project) {
            Entry entry = getEntry(project);
            if (entry == null) {
                entry = new Entry(project);
                .add(entry);
            }
        }

        
Resets the build info for the project. If the project doesn't exist it would be as if calling set(hudson.model.AbstractProject).

Parameters:
project the project to reset.
        protected synchronized void reset(AbstractProject project) {
            Entry entry = getEntry(project);
            if (entry == null) {
                entry = new Entry(project);
                .add(entry);
            } else {
                entry.setBuild(null);
                entry.setBuildCompleted(false);
            }
        }

        
Sets all the values of an entry and adds it if the project has not been added before.

Parameters:
project the project
build the build
buildCompleted if the build is finished.
        private synchronized void set(AbstractProject projectAbstractBuild buildboolean buildCompleted) {
            Entry entry = getEntry(project);
            if (entry == null) {
                entry = new Entry(projectbuild);
                entry.setBuildCompleted(buildCompleted);
                .add(entry);
            } else {
                if (entry.getBuild() == null) {
                    entry.setBuild(build);
                }
                entry.setBuildCompleted(buildCompleted);
            }
        }

        
Tells if all builds have a value (not null).

Returns:
true if it is so.
        public synchronized boolean isAllBuildsSet() {
            for (Entry entry : ) {
                if (entry.getBuild() == null) {
                    return false;
                }
            }
            return true;
        }

        
Tells if all builds have Completed.

Returns:
true if it is so.
        public synchronized boolean isAllBuildsCompleted() {
            for (Entry entry : ) {
                if (!entry.isBuildCompleted()) {
                    return false;
                }
            }
            return true;
        }

        
Returns a string describing the projects and builds status in this memory. Good for logging.

Returns:
a report.
        public synchronized String getStatusReport() {
            StringBuilder str = new StringBuilder("");
            for (Entry entry : ) {
                if (entry.getProject() != null) {
                    str.append("  Project/Build: [").append(entry.getProject().getName()).append("]");
                    str.append(": [#");
                    if (entry.getBuild() != null) {
                        str.append(entry.getBuild().getNumber());
                        str.append(": ").append(entry.getBuild().getResult());
                    } else {
                        str.append("XX: NULL");
                    }
                    str.append("] Completed: ").append(entry.isBuildCompleted());
                } else {
                    str.append("  Project/Build: MISSING PROJECT!");
                }
                str.append("\n");
            }
            return str.toString();
        }

        
Searches the internal list for an entry with the specified project.

Parameters:
project the project.
Returns:
the entry or null if nothis is found.
        private Entry getEntry(AbstractProject project) {
            for (Entry entry : ) {
                if (entry.getProject().equals(project)) {
                    return entry;
                }
            }
            return null;
        }

        
Gets the statistics about builds started.

Returns:
the stats.
        public synchronized BuildsStartedStats getBuildsStartedStats() {
            int started = 0;
            for (Entry entry : ) {
                if (entry.getBuild() != null) {
                    started++;
                }
            }
            return new BuildsStartedStats(.size(), started);
        }

        
Tells if all builds in the memory were successful.

Returns:
true if it is so, false if not all builds have started or not completed or have any different result than hudson.model.Result.SUCCESS.
        public synchronized boolean whereAllBuildsSuccessful() {
            for (Entry entry : ) {
                if (entry.getBuild() == null) {
                    return false;
                } else if (!entry.isBuildCompleted()) {
                    return false;
                }
                Result buildResult = entry.getBuild().getResult();
                if (buildResult != .) {
                    return false;
                }
            }
            return true;
        }

        
Returns if any started and completed build has the result hudson.model.Result.FAILURE.

Returns:
true if it is so.
        public synchronized boolean whereAnyBuildsFailed() {
            for (Entry entry : ) {
                if (entry.getBuild() != null && entry.isBuildCompleted()
                        && entry.getBuild().getResult() == .) {
                    return true;
                }
            }
            return false;
        }

        
Returns if any started and completed build has the result hudson.model.Result.UNSTABLE.

Returns:
true if it is so.
        public synchronized boolean whereAnyBuildsUnstable() {
            for (Entry entry : ) {
                if (entry.getBuild() != null && entry.isBuildCompleted()
                        && entry.getBuild().getResult() == .) {
                    return true;
                }
            }
            return false;
        }

        
Tells if all builds in the memory were not built.

Returns:
true if it is so, false if not all builds have started or not completed or have any different result than hudson.model.Result.NOT_BUILT.
        public synchronized boolean wereAllBuildsNotBuilt() {
            for (Entry entry : ) {
                if (entry.getBuild() == null) {
                    return false;
                } else if (!entry.isBuildCompleted()) {
                    return false;
                }
                Result buildResult = entry.getBuild().getResult();
                if (buildResult != .) {
                    return false;
                }
            }
            return true;
        }
        //CS IGNORE FinalClass FOR NEXT 5 LINES. REASON: Testability.

        
A project-build entry in the list of a MemoryImprint.
        public static class Entry {
            private AbstractProject project;
            private AbstractBuild build;
            private boolean buildCompleted;
            private String unsuccessfulMessage;

            
Constructor.

Parameters:
project the project
build the build.
            private Entry(AbstractProject projectAbstractBuild build) {
                this. = project;
                this. = build;
                 = false;
            }

            
Constructor.

Parameters:
project the project.
            private Entry(AbstractProject project) {
                this. = project;
                 = false;
            }

            
The Project.

Returns:
the project.
            public AbstractProject getProject() {
                return ;
            }

            
The build of a project.

Returns:
the build.
            public AbstractBuild getBuild() {
                return ;
            }

            
The build of a project.

Parameters:
build the build.
            private void setBuild(AbstractBuild build) {
                this. = build;
            }

            
Sets the unsuccessful message for an entry.

Parameters:
unsuccessfulMessage the message.
            private void setUnsuccessfulMessage(String unsuccessfulMessage) {
                this. = unsuccessfulMessage;
            }

            
Gets the unsuccessful message for an entry.

Returns:
the message.
            public String getUnsuccessfulMessage() {
                return this.;
            }

            
If the build is completed.

Returns:
true if the build is completed.
            public boolean isBuildCompleted() {
                return ;
            }

            
If the build is completed.

Parameters:
buildCompleted true if the build is completed.
            private void setBuildCompleted(boolean buildCompleted) {
                this. = buildCompleted;
            }
        }
    }
New to GrepCode? Check out our FAQ X