Coverage Report - com.aurea.maven.plugins.sonic.sdm.QueueMappings
 
Classes in this File Line Coverage Branch Coverage Complexity
QueueMappings
0%
0/23
0%
0/8
1.857
 
 1  
 package com.aurea.maven.plugins.sonic.sdm;
 2  
 
 3  
 import java.util.ArrayList;
 4  
 import java.util.Collections;
 5  
 import java.util.List;
 6  
 
 7  
 import org.apache.maven.plugin.MojoExecutionException;
 8  
 import org.codehaus.plexus.util.xml.Xpp3Dom;
 9  
 
 10  
 /**
 11  
  * A list of queue mappings and the logic to calculate which queues go with which mapping.
 12  
  * 
 13  
  * @author Gerco Dries
 14  
  * 
 15  
  */
 16  
 public class QueueMappings {
 17  
 
 18  
 
 19  
   /**
 20  
    * An ordered list of queue mappings. The first mapping to match a queue "wins".
 21  
    */
 22  
   private final List<QueueMapping> mappings;
 23  
 
 24  
   /**
 25  
    * Create a new, empty QueueMappings.
 26  
    */
 27  0
   public QueueMappings() {
 28  
 
 29  0
     this.mappings = new ArrayList<QueueMapping>();
 30  0
   }
 31  
 
 32  
   /**
 33  
    * Create QueueMappings from the specified dom element. It is expected to contain <code>defaultMapping</code> and
 34  
    * <code>queueMapping</code> elements.
 35  
    * 
 36  
    * @param dom
 37  
    *          The document to read
 38  
    * @param defaultQueuesId
 39  
    *          The id of the queues element to use if there is no defaultMapping definition
 40  
    */
 41  
   public QueueMappings(Xpp3Dom dom, String defaultQueuesId) {
 42  
 
 43  0
     this();
 44  
 
 45  
     // Read all queueMapping elements and add them to the list.
 46  0
     Xpp3Dom[] queueMappingElements = dom.getChildren("queueMapping");
 47  0
     for (Xpp3Dom queueMappingElement : queueMappingElements) {
 48  0
       addMapping(new QueueMapping(queueMappingElement));
 49  
     }
 50  
 
 51  
     // If there is a defaultMapping element, add it now. Otherwise create a new
 52  
     // mapping to map all queues to the specified default queues section.
 53  0
     Xpp3Dom defaultMappingElement = dom.getChild("defaultMapping");
 54  0
     if (defaultMappingElement != null) {
 55  0
       addMapping(new QueueMapping(defaultMappingElement));
 56  
     } else {
 57  0
       addMapping(new QueueMapping(defaultQueuesId, ".*"));
 58  
     }
 59  0
   }
 60  
 
 61  
   public void addMapping(QueueMapping mapping) {
 62  
 
 63  0
     mappings.add(mapping);
 64  0
   }
 65  
 
 66  
   public void removeMapping(QueueMapping mapping) {
 67  
 
 68  0
     mappings.remove(mapping);
 69  0
   }
 70  
 
 71  
   public List<QueueMapping> getMappings() {
 72  
 
 73  0
     return Collections.unmodifiableList(mappings);
 74  
   }
 75  
 
 76  
   /**
 77  
    * Find the Queues element this queue should be added to by evaluating all configured mappings.
 78  
    * 
 79  
    * @param queueName
 80  
    * @return
 81  
    * @throws MojoExecutionException
 82  
    */
 83  
   public QueueMapping getMappingForQueue(String queueName) throws MojoExecutionException {
 84  
 
 85  
     /*
 86  
      * Because the mappings list is ordered, the first one to match the queue name is the correct one. The default
 87  
      * element is always the last one in the list so not matching anything is an unrecoverable error.
 88  
      */
 89  0
     for (QueueMapping mapping : mappings) {
 90  0
       if (mapping.matches(queueName)) {
 91  0
         return mapping;
 92  
       }
 93  0
     }
 94  
 
 95  0
     throw new MojoExecutionException("Unable to find a matching queue mapping for queue " + queueName);
 96  
   }
 97  
 
 98  
   @Override
 99  
   public String toString() {
 100  
 
 101  0
     return "Queue mappings: " + mappings;
 102  
   }
 103  
 }