Coverage Report - de.smartics.properties.resource.domain.ClassPathEnvironment
 
Classes in this File Line Coverage Branch Coverage Complexity
ClassPathEnvironment
0%
0/40
0%
0/10
1.667
 
 1  
 /*
 2  
  * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package de.smartics.properties.resource.domain;
 17  
 
 18  
 import java.io.Serializable;
 19  
 import java.net.URL;
 20  
 import java.util.ArrayList;
 21  
 import java.util.Collections;
 22  
 import java.util.HashMap;
 23  
 import java.util.List;
 24  
 import java.util.Map;
 25  
 
 26  
 import javax.annotation.CheckForNull;
 27  
 import javax.annotation.concurrent.NotThreadSafe;
 28  
 
 29  
 import org.slf4j.Logger;
 30  
 import org.slf4j.LoggerFactory;
 31  
 
 32  
 import de.smartics.util.lang.Arg;
 33  
 
 34  
 /**
 35  
  * Defines the environment of class path resources to access.
 36  
  */
 37  
 @NotThreadSafe
 38  
 public final class ClassPathEnvironment implements Serializable
 39  
 {
 40  
   // ********************************* Fields *********************************
 41  
 
 42  
   // --- constants ------------------------------------------------------------
 43  
 
 44  
   /**
 45  
    * The class version identifier.
 46  
    */
 47  
   private static final long serialVersionUID = 1L;
 48  
 
 49  
   /**
 50  
    * Reference to the logger for this class.
 51  
    */
 52  0
   private static final Logger LOG = LoggerFactory
 53  
       .getLogger(ClassPathEnvironment.class);
 54  
 
 55  
   // --- members --------------------------------------------------------------
 56  
 
 57  
   /**
 58  
    * The map of artifact IDs (GAV concatenated and separated by colons) to the
 59  
    * artifact references.
 60  
    */
 61  0
   private final Map<String, ArtifactRef> index =
 62  
       new HashMap<String, ArtifactRef>();
 63  
 
 64  
   /**
 65  
    * The list of artifact references found on this class path.
 66  
    *
 67  
    * @serial
 68  
    */
 69  0
   private final List<ArtifactRef> artifactRefs = new ArrayList<ArtifactRef>();
 70  
 
 71  
   // ****************************** Initializer *******************************
 72  
 
 73  
   // ****************************** Constructors ******************************
 74  
 
 75  
   /**
 76  
    * Default constructor.
 77  
    */
 78  
   public ClassPathEnvironment()
 79  0
   {
 80  0
   }
 81  
 
 82  
   /**
 83  
    * Convenience constructor that allows to add artifact references to create an
 84  
    * initial list.
 85  
    *
 86  
    * @param artifactRefs the initial collection of artifact references to add as
 87  
    *          roots.
 88  
    */
 89  
   public ClassPathEnvironment(final ArtifactRef... artifactRefs)
 90  
   {
 91  0
     this();
 92  
 
 93  0
     for (final ArtifactRef artifactRef : artifactRefs)
 94  
     {
 95  0
       addArchiveArtifactRef(artifactRef);
 96  
     }
 97  0
   }
 98  
 
 99  
   /**
 100  
    * Convenience constructor that allows to add artifact references to create an
 101  
    * initial list.
 102  
    *
 103  
    * @param artifactRefs the initial collection of artifact references to add as
 104  
    *          roots.
 105  
    */
 106  
   public ClassPathEnvironment(final List<ArtifactRef> artifactRefs)
 107  0
   {
 108  0
     for (final ArtifactRef artifactRef : artifactRefs)
 109  
     {
 110  0
       addArchiveArtifactRef(artifactRef);
 111  
     }
 112  0
   }
 113  
 
 114  
   // ****************************** Inner Classes *****************************
 115  
 
 116  
   // ********************************* Methods ********************************
 117  
 
 118  
   // --- init -----------------------------------------------------------------
 119  
 
 120  
   // --- get&set --------------------------------------------------------------
 121  
 
 122  
   // --- business -------------------------------------------------------------
 123  
   /**
 124  
    * Returns a list of artifact references.
 125  
    *
 126  
    * @return an unmodifiable list of artifact references registered in this
 127  
    *         environment.
 128  
    */
 129  
   public List<ArtifactRef> getArtifactRefs()
 130  
   {
 131  0
     return Collections.unmodifiableList(artifactRefs);
 132  
   }
 133  
 
 134  
   /**
 135  
    * Returns a list of URLs that point to the physical locations of the
 136  
    * artifacts.
 137  
    *
 138  
    * @return a list of URLs that point to the physical locations of the
 139  
    *         artifacts.
 140  
    */
 141  
   public List<URL> getUrls()
 142  
   {
 143  0
     final List<URL> urls = new ArrayList<URL>(artifactRefs.size());
 144  0
     for (final ArtifactRef ref : artifactRefs)
 145  
     {
 146  0
       final URL url = ref.getUrl();
 147  0
       urls.add(url);
 148  0
     }
 149  
 
 150  0
     return urls;
 151  
   }
 152  
 
 153  
   /**
 154  
    * Returns the artifact reference by its short ID.
 155  
    *
 156  
    * @param artifactId the GAV concatenated and separated by colons.
 157  
    * @return the associated artifact reference or <code>null</code> if there is
 158  
    *         no artifact reference with the given {@code artifactId} registered.
 159  
    * @throws NullPointerException if {@code artifactId} is <code>null</code>.
 160  
    */
 161  
   @CheckForNull
 162  
   public ArtifactRef getArtifactRef(final String artifactId)
 163  
     throws NullPointerException
 164  
   {
 165  0
     Arg.checkNotNull("artifactId", artifactId);
 166  
 
 167  0
     return index.get(artifactId);
 168  
   }
 169  
 
 170  
   /**
 171  
    * Adds the given artifact reference to the list of artifact references. All
 172  
    * references are considered to be roots to search for class path resources.
 173  
    * <p>
 174  
    * If {@code archiveArtifactRef} is not an {@link ArtifactRef#isArchive()
 175  
    * archive}, it will <strong>not be added</strong>.
 176  
    * </p>
 177  
    *
 178  
    * @param archiveArtifactRef the artifact reference to add.
 179  
    * @throws NullPointerException if {@code archiveArtifactRef} is
 180  
    *           <code>null</code>.
 181  
    */
 182  
   public void addArchiveArtifactRef(final ArtifactRef archiveArtifactRef)
 183  
     throws NullPointerException
 184  
   {
 185  0
     Arg.checkNotNull("archiveArtifactRef", archiveArtifactRef);
 186  
 
 187  0
     if (!archiveArtifactRef.isArchive())
 188  
     {
 189  0
       LOG.debug("Dropping '" + archiveArtifactRef
 190  
                 + "' since it is not an archive.");
 191  0
       return;
 192  
     }
 193  
 
 194  0
     artifactRefs.add(archiveArtifactRef);
 195  0
     final String artifactId = archiveArtifactRef.getId().toShortString();
 196  0
     index.put(artifactId, archiveArtifactRef);
 197  0
   }
 198  
 
 199  
   /**
 200  
    * Adds the given artifact reference to the list of artifact references. All
 201  
    * references are considered to be roots to search for class path resources.
 202  
    *
 203  
    * @param artifactRef the artifact reference to add.
 204  
    * @throws NullPointerException if {@code artifactRef} is <code>null</code>.
 205  
    */
 206  
   public void addAnyArtifactRef(final ArtifactRef artifactRef)
 207  
     throws NullPointerException
 208  
   {
 209  0
     Arg.checkNotNull("artifactRef", artifactRef);
 210  
 
 211  0
     artifactRefs.add(artifactRef);
 212  0
     final String artifactId = artifactRef.getId().toShortString();
 213  0
     index.put(artifactId, artifactRef);
 214  0
   }
 215  
 
 216  
   // --- object basics --------------------------------------------------------
 217  
 
 218  
   /**
 219  
    * Returns the string representation of the object.
 220  
    *
 221  
    * @return the string representation of the object.
 222  
    */
 223  
   @Override
 224  
   public String toString()
 225  
   {
 226  0
     final StringBuilder buffer = new StringBuilder();
 227  
 
 228  0
     buffer.append("Class path roots:");
 229  
 
 230  0
     for (final ArtifactRef artifactRef : artifactRefs)
 231  
     {
 232  0
       buffer.append(' ').append(artifactRef);
 233  
     }
 234  
 
 235  0
     return buffer.toString();
 236  
   }
 237  
 }