Coverage Report - de.smartics.properties.spi.core.classpath.PropertiesFilesLoader
 
Classes in this File Line Coverage Branch Coverage Complexity
PropertiesFilesLoader
0%
0/23
0%
0/12
3.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.spi.core.classpath;
 17  
 
 18  
 import java.net.URL;
 19  
 import java.net.URLClassLoader;
 20  
 import java.util.Collection;
 21  
 import java.util.Collections;
 22  
 import java.util.Iterator;
 23  
 import java.util.Set;
 24  
 import java.util.regex.Pattern;
 25  
 
 26  
 import org.reflections.Reflections;
 27  
 import org.reflections.scanners.ResourcesScanner;
 28  
 import org.reflections.util.ConfigurationBuilder;
 29  
 
 30  
 import de.smartics.properties.api.core.domain.PropertiesContext;
 31  
 
 32  
 /**
 33  
  * Loads properties files found on the class path.
 34  
  */
 35  0
 public final class PropertiesFilesLoader
 36  
 {
 37  
   // ********************************* Fields *********************************
 38  
 
 39  
   // --- constants ------------------------------------------------------------
 40  
 
 41  
   /**
 42  
    * Pattern to filter properties files.
 43  
    * <p>
 44  
    * Every properties file (with file extension <code>properties</code> is
 45  
    * allowed as long as the name does not contain any underscores. Underscores
 46  
    * are used to distinguish localized information (like
 47  
    * <code>my-properties_en.properties</code>) and therefore must not
 48  
    * considered.
 49  
    * </p>
 50  
    *
 51  
    * @impl Note that the pattern here is only the file name pattern without a
 52  
    *       path.
 53  
    */
 54  0
   private static final Pattern PROPERTIES_PATTERN = Pattern
 55  
       .compile("[^_]+\\.properties");
 56  
 
 57  
   /**
 58  
    * Default scanner to use.
 59  
    */
 60  0
   private static final ResourcesScanner SCANNER = new ResourcesScanner();
 61  
 
 62  
   // --- members --------------------------------------------------------------
 63  
 
 64  
   // ****************************** Initializer *******************************
 65  
 
 66  
   // ****************************** Constructors ******************************
 67  
 
 68  
   // ****************************** Inner Classes *****************************
 69  
 
 70  
   // ********************************* Methods ********************************
 71  
 
 72  
   // --- init -----------------------------------------------------------------
 73  
 
 74  
   private static Reflections buildReflections(final Collection<URL> urls)
 75  
   {
 76  0
     final ClassLoader classLoader =
 77  
         new URLClassLoader(urls.toArray(new URL[urls.size()]), Thread
 78  
             .currentThread().getContextClassLoader());
 79  
 
 80  0
     final ConfigurationBuilder builder = new ConfigurationBuilder();
 81  0
     builder.addScanners(SCANNER).addClassLoader(classLoader).addUrls(urls);
 82  0
     final Reflections reflections = new Reflections(builder);
 83  0
     return reflections;
 84  
   }
 85  
 
 86  
   // --- get&set --------------------------------------------------------------
 87  
 
 88  
   // --- business -------------------------------------------------------------
 89  
 
 90  
   /**
 91  
    * Returns all files with the extension <code>.properties</code>.
 92  
    *
 93  
    * @param urls the class roots to search for files that have the
 94  
    *          <code>.properties</code> file name extension.
 95  
    * @return the names of properties files relative to the class path root.
 96  
    */
 97  
   public Set<String> getPropertiesFiles(final Collection<URL> urls)
 98  
   {
 99  0
     if (urls == null || urls.isEmpty())
 100  
     {
 101  0
       return Collections.emptySet();
 102  
     }
 103  
 
 104  0
     final Reflections reflections = buildReflections(urls);
 105  0
     final Set<String> pathes = reflections.getResources(PROPERTIES_PATTERN);
 106  
 
 107  0
     return pathes;
 108  
   }
 109  
 
 110  
   /**
 111  
    * Returns all boot properties that are defined to control the configuration
 112  
    * parsing process. Properties are stored in files matching
 113  
    * <code>{@value de.smartics.properties.api.core.domain.PropertiesContext#BOOT_PROPERTIES</code>.
 114  
    *
 115  
    * @param urls the URLs to search in.
 116  
    * @return the names of properties files relative to the class path root.
 117  
    */
 118  
   public Set<String> getBootPropertiesFiles(final Collection<URL> urls)
 119  
   {
 120  0
     if (urls == null || urls.isEmpty())
 121  
     {
 122  0
       return Collections.emptySet();
 123  
     }
 124  
 
 125  0
     final Reflections reflections = buildReflections(urls);
 126  0
     final Set<String> pathes =
 127  
         reflections.getResources(PropertiesContext.BOOT_PROPERTIES_PATTERN);
 128  
 
 129  0
     for (final Iterator<String> i = pathes.iterator(); i.hasNext();)
 130  
     {
 131  0
       final String path = i.next();
 132  0
       if (!path.endsWith(PropertiesContext.BOOT_PROPERTIES))
 133  
       {
 134  0
         i.remove();
 135  
       }
 136  0
     }
 137  
 
 138  0
     return pathes;
 139  
   }
 140  
 
 141  
   // --- object basics --------------------------------------------------------
 142  
 
 143  
 }